Debian Codegraph @

closure-library/closure/goog/demos/dragdrop.html

raw
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.fx.DragDrop</title>
11 <meta charset="utf-8">
12 <script src="../base.js"></script>
13 <script>
14 goog.require('goog.fx.DragDrop');
15 goog.require('goog.fx.DragDropGroup');
16 goog.require('goog.dom');
17 goog.require('goog.dom.TagName');
18 </script>
19 <link rel="stylesheet" href="css/demo.css">
20 <style>
21 body {
22 margin: 10px;
23 }
24 ul {
25 padding: 0px;
26 }
27 li {
28 list-style: none;
29 }
30 li, div {
31 font: menu;
32 width: 20ex;
33 border: 1px solid gray;
34 margin: 1px;
35 padding: 0px 2px 0px 2px;
36 background: silver;
37 }
38 .source {
39 cursor: move;
40 -moz-user-select: none;
41 }
42 .drag {
43 cursor: move;
44 background: green;
45 }
46 .target {
47
48 }
49 #list2 {
50 margin: 0px 30px 30px 30px;
51 padding-left: 30px;
52 }
53 .foo {
54 position: absolute;
55 background: pink;
56 padding: 5px;
57 }
58 </style>
59 </head>
60 <body>
61 <h1>goog.fx.DragDrop</h1>
62
63 List 1 (combined source/target, can be dropped on list 1, list 2, button 1 or
64 button 2).
65 <ul id="list1">
66 <li>Item 1.1</li>
67 <li>Item 1.2</li>
68 <li>Item 1.3</li>
69 <li>Item 1.4</li>
70 <li>Item 1.5</li>
71 <li>Item 1.6</li>
72 <li>Item 1.7</li>
73 <li>Item 1.8</li>
74 <li>Item 1.9</li>
75 <li>Item 1.10</li>
76 <li>Item 1.11</li>
77 <li>Item 1.12</li>
78 <li>Item 1.13</li>
79 <li>Item 1.14</li>
80 <li>Item 1.15</li>
81 </ul>
82
83 List 2 (source only, can be dropped on list 1 or button 2)
84 <ul id="list2">
85 <li>Item 2.1</li>
86 <li>Item 2.2</li>
87 <li>Item 2.3</li>
88 <li>Item 2.4</li>
89 <li>Item 2.5</li>
90 <li>Item 2.6</li>
91 <li>Item 2.7</li>
92 <li>Item 2.8</li>
93 <li>Item 2.9</li>
94 <li>Item 2.10</li>
95 <li>Item 2.11</li>
96 <li>Item 2.12</li>
97 <li>Item 2.13</li>
98 <li>Item 2.14</li>
99 <li>Item 2.15</li>
100 </ul>
101
102 <div id="button1">
103 Button 1 (combined source/target, can be dropped on list 1)
104 </div>
105
106 <div id="button2">
107 Button 2 (target only)
108 </div>
109
110
111 <script>
112 // Custom implementation demo. Overrides createDragElement and
113 // positionDragElement.
114 function FooDrag(element, opt_data) {
115 goog.fx.DragDrop.call(this, element, opt_data);
116 }
117 goog.inherits(FooDrag, goog.fx.DragDrop);
118
119 FooDrag.prototype.createDragElement = function(sourceEl) {
120 return goog.dom.createDom(goog.dom.TagName.DIV, 'foo', 'Custom drag element');
121 };
122
123 FooDrag.prototype.getDragElementPosition = function(sourceEl, el, event) {
124 return new goog.math.Coordinate(event.clientX, event.clientY);
125 };
126
127
128 var button1, button2, list1, list2, i, len, nodes, el;
129
130 // Create drop targets (either by id or element reference)
131 button1 = new FooDrag(
132 document.getElementById('button1'), 'button 1'
133 );
134 button2 = new goog.fx.DragDrop('button2', 'button 2');
135
136 // Create drag clusters (multiple elements shares the same
137 // drag properties)
138 list1 = new goog.fx.DragDropGroup();
139 list2 = new goog.fx.DragDropGroup();
140
141 nodes = document.getElementById('list1').childNodes;
142 len = nodes.length;
143 for (i = 0; i < len; i++) {
144 el = nodes[i];
145 if ((el.nodeType == 1) && (el.nodeName == 'LI')) {
146 list1.addItem(el, el.firstChild.nodeValue);
147 }
148 }
149
150 nodes = document.getElementById('list2').childNodes;
151 len = nodes.length;
152 for (i = 0; i < len; i++) {
153 el = nodes[i];
154 if ((el.nodeType == 1) && (el.nodeName == 'LI')) {
155 list2.addItem(el, el.firstChild.nodeValue);
156 }
157 }
158
159 // Set valid targets for list1
160 list1.addTarget(button1);
161 list1.addTarget(button2);
162 list1.addTarget(list1);
163
164 // Set valid targets for list2
165 list2.addTarget(button2);
166 list2.addTarget(list1);
167
168 // Set valid target for button1 (allow button1 to be dragged onto list1)
169 button1.addTarget(list1);
170
171 // Set additional classes used to indicate dragging
172 button1.setSourceClass('source');
173 button1.setTargetClass('target');
174 button1.setDragClass('drag');
175 button2.setSourceClass('source');
176 button2.setTargetClass('target');
177 list1.setSourceClass('source');
178 list1.setTargetClass('target');
179 list2.setSourceClass('source');
180
181 // Init drag objects
182 button1.init();
183 button2.init();
184 list1.init();
185 list2.init();
186
187 // Set up event handlers
188 goog.events.listen(list1, 'dragover', dragOver);
189 goog.events.listen(list1, 'dragout', dragOut);
190 goog.events.listen(list1, 'drop', dropList1);
191 goog.events.listen(list1, 'drag', dragList1);
192 goog.events.listen(list1, 'dragstart', dragStart);
193 goog.events.listen(list1, 'dragend', dragEnd);
194
195 goog.events.listen(list2, 'dragover', dragOver);
196 goog.events.listen(list2, 'dragout', dragOut);
197 goog.events.listen(list2, 'drop', drop);
198 goog.events.listen(list2, 'dragstart', dragStart);
199 goog.events.listen(list2, 'dragend', dragEnd);
200
201 goog.events.listen(button1, 'dragover', dragOver);
202 goog.events.listen(button1, 'dragout', dragOut);
203 goog.events.listen(button1, 'drop', drop);
204 goog.events.listen(button1, 'dragstart', dragStart);
205 goog.events.listen(button1, 'dragend', dragEnd);
206
207 goog.events.listen(button2, 'dragover', dragOver);
208 goog.events.listen(button2, 'dragout', dragOut);
209 goog.events.listen(button2, 'drop', drop);
210
211 goog.events.listen(document.getElementById('button1'), 'click',
212 function(e) { alert('click'); });
213
214 function dragOver(event) {
215 event.dropTargetItem.element.style.background = 'red';
216 }
217
218 function dragOut(event) {
219 event.dropTargetItem.element.style.background = 'silver';
220 }
221
222 function drop(event) {
223 event.dropTargetItem.element.style.background = 'silver';
224 var str = [
225 event.dragSourceItem.data,
226 ' dropped onto ',
227 event.dropTargetItem.data,
228 ' at ',
229 event.viewportX,
230 'x',
231 event.viewportY
232 ];
233 alert(str.join(''));
234 }
235
236 function dropList1(event) {
237 event.dropTargetItem.element.style.background = 'silver';
238 var str = [
239 event.dragSourceItem.data,
240 ' dropped onto ',
241 event.dropTargetItem.data,
242 ' in list 1.'
243 ];
244 alert(str.join(''));
245 }
246
247 function dragList1(event) {
248 var str = [
249 event.dragSourceItem.data,
250 ' dragged from list 1'
251 ];
252 alert(str.join(''));
253 }
254
255 function dragStart(event) {
256 goog.style.setOpacity(event.dragSourceItem.element, 0.5);
257 }
258
259 function dragEnd(event) {
260 goog.style.setOpacity(event.dragSourceItem.element, 1.0);
261 }
262 </script>
263 </body>
264 </html>