/**
* 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.
* @module dd
* @main dd
* @submodule dd-constrain
*/
/**
* Plugin for the dd-drag module to add the constraining methods to it. It supports constraining to a node or viewport. It supports tick based moves and XY axis constraints.
* @class DDConstrained
* @extends Base
* @constructor
* @namespace Plugin
*/
var DRAG_NODE = 'dragNode',
OFFSET_HEIGHT = 'offsetHeight',
OFFSET_WIDTH = 'offsetWidth',
HOST = 'host',
TICK_X_ARRAY = 'tickXArray',
TICK_Y_ARRAY = 'tickYArray',
TOP = 'top',
RIGHT = 'right',
BOTTOM = 'bottom',
LEFT = 'left',
VIEW = 'view',
proto = null,
/**
* @event drag:tickAlignX
* @description Fires when this node is aligned with the tickX value.
* @param {EventFacade} event An Event Facade object
* @type {CustomEvent}
*/
EV_TICK_ALIGN_X = 'drag:tickAlignX',
/**
* @event drag:tickAlignY
* @description Fires when this node is aligned with the tickY value.
* @param {EventFacade} event An Event Facade object
* @type {CustomEvent}
*/
EV_TICK_ALIGN_Y = 'drag:tickAlignY',
C = function(config) {
this._lazyAddAttrs = false;
};
C.NAME = 'ddConstrained';
/**
* @property NS
* @default con
* @readonly
* @protected
* @static
* @description The Constrained instance will be placed on the Drag instance under the con namespace.
* @type {String}
*/
C.NS = 'con';
C.ATTRS = {
host: {
},
/**
* @attribute stickX
* @description Stick the drag movement to the X-Axis. Default: false
* @type Boolean
*/
stickX: {
value: false
},
/**
* @attribute stickY
* @description Stick the drag movement to the Y-Axis
* @type Boolean
*/
stickY: {
value: false
},
/**
* @attribute tickX
* @description The X tick offset the drag node should snap to on each drag move. False for no ticks. Default: false
*/
tickX: {
value: false
},
/**
* @attribute tickY
* @description The Y tick offset the drag node should snap to on each drag move. False for no ticks. Default: false
*/
tickY: {
value: false
},
/**
* @attribute tickXArray
* @description An array of page coordinates to use as X ticks for drag movement.
* @type Array
*/
tickXArray: {
value: false
},
/**
* @attribute tickYArray
* @description An array of page coordinates to use as Y ticks for drag movement.
* @type Array
*/
tickYArray: {
value: false
},
/**
* @attribute gutter
* @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)
* @type String
*/
gutter: {
value: '0',
}
},
/**
* @attribute constrain
* @description Will attempt to constrain the drag node to the boundaries. Arguments:<br>
* 'view': Contrain to Viewport<br>
* '#selector_string': Constrain to this node<br>
* '{Region Object}': An Object Literal containing a valid region (top, right, bottom, left) of page positions
*/
constrain: {
if (node) {
}
return con;
}
},
/**
* @deprecated
* @attribute constrain2region
* @description An Object Literal containing a valid region (top, right, bottom, left) of page positions to constrain the drag node to.
* @type Object
*/
setter: function(r) {
return this.set('constrain', r);
}
},
/**
* @deprecated
* @attribute constrain2node
* @description Will attempt to constrain the drag node to the boundaries of this node.
* @type Object
*/
setter: function(n) {
}
},
/**
* @deprecated
* @attribute constrain2view
* @description Will attempt to constrain the drag node to the boundaries of the viewport region.
* @type Object
*/
setter: function(n) {
}
},
/**
* @attribute cacheRegion
* @description Should the region be cached for performace. Default: true
* @type Boolean
*/
cacheRegion: {
value: true
}
};
proto = {
_lastTickXFired: null,
_lastTickYFired: null,
initializer: function() {
this._createEvents();
},
destructor: function() {
if (this._cacheHandle) {
this._cacheHandle.detach();
}
this._cacheHandle = null;
},
/**
* @private
* @method _createEvents
* @description This method creates all the events for this Event Target and publishes them so we get Event Bubbling.
*/
_createEvents: function() {
var instance = this;
var ev = [
];
this.publish(v, {
type: v,
emitFacade: true,
bubbles: true,
queuable: false,
prefix: 'drag'
});
}, this);
},
/**
* @private
* @method _handleEnd
* @description Fires on drag:end
*/
_handleEnd: function() {
this._lastTickYFired = null;
this._lastTickXFired = null;
},
/**
* @private
* @method _handleStart
* @description Fires on drag:start and clears the _regionCache
*/
_handleStart: function() {
this.resetCache();
},
/**
* @private
* @property _regionCache
* @description Store a cache of the region that we are constraining to
* @type Object
*/
_regionCache: null,
/**
* Event handle for window resize event.
* @private
* @property _cacheHandle
* @type {Event}
*/
_cacheHandle: null,
/**
* @private
* @method _cacheRegion
* @description Get's the region and caches it, called from window.resize and when the cache is null
*/
_cacheRegion: function() {
},
/**
* @method resetCache
* @description Reset the internal region cache.
*/
resetCache: function() {
this._regionCache = null;
},
/**
* @private
* @method _getConstraint
* @description Standardizes the 'constraint' attribute
*/
_getConstraint: function() {
g = this.get('gutter'),
if (con) {
if (!this._regionCache) {
this._cacheRegion();
}
if (!this.get('cacheRegion')) {
this.resetCache();
}
}
}
}
}
Y.each(g, function(i, n) {
region[n] -= i;
} else {
region[n] += i;
}
});
return region;
},
/**
* @method getRegion
* @description Get the active region: viewport, node, custom region
* @param {Boolean} inc Include the node's height and width
* @return {Object} The active region.
*/
r = this._getConstraint();
if (inc) {
}
return r;
},
/**
* @private
* @method _checkRegion
* @description Check if xy is inside a given region, if not change to it be inside.
* @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.
* @return {Array} The new XY that is inside the region
*/
_checkRegion: function(_xy) {
r = this.getRegion(),
}
}
}
}
return _xy;
},
/**
* @method inRegion
* @description Checks if the XY passed or the dragNode is inside the active region.
* @param {Array} xy Optional XY to check, if not supplied this.get('dragNode').getXY() is used.
* @return {Boolean} True if the XY is inside the region, false otherwise.
*/
inside = false;
inside = true;
}
return inside;
},
/**
* @method align
* @description Modifies the Drag.actXY method from the after drag:align event. This is where the constraining happens.
*/
align: function() {
r = this.getRegion(true);
if (this.get('stickX')) {
}
if (this.get('stickY')) {
}
if (r) {
}
},
/**
* @method drag
* @description Fires after drag:drag. Handle the tickX and tickX align events.
*/
this._tickAlignX();
}
this._tickAlignY();
}
},
/**
* @private
* @method _checkTicks
* @description This method delegates the proper helper method for tick calculations
* @param {Array} xy The XY coords for the Drag
* @param {Object} r The optional region that we are bound to.
* @return {Array} The calced XY coords
*/
_checkTicks: function(xy, r) {
}
}
if (this.get(TICK_X_ARRAY)) {
}
if (this.get(TICK_Y_ARRAY)) {
}
return xy;
},
/**
* @private
* @method _tickAlignX
* @description Fires when the actXY[0] reach a new value respecting the tickX gap.
*/
_tickAlignX: function() {
this.fire(EV_TICK_ALIGN_X);
},
/**
* @private
* @method _tickAlignY
* @description Fires when the actXY[1] reach a new value respecting the tickY gap.
*/
_tickAlignY: function() {
this.fire(EV_TICK_ALIGN_Y);
}
};
Y.namespace('Plugin');
Y.Plugin.DDConstrained = C;
/**
* @for DDM
* @namespace DD
* @private
* @method _calcTicks
* @description Helper method to calculate the tick offsets for a given position
* @param {Number} pos The current X or Y position
* @param {Number} start The start X or Y position
* @param {Number} tick The X or Y tick increment
* @param {Number} off1 The min offset that we can't pass (region)
* @param {Number} off2 The max offset that we can't pass (region)
* @return {Number} The new position based on the tick calculation
*/
}
}
}
}
}
return pos;
},
/**
* @for DDM
* @namespace DD
* @private
* @method _calcTickArray
* @description This method is used with the tickXArray and tickYArray config options
* @param {Number} pos The current X or Y position
* @param {Number} ticks The array containing our custom tick positions.
* @param {Number} off1 The min offset that we can't pass (region)
* @param {Number} off2 The max offset that we can't pass (region)
* @return The tick position
*/
return pos;
return ticks[0];
} else {
for (i = 0; i < len; i++) {
next = (i + 1);
if (ticks[i]) {
} else {
}
}
}
return ret;
}
}
}
}
});