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