-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.quinn.min.js
1 lines (1 loc) · 10.3 KB
/
jquery.quinn.min.js
1
!function(s,h){"use strict";var i="mousemove",a="mousedown",n="mouseup",r=!1;function o(t){var e=t.originalEvent;return(e.touches&&e.touches.length?e.touches[0]:e.changedTouches&&e.changedTouches.length?e.changedTouches[0]:t).pageX}function l(t,e,i){t=t.attr(i)||t.data("quinn-"+i);return t?parseFloat(t):e.options[i]}function u(t,e){var i,s;h.bindAll(this,"clickBar","startDrag","drag","endDrag","handleKeyboardEvent","enableKeyboardEvents","disableKeyboardEvents"),this.wrapper=t,this.options=i=h.extend({},u.defaults,e),this.callbacks={},this.disabled=!1,this.activeHandle=null,this.previousValue=null,t.is("input")&&this.enhance(t),this.drawTo=(s=i.drawTo,e=i.min,t=i.max,s=s||{},{left:h.has(s,"left")?s.left:e,right:h.has(s,"right")?s.right:t}),this.model=new d(this,this.options.strict),this.renderer=new this.options.renderer(this),this.keyFloodTimer=null,this.wrapperWidth=0,this.wrapperOffset=0,this.on("setup",i.setup),this.on("begin",i.begin),this.on("drag",i.drag),this.on("change",i.change),this.on("abort",i.abort),h.isFunction(this.renderer.render)&&this.renderer.render(),!0===i.disable&&this.disable(),this.trigger("setup",this.model.value)}function d(t,e){var i,s;for(this.options=t=t.options,this.step=t.step,this.only=t.only,this.values=[],this.minimum=this.roundToStep(t.min),this.maximum=this.roundToStep(t.max),this.minimum<t.min&&(this.minimum+=this.step),this.maximum>t.max&&(this.maximum-=this.step),i=(t=void(s=0)===t.value||null===t.value?this.minimum:h.isArray(t.value)?t.value:[t.value]).length;s<i;s++)this.values[s]=null;this.setValue(t,e)}"ontouchstart"in document.documentElement&&(i="touchmove",a="touchstart",n="touchend",r=!0),u.VERSION="1.2.2",u.prototype.on=function(t,e){return h.isString(t)&&h.isFunction(e)&&(this.callbacks[t]||(this.callbacks[t]=[]),this.callbacks[t].push(e)),this},u.prototype.trigger=function(t,e){var i,s=this.callbacks[t]||[],a=0;for(void 0===e&&(e=this.value);i=s[a++];)if(!1===i(e,this))return!1;return!0},u.prototype.enable=function(){this.disabled&&(this.disabled=!1,this.trigger("enabled"))},u.prototype.disable=function(){this.disabled||(this.disabled=!0,this.trigger("disabled"))},u.prototype.setValue=function(t,e,i){return this.start()&&(!1!==this.setTentativeValue(t,e,i)?this.resolve():this.reject()),this.model.value},u.prototype.setTentativeValue=function(t,e,i){var s,a,n,r=this.model.value;if(null==t)return!1;if(1<this.model.values.length&&h.isNumber(t)){if(null===this.activeHandle)return!1;n=this.model.sanitizeValue(t),s=(t=h.clone(this.model.values))[this.activeHandle-1],a=t[this.activeHandle+1],null!==s&&n<=s&&(n=s+this.options.step),null!==a&&a<=n&&(n=a-this.options.step),t[this.activeHandle]=n}return!1===(t=this.model.setValue(t))||!i&&!this.trigger("drag",t)?(this.model.setValue(r),!1!==t&&this.trigger("drag",r),!1):(this.trigger("redraw",e),t)},u.prototype.stepUp=function(t,e,i){i=i?this.setTentativeValue:this.setValue;return 1<this.model.values.length?this.model.value:h.bind(i,this)(this.model.value+this.model.step*(t||1),e)},u.prototype.stepDown=function(t,e,i){return this.stepUp(-(t||1),e,i)},u.prototype.start=function(){return!(!0===this.disabled||!this.trigger("begin"))&&(this.previousValue=this.model.value,this.wrapperWidth=this.wrapper.width(),this.wrapperOffset=this.wrapper.offset().left,!0)},u.prototype.resolve=function(){return this.deactivateActiveHandle(),h.isEqual(this.previousValue,this.model.value)?(this.reject(),!1):this.trigger("change",this.model.value)?void(this.previousValue=null):(this.setTentativeValue(this.previousValue),this.reject(),!1)},u.prototype.reject=function(){return this.previousValue=null,this.deactivateActiveHandle(),this.trigger("abort")},u.prototype.valueFromMouse=function(t){var e=this.positionFromMouse(t),t=this.drawTo.right-this.drawTo.left;return this.drawTo.left+t*e},u.prototype.positionFromMouse=function(t){var e=this.wrapperOffset+this.wrapperWidth,t=t<this.wrapperOffset?0:e<t?this.wrapperWidth:t-this.wrapperOffset;return t/this.wrapperWidth},u.prototype.startDrag=function(t,e){return!r&&1!==t.which||((e||this.start())&&(this.activateHandleWithEvent(t),s(document).on(n+".quinn",this.endDrag).on(i+".quinn",this.drag).on("mouseenter.quinn",this.endDrag)),!1)},u.prototype.drag=function(t){return this.setTentativeValue(this.valueFromMouse(o(t)),!1),t.preventDefault()},u.prototype.endDrag=function(t){return s(document).off(n+".quinn").off(i+".quinn").off("mouseenter.quinn"),this.resolve(),t.preventDefault()},u.prototype.clickBar=function(t){return!r&&1!==t.which||(this.start()&&(this.activateHandleWithEvent(t),this.setTentativeValue(this.valueFromMouse(o(t))),this.startDrag(t,!0)),t.preventDefault())},u.prototype.activateHandleWithEvent=function(t){var e;if(this.activeHandle)return!1;e=this.valueFromMouse(o(t)),t=h.min(this.model.values,function(t){return Math.abs(t-e)}),-1!==(t=h.indexOf(this.model.values,t))&&(this.activeHandle=t,this.trigger("handleOn",this.activeHandle))},u.prototype.deactivateActiveHandle=function(){null!==this.activeHandle&&-1!==this.activeHandle&&(this.trigger("handleOff",this.activeHandle),this.activeHandle=null)},u.prototype.enableKeyboardEvents=function(t){s(t.target).on("keydown",this.handleKeyboardEvent),s(t.target).on("keyup",this.handleKeyboardEvent)},u.prototype.disableKeyboardEvents=function(t){s(t.target).off("keydown",this.handleKeyboardEvent),s(t.target).off("keyup",this.handleKeyboardEvent)},u.prototype.handleKeyboardEvent=function(t){if("keydown"===t.type){if(this.keyFloodTimer&&(window.clearTimeout(this.keyFloodTimer),this.keyFloodTimer=null),null==this.previousValue&&!this.start())return!1}else if("keyup"===t.type)return null!=this.previousValue&&(this.options.keyFloodWait?this.keyFloodTimer=window.setTimeout(h.bind(this.resolve,this),this.options.keyFloodWait):this.resolve()),!0;switch(t.which){case 33:this.stepUp(10,!1,!0);break;case 34:this.stepDown(10,!1,!0);break;case 37:case 40:t.metaKey?this.setTentativeValue(this.model.minimum,!1):this.stepDown(t.altKey?10:1,!1,!0);break;case 39:case 38:t.metaKey?this.setTentativeValue(this.model.maximum,!1):this.stepUp(t.altKey?10:1,!1,!0);break;default:return!0}t.preventDefault()},u.prototype.enhance=function(e){var t;this.options.disabled=e.attr("disabled"),this.options.step=l(e,this,"step"),this.options.value=l(e,this,"value"),this.options.min=l(e,this,"min"),this.options.max=l(e,this,"max"),this.wrapper=(t=e,s("<div />").css({width:t.outerWidth(),margin:t.css("margin"),display:"inline-block"}).insertAfter(e.hide())),this.on("change",function(t){e.val(t)}),e.on("change",h.bind(function(){this.setValue(e.val())},this))},d.prototype.setValue=function(t,e){for(var i=this.values,s=0,a=(t=h.isArray(t)?h.clone(t):[t]).length;s<a;s++)t[s]=this.sanitizeValue(t[s],e);return!h.isEqual(t,i)&&(this.value=this.values=t,1===this.values.length&&(this.value=t[0]),this.value)},d.prototype.roundToStep=function(e){var t=1/this.step,t=Math.round(e*t)/t;return h.isArray(this.only)&&(t=h.min(this.only,function(t){return Math.abs(t-e)})),t>this.maximum?t-this.step:t<this.minimum?t+this.step:t},d.prototype.sanitizeValue=function(t,e){return!1!==e&&(t=this.roundToStep(t)),t>this.maximum?this.maximum:t<this.minimum?this.minimum:t},(u.Renderer=function(t){h.bindAll(this,"render","redraw");var e=this;this.quinn=t,this.model=t.model,this.wrapper=t.wrapper,this.options=t.options,this.handles=[],this.lastDraw=[],t.on("redraw",this.redraw),t.on("handleOn",function(t){e.handles[t].addClass("active")}),t.on("handleOff",function(t){e.handles[t].removeClass("active")}),t.on("enabled",function(){e.wrapper.removeClass("disabled"),1!==e.options.disabledOpacity&&e.wrapper.css("opacity",1)}),t.on("disabled",function(){e.wrapper.addClass("disabled"),1!==e.options.disabledOpacity&&e.wrapper.css("opacity",e.options.disabledOpacity)})}).prototype.render=function(){var t,e;function i(t){t.append(s('<div class="left" />')),t.append(s('<div class="main" />')),t.append(s('<div class="right" />'))}for(this.width=this.wrapper.width(),this.bar=s('<div class="bar" />'),this.deltaBar=s('<div class="delta-bar" />'),1<this.model.values.length&&this.wrapper.addClass("multiple"),i(this.bar),this.model.values.length<=2&&(i(this.deltaBar),this.bar.append(this.deltaBar)),this.wrapper.html(this.bar),this.wrapper.addClass("quinn"),t=0,e=this.model.values.length;t<e;t++)this.handles[t]=s('<span class="handle" tabindex="0" role="slider"></span>').attr("aria-valuemin",this.model.minimum).attr("aria-valuemax",this.model.maximum).attr("aria-valuenow",this.model.values[t]),this.handles[t].on(a,this.quinn.startDrag),this.quinn.model.values.length<2&&(this.handles[t].on("focus",this.quinn.enableKeyboardEvents),this.handles[t].on("blur",this.quinn.disableKeyboardEvents)),this.bar.append(this.handles[t]);this.wrapper.on(a,this.quinn.clickBar),this.barHeight=this.bar.height(),this.handleOverhang=parseInt(this.handles[0].css("margin-left"),10)+parseInt(this.handles[0].css("margin-right"),10),this.redraw(!1)},u.Renderer.prototype.redraw=function(s){var a=this;!1!==s&&(s=!0),h.each(this.model.values,function(t,e){var i;return t===a.lastDraw[e]||(i=a.handles[e].stop(),e=a.position(t)+"px",i.attr("aria-valuenow",t),void(s&&a.options.effects?i.animate({left:e},{duration:a.options.effectSpeed,step:a.redrawDeltaBarInStep(i)}):(i.css("left",e),a.redrawDeltaBar(t,i))))}),this.lastDraw=h.clone(this.model.values)},u.Renderer.prototype.redrawDeltaBar=function(t,e){var i=null,s=null,a=parseInt(e.css("left"),10)+this.barHeight;this.deltaBar.stop(!0),1<this.model.values.length?e?e===this.handles[0]?i=a:s=a:(i=t[0],s=t[1]):t<0?(i=a,s=this.position(0,!0)):(s=a,i=this.position(0,!0)),null!==i&&this.deltaBar.css("left",i),null!==s&&(s=this.width-s,this.deltaBar.css("right",s))},u.Renderer.prototype.redrawDeltaBarInStep=function(e){if(!this.deltaBar)return function(){};var i=this.quinn.drawTo.left,s=this.quinn.drawTo.right,a=Math.ceil(this.deltaBar.height()/2),n=this;return function(t){return t=(t+a)/n.width,n.redrawDeltaBar(t*(s-i)+i,e),!0}},u.Renderer.prototype.position=function(t,e){var i=this.quinn.drawTo.right-this.quinn.drawTo.left,s=this.width;return e||(s-=this.handles[0].width()+this.handleOverhang),(i=(t-this.quinn.drawTo.left)/i*s)<0?0:s<i?s:Math.round(i)},u.defaults={min:0,max:100,drawTo:null,step:1,value:null,strict:!0,only:null,disable:!1,disabledOpacity:.5,width:null,handleWidth:null,drag:null,change:null,setup:null,renderer:u.Renderer,keyFloodWait:!1,effectSpeed:"fast",effects:!0},s.fn.quinn=function(t){return s.each(this,function(){new u(s(this),t)})},s.Quinn=u}(jQuery,_);