stopevent.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <html>
  2. <!--
  3. Copyright 2010 The Closure Library Authors. All Rights Reserved.
  4. Use of this source code is governed by the Apache License, Version 2.0.
  5. See the COPYING file for details.
  6. -->
  7. <head>
  8. <title>Stop Event Propagation</title>
  9. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  10. <script type="text/javascript" src="../base.js"></script>
  11. <script type="text/javascript">
  12. goog.require('goog.events');
  13. goog.require('goog.events.EventType');
  14. </script>
  15. <style type="text/css">
  16. html, body {
  17. }
  18. #tree ul {
  19. font: normal 12px arial;
  20. list-style: none;
  21. margin: 0px;
  22. padding:0px;
  23. }
  24. #tree ul ul {
  25. padding-left: 36px;
  26. }
  27. html>body #tree span {
  28. position: relative;
  29. top: -2px;
  30. }
  31. #tree div:hover {
  32. background-color: #EEE;
  33. }
  34. #tree {
  35. width: 300px;
  36. height: 800px;
  37. border: 2px solid #EEE;
  38. float: left;
  39. }
  40. #log {
  41. width: 400px;
  42. height: 800px;
  43. border: 2px solid #EEE;
  44. border-left: 0px;
  45. margin-top: 0px;
  46. background-color: #FAFAFA;
  47. }
  48. </style>
  49. <script type="text/javascript">
  50. var depth = 3;
  51. var bredth = 2;
  52. function writeTree(pos, preid) {
  53. if (!pos) pos = 0;
  54. document.write('<ul id="ol' + (preid || '') + '">');
  55. for (var i = 1; i <= bredth; i++) {
  56. document.write('<li id="li-' + (preid || '') + i + '">');
  57. document.write('<div>');
  58. document.write('<input type="checkbox" id="chk1-' +
  59. (preid || '') + i + '" />');
  60. document.write('<input type="checkbox" id="chk2-' +
  61. (preid || '') + i + '" />');
  62. document.write('<span>');
  63. document.write((preid || '') + i);
  64. document.write('</span></div>');
  65. if (pos < depth) writeTree(pos+1, (preid || '') + i + '-');
  66. document.write('</li>');
  67. }
  68. document.write('</ul>');
  69. }
  70. // Dirty little buffered log so that logging doesn't affect times.
  71. var start = (new Date).getTime();
  72. var buffer = '';
  73. var timer = null;
  74. function log(str) {
  75. var time = ((new Date) - start) / 1000;
  76. buffer = '[' + time + '] ' + str + '\n' + buffer;
  77. clearTimeout(timer);
  78. timer = setTimeout(sendBuffer, 250);
  79. }
  80. function sendBuffer() {
  81. document.getElementById('log').value = buffer +
  82. document.getElementById('log').value;
  83. buffer = '';
  84. }
  85. function doLoad() {
  86. if (arguments.callee.loaded) return;
  87. arguments.callee.loaded = true;
  88. document.getElementById('log').value = '';
  89. log('LOADED');
  90. log('Adding handlers');
  91. var lis = document.getElementById('tree').getElementsByTagName('li');
  92. for (var i = 0; i < lis.length; i++) {
  93. goog.events.listen(lis[i], goog.events.EventType.MOUSEDOWN,
  94. handleCapture, true);
  95. goog.events.listen(lis[i], goog.events.EventType.MOUSEDOWN,
  96. handleBubble, false);
  97. }
  98. log('Finished adding handlers');
  99. goog.events.listen(document.getElementById('log'),
  100. goog.events.EventType.DBLCLICK, function() { this.value = ''; });
  101. }
  102. function handleCapture(e) {
  103. if (e.target.tagName != 'INPUT') {
  104. var id = e.currentTarget.id.replace(/li\-/, '');
  105. if (document.getElementById('chk1-' + id).checked) {
  106. log('Capture - ' + id + ' [Cancelled]');
  107. e.stop();
  108. } else {
  109. log('Capture - ' + id);
  110. }
  111. }
  112. }
  113. function handleBubble(e) {
  114. if (e.target.tagName != 'INPUT') {
  115. var id = e.currentTarget.id.replace(/li\-/, '');
  116. if (document.getElementById('chk2-' + id).checked) {
  117. log('Bubble - ' + id + ' [Cancelled]');
  118. e.stop();
  119. } else {
  120. log('Bubble - ' + id);
  121. }
  122. }
  123. }
  124. goog.events.listen(window, goog.events.EventType.LOAD, doLoad);
  125. </script>
  126. </head>
  127. <body id="documentbody">
  128. <h1>Stop Event</h1>
  129. <p><strong>Test the cancelling of capture and bubbling events.</strong> Click
  130. one of the nodes to see the event trace, then use the check boxes to cancel
  131. the capture or bubble at a given branch. (Double click the text area to clear
  132. it)</p>
  133. <div id="tree">
  134. <script type="text/javascript">writeTree();</script>
  135. </div>
  136. <textarea id="log"></textarea>
  137. </body>
  138. </html>