dragdrop.html 6.4 KB

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