checkbox.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2010 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <title>goog.ui.Checkbox</title>
  10. <script src="../base.js"></script>
  11. <script>
  12. goog.require('goog.debug.DivConsole');
  13. goog.require('goog.debug.LogManager');
  14. goog.require('goog.dom');
  15. goog.require('goog.events');
  16. goog.require('goog.log');
  17. goog.require('goog.ui.Checkbox');
  18. goog.require('goog.ui.Checkbox.State');
  19. </script>
  20. <link rel="stylesheet" href="css/demo.css">
  21. <link rel="stylesheet" href="../css/checkbox.css">
  22. </head>
  23. <body>
  24. <h1>goog.ui.Checkbox</h1>
  25. <p>This is a tri-state checkbox.</p>
  26. <div>
  27. <span id="enable" class="goog-checkbox-checked"></span>Enable/disable</div>
  28. <br>
  29. <div><span id="all" class="goog-checkbox-undetermined"></span>root</div>
  30. <div style="margin-left: 1em;">
  31. <div><span id="leaf1" class="goog-checkbox-checked"></span>leaf 1</div>
  32. <div><span id="leaf2"></span>leaf 2</div>
  33. </div>
  34. <br>
  35. <div id="render">
  36. Created with render
  37. </div>
  38. <br>
  39. <div id="decorate">
  40. Created with decorate
  41. <span class="goog-checkbox"></span>
  42. <span class="goog-checkbox goog-checkbox-checked"></span>
  43. <span class="goog-checkbox goog-checkbox-undetermined"></span>
  44. <span class="goog-checkbox goog-checkbox-disabled"></span>
  45. </div>
  46. <br><br>
  47. <!-- Event log. -->
  48. <fieldset class="goog-debug-panel">
  49. <legend>Event Log for 'root', 'leaf1', 'leaf2'</legend>
  50. <div id="log" style="height: 500px"></div>
  51. </fieldset>
  52. <script type="text/javascript">
  53. // Set up a logger.
  54. goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL);
  55. var logger = goog.log.getLogger('demo');
  56. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  57. logconsole.setCapturing(true);
  58. var EVENTS = goog.object.getValues(goog.ui.Component.EventType);
  59. goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.');
  60. function logEvent(name, e) {
  61. goog.log.info(logger, '"' + name + '" dispatched: ' + e.type);
  62. }
  63. var all = new goog.ui.Checkbox();
  64. all.decorate(goog.dom.getElement('all'));
  65. all.setLabel(all.getElement().parentNode);
  66. goog.events.listen(all, EVENTS, goog.partial(logEvent, 'root'));
  67. var leaf1 = new goog.ui.Checkbox();
  68. leaf1.decorate(goog.dom.getElement('leaf1'));
  69. leaf1.setLabel(leaf1.getElement().parentNode);
  70. goog.events.listen(leaf1, EVENTS, goog.partial(logEvent, 'leaf1'));
  71. var leaf2 = new goog.ui.Checkbox();
  72. leaf2.decorate(goog.dom.getElement('leaf2'));
  73. leaf2.setLabel(leaf2.getElement().parentNode);
  74. goog.events.listen(leaf2, EVENTS, goog.partial(logEvent, 'leaf2'));
  75. var enable = new goog.ui.Checkbox();
  76. enable.setLabel(goog.dom.getElement('enable').parentNode);
  77. enable.decorate(goog.dom.getElement('enable'));
  78. var states = [false, true, null];
  79. goog.array.forEach(states, function(state) {
  80. new goog.ui.Checkbox(state).render(goog.dom.getElement('render'));
  81. });
  82. var checkbox = new goog.ui.Checkbox();
  83. checkbox.render(goog.dom.getElement('render'));
  84. checkbox.setEnabled(false);
  85. var decorateNodes = goog.dom.getElementsByTagNameAndClass('span', null,
  86. goog.dom.getElement('decorate'));
  87. for (var i = 0, len = decorateNodes.length; i < len; i++) {
  88. goog.ui.decorate(decorateNodes[i]);
  89. }
  90. function rootChanged(e) {
  91. leaf1.setChecked(all.getChecked());
  92. leaf2.setChecked(all.getChecked());
  93. }
  94. function leafChanged(e) {
  95. var same = leaf1.getChecked() == leaf2.getChecked();
  96. all.setChecked(same ? leaf1.getChecked() :
  97. goog.ui.Checkbox.State.UNDETERMINED);
  98. }
  99. function enableTree(enable) {
  100. all.setEnabled(enable);
  101. leaf1.setEnabled(enable);
  102. leaf2.setEnabled(enable);
  103. }
  104. goog.events.listen(all, goog.ui.Component.EventType.CHANGE, rootChanged);
  105. goog.events.listen(leaf1, goog.ui.Component.EventType.CHANGE, leafChanged);
  106. goog.events.listen(leaf2, goog.ui.Component.EventType.CHANGE, leafChanged);
  107. goog.events.listen(enable, goog.ui.Component.EventType.CHANGE,
  108. function() { enableTree(enable.getChecked()); });
  109. </script>
  110. </body>
  111. </html>