2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('resize-base', function(Y) {
11 * The Resize Utility allows you to make an HTML element resizable.
17 isArray = Lang.isArray,
18 isBoolean = Lang.isBoolean,
19 isNumber = Lang.isNumber,
20 isString = Lang.isString,
24 indexOf = YArray.indexOf,
29 HANDLE_SUB = '{handle}',
33 ACTIVE_HANDLE = 'activeHandle',
34 ACTIVE_HANDLE_NODE = 'activeHandleNode',
36 AUTO_HIDE = 'autoHide',
39 CLASS_NAME = 'className',
41 DEF_MIN_HEIGHT = 'defMinHeight',
42 DEF_MIN_WIDTH = 'defMinWidth',
50 NODE_NAME = 'nodeName',
52 OFFSET_HEIGHT = 'offsetHeight',
53 OFFSET_WIDTH = 'offsetWidth',
55 PARENT_NODE = 'parentNode',
56 POSITION = 'position',
57 RELATIVE = 'relative',
59 RESIZING = 'resizing',
67 WRAP_TYPES = 'wrapTypes',
69 EV_MOUSE_UP = 'resize:mouseUp',
70 EV_RESIZE = 'resize:resize',
71 EV_RESIZE_ALIGN = 'resize:align',
72 EV_RESIZE_END = 'resize:end',
73 EV_RESIZE_START = 'resize:start',
85 return Array.prototype.slice.call(arguments).join(SPACE);
88 // round the passed number to get rid of pixel-flickering
89 toRoundNumber = function(num) {
90 return Math.round(parseFloat(num)) || 0;
93 getCompStyle = function(node, val) {
94 return node.getComputedStyle(val);
97 handleAttrName = function(handle) {
98 return HANDLE + handle.toUpperCase();
101 isNode = function(v) {
102 return (v instanceof Y.Node);
105 toInitialCap = Y.cached(
107 return str.substring(0, 1).toUpperCase() + str.substring(1);
111 capitalize = Y.cached(function() {
113 args = YArray(arguments, 0, true);
115 YArray.each(args, function(part, i) {
117 part = toInitialCap(part);
122 return out.join(EMPTY_STR);
125 getCN = Y.ClassNameManager.getClassName,
127 CSS_RESIZE = getCN(RESIZE),
128 CSS_RESIZE_HANDLE = getCN(RESIZE, HANDLE),
129 CSS_RESIZE_HANDLE_ACTIVE = getCN(RESIZE, HANDLE, ACTIVE),
130 CSS_RESIZE_HANDLE_INNER = getCN(RESIZE, HANDLE, INNER),
131 CSS_RESIZE_HANDLE_INNER_PLACEHOLDER = getCN(RESIZE, HANDLE, INNER, HANDLE_SUB),
132 CSS_RESIZE_HANDLE_PLACEHOLDER = getCN(RESIZE, HANDLE, HANDLE_SUB),
133 CSS_RESIZE_HIDDEN_HANDLES = getCN(RESIZE, HIDDEN, HANDLES),
134 CSS_RESIZE_WRAPPER = getCN(RESIZE, WRAPPER);
137 * A base class for Resize, providing:
139 * <li>Basic Lifecycle (initializer, renderUI, bindUI, syncUI, destructor)</li>
140 * <li>Applies drag handles to an element to make it resizable</li>
141 * <li>Here is the list of valid resize handles:
142 * <code>[ 't', 'tr', 'r', 'br', 'b', 'bl', 'l', 'tl' ]</code>. You can
143 * read this list as top, top-right, right, bottom-right, bottom,
144 * bottom-left, left, top-left.</li>
145 * <li>The drag handles are inserted into the element and positioned
146 * absolute. Some elements, such as a textarea or image, don't support
147 * children. To overcome that, set wrap:true in your config and the
148 * element willbe wrapped for you automatically.</li>
153 * <pre><code>var instance = new Y.Resize({
155 * preserveRatio: true,
159 * handles: 't, tr, r, br, b, bl, l, tl'
163 * Check the list of <a href="Resize.html#configattributes">Configuration Attributes</a> available for
167 * @param config {Object} Object literal specifying widget configuration properties.
172 Resize.superclass.constructor.apply(this, arguments);
177 * Static property provides a string to identify the class.
179 * @property Resize.NAME
186 * Static property used to define the default attribute
187 * configuration for the Resize.
189 * @property Resize.ATTRS
195 * Stores the active handle during the resize.
197 * @attribute activeHandle
204 validator: function(v) {
205 return Y.Lang.isString(v) || Y.Lang.isNull(v);
210 * Stores the active handle element during the resize.
212 * @attribute activeHandleNode
223 * False to ensure that the resize handles are always visible, true to
224 * display them only when the user mouses over the resizable borders.
226 * @attribute autoHide
236 * The default minimum height of the element. Only used when
237 * ResizeConstrained is not plugged.
239 * @attribute defMinHeight
249 * The default minimum width of the element. Only used when
250 * ResizeConstrained is not plugged.
252 * @attribute defMinWidth
262 * The handles to use (any combination of): 't', 'b', 'r', 'l', 'bl',
263 * 'br', 'tl', 'tr'. Can use a shortcut of All.
267 * @type Array | String
270 setter: '_setHandles',
275 * The selector or element to resize. Required.
285 * True when the element is being Resized.
287 * @attribute resizing
297 * True to wrap an element with a div if needed (required for textareas
298 * and images, defaults to false) in favor of the handles config option.
299 * The wrapper element type (default div) could be over-riden passing the
300 * <code>wrapper</code> attribute.
313 * Elements that requires a wrapper by default. Normally are elements
314 * which cannot have children elements.
316 * @attribute wrapTypes
317 * @default /canvas|textarea|input|select|button|img/i
323 value: /^canvas|textarea|input|select|button|img|iframe|table|embed$/i
327 * Element to wrap the <code>wrapTypes</code>. This element will house
328 * the handles elements.
332 * @type String | Node
337 valueFn: '_valueWrapper',
343 b: function(instance, dx, dy) {
344 var info = instance.info,
345 originalInfo = instance.originalInfo;
347 info.offsetHeight = originalInfo.offsetHeight + dy;
350 l: function(instance, dx, dy) {
351 var info = instance.info,
352 originalInfo = instance.originalInfo;
354 info.left = originalInfo.left + dx;
355 info.offsetWidth = originalInfo.offsetWidth - dx;
358 r: function(instance, dx, dy) {
359 var info = instance.info,
360 originalInfo = instance.originalInfo;
362 info.offsetWidth = originalInfo.offsetWidth + dx;
365 t: function(instance, dx, dy) {
366 var info = instance.info,
367 originalInfo = instance.originalInfo;
369 info.top = originalInfo.top + dy;
370 info.offsetHeight = originalInfo.offsetHeight - dy;
373 tr: function(instance, dx, dy) {
374 this.t.apply(this, arguments);
375 this.r.apply(this, arguments);
378 bl: function(instance, dx, dy) {
379 this.b.apply(this, arguments);
380 this.l.apply(this, arguments);
383 br: function(instance, dx, dy) {
384 this.b.apply(this, arguments);
385 this.r.apply(this, arguments);
388 tl: function(instance, dx, dy) {
389 this.t.apply(this, arguments);
390 this.l.apply(this, arguments);
394 capitalize: capitalize
402 * Array containing all possible resizable handles.
404 * @property ALL_HANDLES
407 ALL_HANDLES: [ T, TR, R, BR, B, BL, L, TL ],
410 * Regex which matches with the handles that could change the height of
411 * the resizable element.
413 * @property REGEX_CHANGE_HEIGHT
416 REGEX_CHANGE_HEIGHT: /^(t|tr|b|bl|br|tl)$/i,
419 * Regex which matches with the handles that could change the left of
420 * the resizable element.
422 * @property REGEX_CHANGE_LEFT
425 REGEX_CHANGE_LEFT: /^(tl|l|bl)$/i,
428 * Regex which matches with the handles that could change the top of
429 * the resizable element.
431 * @property REGEX_CHANGE_TOP
434 REGEX_CHANGE_TOP: /^(tl|t|tr)$/i,
437 * Regex which matches with the handles that could change the width of
438 * the resizable element.
440 * @property REGEX_CHANGE_WIDTH
443 REGEX_CHANGE_WIDTH: /^(bl|br|l|r|tl|tr)$/i,
446 * Template used to create the resize wrapper node when needed.
448 * @property WRAP_TEMPLATE
451 WRAP_TEMPLATE: '<div class="'+CSS_RESIZE_WRAPPER+'"></div>',
454 * Template used to create each resize handle.
456 * @property HANDLE_TEMPLATE
459 HANDLE_TEMPLATE: '<div class="'+concat(CSS_RESIZE_HANDLE, CSS_RESIZE_HANDLE_PLACEHOLDER)+'">' +
460 '<div class="'+concat(CSS_RESIZE_HANDLE_INNER, CSS_RESIZE_HANDLE_INNER_PLACEHOLDER)+'"> </div>' +
465 * Each box has a content area and optional surrounding padding and
466 * border areas. This property stores the sum of all horizontal
467 * surrounding * information needed to adjust the node height.
469 * @property totalHSurrounding
473 totalHSurrounding: 0,
476 * Each box has a content area and optional surrounding padding and
477 * border areas. This property stores the sum of all vertical
478 * surrounding * information needed to adjust the node height.
480 * @property totalVSurrounding
484 totalVSurrounding: 0,
487 * Stores the <a href="Resize.html#config_node">node</a>
488 * surrounding information retrieved from
489 * <a href="Resize.html#method__getBoxSurroundingInfo">_getBoxSurroundingInfo</a>.
491 * @property nodeSurrounding
495 nodeSurrounding: null,
498 * Stores the <a href="Resize.html#config_wrapper">wrapper</a>
499 * surrounding information retrieved from
500 * <a href="Resize.html#method__getBoxSurroundingInfo">_getBoxSurroundingInfo</a>.
502 * @property wrapperSurrounding
506 wrapperSurrounding: null,
509 * Whether the handle being dragged can change the height.
511 * @property changeHeightHandles
515 changeHeightHandles: false,
518 * Whether the handle being dragged can change the left.
520 * @property changeLeftHandles
524 changeLeftHandles: false,
527 * Whether the handle being dragged can change the top.
529 * @property changeTopHandles
533 changeTopHandles: false,
536 * Whether the handle being dragged can change the width.
538 * @property changeWidthHandles
542 changeWidthHandles: false,
545 * Store DD.Delegate reference for the respective Resize instance.
554 * Stores the current values for the height, width, top and left. You are
555 * able to manipulate these values on resize in order to change the resize
565 * Stores the last values for the height, width, top and left.
574 * Stores the original values for the height, width, top and left, stored
577 * @property originalInfo
584 * Construction logic executed during Resize instantiation. Lifecycle.
586 * @method initializer
589 initializer: function() {
594 * Create the DOM structure for the Resize. Lifecycle.
599 renderUI: function() {
602 instance._renderHandles();
606 * Bind the events on the Resize UI. Lifecycle.
614 instance._createEvents();
616 instance._bindHandle();
620 * Sync the Resize UI.
628 this.get(NODE).addClass(CSS_RESIZE);
630 // hide handles if AUTO_HIDE is true
631 instance._setHideHandlesUI(
632 instance.get(AUTO_HIDE)
637 * Descructor lifecycle implementation for the Resize class. Purges events attached
638 * to the node (and all child nodes) and removes the Resize handles.
643 destructor: function() {
645 node = instance.get(NODE),
646 wrapper = instance.get(WRAPPER),
647 pNode = wrapper.get(PARENT_NODE);
649 // purgeElements on boundingBox
650 Y.Event.purgeElement(wrapper, true);
652 // destroy handles dd and remove them from the dom
653 instance.eachHandle(function(handleEl) {
654 instance.delegate.dd.destroy();
657 handleEl.remove(true);
661 if (instance.get(WRAP)) {
662 instance._copyStyles(wrapper, node);
665 pNode.insertBefore(node, wrapper);
668 wrapper.remove(true);
671 node.removeClass(CSS_RESIZE);
672 node.removeClass(CSS_RESIZE_HIDDEN_HANDLES);
676 * Creates DOM (or manipulates DOM for progressive enhancement)
677 * This method is invoked by initializer(). It's chained automatically for
678 * subclasses if required.
683 renderer: function() {
690 * <p>Loop through each handle which is being used and executes a callback.</p>
692 * <pre><code>instance.eachHandle(
693 * function(handleName, index) { ... }
697 * @param {function} fn Callback function to be executed for each handle.
699 eachHandle: function(fn) {
703 instance.get(HANDLES),
704 function(handle, i) {
705 var handleEl = instance.get(
706 handleAttrName(handle)
709 fn.apply(instance, [handleEl, handle, i]);
715 * Bind the handles DragDrop events to the Resize instance.
720 _bindDD: function() {
723 instance.delegate = new Y.DD.Delegate(
725 bubbleTargets: instance,
726 container: instance.get(WRAPPER),
733 nodes: DOT+CSS_RESIZE_HANDLE,
738 instance.on('drag:drag', instance._handleResizeEvent);
739 instance.on('drag:dropmiss', instance._handleMouseUpEvent);
740 instance.on('drag:end', instance._handleResizeEndEvent);
741 instance.on('drag:start', instance._handleResizeStartEvent);
745 * Bind the events related to the handles (_onHandleMouseEnter, _onHandleMouseLeave).
747 * @method _bindHandle
750 _bindHandle: function() {
752 wrapper = instance.get(WRAPPER);
754 wrapper.on('mouseenter', Y.bind(instance._onWrapperMouseEnter, instance));
755 wrapper.on('mouseleave', Y.bind(instance._onWrapperMouseLeave, instance));
756 wrapper.delegate('mouseenter', Y.bind(instance._onHandleMouseEnter, instance), DOT+CSS_RESIZE_HANDLE);
757 wrapper.delegate('mouseleave', Y.bind(instance._onHandleMouseLeave, instance), DOT+CSS_RESIZE_HANDLE);
761 * Create the custom events used on the Resize.
763 * @method _createEvents
766 _createEvents: function() {
768 // create publish function for kweight optimization
769 publish = function(name, fn) {
770 instance.publish(name, {
780 * Handles the resize start event. Fired when a handle starts to be
783 * @event resize:start
784 * @preventable _defResizeStartFn
785 * @param {Event.Facade} event The resize start event.
787 * @type {Event.Custom}
789 publish(EV_RESIZE_START, this._defResizeStartFn);
792 * Handles the resize event. Fired on each pixel when the handle is
795 * @event resize:resize
796 * @preventable _defResizeFn
797 * @param {Event.Facade} event The resize event.
799 * @type {Event.Custom}
801 publish(EV_RESIZE, this._defResizeFn);
804 * Handles the resize align event.
806 * @event resize:align
807 * @preventable _defResizeAlignFn
808 * @param {Event.Facade} event The resize align event.
810 * @type {Event.Custom}
812 publish(EV_RESIZE_ALIGN, this._defResizeAlignFn);
815 * Handles the resize end event. Fired when a handle stop to be
819 * @preventable _defResizeEndFn
820 * @param {Event.Facade} event The resize end event.
822 * @type {Event.Custom}
824 publish(EV_RESIZE_END, this._defResizeEndFn);
827 * Handles the resize mouseUp event. Fired when a mouseUp event happens on a
830 * @event resize:mouseUp
831 * @preventable _defMouseUpFn
832 * @param {Event.Facade} event The resize mouseUp event.
834 * @type {Event.Custom}
836 publish(EV_MOUSE_UP, this._defMouseUpFn);
840 * Responsible for loop each handle element and append to the wrapper.
842 * @method _renderHandles
845 _renderHandles: function() {
847 wrapper = instance.get(WRAPPER);
849 instance.eachHandle(function(handleEl) {
850 wrapper.append(handleEl);
855 * Creates the handle element based on the handle name and initialize the
858 * @method _buildHandle
859 * @param {String} handle Handle name ('t', 'tr', 'b', ...).
862 _buildHandle: function(handle) {
865 return Y.Node.create(
866 Y.substitute(instance.HANDLE_TEMPLATE, {
873 * Basic resize calculations.
875 * @method _calcResize
878 _calcResize: function() {
880 handle = instance.handle,
881 info = instance.info,
882 originalInfo = instance.originalInfo,
884 dx = info.actXY[0] - originalInfo.actXY[0],
885 dy = info.actXY[1] - originalInfo.actXY[1];
887 if (handle && Y.Resize.RULES[handle]) {
888 Y.Resize.RULES[handle](instance, dx, dy);
895 * Helper method to update the current size value on
896 * <a href="Resize.html#property_info">info</a> to respect the
897 * min/max values and fix the top/left calculations.
900 * @param {String} offset 'offsetHeight' or 'offsetWidth'
901 * @param {number} size Size to restrict the offset
904 _checkSize: function(offset, size) {
906 info = instance.info,
907 originalInfo = instance.originalInfo,
908 axis = (offset == OFFSET_HEIGHT) ? TOP : LEFT;
910 // forcing the offsetHeight/offsetWidth to be the passed size
913 // predicting, based on the original information, the last left valid in case of reach the min/max dimension
914 // this calculation avoid browser event leaks when user interact very fast
915 if (((axis == LEFT) && instance.changeLeftHandles) ||
916 ((axis == TOP) && instance.changeTopHandles)) {
918 info[axis] = originalInfo[axis] + originalInfo[offset] - size;
923 * Copy relevant styles of the <a href="Resize.html#config_node">node</a>
924 * to the <a href="Resize.html#config_wrapper">wrapper</a>.
926 * @method _copyStyles
927 * @param {Node} node Node from.
928 * @param {Node} wrapper Node to.
931 _copyStyles: function(node, wrapper) {
932 var position = node.getStyle(POSITION).toLowerCase(),
933 surrounding = this._getBoxSurroundingInfo(node),
936 // resizable wrapper should be positioned
937 if (position == STATIC) {
943 left: getCompStyle(node, LEFT),
944 top: getCompStyle(node, TOP)
947 Y.mix(wrapperStyle, surrounding.margin);
948 Y.mix(wrapperStyle, surrounding.border);
950 wrapper.setStyles(wrapperStyle);
952 // remove margin and border from the internal node
953 node.setStyles({ border: 0, margin: 0 });
956 node.get(OFFSET_WIDTH) + surrounding.totalHBorder,
957 node.get(OFFSET_HEIGHT) + surrounding.totalVBorder
961 // extract handle name from a string
962 // using Y.cached to memoize the function for performance
963 _extractHandleName: Y.cached(
965 var className = node.get(CLASS_NAME),
967 match = className.match(
969 getCN(RESIZE, HANDLE, '(\\w{1,2})\\b')
973 return match ? match[1] : null;
978 * <p>Generates metadata to the <a href="Resize.html#property_info">info</a>
979 * and <a href="Resize.html#property_originalInfo">originalInfo</a></p>
980 * <pre><code>bottom, actXY, left, top, offsetHeight, offsetWidth, right</code></pre>
984 * @param {EventFacade} event
987 _getInfo: function(node, event) {
989 drag = event.dragEvent.target,
990 nodeXY = node.getXY(),
993 offsetHeight = node.get(OFFSET_HEIGHT),
994 offsetWidth = node.get(OFFSET_WIDTH);
997 // the xy that the node will be set to. Changing this will alter the position as it's dragged.
998 actXY = (drag.actXY.length ? drag.actXY : drag.lastXY);
1003 bottom: (nodeY + offsetHeight),
1005 offsetHeight: offsetHeight,
1006 offsetWidth: offsetWidth,
1007 right: (nodeX + offsetWidth),
1013 * Each box has a content area and optional surrounding margin,
1014 * padding and * border areas. This method get all this information from
1015 * the passed node. For more reference see
1016 * <a href="http://www.w3.org/TR/CSS21/box.html#box-dimensions">
1017 * http://www.w3.org/TR/CSS21/box.html#box-dimensions</a>.
1019 * @method _getBoxSurroundingInfo
1020 * @param {Node} node
1024 _getBoxSurroundingInfo: function(node) {
1032 Y.each([ TOP, RIGHT, BOTTOM, LEFT ], function(dir) {
1033 var paddingProperty = capitalize(PADDING, dir),
1034 marginProperty = capitalize(MARGIN, dir),
1035 borderWidthProperty = capitalize(BORDER, dir, WIDTH),
1036 borderColorProperty = capitalize(BORDER, dir, COLOR),
1037 borderStyleProperty = capitalize(BORDER, dir, STYLE);
1039 info.border[borderColorProperty] = getCompStyle(node, borderColorProperty);
1040 info.border[borderStyleProperty] = getCompStyle(node, borderStyleProperty);
1041 info.border[borderWidthProperty] = getCompStyle(node, borderWidthProperty);
1042 info.margin[marginProperty] = getCompStyle(node, marginProperty);
1043 info.padding[paddingProperty] = getCompStyle(node, paddingProperty);
1047 info.totalHBorder = (toRoundNumber(info.border.borderLeftWidth) + toRoundNumber(info.border.borderRightWidth));
1048 info.totalHPadding = (toRoundNumber(info.padding.paddingLeft) + toRoundNumber(info.padding.paddingRight));
1049 info.totalVBorder = (toRoundNumber(info.border.borderBottomWidth) + toRoundNumber(info.border.borderTopWidth));
1050 info.totalVPadding = (toRoundNumber(info.padding.paddingBottom) + toRoundNumber(info.padding.paddingTop));
1056 * Sync the Resize UI with internal values from
1057 * <a href="Resize.html#property_info">info</a>.
1062 _syncUI: function() {
1063 var instance = this,
1064 info = instance.info,
1065 wrapperSurrounding = instance.wrapperSurrounding,
1066 wrapper = instance.get(WRAPPER),
1067 node = instance.get(NODE);
1069 wrapper.sizeTo(info.offsetWidth, info.offsetHeight);
1071 if (instance.changeLeftHandles || instance.changeTopHandles) {
1072 wrapper.setXY([info.left, info.top]);
1075 // if a wrap node is being used
1076 if (!wrapper.compareTo(node)) {
1077 // the original internal node borders were copied to the wrapper on _copyStyles, to compensate that subtract the borders from the internal node
1079 info.offsetWidth - wrapperSurrounding.totalHBorder,
1080 info.offsetHeight - wrapperSurrounding.totalVBorder
1084 // prevent webkit textarea resize
1086 node.setStyle(RESIZE, NONE);
1091 * Update <code>instance.changeHeightHandles,
1092 * instance.changeLeftHandles, instance.changeTopHandles,
1093 * instance.changeWidthHandles</code> information.
1095 * @method _updateChangeHandleInfo
1098 _updateChangeHandleInfo: function(handle) {
1099 var instance = this;
1101 instance.changeHeightHandles = instance.REGEX_CHANGE_HEIGHT.test(handle);
1102 instance.changeLeftHandles = instance.REGEX_CHANGE_LEFT.test(handle);
1103 instance.changeTopHandles = instance.REGEX_CHANGE_TOP.test(handle);
1104 instance.changeWidthHandles = instance.REGEX_CHANGE_WIDTH.test(handle);
1108 * Update <a href="Resize.html#property_info">info</a> values (bottom, actXY, left, top, offsetHeight, offsetWidth, right).
1110 * @method _updateInfo
1113 _updateInfo: function(event) {
1114 var instance = this;
1116 instance.info = instance._getInfo(instance.get(WRAPPER), event);
1121 * <a href="Resize.html#property_nodeSurrounding">nodeSurrounding</a>,
1122 * <a href="Resize.html#property_nodeSurrounding">wrapperSurrounding</a>,
1123 * <a href="Resize.html#property_nodeSurrounding">totalVSurrounding</a>,
1124 * <a href="Resize.html#property_nodeSurrounding">totalHSurrounding</a>.
1126 * @method _updateSurroundingInfo
1129 _updateSurroundingInfo: function() {
1130 var instance = this,
1131 node = instance.get(NODE),
1132 wrapper = instance.get(WRAPPER),
1133 nodeSurrounding = instance._getBoxSurroundingInfo(node),
1134 wrapperSurrounding = instance._getBoxSurroundingInfo(wrapper);
1136 instance.nodeSurrounding = nodeSurrounding;
1137 instance.wrapperSurrounding = wrapperSurrounding;
1139 instance.totalVSurrounding = (nodeSurrounding.totalVPadding + wrapperSurrounding.totalVBorder);
1140 instance.totalHSurrounding = (nodeSurrounding.totalHPadding + wrapperSurrounding.totalHBorder);
1144 * Set the active state of the handles.
1146 * @method _setActiveHandlesUI
1147 * @param {boolean} val True to activate the handles, false to deactivate.
1150 _setActiveHandlesUI: function(val) {
1151 var instance = this,
1152 activeHandleNode = instance.get(ACTIVE_HANDLE_NODE);
1154 if (activeHandleNode) {
1156 // remove CSS_RESIZE_HANDLE_ACTIVE from all handles before addClass on the active
1157 instance.eachHandle(
1158 function(handleEl) {
1159 handleEl.removeClass(CSS_RESIZE_HANDLE_ACTIVE);
1163 activeHandleNode.addClass(CSS_RESIZE_HANDLE_ACTIVE);
1166 activeHandleNode.removeClass(CSS_RESIZE_HANDLE_ACTIVE);
1172 * Setter for the handles attribute
1174 * @method _setHandles
1176 * @param {String} val
1178 _setHandles: function(val) {
1179 var instance = this,
1182 // handles attr accepts both array or string
1186 else if (isString(val)) {
1187 // if the handles attr passed in is an ALL string...
1188 if (val.toLowerCase() == ALL) {
1189 handles = instance.ALL_HANDLES;
1191 // otherwise, split the string to extract the handles
1196 var handle = trim(node);
1198 // if its a valid handle, add it to the handles output
1199 if (indexOf(instance.ALL_HANDLES, handle) > -1) {
1200 handles.push(handle);
1211 * Set the visibility of the handles.
1213 * @method _setHideHandlesUI
1214 * @param {boolean} val True to hide the handles, false to show.
1217 _setHideHandlesUI: function(val) {
1218 var instance = this,
1219 wrapper = instance.get(WRAPPER);
1221 if (!instance.get(RESIZING)) {
1223 wrapper.addClass(CSS_RESIZE_HIDDEN_HANDLES);
1226 wrapper.removeClass(CSS_RESIZE_HIDDEN_HANDLES);
1232 * Setter for the wrap attribute
1236 * @param {boolean} val
1238 _setWrap: function(val) {
1239 var instance = this,
1240 node = instance.get(NODE),
1241 nodeName = node.get(NODE_NAME),
1242 typeRegex = instance.get(WRAP_TYPES);
1244 // if nodeName is listed on WRAP_TYPES force use the wrapper
1245 if (typeRegex.test(nodeName)) {
1253 * Default resize:mouseUp handler
1255 * @method _defMouseUpFn
1256 * @param {EventFacade} event The Event object
1259 _defMouseUpFn: function(event) {
1260 var instance = this;
1262 instance.set(RESIZING, false);
1266 * Default resize:resize handler
1268 * @method _defResizeFn
1269 * @param {EventFacade} event The Event object
1272 _defResizeFn: function(event) {
1273 var instance = this;
1275 instance._resize(event);
1279 * Logic method for _defResizeFn. Allow AOP.
1282 * @param {EventFacade} event The Event object
1285 _resize: function(event) {
1286 var instance = this;
1288 instance._handleResizeAlignEvent(event.dragEvent);
1290 // _syncUI of the wrapper, not using proxy
1295 * Default resize:align handler
1297 * @method _defResizeAlignFn
1298 * @param {EventFacade} event The Event object
1301 _defResizeAlignFn: function(event) {
1302 var instance = this;
1304 instance._resizeAlign(event);
1308 * Logic method for _defResizeAlignFn. Allow AOP.
1310 * @method _resizeAlign
1311 * @param {EventFacade} event The Event object
1314 _resizeAlign: function(event) {
1315 var instance = this,
1320 instance.lastInfo = instance.info;
1322 // update the instance.info values
1323 instance._updateInfo(event);
1325 info = instance.info;
1327 // basic resize calculations
1328 instance._calcResize();
1330 // if Y.Plugin.ResizeConstrained is not plugged, check for min dimension
1331 if (!instance.con) {
1332 defMinHeight = (instance.get(DEF_MIN_HEIGHT) + instance.totalVSurrounding);
1333 defMinWidth = (instance.get(DEF_MIN_WIDTH) + instance.totalHSurrounding);
1335 if (info.offsetHeight <= defMinHeight) {
1336 instance._checkSize(OFFSET_HEIGHT, defMinHeight);
1339 if (info.offsetWidth <= defMinWidth) {
1340 instance._checkSize(OFFSET_WIDTH, defMinWidth);
1346 * Default resize:end handler
1348 * @method _defResizeEndFn
1349 * @param {EventFacade} event The Event object
1352 _defResizeEndFn: function(event) {
1353 var instance = this;
1355 instance._resizeEnd(event);
1359 * Logic method for _defResizeEndFn. Allow AOP.
1361 * @method _resizeEnd
1362 * @param {EventFacade} event The Event object
1365 _resizeEnd: function(event) {
1366 var instance = this,
1367 drag = event.dragEvent.target;
1369 // reseting actXY from drag when drag end
1372 // syncUI when resize end
1375 instance._setActiveHandlesUI(false);
1377 instance.set(ACTIVE_HANDLE, null);
1378 instance.set(ACTIVE_HANDLE_NODE, null);
1380 instance.handle = null;
1384 * Default resize:start handler
1386 * @method _defResizeStartFn
1387 * @param {EventFacade} event The Event object
1390 _defResizeStartFn: function(event) {
1391 var instance = this;
1393 instance._resizeStart(event);
1397 * Logic method for _defResizeStartFn. Allow AOP.
1399 * @method _resizeStart
1400 * @param {EventFacade} event The Event object
1403 _resizeStart: function(event) {
1404 var instance = this,
1405 wrapper = instance.get(WRAPPER);
1407 instance.handle = instance.get(ACTIVE_HANDLE);
1409 instance.set(RESIZING, true);
1411 instance._updateSurroundingInfo();
1413 // create an originalInfo information for reference
1414 instance.originalInfo = instance._getInfo(wrapper, event);
1416 instance._updateInfo(event);
1420 * Fires the resize:mouseUp event.
1422 * @method _handleMouseUpEvent
1423 * @param {EventFacade} event resize:mouseUp event facade
1426 _handleMouseUpEvent: function(event) {
1427 this.fire(EV_MOUSE_UP, { dragEvent: event, info: this.info });
1431 * Fires the resize:resize event.
1433 * @method _handleResizeEvent
1434 * @param {EventFacade} event resize:resize event facade
1437 _handleResizeEvent: function(event) {
1438 this.fire(EV_RESIZE, { dragEvent: event, info: this.info });
1442 * Fires the resize:align event.
1444 * @method _handleResizeAlignEvent
1445 * @param {EventFacade} event resize:resize event facade
1448 _handleResizeAlignEvent: function(event) {
1449 this.fire(EV_RESIZE_ALIGN, { dragEvent: event, info: this.info });
1453 * Fires the resize:end event.
1455 * @method _handleResizeEndEvent
1456 * @param {EventFacade} event resize:end event facade
1459 _handleResizeEndEvent: function(event) {
1460 this.fire(EV_RESIZE_END, { dragEvent: event, info: this.info });
1464 * Fires the resize:start event.
1466 * @method _handleResizeStartEvent
1467 * @param {EventFacade} event resize:start event facade
1470 _handleResizeStartEvent: function(event) {
1471 if (!this.get(ACTIVE_HANDLE)) {
1472 //This handles the "touch" case
1473 this._setHandleFromNode(event.target.get('node'));
1475 this.fire(EV_RESIZE_START, { dragEvent: event, info: this.info });
1479 * Mouseenter event handler for the <a href="Resize.html#config_wrapper">wrapper</a>.
1481 * @method _onWrapperMouseEnter
1482 * @param {EventFacade} event
1485 _onWrapperMouseEnter: function(event) {
1486 var instance = this;
1488 if (instance.get(AUTO_HIDE)) {
1489 instance._setHideHandlesUI(false);
1494 * Mouseleave event handler for the <a href="Resize.html#config_wrapper">wrapper</a>.
1496 * @method _onWrapperMouseLeave
1497 * @param {EventFacade} event
1500 _onWrapperMouseLeave: function(event) {
1501 var instance = this;
1503 if (instance.get(AUTO_HIDE)) {
1504 instance._setHideHandlesUI(true);
1509 * Handles setting the activeHandle from a node, used from startDrag (for touch) and mouseenter (for mouse).
1511 * @method _setHandleFromNode
1512 * @param {Node} node
1515 _setHandleFromNode: function(node) {
1516 var instance = this,
1517 handle = instance._extractHandleName(node);
1519 if (!instance.get(RESIZING)) {
1520 instance.set(ACTIVE_HANDLE, handle);
1521 instance.set(ACTIVE_HANDLE_NODE, node);
1523 instance._setActiveHandlesUI(true);
1524 instance._updateChangeHandleInfo(handle);
1529 * Mouseenter event handler for the handles.
1531 * @method _onHandleMouseEnter
1532 * @param {EventFacade} event
1535 _onHandleMouseEnter: function(event) {
1536 this._setHandleFromNode(event.currentTarget);
1540 * Mouseout event handler for the handles.
1542 * @method _onHandleMouseLeave
1543 * @param {EventFacade} event
1546 _onHandleMouseLeave: function(event) {
1547 var instance = this;
1549 if (!instance.get(RESIZING)) {
1550 instance._setActiveHandlesUI(false);
1555 * Default value for the wrapper attribute
1557 * @method _valueWrapper
1561 _valueWrapper: function() {
1562 var instance = this,
1563 node = instance.get(NODE),
1564 pNode = node.get(PARENT_NODE),
1565 // by deafult the wrapper is always the node
1568 // if the node is listed on the wrapTypes or wrap is set to true, create another wrapper
1569 if (instance.get(WRAP)) {
1570 wrapper = Y.Node.create(instance.WRAP_TEMPLATE);
1573 pNode.insertBefore(wrapper, node);
1576 wrapper.append(node);
1578 instance._copyStyles(node, wrapper);
1580 // remove positioning of wrapped node, the WRAPPER take care about positioning
1593 Y.each(Y.Resize.prototype.ALL_HANDLES, function(handle, i) {
1594 // creating ATTRS with the handles elements
1595 Y.Resize.ATTRS[handleAttrName(handle)] = {
1596 setter: function() {
1597 return this._buildHandle(handle);
1605 }, '3.3.0' ,{requires:['base', 'widget', 'substitute', 'event', 'oop', 'dd-drag', 'dd-delegate', 'dd-drop'], skinnable:true});
1606 YUI.add('resize-proxy', function(Y) {
1608 var ACTIVE_HANDLE_NODE = 'activeHandleNode',
1610 DRAG_CURSOR = 'dragCursor',
1612 PARENT_NODE = 'parentNode',
1614 PROXY_NODE = 'proxyNode',
1616 RESIZE_PROXY = 'resize-proxy',
1617 WRAPPER = 'wrapper',
1619 getCN = Y.ClassNameManager.getClassName,
1621 CSS_RESIZE_PROXY = getCN(RESIZE, PROXY);
1623 function ResizeProxy() {
1624 ResizeProxy.superclass.constructor.apply(this, arguments);
1627 Y.mix(ResizeProxy, {
1634 * The Resize proxy element.
1636 * @attribute proxyNode
1637 * @default Generated using an internal HTML markup
1638 * @type String | Node
1642 valueFn: function() {
1643 return Y.Node.create(this.PROXY_TEMPLATE);
1649 Y.extend(ResizeProxy, Y.Plugin.Base, {
1651 * Template used to create the resize proxy.
1653 * @property PROXY_TEMPLATE
1656 PROXY_TEMPLATE: '<div class="'+CSS_RESIZE_PROXY+'"></div>',
1658 initializer: function() {
1659 var instance = this;
1661 instance.afterHostEvent('resize:start', instance._afterResizeStart);
1662 instance.beforeHostMethod('_resize', instance._beforeHostResize);
1663 instance.afterHostMethod('_resizeEnd', instance._afterHostResizeEnd);
1666 destructor: function() {
1667 var instance = this;
1669 instance.get(PROXY_NODE).remove(true);
1672 _afterHostResizeEnd: function(event) {
1673 var instance = this,
1674 drag = event.dragEvent.target;
1676 // reseting actXY from drag when drag end
1679 // if proxy is true, hide it on resize end
1680 instance._syncProxyUI();
1682 instance.get(PROXY_NODE).hide();
1685 _afterResizeStart: function(event) {
1686 var instance = this;
1688 instance._renderProxy();
1691 _beforeHostResize: function(event) {
1692 var instance = this,
1693 host = this.get(HOST);
1695 host._handleResizeAlignEvent(event.dragEvent);
1697 // if proxy is true _syncProxyUI instead of _syncUI
1698 instance._syncProxyUI();
1700 return new Y.Do.Prevent();
1704 * Render the <a href="ResizeProxy.html#config_proxyNode">proxyNode</a> element and
1705 * make it sibling of the <a href="Resize.html#config_node">node</a>.
1707 * @method _renderProxy
1710 _renderProxy: function() {
1711 var instance = this,
1712 host = this.get(HOST),
1713 proxyNode = instance.get(PROXY_NODE);
1715 if (!proxyNode.inDoc()) {
1716 host.get(WRAPPER).get(PARENT_NODE).append(
1723 * Sync the proxy UI with internal values from
1724 * <a href="ResizeProxy.html#property_info">info</a>.
1726 * @method _syncProxyUI
1729 _syncProxyUI: function() {
1730 var instance = this,
1731 host = this.get(HOST),
1733 activeHandleNode = host.get(ACTIVE_HANDLE_NODE),
1734 proxyNode = instance.get(PROXY_NODE),
1735 cursor = activeHandleNode.getStyle(CURSOR);
1737 proxyNode.show().setStyle(CURSOR, cursor);
1739 host.delegate.dd.set(DRAG_CURSOR, cursor);
1741 proxyNode.sizeTo(info.offsetWidth, info.offsetHeight);
1743 proxyNode.setXY([ info.left, info.top ]);
1747 Y.namespace('Plugin');
1748 Y.Plugin.ResizeProxy = ResizeProxy;
1751 }, '3.3.0' ,{requires:['resize-base', 'plugin'], skinnable:false});
1752 YUI.add('resize-constrain', function(Y) {
1755 isBoolean = Lang.isBoolean,
1756 isNumber = Lang.isNumber,
1757 isString = Lang.isString,
1758 capitalize = Y.Resize.capitalize,
1760 isNode = function(v) {
1761 return (v instanceof Y.Node);
1764 toNumber = function(num) {
1765 return parseFloat(num) || 0;
1768 BORDER_BOTTOM_WIDTH = 'borderBottomWidth',
1769 BORDER_LEFT_WIDTH = 'borderLeftWidth',
1770 BORDER_RIGHT_WIDTH = 'borderRightWidth',
1771 BORDER_TOP_WIDTH = 'borderTopWidth',
1775 CONSTRAIN = 'constrain',
1778 MAX_HEIGHT = 'maxHeight',
1779 MAX_WIDTH = 'maxWidth',
1780 MIN_HEIGHT = 'minHeight',
1781 MIN_WIDTH = 'minWidth',
1783 OFFSET_HEIGHT = 'offsetHeight',
1784 OFFSET_WIDTH = 'offsetWidth',
1785 PRESEVE_RATIO = 'preserveRatio',
1787 RESIZE_CONTRAINED = 'resizeConstrained',
1794 VIEWPORT_REGION = 'viewportRegion';
1796 function ResizeConstrained() {
1797 ResizeConstrained.superclass.constructor.apply(this, arguments);
1800 Y.mix(ResizeConstrained, {
1801 NAME: RESIZE_CONTRAINED,
1807 * Will attempt to constrain the resize node to the boundaries. Arguments:<br>
1808 * 'view': Contrain to Viewport<br>
1809 * '#selector_string': Constrain to this node<br>
1810 * '{Region Object}': An Object Literal containing a valid region (top, right, bottom, left) of page positions
1812 * @attribute constrain
1813 * @type {String/Object/Node}
1816 setter: function(v) {
1817 if (v && (isNode(v) || isString(v) || v.nodeType)) {
1826 * The minimum height of the element
1828 * @attribute minHeight
1838 * The minimum width of the element
1840 * @attribute minWidth
1850 * The maximum height of the element
1852 * @attribute maxHeight
1862 * The maximum width of the element
1864 * @attribute maxWidth
1874 * Maintain the element's ratio when resizing.
1876 * @attribute preserveRatio
1882 validator: isBoolean
1886 * The number of x ticks to span the resize to.
1890 * @type Number | false
1897 * The number of y ticks to span the resize to.
1901 * @type Number | false
1909 Y.extend(ResizeConstrained, Y.Plugin.Base, {
1911 * Stores the <code>constrain</code>
1912 * surrounding information retrieved from
1913 * <a href="Resize.html#method__getBoxSurroundingInfo">_getBoxSurroundingInfo</a>.
1915 * @property constrainSurrounding
1919 constrainSurrounding: null,
1921 initializer: function() {
1922 var instance = this,
1923 host = instance.get(HOST);
1925 host.delegate.dd.plug(
1926 Y.Plugin.DDConstrained,
1928 tickX: instance.get(TICK_X),
1929 tickY: instance.get(TICK_Y)
1933 host.after('resize:align', Y.bind(instance._handleResizeAlignEvent, instance));
1934 host.on('resize:start', Y.bind(instance._handleResizeStartEvent, instance));
1938 * Helper method to update the current values on
1939 * <a href="Resize.html#property_info">info</a> to respect the
1942 * @method _checkConstrain
1943 * @param {String} axis 'top' or 'left'
1944 * @param {String} axisConstrain 'bottom' or 'right'
1945 * @param {String} offset 'offsetHeight' or 'offsetWidth'
1948 _checkConstrain: function(axis, axisConstrain, offset) {
1949 var instance = this,
1954 host = instance.get(HOST),
1956 constrainBorders = instance.constrainSurrounding.border,
1957 region = instance._getConstrainRegion();
1960 point1 = info[axis] + info[offset];
1961 point1Constrain = region[axisConstrain] - toNumber(constrainBorders[capitalize(BORDER, axisConstrain, WIDTH)]);
1963 if (point1 >= point1Constrain) {
1964 info[offset] -= (point1 - point1Constrain);
1967 point2 = info[axis];
1968 point2Constrain = region[axis] + toNumber(constrainBorders[capitalize(BORDER, axis, WIDTH)]);
1970 if (point2 <= point2Constrain) {
1971 info[axis] += (point2Constrain - point2);
1972 info[offset] -= (point2Constrain - point2);
1978 * Update the current values on <a href="Resize.html#property_info">info</a>
1979 * to respect the maxHeight and minHeight.
1981 * @method _checkHeight
1984 _checkHeight: function() {
1985 var instance = this,
1986 host = instance.get(HOST),
1988 maxHeight = (instance.get(MAX_HEIGHT) + host.totalVSurrounding),
1989 minHeight = (instance.get(MIN_HEIGHT) + host.totalVSurrounding);
1991 instance._checkConstrain(TOP, BOTTOM, OFFSET_HEIGHT);
1993 if (info.offsetHeight > maxHeight) {
1994 host._checkSize(OFFSET_HEIGHT, maxHeight);
1997 if (info.offsetHeight < minHeight) {
1998 host._checkSize(OFFSET_HEIGHT, minHeight);
2003 * Update the current values on <a href="Resize.html#property_info">info</a>
2004 * calculating the correct ratio for the other values.
2006 * @method _checkRatio
2009 _checkRatio: function() {
2010 var instance = this,
2011 host = instance.get(HOST),
2013 originalInfo = host.originalInfo,
2014 oWidth = originalInfo.offsetWidth,
2015 oHeight = originalInfo.offsetHeight,
2016 oTop = originalInfo.top,
2017 oLeft = originalInfo.left,
2018 oBottom = originalInfo.bottom,
2019 oRight = originalInfo.right,
2020 // wRatio/hRatio functions keep the ratio information always synced with the current info information
2021 // RETURN: percentage how much width/height has changed from the original width/height
2022 wRatio = function() {
2023 return (info.offsetWidth/oWidth);
2025 hRatio = function() {
2026 return (info.offsetHeight/oHeight);
2028 isClosestToHeight = host.changeHeightHandles,
2036 // check whether the resizable node is closest to height or not
2037 if (instance.get(CONSTRAIN) && host.changeHeightHandles && host.changeWidthHandles) {
2038 constrainRegion = instance._getConstrainRegion();
2039 constrainBorders = instance.constrainSurrounding.border;
2040 bottomDiff = (constrainRegion.bottom - toNumber(constrainBorders[BORDER_BOTTOM_WIDTH])) - oBottom;
2041 leftDiff = oLeft - (constrainRegion.left + toNumber(constrainBorders[BORDER_LEFT_WIDTH]));
2042 rightDiff = (constrainRegion.right - toNumber(constrainBorders[BORDER_RIGHT_WIDTH])) - oRight;
2043 topDiff = oTop - (constrainRegion.top + toNumber(constrainBorders[BORDER_TOP_WIDTH]));
2045 if (host.changeLeftHandles && host.changeTopHandles) {
2046 isClosestToHeight = (topDiff < leftDiff);
2048 else if (host.changeLeftHandles) {
2049 isClosestToHeight = (bottomDiff < leftDiff);
2051 else if (host.changeTopHandles) {
2052 isClosestToHeight = (topDiff < rightDiff);
2055 isClosestToHeight = (bottomDiff < rightDiff);
2059 // when the height of the resizable element touch the border of the constrain first
2060 // force the offsetWidth to be calculated based on the height ratio
2061 if (isClosestToHeight) {
2062 info.offsetWidth = oWidth*hRatio();
2063 instance._checkWidth();
2064 info.offsetHeight = oHeight*wRatio();
2067 info.offsetHeight = oHeight*wRatio();
2068 instance._checkHeight();
2069 info.offsetWidth = oWidth*hRatio();
2072 // fixing the top on handles which are able to change top
2073 // the idea here is change the top based on how much the height has changed instead of follow the dy
2074 if (host.changeTopHandles) {
2075 info.top = oTop + (oHeight - info.offsetHeight);
2078 // fixing the left on handles which are able to change left
2079 // the idea here is change the left based on how much the width has changed instead of follow the dx
2080 if (host.changeLeftHandles) {
2081 info.left = oLeft + (oWidth - info.offsetWidth);
2084 // rounding values to avoid pixel jumpings
2085 Y.each(info, function(value, key) {
2086 if (isNumber(value)) {
2087 info[key] = Math.round(value);
2093 * Check whether the resizable node is inside the constrain region.
2095 * @method _checkRegion
2099 _checkRegion: function() {
2100 var instance = this,
2101 host = instance.get(HOST),
2102 region = instance._getConstrainRegion();
2104 return Y.DOM.inRegion(null, region, true, host.info);
2108 * Update the current values on <a href="Resize.html#property_info">info</a>
2109 * to respect the maxWidth and minWidth.
2111 * @method _checkWidth
2114 _checkWidth: function() {
2115 var instance = this,
2116 host = instance.get(HOST),
2118 maxWidth = (instance.get(MAX_WIDTH) + host.totalHSurrounding),
2119 minWidth = (instance.get(MIN_WIDTH) + host.totalHSurrounding);
2121 instance._checkConstrain(LEFT, RIGHT, OFFSET_WIDTH);
2123 if (info.offsetWidth < minWidth) {
2124 host._checkSize(OFFSET_WIDTH, minWidth);
2127 if (info.offsetWidth > maxWidth) {
2128 host._checkSize(OFFSET_WIDTH, maxWidth);
2133 * Get the constrain region based on the <code>constrain</code>
2136 * @method _getConstrainRegion
2138 * @return {Object Region}
2140 _getConstrainRegion: function() {
2141 var instance = this,
2142 host = instance.get(HOST),
2143 node = host.get(NODE),
2144 constrain = instance.get(CONSTRAIN),
2148 if (constrain == VIEW) {
2149 region = node.get(VIEWPORT_REGION);
2151 else if (isNode(constrain)) {
2152 region = constrain.get(REGION);
2162 _handleResizeAlignEvent: function(event) {
2163 var instance = this,
2164 host = instance.get(HOST);
2166 // check the max/min height and locking top when these values are reach
2167 instance._checkHeight();
2169 // check the max/min width and locking left when these values are reach
2170 instance._checkWidth();
2172 // calculating the ratio, for proportionally resizing
2173 if (instance.get(PRESEVE_RATIO)) {
2174 instance._checkRatio();
2177 if (instance.get(CONSTRAIN) && !instance._checkRegion()) {
2178 host.info = host.lastInfo;
2182 _handleResizeStartEvent: function(event) {
2183 var instance = this,
2184 constrain = instance.get(CONSTRAIN),
2185 host = instance.get(HOST);
2187 instance.constrainSurrounding = host._getBoxSurroundingInfo(constrain);
2191 Y.namespace('Plugin');
2192 Y.Plugin.ResizeConstrained = ResizeConstrained;
2195 }, '3.3.0' ,{requires:['resize-base', 'plugin'], skinnable:false});
2198 YUI.add('resize', function(Y){}, '3.3.0' ,{use:['resize-base', 'resize-proxy', 'resize-constrain']});