aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/demos/event-propagation.html
blob: 47eac153afad27b81439aa0d881eddc4c3a73d5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<title>goog.events</title>
<script src="../base.js"></script>
<script>
goog.require('goog.events');
</script>
<link rel="stylesheet" href="css/demo.css">
<style>

html, body {
  background-color: #FFF;
  width: 100%;
  height: 100%;
  padding: 0px;
  margin: 0px;
}


h1 {
  font: normal 24px arial;
  color: #009;
  margin-top: 10px;
  margin-left: 10px;
  margin-right: 0px;
  margin-bottom: 0px;
  width: 450px;
}

p {
  font: normal 14px arial;
  color: #999;
  margin-top: 0px;
  margin-left: 10px;
  width: 450px;
}

#tree ul {
  font: normal 12px arial;
  list-style: none;
  margin: 0px;
  padding:0px;
}

#tree ul ul {
  padding-left: 36px;
}

html>body #tree span {
  position: relative;
  top: -2px;
}

#tree div:hover {
  background-color: #EEE;
}

#tree {
  width: 300px;
  height: 800px;
  border: 2px solid #EEE;
  float: left;
}

#log {
  width: 400px;
  height: 800px;
  border: 2px solid #EEE;
  border-left: 0px;
  margin-top: 0px;
  background-color: #FAFAFA;
}

</style>
<script>

var depth = 3;
var breadth = 2;
function writeTree(pos, preId) {
  if (!pos) pos = 0;

  document.write('<ul id="ol' + (preId || '') + '">');

  for (var i = 1; i <= breadth; i++) {
    document.write('<li id="li-' + (preId || '') + i + '">');
    document.write('<div>');
    document.write('<input type="checkbox" id="chk1-' + (preId || '') + i + '" />');
    document.write('<input type="checkbox" id="chk2-' + (preId || '') + i + '" />');
    document.write('<span>');
    document.write((preId || '') + i);
    document.write('</span></div>');
    if (pos < depth) writeTree(pos + 1, (preId || '') + i + '-');
    document.write('</li>');
  }

  document.write('</ul>');
}


// Dirty little buffered log so that logging doesn't affect times.
var start = goog.now();
var buffer = '';
var timer = null;
function log(str) {
  var time = ((new Date()).getTime() - start) / 1000;
  buffer = '[' + time + '] ' + str + '\n' + buffer;
  clearTimeout(timer);
  timer = setTimeout(sendBuffer, 250);
}

function sendBuffer() {
  document.getElementById('log').value = buffer +
      document.getElementById('log').value;
  buffer = '';
}



function doLoad() {
  if (arguments.callee.loaded) return;
  arguments.callee.loaded = true;

  document.getElementById('log').value = '';
  log('LOADED');

  log('Adding handlers');
  var lis = document.getElementById('tree').getElementsByTagName('li');
  for (var i = 0; i < lis.length; i++) {
    //lis[i].addEventListener('mousedown', handleCapture, true);
    //lis[i].addEventListener('mousedown', handleBubble, false);
    goog.events.listen(lis[i], 'mousedown', handleCapture, true);
    goog.events.listen(lis[i], 'mousedown', handleBubble, false);
  }
  log('Finished adding handlers');


  goog.events.listen(document.getElementById('log'),
    'dblclick', function() { this.value = ''; });
}

function handleCapture(e) {
  if (e.target.tagName != 'INPUT') {
    var id = e.currentTarget.id.replace(/li\-/, '');

    if (document.getElementById('chk1-' + id).checked) {
      e.stopPropagation();
      log('Capture - ' + id + ' [Cancelled]');
    } else {
      log('Capture - ' + id);
    }
  }
}

function handleBubble(e) {
  if (e.target.tagName != 'INPUT') {
    var id = e.currentTarget.id.replace(/li\-/, '');

    if (document.getElementById('chk2-' + id).checked) {
      e.stopPropagation();
      log('Bubble - ' + id + ' [Cancelled]');
    } else {
      log('Bubble - ' + id);
    }
  }
}

goog.events.listen(window, 'load', doLoad);

</script>
</head>
<body>
  <h1>goog.events - Stop Propagation</h1>
  <p><strong>Test the cancelling of capture and bubbling events.</strong>  Click
  one of the nodes to see the event trace, then use the check boxes to cancel the
  capture or bubble at a given branch.
  (Double click the text area to clear it)</p>

  <div id="tree">
  <script>writeTree();</script>
  </div>

  <textarea id="log"></textarea>

  </body>
</html>