| 1 |
define(["dojo", "dojox", "dojo/dnd/Selector"], function(dojo, dojox) { |
| 2 |
|
| 3 |
return dojo.declare('dojox.dnd.Selector', dojo.dnd.Selector, { |
| 4 |
|
| 5 |
conservative: true, |
| 6 |
|
| 7 |
isSelected: function(node) { |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
var id = dojo.isString(node) ? node : node.id, |
| 13 |
item = this.getItem(id); |
| 14 |
return item && this.selected[id]; |
| 15 |
}, |
| 16 |
|
| 17 |
selectNode: function(node, add) { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
if (!add) { |
| 26 |
this.selectNone(); |
| 27 |
} |
| 28 |
var id = dojo.isString(node) ? node : node.id, |
| 29 |
item = this.getItem(id); |
| 30 |
if (item) { |
| 31 |
this._removeAnchor(); |
| 32 |
this.anchor = dojo.byId(node); |
| 33 |
this._addItemClass(this.anchor, 'Anchor'); |
| 34 |
this.selection[id] = 1; |
| 35 |
this._addItemClass(this.anchor, 'Selected'); |
| 36 |
} |
| 37 |
return this; |
| 38 |
}, |
| 39 |
|
| 40 |
deselectNode: function(node) { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
var id = dojo.isString(node) ? node : node.id, |
| 46 |
item = this.getItem(id); |
| 47 |
if (item && this.selection[id]) { |
| 48 |
if (this.anchor === dojo.byId(node)) { |
| 49 |
this._removeAnchor(); |
| 50 |
} |
| 51 |
delete this.selection[id]; |
| 52 |
this._removeItemClass(this.anchor, 'Selected'); |
| 53 |
} |
| 54 |
return this; |
| 55 |
}, |
| 56 |
|
| 57 |
selectByBBox: function(left, top, right, bottom, add) { |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
if (!add) { |
| 75 |
this.selectNone(); |
| 76 |
} |
| 77 |
this.forInItems(function(data, id) { |
| 78 |
var node = dojo.byId(id); |
| 79 |
if (node && this._isBoundedByBox(node, left, top, right, bottom)) { |
| 80 |
this.selectNode(id, true); |
| 81 |
} |
| 82 |
}, this); |
| 83 |
return this; |
| 84 |
}, |
| 85 |
|
| 86 |
_isBoundedByBox: function(node, left, top, right, bottom) { |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
return this.conservative ? this._conservativeBBLogic(node, left, top, right, bottom) : this._liberalBBLogic(node, left, top, right, bottom); |
| 101 |
}, |
| 102 |
|
| 103 |
shift: function(toNext, add) { |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
var selectedNodes = this.getSelectedNodes(); |
| 114 |
if (selectedNodes && selectedNodes.length) { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
this.selectNode(this._getNodeId(selectedNodes[selectedNodes.length - 1].id, toNext), add); |
| 119 |
} |
| 120 |
}, |
| 121 |
|
| 122 |
_getNodeId: function(nodeId, toNext) { |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
var allNodes = this.getAllNodes(), newId = nodeId; |
| 130 |
for (var i = 0, l = allNodes.length; i < l; ++i) { |
| 131 |
if (allNodes[i].id == nodeId) { |
| 132 |
|
| 133 |
var j = Math.min(l - 1, Math.max(0, i + (toNext ? 1 : -1))); |
| 134 |
if (i != j) { |
| 135 |
|
| 136 |
newId = allNodes[j].id; |
| 137 |
} |
| 138 |
break; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return newId; |
| 143 |
}, |
| 144 |
|
| 145 |
_conservativeBBLogic: function(node, left, top, right, bottom) { |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
var c = dojo.coords(node), t; |
| 152 |
|
| 153 |
if (left > right) { |
| 154 |
t = left; |
| 155 |
left = right; |
| 156 |
right = t; |
| 157 |
} |
| 158 |
if (top > bottom) { |
| 159 |
t = top; |
| 160 |
top = bottom; |
| 161 |
bottom = t; |
| 162 |
} |
| 163 |
return c.x >= left && c.x + c.w <= right && c.y >= top && c.y + c.h <= bottom; |
| 164 |
}, |
| 165 |
|
| 166 |
_liberalBBLogic: function(node, left, top, right, bottom) { |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
var c = dojo.position(node), xBounded, yBounded, tlx, tly, brx, bry, leftGreater = false, bottomGreater = false, |
| 174 |
nodeTlx = c.x, nodeTly = c.y, nodeBrx = c.x + c.w, nodeBry = c.y + c.h; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
if (left < right) { |
| 180 |
tlx = left; |
| 181 |
tly = top; |
| 182 |
} else { |
| 183 |
leftGreater = true; |
| 184 |
tlx = right; |
| 185 |
tly = bottom; |
| 186 |
} |
| 187 |
if (top < bottom) { |
| 188 |
bottomGreater = true; |
| 189 |
brx = right; |
| 190 |
bry = bottom; |
| 191 |
} else { |
| 192 |
brx = left; |
| 193 |
bry = top; |
| 194 |
tlx = right; |
| 195 |
tly = bottom; |
| 196 |
} |
| 197 |
if (leftGreater && bottomGreater) { |
| 198 |
|
| 199 |
brx = left; |
| 200 |
bry = bottom; |
| 201 |
tlx = right; |
| 202 |
tly = top; |
| 203 |
} |
| 204 |
xBounded = (nodeTlx >= tlx || nodeBrx <= brx) && (tlx <= nodeBrx && brx >= nodeTlx) || (nodeTlx <= tlx && nodeBrx >= brx); |
| 205 |
yBounded = (tly <= nodeBry && bry >= nodeTly) || (nodeBry >= bry && nodeTly <= bry); |
| 206 |
return xBounded && yBounded; |
| 207 |
} |
| 208 |
} |
| 209 |
); |
| 210 |
}); |