Selector.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. define("dojox/dnd/Selector", ["dojo", "dojox", "dojo/dnd/Selector"], function(dojo, dojox) {
  2. return dojo.declare('dojox.dnd.Selector', dojo.dnd.Selector, {
  3. conservative: true,
  4. isSelected: function(node) {
  5. // summary:
  6. // checks if node is selected
  7. // node: String|DomNode:
  8. // Node to check (id or DOM Node)
  9. var id = dojo.isString(node) ? node : node.id,
  10. item = this.getItem(id);
  11. return item && this.selected[id]; // Boolean
  12. },
  13. selectNode: function(node, add) {
  14. // summary:
  15. // selects a node
  16. // node: String|DomNode:
  17. // Node to select (id or DOM Node)
  18. // add: Boolean?:
  19. // If true, node is added to selection, otherwise current
  20. // selection is removed, and node will be the only selection.
  21. if (!add) {
  22. this.selectNone();
  23. }
  24. var id = dojo.isString(node) ? node : node.id,
  25. item = this.getItem(id);
  26. if (item) {
  27. this._removeAnchor();
  28. this.anchor = dojo.byId(node);
  29. this._addItemClass(this.anchor, 'Anchor');
  30. this.selection[id] = 1;
  31. this._addItemClass(this.anchor, 'Selected');
  32. }
  33. return this; // self
  34. },
  35. deselectNode: function(node) {
  36. // summary:
  37. // deselects a node
  38. // node: String|DomNode:
  39. // Node to deselect (id or DOM Node)
  40. var id = dojo.isString(node) ? node : node.id,
  41. item = this.getItem(id);
  42. if (item && this.selection[id]) {
  43. if (this.anchor === dojo.byId(node)) {
  44. this._removeAnchor();
  45. }
  46. delete this.selection[id];
  47. this._removeItemClass(this.anchor, 'Selected');
  48. }
  49. return this; // self
  50. },
  51. selectByBBox: function(left, top, right, bottom, add) {
  52. // summary:
  53. // selects nodes by bounding box
  54. // left: Number:
  55. // Left coordinate of the bounding box
  56. // top: Number:
  57. // Top coordinate of the bounding box
  58. // right: Number:
  59. // Right coordinate of the bounding box
  60. // bottom: Number:
  61. // Bottom coordinate of the bounding box
  62. // add: Boolean?:
  63. // If true, node is added to selection, otherwise current
  64. // selection is removed, and node will be the only selection.
  65. // user has drawn a bounding box ... time to see whether any dom nodes
  66. // in this container satisfy the bounding box range.
  67. if (!add) {
  68. this.selectNone();
  69. }
  70. this.forInItems(function(data, id) {
  71. var node = dojo.byId(id);
  72. if (node && this._isBoundedByBox(node, left, top, right, bottom)) {
  73. this.selectNode(id, true);
  74. }
  75. }, this);
  76. return this; // self
  77. },
  78. _isBoundedByBox: function(node, left, top, right, bottom) {
  79. // summary:
  80. // figures out whether certain coodinates bound a particular
  81. // dom node.
  82. // node: String|DomNode:
  83. // Node to check (id or DOM Node)
  84. // left: Number:
  85. // Left coordinate of the bounding box
  86. // top: Number:
  87. // Top coordinate of the bounding box
  88. // right: Number:
  89. // Right coordinate of the bounding box
  90. // bottom: Number:
  91. // Bottom coordinate of the bounding box
  92. return this.conservative ? this._conservativeBBLogic(node, left, top, right, bottom) : this._liberalBBLogic(node, left, top, right, bottom);
  93. },
  94. shift: function(toNext, add) {
  95. // summary:
  96. // shifts the currently selected dnd item forwards and backwards.
  97. // One possible use would be to allow a user select different
  98. // dnd items using the right and left keys.
  99. // toNext: Boolean:
  100. // If true, we select the next node, otherwise the previous one.
  101. // add: Boolean?:
  102. // If true, add to selection, otherwise current selection is
  103. // removed before adding any nodes.
  104. var selectedNodes = this.getSelectedNodes();
  105. if (selectedNodes && selectedNodes.length) {
  106. // only delegate to selectNode if at least one node is selected.
  107. // If multiple nodes are selected assume that we go with
  108. // the last selected node.
  109. this.selectNode(this._getNodeId(selectedNodes[selectedNodes.length - 1].id, toNext), add);
  110. }
  111. },
  112. _getNodeId: function(nodeId, toNext) {
  113. // summary:
  114. // finds a next/previous node in relation to nodeId
  115. // nodeId: String:
  116. // the id of the node to use as the base node
  117. // toNext: Boolean:
  118. // If true, we select the next node, otherwise the previous one.
  119. var allNodes = this.getAllNodes(), newId = nodeId;
  120. for (var i = 0, l = allNodes.length; i < l; ++i) {
  121. if (allNodes[i].id == nodeId) {
  122. // have a match ... make sure we don't go outside
  123. var j = Math.min(l - 1, Math.max(0, i + (toNext ? 1 : -1)));
  124. if (i != j) {
  125. // we should be fine to go with the id the user has requested.
  126. newId = allNodes[j].id;
  127. }
  128. break;
  129. }
  130. }
  131. // if we don't get a match, the newId defaults to the currently selected node
  132. return newId;
  133. },
  134. _conservativeBBLogic: function(node, left, top, right, bottom) {
  135. // summary:
  136. // logic which determines whether a node is bounded by the
  137. // left,top,right,bottom parameters. This function returns true
  138. // only if the coordinates of the node parameter are fully
  139. // encompassed by the box determined by the left, top, right, bottom parameters.
  140. var c = dojo.coords(node), t;
  141. // normalize input
  142. if (left > right) {
  143. t = left;
  144. left = right;
  145. right = t;
  146. }
  147. if (top > bottom) {
  148. t = top;
  149. top = bottom;
  150. bottom = t;
  151. }
  152. return c.x >= left && c.x + c.w <= right && c.y >= top && c.y + c.h <= bottom; // Boolean
  153. },
  154. _liberalBBLogic: function(node, left, top, right, bottom) {
  155. // summary:
  156. // logic which determines whether a node is bounded by the
  157. // left,top,right,bottom parameters. Allows for the case where
  158. // any section of the box determined by the left,top,right,bottom parameters
  159. // overlapping the coordinates of the node parameter constitutes a true
  160. // return value
  161. var c = dojo.position(node), xBounded, yBounded, tlx, tly, brx, bry, leftGreater = false, bottomGreater = false,
  162. nodeTlx = c.x, nodeTly = c.y, nodeBrx = c.x + c.w, nodeBry = c.y + c.h;
  163. // tlx, tly represents the x,y coordinates for the top left of the bounding box
  164. // brx, bry represents the x,y coordinates for the bottom right of the bounding box
  165. // nodeTlx, nodeTly represents the x,y coordinates for the top left of the dom node
  166. // nodeBrx, nodeBry represents the x,y coordinates for the bottom right of the dom node
  167. if (left < right) {
  168. tlx = left;
  169. tly = top;
  170. } else {
  171. leftGreater = true;
  172. tlx = right;
  173. tly = bottom;
  174. }
  175. if (top < bottom) {
  176. bottomGreater = true;
  177. brx = right;
  178. bry = bottom;
  179. } else {
  180. brx = left;
  181. bry = top;
  182. tlx = right;
  183. tly = bottom;
  184. }
  185. if (leftGreater && bottomGreater) {
  186. // accommodate for the case where the user is drawing from top down and from right to left.
  187. brx = left;
  188. bry = bottom;
  189. tlx = right;
  190. tly = top;
  191. }
  192. xBounded = (nodeTlx >= tlx || nodeBrx <= brx) && (tlx <= nodeBrx && brx >= nodeTlx) || (nodeTlx <= tlx && nodeBrx >= brx);
  193. yBounded = (tly <= nodeBry && bry >= nodeTly) || (nodeBry >= bry && nodeTly <= bry);
  194. return xBounded && yBounded; // Boolean
  195. }
  196. }
  197. );
  198. });