2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.net/yui/license.txt
7
YUI.add('dd-constrain', function(Y) {
10
* The Drag & Drop Utility allows you to create a draggable interface efficiently, buffering you from browser-level abnormalities and enabling you to focus on the interesting logic surrounding your particular implementation. This component enables you to create a variety of standard draggable objects with just a few lines of code and then, using its extensive API, add your own specific implementation logic.
12
* @submodule dd-constrain
15
* This class extends the dd-drag module to add the constraining methods to it. It supports constraining to a region, node or viewport. It also
16
* supports tick based moves and XY axis constraints.
17
* @class DragConstrained
22
var DRAG_NODE = 'dragNode',
23
OFFSET_HEIGHT = 'offsetHeight',
24
OFFSET_WIDTH = 'offsetWidth';
28
C.superclass.constructor.apply(this, arguments);
32
C.NAME = 'dragConstrained';
37
* @description Stick the drag movement to the X-Axis. Default: false
45
* @description Stick the drag movement to the Y-Axis
53
* @description The X tick offset the drag node should snap to on each drag move. False for no ticks. Default: false
61
* @description The Y tick offset the drag node should snap to on each drag move. False for no ticks. Default: false
68
* @attribute tickXArray
69
* @description An array of page coordinates to use as X ticks for drag movement.
76
* @attribute tickYArray
77
* @description An array of page coordinates to use as Y ticks for drag movement.
84
* @attribute constrain2region
85
* @description An Object Literal containing a valid region (top, right, bottom, left) of page positions to constrain the drag node to.
91
if (Y.Lang.isObject(r)) {
100
if (Y.Lang.isObject(r)) {
101
if (r.top && r.right && r.left && r.bottom) {
106
Y.log('constrain2region expecting a valid object with (top, right, bottom, left properties)', 'error', 'DragConstrain');
109
} else if (r !== false) {
110
Y.log('constrain2region expecting an object', 'error', 'DragConstrain');
117
* @description CSS style string for the gutter of a region (supports negative values): '5 0' (sets top and bottom to 5px, left and right to 0px), '1 2 3 4' (top 1px, right 2px, bottom 3px, left 4px)
122
set: function(gutter) {
123
return Y.DD.DDM.cssSizestoObject(gutter);
127
* @attribute constrain2node
128
* @description Will attempt to constrain the drag node to the bounderies of this node.
134
if (!this.get('constrain2region')) {
135
var node = Y.Node.get(n);
139
} else if (this.get('constrain2region') !== false) {
140
Y.log('constrain2region found, can not have both. Node ignored', 'warn', 'DragConstrain');
146
* @attribute constrain2view
147
* @description Will attempt to constrain the drag node to the bounderies of the viewport region.
157
C.superclass.start.apply(this, arguments);
158
this._regionCache = null;
162
* @property _regionCache
163
* @description Store a cache of the region that we are constraining to
169
* @method _cacheRegion
170
* @description Get's the region and caches it, called from window.resize and when the cache is null
172
_cacheRegion: function() {
173
this._regionCache = this.get('constrain2node').get('region');
177
* @description Get the active region: viewport, node, custom region
178
* @param {Boolean} inc Include the node's height and width
181
getRegion: function(inc) {
183
if (this.get('constrain2node')) {
184
if (!this._regionCache) {
185
Y.on('resize', this._cacheRegion, this, true, window);
188
r = Y.clone(this._regionCache);
189
} else if (this.get('constrain2region')) {
190
r = this.get('constrain2region');
191
} else if (this.get('constrain2view')) {
192
r = this.get('node').get('viewportRegion');
196
var g = this.get('gutter');
197
Y.each(g, function(i, n) {
198
if ((n == 'right') || (n == 'bottom')) {
205
var oh = this.get(DRAG_NODE).get(OFFSET_HEIGHT),
206
ow = this.get(DRAG_NODE).get(OFFSET_WIDTH);
207
r.right = r.right - ow;
208
r.bottom = r.bottom - oh;
214
* @method _checkRegion
216
* @param {Array} _xy The XY to check if it's in the current region, if it isn't inside the region, it will reset the xy array to be inside the region.
217
* @return {Array} The new XY that is inside the region
219
_checkRegion: function(_xy) {
221
r = this.getRegion(),
222
oh = this.get(DRAG_NODE).get(OFFSET_HEIGHT),
223
ow = this.get(DRAG_NODE).get(OFFSET_WIDTH);
225
if (oxy[1] > (r.bottom - oh)) {
226
_xy[1] = (r.bottom - oh);
228
if (r.top > oxy[1]) {
232
if (oxy[0] > (r.right - ow)) {
233
_xy[0] = (r.right - ow);
235
if (r.left > oxy[0]) {
243
* @description Checks if the XY passed or the dragNode is inside the active region.
244
* @param {Array} xy Optional XY to check, if not supplied this.get('dragNode').getXY() is used.
245
* @return {Boolean} True if the XY is inside the region, false otherwise.
247
inRegion: function(xy) {
248
xy = xy || this.get(DRAG_NODE).getXY();
250
var _xy = this._checkRegion([xy[0], xy[1]]),
252
if ((xy[0] === _xy[0]) && (xy[1] === _xy[1])) {
260
* @description Override of Drag _align to account for region checking and tick checking
261
* @param {Array} xy The XY to check for ticks and region
262
* @return {Array} The modified XY coords.
264
_align: function(xy) {
265
var _xy = C.superclass._align.apply(this, arguments),
266
r = this.getRegion(true);
268
if (this.get('stickX')) {
269
_xy[1] = (this.startXY[1] - this.deltaXY[1]);
271
if (this.get('stickY')) {
272
_xy[0] = (this.startXY[0] - this.deltaXY[0]);
277
_xy = this._checkRegion(_xy);
280
_xy = this._checkTicks(_xy, r);
286
* @description Helper method to calculate the tick offsets for a given position
287
* @param {Number} pos The current X or Y position
288
* @param {Number} start The start X or Y position
289
* @param {Number} tick The X or Y tick increment
290
* @param {Number} off1 The min offset that we can't pass (region)
291
* @param {Number} off2 The max offset that we can't pass (region)
292
* @return {Number} The new position based on the tick calculation
294
_calcTicks: function(pos, start, tick, off1, off2) {
295
var ix = ((pos - start) / tick),
296
min = Math.floor(ix),
298
if ((min !== 0) || (max !== 0)) {
299
if ((ix >= min) && (ix <= max)) {
300
pos = (start + (tick * min));
303
pos = (start + (tick * (min + 1)));
306
pos = (start + (tick * (min - 1)));
315
* @method _calcTickArray
316
* @description This method is used with the tickXArray and tickYArray config options
317
* @param {Number} pos The current X or Y position
318
* @param {Number} ticks The array containing our custom tick positions.
319
* @param {Number} off1 The min offset that we can't pass (region)
320
* @param {Number} off2 The max offset that we can't pass (region)
321
* @return The tick position
323
_calcTickArray: function(pos, ticks, off1, off2) {
324
var i = 0, len = ticks.length, next = 0;
326
if (!ticks || (ticks.length === 0)) {
328
} else if (ticks[0] >= pos) {
331
for (i = 0; i < len; i++) {
333
if (ticks[next] && ticks[next] >= pos) {
334
var diff1 = pos - ticks[i],
335
diff2 = ticks[next] - pos;
336
var ret = (diff2 > diff1) ? ticks[i] : ticks[next];
342
ret = ticks[len - 1];
350
return ticks[ticks.length - 1];
355
* @method _checkTicks
356
* @description This method delegates the proper helper method for tick calculations
357
* @param {Array} xy The XY coords for the Drag
358
* @param {Object} r The optional region that we are bound to.
359
* @return {Array} The calced XY coords
361
_checkTicks: function(xy, r) {
362
var lx = (this.startXY[0] - this.deltaXY[0]),
363
ly = (this.startXY[1] - this.deltaXY[1]),
364
xt = this.get('tickX'),
365
yt = this.get('tickY');
366
if (xt && !this.get('tickXArray')) {
367
xy[0] = this._calcTicks(xy[0], lx, xt, r.left, r.right);
369
if (yt && !this.get('tickYArray')) {
370
xy[1] = this._calcTicks(xy[1], ly, yt, r.top, r.bottom);
372
if (this.get('tickXArray')) {
373
xy[0] = this._calcTickArray(xy[0], this.get('tickXArray'), r.left, r.right);
375
if (this.get('tickYArray')) {
376
xy[1] = this._calcTickArray(xy[1], this.get('tickYArray'), r.top, r.bottom);
383
Y.extend(C, Y.DD.Drag, proto);
384
//Set this to DD.Drag for other extensions
389
}, '3.0.0pr2' ,{requires:['dd-drag', 'dd-proxy'], skinnable:false});