dndSelector.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. if (!dojo._hasResource["dijit._tree.dndSelector"]) { // _hasResource checks
  2. // added by build. Do
  3. // not use _hasResource
  4. // directly in your
  5. // code.
  6. dojo._hasResource["dijit._tree.dndSelector"] = true;
  7. dojo.provide("dijit._tree.dndSelector");
  8. dojo.require("dojo.dnd.common");
  9. dojo.require("dijit._tree.dndContainer");
  10. dojo.declare("dijit._tree.dndSelector", dijit._tree.dndContainer, {
  11. constructor : function(tree, params) {
  12. this.selection = {};
  13. this.anchor = null;
  14. this.simpleSelection = false;
  15. this.events.push(dojo.connect(this.tree.domNode, "onmousedown",
  16. this, "onMouseDown"), dojo.connect(
  17. this.tree.domNode, "onmouseup", this, "onMouseUp"));
  18. },
  19. // object attributes (for markup)
  20. singular : false, // is singular property
  21. // methods
  22. getSelectedItems : function() {
  23. var selectedItems = []
  24. for (var i in this.selection) {
  25. selectedItems
  26. .push(dijit.getEnclosingWidget(this.selection[i]).item);
  27. }
  28. return selectedItems;
  29. },
  30. getSelectedNodes : function() {
  31. return this.selection;
  32. },
  33. selectNone : function() {
  34. // summary: unselects all items
  35. return this._removeSelection()._removeAnchor(); // self
  36. },
  37. insertItems : function(item, parent) {
  38. // summary: inserts new data items (see Container's insertNodes
  39. // method for details)
  40. // we actually need to add things to the store here instead of
  41. // adding noes to the tree directly
  42. },
  43. destroy : function() {
  44. // summary: prepares the object to be garbage-collected
  45. dojo.dnd.Selector.superclass.destroy.call(this);
  46. this.selection = this.anchor = null;
  47. },
  48. // mouse events
  49. onMouseDown : function(e) {
  50. // summary: event processor for onmousedown
  51. // e: Event: mouse event
  52. if (!this.current) {
  53. return;
  54. }
  55. var item = dijit.getEnclosingWidget(this.current).item
  56. var id = this.tree.store.getIdentity(item);
  57. if (!this.current.id) {
  58. this.current.id = id;
  59. }
  60. if (!this.current.type) {
  61. this.current.type = "data";
  62. }
  63. if (!this.singular && !dojo.dnd.getCopyKeyState(e) && !e.shiftKey
  64. && (this.current.id in this.selection)) {
  65. this.simpleSelection = true;
  66. dojo.stopEvent(e);
  67. return;
  68. }
  69. if (this.singular) {
  70. if (this.anchor == this.current) {
  71. if (dojo.dnd.getCopyKeyState(e)) {
  72. this.selectNone();
  73. }
  74. } else {
  75. this.selectNone();
  76. this.anchor = this.current;
  77. this._addItemClass(this.anchor, "Anchor");
  78. this.selection[this.current.id] = this.current;
  79. }
  80. } else {
  81. if (!this.singular && e.shiftKey) {
  82. if (dojo.dnd.getCopyKeyState(e)) {
  83. // TODO add range to selection
  84. } else {
  85. // TODO select new range from anchor
  86. }
  87. } else {
  88. if (dojo.dnd.getCopyKeyState(e)) {
  89. if (this.anchor == this.current) {
  90. delete this.selection[this.anchor.id];
  91. this._removeAnchor();
  92. } else {
  93. if (this.current.id in this.selection) {
  94. this._removeItemClass(this.current, "Selected");
  95. delete this.selection[this.current.id];
  96. } else {
  97. if (this.anchor) {
  98. this
  99. ._removeItemClass(this.anchor,
  100. "Anchor");
  101. this._addItemClass(this.anchor, "Selected");
  102. }
  103. this.anchor = this.current;
  104. this._addItemClass(this.current, "Anchor");
  105. this.selection[this.current.id] = this.current;
  106. }
  107. }
  108. } else {
  109. var item = dijit.getEnclosingWidget(this.current).item
  110. var id = this.tree.store.getIdentity(item);
  111. if (!(id in this.selection)) {
  112. this.selectNone();
  113. this.anchor = this.current;
  114. this._addItemClass(this.current, "Anchor");
  115. this.selection[id] = this.current;
  116. }
  117. }
  118. }
  119. }
  120. dojo.stopEvent(e);
  121. },
  122. onMouseMove : function() {
  123. },
  124. onOverEvent : function() {
  125. this.onmousemoveEvent = dojo.connect(this.node, "onmousemove",
  126. this, "onMouseMove");
  127. },
  128. onMouseUp : function(e) {
  129. // summary: event processor for onmouseup
  130. // e: Event: mouse event
  131. if (!this.simpleSelection) {
  132. return;
  133. }
  134. this.simpleSelection = false;
  135. this.selectNone();
  136. if (this.current) {
  137. this.anchor = this.current;
  138. this._addItemClass(this.anchor, "Anchor");
  139. this.selection[this.current.id] = this.current;
  140. }
  141. },
  142. _removeSelection : function() {
  143. // summary: unselects all items
  144. var e = dojo.dnd._empty;
  145. for (var i in this.selection) {
  146. if (i in e) {
  147. continue;
  148. }
  149. var node = dojo.byId(i);
  150. if (node) {
  151. this._removeItemClass(node, "Selected");
  152. }
  153. }
  154. this.selection = {};
  155. return this; // self
  156. },
  157. _removeAnchor : function() {
  158. if (this.anchor) {
  159. this._removeItemClass(this.anchor, "Anchor");
  160. this.anchor = null;
  161. }
  162. return this; // self
  163. }
  164. });
  165. }