3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('dial', function(Y) {
10 * Create a circular dial value range input visualized as a draggable handle on a
15 var supportsVML = false;
18 if (Y.UA.ie && Y.UA.ie < 9){
27 * Create a dial to represent an input control capable of representing a
28 * series of intermediate states based on the position of the Dial's handle.
29 * These states are typically aligned to a value algorithm whereby the angle of the handle's
30 * position corresponds to a given value.
34 * @param config {Object} Configuration object
37 function Dial(config) {
38 Dial.superclass.constructor.apply(this, arguments);
41 // Y.Dial static properties
44 * The identity of the widget.
56 * Static property used to define the default attribute configuration of
67 * minimum value allowed
78 * maximum value allowed
89 * diameter of the circular background object.
90 * Other objects scale accordingly.
91 * Set this only before rendering.
94 * @type {Number} the number of px in diameter
103 * diameter of the handle object which users drag to change the value.
104 * Dial sets the pixel dimension of the handle equal to handleDiameter * diameter.
105 * Set this only before rendering.
107 * @attribute handleDiameter
117 * diameter of the marker object which follows the angle of the handle during value changes.
118 * Dial sets the pixel dimension of the marker equal to markerDiameter * diameter.
119 * Set this only before rendering.
121 * @attribute markerDiameter
131 * diameter of the center button object.
132 * Dial sets the pixel dimension of the centerButton equal to centerButtonDiameter * diameter.
133 * Set this only before rendering.
135 * @attribute centerButtonDiameter
140 centerButtonDiameter : {
145 * initial value of the Dial
153 validator: function(val) {
154 return this._validateValue(val);
159 * amount to increment/decrement the dial value
160 * when the arrow up/down/left/right keys are pressed
162 * @attribute minorStep
171 * amount to increment/decrement the dial value
172 * when the page up/down keys are pressed
174 * @attribute majorStep
183 * number of value increments in one 360 degree revolution
184 * of the handle around the dial
186 * @attribute stepsPerRevolution
190 stepsPerRevolution : {
195 * number of decimal places of accuracy in the value
197 * @attribute decimalPlaces
206 * visible strings for the dial UI. This attribute is
207 * defined by the base Widget class but has an empty value. The
208 * Dial is simply providing a default value for the attribute.
209 * Gets localized strings in the current language
212 * @type {Object} the values are HTML strings
213 * @default {label: 'My label', resetStr: 'Reset', tooltipHandle: 'Drag to set value'}
216 valueFn: function () {
217 return Y.Intl.get('dial');
222 * distance from the center of the dial to the
223 * center of the marker and handle, when at rest.
224 * The value is a percent of the radius of the dial.
226 * @attribute handleDistance
237 * returns a properly formed yui class name
240 * @param {String} string to be appended at the end of class name
244 function makeClassName(str) {
245 return Y.ClassNameManager.getClassName(Dial.NAME, str);
248 /** array of static constants used to identify the classname applied to the Dial DOM objects
250 * @property CSS_CLASSES
256 label : makeClassName("label"),
257 labelString : makeClassName("label-string"),
258 valueString : makeClassName("value-string"),
259 northMark : makeClassName("north-mark"),
260 ring : makeClassName('ring'),
261 ringVml : makeClassName('ring-vml'),
262 marker : makeClassName("marker"),
263 markerVml : makeClassName("marker-vml"),
264 markerMaxMin : makeClassName("marker-max-min"),
265 centerButton : makeClassName("center-button"),
266 centerButtonVml : makeClassName('center-button-vml'),
267 resetString : makeClassName("reset-string"),
268 handle : makeClassName("handle"),
269 handleVml : makeClassName("handle-vml"),
270 hidden : makeClassName("hidden"),
271 dragging : Y.ClassNameManager.getClassName("dd-dragging")
274 /* Static constants used to define the markup templates used to create Dial DOM elements */
278 * template that will contain the Dial's label.
280 * @property LABEL_TEMPLATE
282 * @default <div class="[...-label]"><span id="" class="[...-label-string]">{label}</span><span class="[...-value-string]"></span></div>
286 Dial.LABEL_TEMPLATE = '<div class="' + Dial.CSS_CLASSES.label + '"><span id="" class="' + Dial.CSS_CLASSES.labelString + '">{label}</span><span class="' + Dial.CSS_CLASSES.valueString + '"></span></div>';
288 if(supportsVML === false){
290 * template that will contain the Dial's background ring.
292 * @property RING_TEMPLATE
294 * @default <div class="[...-ring]"><div class="[...-northMark]"></div></div>
297 Dial.RING_TEMPLATE = '<div class="' + Dial.CSS_CLASSES.ring + '"><div class="' + Dial.CSS_CLASSES.northMark + '"></div></div>';
300 * template that will contain the Dial's current angle marker.
302 * @property MARKER_TEMPLATE
304 * @default <div class="[...-marker] [...-marker-hidden]"><div class="[...-markerUser]"></div></div>
307 Dial.MARKER_TEMPLATE = '<div class="' + Dial.CSS_CLASSES.marker + ' ' + Dial.CSS_CLASSES.hidden + '"></div>';
310 * template that will contain the Dial's center button.
312 * @property CENTER_BUTTON_TEMPLATE
314 * @default <div class="[...-centerButton]"><div class="[...-resetString]">' + Y.substitute('{resetStr}', Dial.ATTRS.strings.value) + '</div></div>
317 Dial.CENTER_BUTTON_TEMPLATE = '<div class="' + Dial.CSS_CLASSES.centerButton + '"><div class="' + Dial.CSS_CLASSES.resetString + ' ' + Dial.CSS_CLASSES.hidden + '">{resetStr}</div></div>';
320 * template that will contain the Dial's handle.
322 * @property HANDLE_TEMPLATE
324 * @default <div class="[...-handle]"><div class="[...-handleUser]" aria-labelledby="" aria-valuetext="" aria-valuemax="" aria-valuemin="" aria-valuenow="" role="slider" tabindex="0"></div></div>';// title="{tooltipHandle}"
327 Dial.HANDLE_TEMPLATE = '<div class="' + Dial.CSS_CLASSES.handle + '" aria-labelledby="" aria-valuetext="" aria-valuemax="" aria-valuemin="" aria-valuenow="" role="slider" tabindex="0" title="{tooltipHandle}">';
330 Dial.RING_TEMPLATE = '<div class="' + Dial.CSS_CLASSES.ring + ' ' + Dial.CSS_CLASSES.ringVml + '">'+
331 '<div class="' + Dial.CSS_CLASSES.northMark + '"></div>'+
332 '<v:oval strokecolor="#ceccc0" strokeweight="1px"><v:fill type=gradient color="#8B8A7F" color2="#EDEDEB" angle="45"/></v:oval>'+
335 Dial.MARKER_TEMPLATE = '<div class="' + Dial.CSS_CLASSES.markerVml + ' ' + Dial.CSS_CLASSES.hidden + '">'+
336 '<v:oval stroked="false">'+
337 '<v:fill opacity="20%" color="#000"/>'+
341 Dial.CENTER_BUTTON_TEMPLATE = '<div class="' + Dial.CSS_CLASSES.centerButton + ' ' + Dial.CSS_CLASSES.centerButtonVml + '">'+
342 '<v:oval strokecolor="#ceccc0" strokeweight="1px">'+
343 '<v:fill type=gradient color="#C7C5B9" color2="#fefcf6" colors="35% #d9d7cb, 65% #fefcf6" angle="45"/>'+
344 '<v:shadow on="True" color="#000" opacity="10%" offset="2px, 2px"/>'+
346 '<div class="' + Dial.CSS_CLASSES.resetString + ' ' + Dial.CSS_CLASSES.hidden + '">{resetStr}</div>'+
349 Dial.HANDLE_TEMPLATE = '<div class="' + Dial.CSS_CLASSES.handleVml + '" aria-labelledby="" aria-valuetext="" aria-valuemax="" aria-valuemin="" aria-valuenow="" role="slider" tabindex="0" title="{tooltipHandle}">'+
350 '<v:oval stroked="false">'+
351 '<v:fill opacity="20%" color="#6C3A3A"/>'+
357 /* Dial extends the base Widget class */
358 Y.extend(Dial, Widget, {
361 * creates the DOM structure for the Dial.
366 renderUI : function() {
369 this._renderMarker();
370 this._renderCenterButton();
371 this._renderHandle();
374 this.contentBox = this.get("contentBox");
377 this._originalValue = this.get('value');
378 this._minValue = this.get('min'); // saves doing a .get many times, but we need to remember to update this if/when we allow changing min or max after instantiation
379 this._maxValue = this.get('max');
380 this._stepsPerRevolution = this.get('stepsPerRevolution');
381 this._minTimesWrapped = (Math.floor(this._minValue / this._stepsPerRevolution - 1));
382 this._maxTimesWrapped = (Math.floor(this._maxValue / this._stepsPerRevolution + 1));
385 this._timesWrapped = 0;
386 this._angle = this._getAngleFromValue(this.get('value'));
387 this._prevAng = this._angle;
390 this._setTimesWrappedFromValue(this._originalValue);
391 this._handleNode.set('aria-valuemin', this._minValue);
392 this._handleNode.set('aria-valuemax', this._maxValue);
396 * Sets -webkit-border-radius to 50% of width/height of the ring, handle, marker, and center-button.
397 * This is needed for iOS 3.x.
398 * The objects render square if the radius is > 50% of the width/height
399 * @method _setBorderRadius
402 _setBorderRadius : function(){
403 this._ringNode.setStyles({'WebkitBorderRadius':this._ringNodeRadius + 'px',
404 'MozBorderRadius':this._ringNodeRadius + 'px',
405 'borderRadius':this._ringNodeRadius + 'px'
407 this._handleNode.setStyles({'WebkitBorderRadius':this._handleNodeRadius + 'px',
408 'MozBorderRadius':this._handleNodeRadius + 'px',
409 'borderRadius':this._handleNodeRadius + 'px'
411 this._markerNode.setStyles({'WebkitBorderRadius':this._markerNodeRadius + 'px',
412 'MozBorderRadius':this._markerNodeRadius + 'px',
413 'borderRadius':this._markerNodeRadius + 'px'
415 this._centerButtonNode.setStyles({'WebkitBorderRadius':this._centerButtonNodeRadius + 'px',
416 'MozBorderRadius':this._centerButtonNodeRadius + 'px',
417 'borderRadius':this._centerButtonNodeRadius + 'px'
422 * Handles the mouseenter on the centerButton
424 * @method _handleCenterButtonEnter
427 _handleCenterButtonEnter : function(){
428 this._resetString.removeClass(Dial.CSS_CLASSES.hidden);
432 * Handles the mouseleave on the centerButton
434 * @method _handleCenterButtonLeave
437 _handleCenterButtonLeave : function(){
438 this._resetString.addClass(Dial.CSS_CLASSES.hidden);
442 * Creates the Y.DD.Drag instance used for the handle movement and
443 * binds Dial interaction to the configured value model.
448 bindUI : function() {
449 this.after("valueChange", this._afterValueChange);
451 var boundingBox = this.get("boundingBox"),
452 // Looking for a key event which will fire continously across browsers while the key is held down.
453 keyEvent = (!Y.UA.opera) ? "down:" : "press:",
454 // 38, 40 = arrow up/down, 33, 34 = page up/down, 35 , 36 = end/home
455 keyEventSpec = keyEvent + "38,40,33,34,35,36",
456 // 37 , 39 = arrow left/right
457 keyLeftRightSpec = keyEvent + "37,39",
458 // 37 , 39 = arrow left/right + meta (command/apple key) for mac
459 keyLeftRightSpecMeta = keyEvent + "37+meta,39+meta";
461 Y.on("key", Y.bind(this._onDirectionKey, this), boundingBox, keyEventSpec);
462 Y.on("key", Y.bind(this._onLeftRightKey, this), boundingBox, keyLeftRightSpec);
463 boundingBox.on("key", this._onLeftRightKeyMeta, keyLeftRightSpecMeta, this);
465 Y.on('mouseenter', Y.bind(this._handleCenterButtonEnter, this), this._centerButtonNode);
466 Y.on('mouseleave', Y.bind(this._handleCenterButtonLeave, this), this._centerButtonNode);
467 // Needed to replace mousedown/up with gesturemovestart/end to make behavior on touch devices work the same.
468 Y.on('gesturemovestart', Y.bind(this._resetDial, this), this._centerButtonNode); //[#2530441]
469 Y.on('gesturemoveend', Y.bind(this._handleCenterButtonMouseup, this), this._centerButtonNode);
470 Y.on('gesturemovestart', Y.bind(this._handleHandleMousedown, this), this._handleNode);
472 Y.on('gesturemovestart', Y.bind(this._handleMousedown, this), this._ringNode); // [#2530766]
473 Y.on('gesturemoveend', Y.bind(this._handleRingMouseup, this), this._ringNode);
475 this._dd1 = new Y.DD.Drag({ //// [#2530206] changed global this._dd1 from just var dd1 = new Y.DD.drag so
476 node: this._handleNode,
478 'drag:drag' : Y.bind(this._handleDrag, this),
479 'drag:start' : Y.bind(this._handleDragStart, this),
480 'drag:end' : Y.bind(this._handleDragEnd, this) //,
483 Y.bind(this._dd1.addHandle(this._ringNode), this); // [#2530206] added the ring as a handle to the dd1 (the dd of the handleNode)
487 * Sets _timesWrapped based on Dial value
488 * to net integer revolutions the user dragged the handle around the Dial
490 * @method _setTimesWrappedFromValue
491 * @param val {Number} current value of the Dial
494 _setTimesWrappedFromValue : function(val){
495 if(val % this._stepsPerRevolution === 0){
496 this._timesWrapped = (val / this._stepsPerRevolution);
498 this._timesWrapped = Math.floor(val / this._stepsPerRevolution);
503 * gets the angle of the line from the center of the Dial to the center of the handle
505 * @method _getAngleFromHandleCenter
506 * @param handleCenterX {number}
507 * @param handleCenterY {number}
508 * @return ang {number} the angle
511 _getAngleFromHandleCenter : function(handleCenterX, handleCenterY){
512 var ang = Math.atan( (this._dialCenterY - handleCenterY) / (this._dialCenterX - handleCenterX) ) * (180 / Math.PI);
513 ang = ((this._dialCenterX - handleCenterX) < 0) ? ang + 90 : ang + 90 + 180; // Compensate for neg angles from Math.atan
518 * calculates the XY of the center of the dial relative to the ring node.
519 * This is needed for calculating the angle of the handle
521 * @method _calculateDialCenter
524 _calculateDialCenter : function(){ // #2531111 value, and marker don't track handle when dial position changes on page (resize when inline)
525 this._dialCenterX = this._ringNode.get('offsetWidth') / 2;
526 this._dialCenterY = this._ringNode.get('offsetHeight') / 2;
530 * Handles the mouseup on the ring
532 * @method _handleRingMouseup
535 _handleRingMouseup : function(){
536 this._handleNode.focus(); // need to re-focus on the handle so keyboard is accessible [#2530206]
540 * Handles the mouseup on the centerButton
542 * @method _handleCenterButtonMouseup
545 _handleCenterButtonMouseup : function(){
546 this._handleNode.focus(); // need to re-focus on the handle so keyboard is accessible [#2530206]
550 * Handles the mousedown on the handle
552 * @method _handleHandleMousedown
555 _handleHandleMousedown : function(){
556 this._handleNode.focus(); // need to re-focus on the handle so keyboard is accessible [#2530206]
557 // this is better done here instead of on _handleDragEnd
558 // because we should make the keyboard accessible after a click of the handle
562 * handles the user dragging the handle around the Dial, gets the angle,
563 * checks for wrapping around top center.
564 * Sets the new value of the Dial
566 * @method _handleDrag
567 * @param e {DOMEvent} the drag event object
570 _handleDrag : function(e){
576 // The event was emitted from drag:drag of handle.
577 // The center of the handle is top left position of the handle node + radius of handle.
578 // This is different than a mousedown on the ring.
579 handleCenterX = (parseInt(this._handleNode.getStyle('left'),10) + this._handleNodeRadius);
580 handleCenterY = (parseInt(this._handleNode.getStyle('top'),10) + this._handleNodeRadius);
581 ang = this._getAngleFromHandleCenter(handleCenterX, handleCenterY);
583 // check for need to set timesWrapped
584 if((this._prevAng > 270) && (ang < 90)){ // If wrapping, clockwise
585 if(this._timesWrapped < this._maxTimesWrapped){
586 this._timesWrapped = (this._timesWrapped + 1);
588 }else if((this._prevAng < 90) && (ang > 270)){ // if un-wrapping, counter-clockwise
589 if(this._timesWrapped > this._minTimesWrapped){
590 this._timesWrapped = (this._timesWrapped - 1);
593 newValue = this._getValueFromAngle(ang); // This function needs the current _timesWrapped value. That's why it comes after the _timesWrapped code above
595 // If you've gone past max more than one full revolution, we decrement the _timesWrapped value
596 // This gives the effect of a ratchet mechanism.
597 // It feels like you are never more than one revolution past max
598 // The effect is the same for min, only in reverse.
599 // We can't reset the _timesWrapped to the max or min here.
600 // If we did, the next (continuous) drag would reset the value incorrectly.
601 if(newValue > (this._maxValue + this._stepsPerRevolution) ){
602 this._timesWrapped --;
603 }else if(newValue < (this._minValue - this._stepsPerRevolution) ){
604 this._timesWrapped ++;
606 this._prevAng = ang; // need to keep the previous angle in order to check for wrapping on the next drag, click, or keypress
608 this._handleValuesBeyondMinMax(e, newValue);
612 * handles a mousedown or gesturemovestart event on the ring node
614 * @method _handleMousedown
615 * @param e {DOMEvent} the event object
618 _handleMousedown : function(e){ // #2530306
619 var minAng = this._getAngleFromValue(this._minValue),
620 maxAng = this._getAngleFromValue(this._maxValue),
621 newValue, oppositeMidRangeAngle,
622 handleCenterX, handleCenterY,
625 // The event was emitted from mousedown on the ring node,
626 // so the center of the handle should be the XY of mousedown.
627 if(Y.UA.ios){ // ios adds the scrollLeft and top onto clientX and Y in a native click
628 handleCenterX = (e.clientX - this._ringNode.getX());
629 handleCenterY = (e.clientY - this._ringNode.getY());
631 handleCenterX = (e.clientX + Y.one('document').get('scrollLeft') - this._ringNode.getX());
632 handleCenterY = (e.clientY + Y.one('document').get('scrollTop') - this._ringNode.getY());
634 ang = this._getAngleFromHandleCenter(handleCenterX, handleCenterY);
636 /* ///////////////////////////////////////////////////////////////////////////////////////////////////////
637 * The next sections of logic
638 * set this._timesWrapped in the different cases of value range
639 * and value range position,
640 * then the Dial value is set at the end of this method
641 */ ///////////////////////////////////////////////////////////////////////////////////////////////////////
644 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
645 if(this._maxValue - this._minValue > this._stepsPerRevolution){
646 // Case: range min-to-max is greater than stepsPerRevolution (one revolution)
648 // This checks the shortest way around the dial between the prevAng and this ang.
649 if(Math.abs(this._prevAng - ang) > 180){ // this crossed a wrapping
651 // Only change the _timesWrapped if it's between minTimesWrapped and maxTimesWrapped
652 if((this._timesWrapped > this._minTimesWrapped) &&
653 (this._timesWrapped < this._maxTimesWrapped)
655 // this checks which direction, clock wise or CCW and incr or decr _timesWrapped
656 this._timesWrapped = ((this._prevAng - ang) > 0) ? (this._timesWrapped + 1) : (this._timesWrapped - 1);
658 // special case of getting un-stuck from a min value case
659 // where timesWrapped is minTimesWrapped but new ang won't trigger a cross wrap boundry
660 // because prevAng is set to 0 or > 0
662 (this._timesWrapped === this._minTimesWrapped) &&
663 (ang - this._prevAng < 180)
665 this._timesWrapped ++;
666 } //it didn't cross a wrapping boundary
668 } /////////////////////////////////////////////////////////////////////////////////////////////////////////
669 else if(this._maxValue - this._minValue === this._stepsPerRevolution){
670 // Case: range min-to-max === stepsPerRevolution (one revolution)
671 // This means min and max will be at same angle
672 // This does not mean they are at "north"
674 if(ang < minAng){ // if mousedown angle is < minAng (and maxAng, because they're the same)
675 // The only way it can be, is if min and max are not at north
676 this._timesWrapped = 1;
678 this._timesWrapped = 0;
681 } //////////////////////////////////////////////////////////////////////////////////////////////////////////
682 else if(minAng > maxAng){
683 // Case: range includes the wrap point (north)
684 // Because of "else if"...
685 // range is < stepsPerRevolution
688 (this._prevAng >= minAng) && // if prev angle was greater than angle of min and...
689 (ang <= (minAng + maxAng) / 2) // the angle of this click is less than
690 // the angle opposite the mid-range angle, then...
692 this._timesWrapped ++;
694 (this._prevAng <= maxAng) &&
695 // if prev angle is < max angle and...
697 (ang > (minAng + maxAng) / 2)
698 // the angle of this click is greater than,
699 // the angle opposite the mid-range angle and...
702 this._timesWrapped --;
705 } ////////////////////////////////////////////////////////////////////////////////////////////////////
707 // "else" Case: min-to-max range doesn't include the wrap point
708 // Because of "else if"...
709 // range is still < stepsPerRevolution
711 if ((ang < minAng) || (ang > maxAng)){ // angle is out of range
712 oppositeMidRangeAngle = (((minAng + maxAng) / 2) + 180) % 360;
713 // This is the bisection of the min-to-max range + 180. (opposite the bisection)
715 if(oppositeMidRangeAngle > 180){
716 newValue = ((maxAng < ang) && (ang < oppositeMidRangeAngle)) ? this.get('max') : this.get('min');
717 }else{ //oppositeMidRangeAngle <= 180
718 newValue = ((minAng > ang) && (ang > oppositeMidRangeAngle)) ? this.get('min') : this.get('max');
720 this._prevAng = this._getAngleFromValue(newValue);
721 this.set('value', newValue);
722 this._setTimesWrappedFromValue(newValue);
727 // Now that _timesWrapped is set value .......................................................................
728 newValue = this._getValueFromAngle(ang); // This function needs the correct, current _timesWrapped value.
731 this._handleValuesBeyondMinMax(e, newValue);
735 * handles the case where the value is less than min or greater than max
737 * @method _handleValuesBeyondMinMax
738 * @param e {DOMEvent} the event object
739 * @param newValue {number} current value of the dial
742 _handleValuesBeyondMinMax : function(e, newValue){ // #2530306
743 // If _getValueFromAngle() is passed 0, it increments the _timesWrapped value.
744 // handle hitting max and min and going beyond, stops at max or min
745 if((newValue >= this._minValue) && (newValue <= this._maxValue)) {
746 this.set('value', newValue);
747 // [#2530206] transfer the mousedown event from the _ringNode to the _handleNode drag, so we can mousedown, then continue dragging
748 if(e.currentTarget === this._ringNode){
749 // Delegate to DD's natural behavior
750 this._dd1._handleMouseDownEvent(e);
752 }else if(newValue > this._maxValue){
753 this.set('value', this._maxValue);
754 if(e.type === 'gesturemovestart'){
755 this._prevAng = this._getAngleFromValue(this._maxValue); // #2530766 need for mousedown on the ring; causes prob for drag
757 }else if(newValue < this._minValue){
758 this.set('value', this._minValue);
759 if(e.type === 'gesturemovestart'){
760 this._prevAng = this._getAngleFromValue(this._minValue);
766 * handles the user starting to drag the handle around the Dial
768 * @method _handleDragStart
769 * @param e {DOMEvent} the drag event object
772 _handleDragStart : function(e){
773 this._markerNode.removeClass(Dial.CSS_CLASSES.hidden);
777 * When handle is handleDragEnd, this animates the return to the fixed dial
781 * handles the end of a user dragging the handle, animates the handle returning to
784 * @method _handleDragEnd
787 _handleDragEnd : function(){
788 var node = this._handleNode;
790 duration: 0.08, // seconds
792 left: this._setNodeToFixedRadius(this._handleNode, true)[0] + 'px',
793 top: this._setNodeToFixedRadius(this._handleNode, true)[1] + 'px'
794 }, Y.bind(function(){
795 var value = this.get('value');
796 //[#2530206] only hide marker if not at max or min
797 // more persistant user visibility of when the dial is at max or min
798 if((value > this._minValue) && (value < this._maxValue)){
799 this._markerNode.addClass(Dial.CSS_CLASSES.hidden);
801 this._setTimesWrappedFromValue(value); //#2530766 secondary bug when drag past max + cross wrapping boundry
802 this._prevAng = this._getAngleFromValue(value); //#2530766 secondary bug when drag past max + cross wrapping boundry
809 * returns the XY of the fixed position, handleDistance, from the center of the Dial (resting position).
810 * The XY also represents the angle related to the current value.
811 * If typeArray is true, [X,Y] is returned.
812 * If typeArray is false, the XY of the obj node passed in is set.
814 * @method _setNodeToFixedRadius
816 * @param typeArray {Boolean} true returns an array [X,Y]
818 * @return {Array} an array of [XY] is optionally returned
820 _setNodeToFixedRadius : function(obj, typeArray){
821 var thisAngle = (this._angle - 90),
822 rad = (Math.PI / 180),
823 newY = Math.round(Math.sin(thisAngle * rad) * this._handleDistance),
824 newX = Math.round(Math.cos(thisAngle * rad) * this._handleDistance),
825 dia = obj.get('offsetWidth'); //Ticket #2529852
827 newY = newY - (dia * 0.5);
828 newX = newX - (dia * 0.5);
829 if(typeArray){ // just need the style for css transform left and top to animate the handle drag:end
830 return [(this._ringNodeRadius + newX), (this._ringNodeRadius + newY)];
832 obj.setStyle('left', (this._ringNodeRadius + newX) + 'px');
833 obj.setStyle('top', (this._ringNodeRadius + newY) + 'px');
838 * Synchronizes the DOM state with the attribute settings.
842 syncUI : function() {
843 // Make the marker and the resetString display so their placement and borderRadius can be calculated, then hide them again.
844 // We would have used visibility:hidden in the css of this class,
845 // but IE8 VML never returns to visible after applying visibility:hidden then removing it.
847 this._calculateDialCenter(); // #2531111 initialize center of dial
848 this._setBorderRadius();
849 this._uiSetValue(this.get("value"));
850 this._markerNode.addClass(Dial.CSS_CLASSES.hidden);
851 this._resetString.addClass(Dial.CSS_CLASSES.hidden);
855 * sets the sizes of ring, center-button, marker, handle, and VML ovals in pixels.
856 * Needed only because some IE versions
857 * ignore CSS percent sizes/offsets.
858 * so these must be set in pixels.
859 * Normally these are set in % of the ring.
864 _setSizes : function(){
865 var dia = this.get('diameter'),
866 offset, offsetResetX, offsetResetY,
867 setSize = function(node, dia, percent){
869 node.getElementsByTagName('oval').setStyle('width', (dia * percent) + suffix);
870 node.getElementsByTagName('oval').setStyle('height', (dia * percent) + suffix);
871 node.setStyle('width', (dia * percent) + suffix);
872 node.setStyle('height', (dia * percent) + suffix);
874 setSize(this._ringNode, dia, 1.0);
875 setSize(this._handleNode, dia, this.get('handleDiameter'));
876 setSize(this._markerNode, dia, this.get('markerDiameter'));
877 setSize(this._centerButtonNode, dia, this.get('centerButtonDiameter'));
879 // Set these (used for trig) this way instead of relative to dia,
880 // in case they have borders, have images etc.
881 this._ringNodeRadius = this._ringNode.get('offsetWidth') * 0.5;
882 this._handleNodeRadius = this._handleNode.get('offsetWidth') * 0.5;
883 this._markerNodeRadius = this._markerNode.get('offsetWidth') * 0.5;
884 this._centerButtonNodeRadius = this._centerButtonNode.get('offsetWidth') * 0.5;
885 this._handleDistance = this._ringNodeRadius * this.get('handleDistance');
886 // place the centerButton
887 offset = (this._ringNodeRadius - this._centerButtonNodeRadius);
888 this._centerButtonNode.setStyle('left', offset + 'px');
889 this._centerButtonNode.setStyle('top', offset + 'px');
891 Place the resetString
892 This seems like it should be able to be done with CSS,
893 But since there is also a VML oval in IE that is absolute positioned,
894 The resetString ends up behind the VML oval.
896 offsetResetX = (this._centerButtonNodeRadius - (this._resetString.get('offsetWidth') * 0.5));
897 offsetResetY = (this._centerButtonNodeRadius - (this._resetString.get('offsetHeight') * 0.5));
898 this._resetString.setStyles({'left':offsetResetX + 'px', 'top':offsetResetY + 'px'});
903 * renders the DOM object for the Dial's label
905 * @method _renderLabel
908 _renderLabel : function() {
909 var contentBox = this.get("contentBox"),
910 label = contentBox.one("." + Dial.CSS_CLASSES.label);
912 label = Node.create(Y.substitute(Dial.LABEL_TEMPLATE, this.get('strings')));
913 contentBox.append(label);
915 this._labelNode = label;
916 this._valueStringNode = this._labelNode.one("." + Dial.CSS_CLASSES.valueString);
920 * renders the DOM object for the Dial's background ring
922 * @method _renderRing
925 _renderRing : function() {
926 var contentBox = this.get("contentBox"),
927 ring = contentBox.one("." + Dial.CSS_CLASSES.ring);
929 ring = contentBox.appendChild(Dial.RING_TEMPLATE);
930 ring.setStyles({width:this.get('diameter') + 'px', height:this.get('diameter') + 'px'});
932 this._ringNode = ring;
936 * renders the DOM object for the Dial's background marker which
937 * tracks the angle of the user dragging the handle
939 * @method _renderMarker
942 _renderMarker : function() {
943 var contentBox = this.get("contentBox"),
944 marker = contentBox.one("." + Dial.CSS_CLASSES.marker);
946 marker = contentBox.one('.' + Dial.CSS_CLASSES.ring).appendChild(Dial.MARKER_TEMPLATE);
948 this._markerNode = marker;
952 * renders the DOM object for the Dial's center
954 * @method _renderCenterButton
957 _renderCenterButton : function() {
958 var contentBox = this.get("contentBox"),
959 centerButton = contentBox.one("." + Dial.CSS_CLASSES.centerButton);
961 centerButton = Node.create(Y.substitute(Dial.CENTER_BUTTON_TEMPLATE, this.get('strings')));
962 contentBox.one('.' + Dial.CSS_CLASSES.ring).append(centerButton);
964 this._centerButtonNode = centerButton;
965 this._resetString = this._centerButtonNode.one('.' + Dial.CSS_CLASSES.resetString);
969 * renders the DOM object for the Dial's user draggable handle
971 * @method _renderHandle
974 _renderHandle : function() {
975 var labelId = Dial.CSS_CLASSES.label + Y.guid(), //get this unique id once then use for handle and label for ARIA
976 contentBox = this.get("contentBox"),
977 handle = contentBox.one("." + Dial.CSS_CLASSES.handle);
979 handle = Node.create(Y.substitute(Dial.HANDLE_TEMPLATE, this.get('strings')));
980 handle.setAttribute('aria-labelledby', labelId); // get unique id for specifying a label & handle for ARIA
981 this._labelNode.one('.' + Dial.CSS_CLASSES.labelString).setAttribute('id', labelId); // When handle gets focus, screen reader will include label text when reading the value.
982 contentBox.one('.' + Dial.CSS_CLASSES.ring).append(handle);
984 this._handleNode = handle;
988 * sets the visible UI label HTML string
990 * @method _setLabelString
993 * @deprecated Use DialObjName.set('strings',{'label':'My new label'}); before DialObjName.render();
996 _setLabelString : function(str) {
997 this.get("contentBox").one("." + Dial.CSS_CLASSES.labelString).setContent(str);
1001 * sets the visible UI label HTML string
1003 * @method _setResetString
1006 * @deprecated Use DialObjName.set('strings',{'resetStr':'My new reset string'}); before DialObjName.render();
1008 _setResetString : function(str) {
1009 this.get("contentBox").one("." + Dial.CSS_CLASSES.resetString).setContent(str);
1010 // this._setXYResetString(); // This used to recenter the string in the button. Done with CSS now. Method has been removed.
1011 // this._resetString.setContent(''); //We no longer show/hide the reset string with setContent but by addClass and removeClass .yui3-dial-reset-string-hidden
1015 * sets the tooltip HTML string in the Dial's handle
1017 * @method _setTooltipString
1020 * @deprecated Use DialObjName.set('strings',{'tooltipHandle':'My new tooltip'}); before DialObjName.render();
1022 _setTooltipString : function(str) {
1023 this._handleNode.set('title', str);
1027 * sets the Dial's value in response to key events.
1028 * Left and right keys are in a separate method
1029 * in case an implementation wants to increment values
1030 * but needs left and right arrow keys for other purposes.
1032 * @method _onDirectionKey
1033 * @param e {Event} the key event
1036 _onDirectionKey : function(e) {
1038 switch (e.charCode) {
1054 case 34: // page down
1061 * sets the Dial's value in response to left or right key events
1063 * @method _onLeftRightKey
1064 * @param e {Event} the key event
1067 _onLeftRightKey : function(e) {
1069 switch (e.charCode) {
1080 * sets the Dial's value in response to left or right key events when a meta (mac command/apple) key is also pressed
1082 * @method _onLeftRightKeyMeta
1083 * @param e {Event} the key event
1086 _onLeftRightKeyMeta : function(e) {
1088 switch (e.charCode) {
1089 case 37: // left + meta
1092 case 39: // right + meta
1099 * increments Dial value by a minor increment
1101 * @method _incrMinor
1104 _incrMinor : function(){
1105 var newVal = (this.get('value') + this.get("minorStep"));
1106 newVal = Math.min(newVal, this.get("max"));
1107 // [#2530045] .toFixed returns a string.
1108 // Dial's value needs a number. -0 makes it a number, but removes trailing zeros.
1109 // Added toFixed(...) again in _uiSetValue where content of yui3-dial-value-string is set.
1110 // Removing the toFixed here, loses the feature of "snap-to" when for example, stepsPerRevolution is 10 and decimalPlaces is 0.
1111 this.set('value', newVal.toFixed(this.get('decimalPlaces')) - 0);
1115 * decrements Dial value by a minor increment
1117 * @method _decrMinor
1120 _decrMinor : function(){
1121 var newVal = (this.get('value') - this.get("minorStep"));
1122 newVal = Math.max(newVal, this.get("min"));
1123 this.set('value', newVal.toFixed(this.get('decimalPlaces')) - 0);
1127 * increments Dial value by a major increment
1129 * @method _incrMajor
1132 _incrMajor : function(){
1133 var newVal = (this.get('value') + this.get("majorStep"));
1134 newVal = Math.min(newVal, this.get("max"));
1135 this.set('value', newVal.toFixed(this.get('decimalPlaces')) - 0);
1139 * decrements Dial value by a major increment
1141 * @method _decrMajor
1144 _decrMajor : function(){
1145 var newVal = (this.get('value') - this.get("majorStep"));
1146 newVal = Math.max(newVal, this.get("min"));
1147 this.set('value', newVal.toFixed(this.get('decimalPlaces')) - 0);
1151 * sets Dial value to dial's max attr
1156 _setToMax : function(){
1157 this.set('value', this.get("max"));
1161 * sets Dial value to dial's min attr
1166 _setToMin : function(){
1167 this.set('value', this.get("min"));
1171 * resets Dial value to the orignal initial value.
1173 * @method _resetDial
1176 _resetDial : function(e){
1178 e.stopPropagation(); //[#2530206] need to add so mousedown doesn't propagate to ring and move the handle
1180 this.set('value', this._originalValue);
1181 this._resetString.addClass(Dial.CSS_CLASSES.hidden); //[#2530441]
1182 this._handleNode.focus();
1186 * returns the handle angle associated with the current value of the Dial.
1187 * Returns a number between 0 and 360.
1189 * @method _getAngleFromValue
1190 * @param newVal {Number} the current value of the Dial
1191 * @return {Number} the angle associated with the current Dial value
1194 _getAngleFromValue : function(newVal){
1195 var nonWrappedPartOfValue = newVal % this._stepsPerRevolution,
1196 angleFromValue = nonWrappedPartOfValue / this._stepsPerRevolution * 360;
1197 return (angleFromValue < 0) ? (angleFromValue + 360) : angleFromValue;
1201 * returns the value of the Dial calculated from the current handle angle
1203 * @method _getValueFromAngle
1204 * @param angle {Number} the current angle of the Dial's handle
1205 * @return {Number} the current Dial value corresponding to the handle position
1208 _getValueFromAngle : function(angle){
1210 angle = (360 + angle);
1211 }else if(angle === 0){
1214 var value = (angle / 360) * this._stepsPerRevolution;
1215 value = (value + (this._timesWrapped * this._stepsPerRevolution));
1216 //return Math.round(value * 100) / 100;
1217 return value.toFixed(this.get('decimalPlaces')) - 0;
1221 * calls the method to update the UI whenever the Dial value changes
1223 * @method _afterValueChange
1227 _afterValueChange : function(e) {
1228 this._uiSetValue(e.newVal);
1232 * Changes a value to have the correct decimal places per the attribute decimalPlaces
1234 * @method _valueToDecimalPlaces
1235 * @param val {Number} a raw value to set to the Dial
1236 * @return {Number} the input val changed to have the correct decimal places
1239 _valueToDecimalPlaces : function(val) { // [#2530206] cleaned up and better user feedback of when it's max or min.
1244 * Updates the UI display value of the Dial to reflect
1245 * the value passed in.
1246 * Makes all other needed UI display changes
1248 * @method _uiSetValue
1249 * @param val {Number} value of the Dial
1252 _uiSetValue : function(val) { // [#2530206] cleaned up and better user feedback of when it's max or min.
1253 this._angle = this._getAngleFromValue(val);
1254 if(this._handleNode.hasClass(Dial.CSS_CLASSES.dragging) === false){
1255 this._setTimesWrappedFromValue(val);
1256 this._setNodeToFixedRadius(this._handleNode, false);
1257 this._prevAng = this._getAngleFromValue(this.get('value'));
1259 this._valueStringNode.setContent(val.toFixed(this.get('decimalPlaces'))); // [#2530045]
1260 this._handleNode.set('aria-valuenow', val);
1261 this._handleNode.set('aria-valuetext', val);
1262 this._setNodeToFixedRadius(this._markerNode, false);
1263 if((val === this._maxValue) || (val === this._minValue)){
1264 this._markerNode.addClass(Dial.CSS_CLASSES.markerMaxMin);
1265 if(supportsVML === true){
1266 this._markerNode.getElementsByTagName('fill').set('color', '#AB3232');
1268 this._markerNode.removeClass(Dial.CSS_CLASSES.hidden);
1269 }else{ // not max or min
1270 if(supportsVML === true){
1271 this._markerNode.getElementsByTagName('fill').set('color', '#000');
1273 this._markerNode.removeClass(Dial.CSS_CLASSES.markerMaxMin);
1274 if(this._handleNode.hasClass(Dial.CSS_CLASSES.dragging) === false){ // if not max || min, and not dragging handle, hide the marker
1275 this._markerNode.addClass(Dial.CSS_CLASSES.hidden);
1281 * value attribute default validator. Verifies that
1282 * the value being set lies between the min/max value
1284 * @method _validateValue
1285 * @param val {Number} value of the Dial
1288 _validateValue: function(val) {
1289 var min = this.get("min"),
1290 max = this.get("max");
1291 return (Lang.isNumber(val) && val >= min && val <= max);
1298 }, '3.5.0' ,{requires:['widget', 'dd-drag', 'substitute', 'event-mouseenter', 'event-move', 'event-key', 'transition', 'intl'], lang:['en','es' ], skinnable:true});