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