Selector.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.dnd.Selector"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.dnd.Selector"] = true;
  8. dojo.provide("dojox.dnd.Selector");
  9. dojo.require("dojo.dnd.Selector");
  10. dojo.declare(
  11. "dojox.dnd.Selector",
  12. dojo.dnd.Selector,
  13. {
  14. isSelected: function(node){
  15. // summary:
  16. // checks if node is selected
  17. // node: String|DomNode:
  18. // Node to check (id or DOM Node)
  19. var id = dojo.isString(node) ? node : node.id,
  20. item = this.getItem(id);
  21. return item && this.selected[id]; // Boolean
  22. },
  23. selectNode: function(node, add){
  24. // summary:
  25. // selects a node
  26. // node: String|DomNode:
  27. // Node to select (id or DOM Node)
  28. // add: Boolean?:
  29. // If true, node is added to selection, otherwise current
  30. // selection is removed, and node will be the only selection.
  31. if(!add){
  32. this.selectNone();
  33. }
  34. var id = dojo.isString(node) ? node : node.id,
  35. item = this.getItem(id);
  36. if(item){
  37. this._removeAnchor();
  38. this.anchor = dojo.byId(node);
  39. this._addItemClass(this.anchor, "Anchor");
  40. this.selection[id] = 1;
  41. this._addItemClass(this.anchor, "Selected");
  42. }
  43. return this; // self
  44. },
  45. deselectNode: function(node){
  46. // summary:
  47. // deselects a node
  48. // node: String|DomNode:
  49. // Node to deselect (id or DOM Node)
  50. var id = dojo.isString(node) ? node : node.id,
  51. item = this.getItem(id);
  52. if(item && this.selection[id]){
  53. if(this.anchor === dojo.byId(node)){
  54. this._removeAnchor();
  55. }
  56. delete this.selection[id];
  57. this._removeItemClass(this.anchor, "Selected");
  58. }
  59. return this; // self
  60. },
  61. selectByBBox: function(left, top, right, bottom, add) {
  62. // summary:
  63. // selects nodes by bounding box
  64. // left: Number:
  65. // Left coordinate of the bounding box
  66. // top: Number:
  67. // Top coordinate of the bounding box
  68. // right: Number:
  69. // Right coordinate of the bounding box
  70. // bottom: Number:
  71. // Bottom coordinate of the bounding box
  72. // add: Boolean?:
  73. // If true, node is added to selection, otherwise current
  74. // selection is removed, and node will be the only selection.
  75. // user has drawn a bounding box ... time to see whether any dom nodes
  76. // in this container satisfy the bounding box range.
  77. if(!add){
  78. this.selectNone();
  79. }
  80. this.forInItems(function(data, id){
  81. var node = dojo.byId(id);
  82. if(node && this._isBoundedByBox(node, left, top, right, bottom)){
  83. this.selectNode(id, true);
  84. }
  85. }, this);
  86. return this; // self
  87. },
  88. _isBoundedByBox: function(node, left, top, right, bottom) {
  89. // summary:
  90. // figures out whether certain coodinates bound a particular
  91. // dom node.
  92. // node: String|DomNode:
  93. // Node to check (id or DOM Node)
  94. // left: Number:
  95. // Left coordinate of the bounding box
  96. // top: Number:
  97. // Top coordinate of the bounding box
  98. // right: Number:
  99. // Right coordinate of the bounding box
  100. // bottom: Number:
  101. // Bottom coordinate of the bounding box
  102. var c = dojo.coords(node), t;
  103. // normalize input
  104. if(left > right){
  105. t = left;
  106. left = right;
  107. right = t;
  108. }
  109. if(top > bottom){
  110. t = top;
  111. top = bottom;
  112. bottom = t;
  113. }
  114. return c.x >= left && c.x + c.w <= right && c.y >= top && c.y + c.h <= bottom; // Boolean
  115. },
  116. shift: function(toNext, add) {
  117. // summary:
  118. // shifts the currently selected dnd item forwards and backwards.
  119. // One possible use would be to allow a user select different
  120. // dnd items using the right and left keys.
  121. // toNext: Boolean:
  122. // If true, we select the next node, otherwise the previous one.
  123. // add: Boolean?:
  124. // If true, add to selection, otherwise current selection is
  125. // removed before adding any nodes.
  126. var selectedNodes = this.getSelectedNodes();
  127. if(selectedNodes && selectedNodes.length) {
  128. // only delegate to selectNode if at least one node is selected.
  129. // If multiple nodes are selected assume that we go with
  130. // the last selected node.
  131. this.selectNode(this._getNodeId(selectedNodes[selectedNodes.length - 1].id, toNext), add);
  132. }
  133. },
  134. _getNodeId: function(nodeId, toNext) {
  135. // summary:
  136. // finds a next/previous node in relation to nodeId
  137. // nodeId: String:
  138. // the id of the node to use as the base node
  139. // toNext: Boolean:
  140. // If true, we select the next node, otherwise the previous one.
  141. var allNodes = this.getAllNodes(), newId = nodeId;
  142. for(var i = 0, l = allNodes.length; i < l; ++i) {
  143. if(allNodes[i].id == nodeId) {
  144. // have a match ... make sure we don't go outside
  145. var j = Math.min(l - 1, Math.max(0, i + (toNext ? 1 : -1)));
  146. if(i != j){
  147. // we should be fine to go with the id the user has requested.
  148. newId = allNodes[j].id;
  149. }
  150. break;
  151. }
  152. }
  153. // if we don't get a match, the newId defaults to the currently selected node
  154. return newId;
  155. }
  156. }
  157. );
  158. }