event-propagation.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.events</title>
  10. <script src="../base.js"></script>
  11. <script>
  12. goog.require('goog.events');
  13. </script>
  14. <link rel="stylesheet" href="css/demo.css">
  15. <style>
  16. html, body {
  17. background-color: #FFF;
  18. width: 100%;
  19. height: 100%;
  20. padding: 0px;
  21. margin: 0px;
  22. }
  23. h1 {
  24. font: normal 24px arial;
  25. color: #009;
  26. margin-top: 10px;
  27. margin-left: 10px;
  28. margin-right: 0px;
  29. margin-bottom: 0px;
  30. width: 450px;
  31. }
  32. p {
  33. font: normal 14px arial;
  34. color: #999;
  35. margin-top: 0px;
  36. margin-left: 10px;
  37. width: 450px;
  38. }
  39. #tree ul {
  40. font: normal 12px arial;
  41. list-style: none;
  42. margin: 0px;
  43. padding:0px;
  44. }
  45. #tree ul ul {
  46. padding-left: 36px;
  47. }
  48. html>body #tree span {
  49. position: relative;
  50. top: -2px;
  51. }
  52. #tree div:hover {
  53. background-color: #EEE;
  54. }
  55. #tree {
  56. width: 300px;
  57. height: 800px;
  58. border: 2px solid #EEE;
  59. float: left;
  60. }
  61. #log {
  62. width: 400px;
  63. height: 800px;
  64. border: 2px solid #EEE;
  65. border-left: 0px;
  66. margin-top: 0px;
  67. background-color: #FAFAFA;
  68. }
  69. </style>
  70. <script>
  71. var depth = 3;
  72. var breadth = 2;
  73. function writeTree(pos, preId) {
  74. if (!pos) pos = 0;
  75. document.write('<ul id="ol' + (preId || '') + '">');
  76. for (var i = 1; i <= breadth; i++) {
  77. document.write('<li id="li-' + (preId || '') + i + '">');
  78. document.write('<div>');
  79. document.write('<input type="checkbox" id="chk1-' + (preId || '') + i + '" />');
  80. document.write('<input type="checkbox" id="chk2-' + (preId || '') + i + '" />');
  81. document.write('<span>');
  82. document.write((preId || '') + i);
  83. document.write('</span></div>');
  84. if (pos < depth) writeTree(pos + 1, (preId || '') + i + '-');
  85. document.write('</li>');
  86. }
  87. document.write('</ul>');
  88. }
  89. // Dirty little buffered log so that logging doesn't affect times.
  90. var start = goog.now();
  91. var buffer = '';
  92. var timer = null;
  93. function log(str) {
  94. var time = ((new Date()).getTime() - start) / 1000;
  95. buffer = '[' + time + '] ' + str + '\n' + buffer;
  96. clearTimeout(timer);
  97. timer = setTimeout(sendBuffer, 250);
  98. }
  99. function sendBuffer() {
  100. document.getElementById('log').value = buffer +
  101. document.getElementById('log').value;
  102. buffer = '';
  103. }
  104. function doLoad() {
  105. if (arguments.callee.loaded) return;
  106. arguments.callee.loaded = true;
  107. document.getElementById('log').value = '';
  108. log('LOADED');
  109. log('Adding handlers');
  110. var lis = document.getElementById('tree').getElementsByTagName('li');
  111. for (var i = 0; i < lis.length; i++) {
  112. //lis[i].addEventListener('mousedown', handleCapture, true);
  113. //lis[i].addEventListener('mousedown', handleBubble, false);
  114. goog.events.listen(lis[i], 'mousedown', handleCapture, true);
  115. goog.events.listen(lis[i], 'mousedown', handleBubble, false);
  116. }
  117. log('Finished adding handlers');
  118. goog.events.listen(document.getElementById('log'),
  119. 'dblclick', function() { this.value = ''; });
  120. }
  121. function handleCapture(e) {
  122. if (e.target.tagName != 'INPUT') {
  123. var id = e.currentTarget.id.replace(/li\-/, '');
  124. if (document.getElementById('chk1-' + id).checked) {
  125. e.stopPropagation();
  126. log('Capture - ' + id + ' [Cancelled]');
  127. } else {
  128. log('Capture - ' + id);
  129. }
  130. }
  131. }
  132. function handleBubble(e) {
  133. if (e.target.tagName != 'INPUT') {
  134. var id = e.currentTarget.id.replace(/li\-/, '');
  135. if (document.getElementById('chk2-' + id).checked) {
  136. e.stopPropagation();
  137. log('Bubble - ' + id + ' [Cancelled]');
  138. } else {
  139. log('Bubble - ' + id);
  140. }
  141. }
  142. }
  143. goog.events.listen(window, 'load', doLoad);
  144. </script>
  145. </head>
  146. <body>
  147. <h1>goog.events - Stop Propagation</h1>
  148. <p><strong>Test the cancelling of capture and bubbling events.</strong> Click
  149. one of the nodes to see the event trace, then use the check boxes to cancel the
  150. capture or bubble at a given branch.
  151. (Double click the text area to clear it)</p>
  152. <div id="tree">
  153. <script>writeTree();</script>
  154. </div>
  155. <textarea id="log"></textarea>
  156. </body>
  157. </html>