3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('clickable-rail', function(Y) {
10 * Adds support for mouse interaction with the Slider rail triggering thumb
14 * @submodule clickable-rail
18 * Slider extension that allows clicking on the Slider's rail element,
19 * triggering the thumb to align with the location of the click.
21 * @class ClickableRail
23 function ClickableRail() {
24 this._initClickableRail();
27 Y.ClickableRail = Y.mix(ClickableRail, {
29 // Prototype methods added to host class
33 * Initializes the internal state and sets up events.
35 * @method _initClickableRail
38 _initClickableRail: function () {
39 this._evtGuid = this._evtGuid || (Y.guid() + '|');
42 * Broadcasts when the rail has received a mousedown event and
43 * triggers the thumb positioning. Use
44 * <code>e.preventDefault()</code> or
45 * <code>set("clickableRail", false)</code> to prevent
46 * the thumb positioning.
48 * @event railMouseDown
49 * @preventable _defRailMouseDownFn
51 this.publish('railMouseDown', {
52 defaultFn: this._defRailMouseDownFn
55 this.after('render', this._bindClickableRail);
56 this.on('destroy', this._unbindClickableRail);
60 * Attaches DOM event subscribers to support rail interaction.
62 * @method _bindClickableRail
65 _bindClickableRail: function () {
66 this._dd.addHandle(this.rail);
68 this.rail.on(this._evtGuid + Y.DD.Drag.START_EVENT,
69 Y.bind(this._onRailMouseDown, this));
73 * Detaches DOM event subscribers for cleanup/destruction cycle.
75 * @method _unbindClickableRail
78 _unbindClickableRail: function () {
79 if (this.get('rendered')) {
80 var contentBox = this.get('contentBox'),
81 rail = contentBox.one('.' + this.getClassName('rail'));
83 rail.detach(this.evtGuid + '*');
88 * Dispatches the railMouseDown event.
90 * @method _onRailMouseDown
91 * @param e {DOMEvent} the mousedown event object
94 _onRailMouseDown: function (e) {
95 if (this.get('clickableRail') && !this.get('disabled')) {
96 this.fire('railMouseDown', { ev: e });
102 * Default behavior for the railMouseDown event. Centers the thumb at
103 * the click location and passes control to the DDM to behave as though
104 * the thumb itself were clicked in preparation for a drag operation.
106 * @method _defRailMouseDownFn
107 * @param e {Event} the EventFacade for the railMouseDown custom event
110 _defRailMouseDownFn: function (e) {
113 // Logic that determines which thumb should be used is abstracted
114 // to someday support multi-thumb sliders
115 var dd = this._resolveThumb(e),
116 i = this._key.xyIndex,
117 length = parseFloat(this.get('length'), 10),
123 thumb = dd.get('dragNode');
124 thumbSize = parseFloat(thumb.getStyle(this._key.dim), 10);
126 // Step 1. Allow for aligning to thumb center or edge, etc
127 xy = this._getThumbDestination(e, thumb);
129 // Step 2. Remove page offsets to give just top/left style val
130 xy = xy[ i ] - this.rail.getXY()[i];
132 // Step 3. Constrain within the rail in case of attempt to
133 // center the thumb when clicking on the end of the rail
136 (length - thumbSize));
138 this._uiMoveThumb(xy, { source: 'rail' });
140 // Set e.target for DD's IE9 patch which calls
141 // e.target._node.setCapture() to allow imgs to be dragged.
142 // Without this, setCapture is called from the rail and rail
143 // clicks on other Sliders may have their thumb movements
144 // overridden by a different Slider (the thumb on the wrong
146 e.target = this.thumb.one('img') || this.thumb;
148 // Delegate to DD's natural behavior
149 dd._handleMouseDownEvent(e);
151 // TODO: this won't trigger a slideEnd if the rail is clicked
152 // check if dd._move(e); dd._dragThreshMet = true; dd.start();
153 // will do the trick. Is that even a good idea?
158 * Resolves which thumb to actuate if any. Override this if you want to
159 * support multiple thumbs. By default, returns the Drag instance for
160 * the thumb stored by the Slider.
162 * @method _resolveThumb
163 * @param e {DOMEvent} the mousedown event object
164 * @return {DD.Drag} the Drag instance that should be moved
167 _resolveThumb: function (e) {
168 /* Temporary workaround
169 var primaryOnly = this._dd.get('primaryButtonOnly'),
170 validClick = !primaryOnly || e.button <= 1;
172 return (validClick) ? this._dd : null;
178 * Calculates the top left position the thumb should be moved to to
179 * align the click XY with the center of the specified node.
181 * @method _getThumbDestination
182 * @param e {DOMEvent} The mousedown event object
183 * @param node {Node} The node to position
184 * @return {Array} the [top, left] pixel position of the destination
187 _getThumbDestination: function (e, node) {
188 var offsetWidth = node.get('offsetWidth'),
189 offsetHeight = node.get('offsetHeight');
193 (e.pageX - Math.round((offsetWidth / 2))),
194 (e.pageY - Math.round((offsetHeight / 2)))
200 // Static properties added onto host class
203 * Enable or disable clickable rail support.
205 * @attribute clickableRail
211 validator: Y.Lang.isBoolean
219 }, '3.5.1' ,{requires:['slider-base']});