(function() { var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; window.poisedCurrentSlider = null; Backbone.Poised.Slider = (function(superClass) { extend(Slider, superClass); function Slider() { this.render = bind(this.render, this); this.updateHandlePosition = bind(this.updateHandlePosition, this); this.handlePanEnd = bind(this.handlePanEnd, this); this.handlePan = bind(this.handlePan, this); this.handlePress = bind(this.handlePress, this); this.handlePanStart = bind(this.handlePanStart, this); this.startLiveChange = bind(this.startLiveChange, this); this.initialize = bind(this.initialize, this); return Slider.__super__.constructor.apply(this, arguments); } Slider.prototype.template = _.template('
'); Slider.prototype.className = 'poised slider'; Slider.prototype.startValue = 0; Slider.prototype.range = 100; Slider.prototype.events = { 'tap': 'handleTap', 'panstart': 'handlePanStart', 'press': 'handlePress', 'pan': 'handlePan', 'panend': 'handlePanEnd', 'pressup': 'handlePanEnd' }; Slider.prototype.hammerjs = { recognizers: [ [ Hammer.Rotate, { enable: false } ], [ Hammer.Pinch, { enable: false }, ['rotate'] ], [ Hammer.Swipe, { enable: false } ], [ Hammer.Pan, { direction: Hammer.DIRECTION_HORIZONTAL, threshold: 1 }, ['swipe'] ], [ Hammer.Tap, { threshold: 5 } ], [Hammer.Press] ] }; Slider.prototype.initialize = function(options) { if (options == null) { options = {}; } if (options.model == null) { throw new Error('Missing `model` option'); } if (options.attribute == null) { throw new Error('Missing `attribute` option'); } this.attribute = options.attribute; options = _.chain(options).pick('maxValue', 'minValue', 'stepSize').defaults({ minValue: 0, maxValue: 100, stepSize: 1 }).value(); if (options.maxValue != null) { this.range = options.maxValue; } if (options.minValue != null) { this.startValue = options.minValue; this.range -= options.minValue; } this.stepSize = options.stepSize; return this.model.on("change:" + this.attribute, this.updateHandlePosition); }; Slider.prototype.clearFocus = function() { return $('input').blur(); }; Slider.prototype.handleTap = function(e) { var touchPosition; touchPosition = e.gesture.center.x - this.$bar.offset().left; this.model.set(this.attribute, this.calculateAttributeValue(touchPosition)); return this.trigger('tapChange', this); }; Slider.prototype.isPanning = false; Slider.prototype.startLiveChange = function() { this.isPanning = true; this.clearFocus(); return this.trigger('liveChangeStart', this); }; Slider.prototype.handlePanStart = function(e) { if (Math.abs(e.gesture.deltaX) > Math.abs(e.gesture.deltaY) && !this.isPanning) { return this.startLiveChange(); } }; Slider.prototype.handlePress = function(e) { this.startLiveChange(); return this.handlePan(e); }; Slider.prototype.handlePan = function(e) { var touchPosition; if (this.isPanning) { touchPosition = e.gesture.center.x - this.$el.offset().left; this.model.set(this.attribute, this.calculateAttributeValue(touchPosition)); return this.trigger('liveChange', this); } }; Slider.prototype.handlePanEnd = function(e) { if (this.isPanning) { this.trigger('liveChangeEnd', this); return this.isPanning = false; } }; Slider.prototype.calculateAttributeValue = function(position) { var barWidth; barWidth = this.$bar.width(); position = Math.max(position, 0); position = Math.min(position, barWidth); return this.startValue + Math.round((this.range * position / barWidth) / this.stepSize) * this.stepSize; }; Slider.prototype.calculateHandlePosition = function(val) { val = Math.max(val, this.startValue); val = Math.min(val, this.startValue + this.range); return (val - this.startValue) / this.range * 100; }; Slider.prototype.updateHandlePosition = function(model, value) { var position; position = this.calculateHandlePosition(value); return this.$handle.css('left', (position.toFixed(2)) + "%"); }; Slider.prototype.render = function() { this.$el.html(this.template()); this.$handle = this.$el.find('.handle'); this.$bar = this.$el.find('.bar'); this.updateHandlePosition(this.model, this.model.get(this.attribute)); return this; }; return Slider; })(Backbone.Poised.View); }).call(this);