Commit a9739ebe authored by Rafal Zajac's avatar Rafal Zajac

Update to latest version

parent ef482b3b
...@@ -4,12 +4,13 @@ ...@@ -4,12 +4,13 @@
* (c) Rafal Zajac <rzajac@gmail.com> * (c) Rafal Zajac <rzajac@gmail.com>
* http://github.com/rzajac/angularjs-slider * http://github.com/rzajac/angularjs-slider
* *
* Version: v0.1.17 * Version: v0.1.21
* *
* Licensed under the MIT license * Licensed under the MIT license
*/ */
/*global angular: false */ /*jslint unparam: true */
/*global angular: false, console: false */
angular.module('rzModule', []) angular.module('rzModule', [])
...@@ -177,6 +178,15 @@ function throttle(func, wait, options) { ...@@ -177,6 +178,15 @@ function throttle(func, wait, options) {
*/ */
this.hideLimitLabels = !!attributes.rzSliderHideLimitLabels; this.hideLimitLabels = !!attributes.rzSliderHideLimitLabels;
/**
* Only present model values
*
* Do not allow to change values
*
* @type {boolean}
*/
this.presentOnly = attributes.rzSliderPresentOnly === 'true';
/** /**
* The delta between min and max value * The delta between min and max value
* *
...@@ -196,9 +206,14 @@ function throttle(func, wait, options) { ...@@ -196,9 +206,14 @@ function throttle(func, wait, options) {
* *
* @type {function} * @type {function}
*/ */
this.customTrFn = this.scope.rzSliderTranslate() || function(value) { return '' + value; }; this.customTrFn = this.scope.rzSliderTranslate() || function(value) { return String(value); };
this.unbinders = []; /**
* Array of de-registration functions to call on $destroy
*
* @type {Array.<Function>}
*/
this.deRegFuncs = [];
// Slider DOM elements wrapped in jqLite // Slider DOM elements wrapped in jqLite
this.fullBar = null; // The whole slider bar this.fullBar = null; // The whole slider bar
...@@ -225,7 +240,9 @@ function throttle(func, wait, options) { ...@@ -225,7 +240,9 @@ function throttle(func, wait, options) {
*/ */
init: function() init: function()
{ {
var self = this; var thrLow, thrHigh, unRegFn,
calcDimFn = angular.bind(this, this.calcViewDimensions),
self = this;
this.initElemHandles(); this.initElemHandles();
this.calcViewDimensions(); this.calcViewDimensions();
...@@ -239,20 +256,21 @@ function throttle(func, wait, options) { ...@@ -239,20 +256,21 @@ function throttle(func, wait, options) {
self.updateCeilLab(); self.updateCeilLab();
self.updateFloorLab(); self.updateFloorLab();
self.initHandles(); self.initHandles();
self.bindEvents(); if (!self.presentOnly) { self.bindEvents(); }
}); });
// Recalculate slider view dimensions // Recalculate slider view dimensions
this.scope.$on('reCalcViewDimensions', angular.bind(this, this.calcViewDimensions)); unRegFn = this.scope.$on('reCalcViewDimensions', calcDimFn);
this.deRegFuncs.push(unRegFn);
// Recalculate stuff if view port dimensions have changed // Recalculate stuff if view port dimensions have changed
angular.element($window).on('resize', angular.bind(this, this.calcViewDimensions)); angular.element($window).on('resize', calcDimFn);
this.initHasRun = true; this.initHasRun = true;
// Watch for changes to the model // Watch for changes to the model
var thrLow = throttle(function() thrLow = throttle(function()
{ {
self.setMinAndMax(); self.setMinAndMax();
self.updateLowHandle(self.valueToOffset(self.scope.rzSliderModel)); self.updateLowHandle(self.valueToOffset(self.scope.rzSliderModel));
...@@ -265,7 +283,7 @@ function throttle(func, wait, options) { ...@@ -265,7 +283,7 @@ function throttle(func, wait, options) {
}, 350, { leading: false }); }, 350, { leading: false });
var thrHigh = throttle(function() thrHigh = throttle(function()
{ {
self.setMinAndMax(); self.setMinAndMax();
self.updateHighHandle(self.valueToOffset(self.scope.rzSliderHigh)); self.updateHighHandle(self.valueToOffset(self.scope.rzSliderHigh));
...@@ -283,44 +301,40 @@ function throttle(func, wait, options) { ...@@ -283,44 +301,40 @@ function throttle(func, wait, options) {
// Watchers // Watchers
this.unbinders.push(this.scope.$watch('rzSliderModel', function(newValue, oldValue) unRegFn = this.scope.$watch('rzSliderModel', function(newValue, oldValue)
{ {
if(newValue === oldValue) { return; } if(newValue === oldValue) { return; }
thrLow(); thrLow();
})); });
this.deRegFuncs.push(unRegFn);
this.unbinders.push(this.scope.$watch('rzSliderHigh', function(newValue, oldValue) unRegFn = this.scope.$watch('rzSliderHigh', function(newValue, oldValue)
{ {
if(newValue === oldValue) { return; } if(newValue === oldValue) { return; }
thrHigh(); thrHigh();
})); });
this.deRegFuncs.push(unRegFn);
this.unbinders.push(this.scope.$watch('rzSliderFloor', function(newValue, oldValue) this.scope.$watch('rzSliderFloor', function(newValue, oldValue)
{ {
if(newValue === oldValue) { return; } if(newValue === oldValue) { return; }
self.resetSlider(); self.resetSlider();
})); });
this.deRegFuncs.push(unRegFn);
this.unbinders.push(this.scope.$watch('rzSliderCeil', function(newValue, oldValue) unRegFn = this.scope.$watch('rzSliderCeil', function(newValue, oldValue)
{ {
if(newValue === oldValue) { return; } if(newValue === oldValue) { return; }
self.resetSlider(); self.resetSlider();
}));
$document.on('$destroy', function()
{
self.minH.off('.rzslider');
self.maxH.off('.rzslider');
$document.off('.rzslider');
angular.element($window).off('.rzslider');
}); });
this.deRegFuncs.push(unRegFn);
this.scope.$on('$destroy', function() this.scope.$on('$destroy', function()
{ {
self.unbinders.map(function(unbind) self.minH.off();
{ self.maxH.off();
unbind(); angular.element($window).off('resize', calcDimFn);
}); self.deRegFuncs.map(function(unbind) { unbind(); });
}); });
}, },
...@@ -380,7 +394,7 @@ function throttle(func, wait, options) { ...@@ -380,7 +394,7 @@ function throttle(func, wait, options) {
{ {
useCustomTr = useCustomTr === undefined ? true : useCustomTr; useCustomTr = useCustomTr === undefined ? true : useCustomTr;
var valStr = useCustomTr ? '' + this.customTrFn(value) : '' + value, var valStr = String(useCustomTr ? this.customTrFn(value) : value),
getWidth = false; getWidth = false;
if(label.rzsv === undefined || label.rzsv.length !== valStr.length || (label.rzsv.length > 0 && label.rzsw === 0)) if(label.rzsv === undefined || label.rzsv.length !== valStr.length || (label.rzsv.length > 0 && label.rzsw === 0))
...@@ -594,7 +608,8 @@ function throttle(func, wait, options) { ...@@ -594,7 +608,8 @@ function throttle(func, wait, options) {
updateLowHandle: function(newOffset) updateLowHandle: function(newOffset)
{ {
var delta = Math.abs(this.minH.rzsl - newOffset); var delta = Math.abs(this.minH.rzsl - newOffset);
if(delta === 0 || delta === 1) { return; }
if(delta <= 0 && delta < 1) { return; }
this.setLeft(this.minH, newOffset); this.setLeft(this.minH, newOffset);
this.translateFn(this.scope.rzSliderModel, this.minLab); this.translateFn(this.scope.rzSliderModel, this.minLab);
...@@ -727,11 +742,12 @@ function throttle(func, wait, options) { ...@@ -727,11 +742,12 @@ function throttle(func, wait, options) {
*/ */
roundStep: function(value) roundStep: function(value)
{ {
var step = this.step / Math.pow(10,this.precision), var step = this.step,
remainder = (value - this.minValue) % step, remainder = +((value - this.minValue) % step).toFixed(3),
steppedValue = remainder > (step / 2) ? value + step - remainder : value - remainder; steppedValue = remainder > (step / 2) ? value + step - remainder : value - remainder;
return +(steppedValue).toFixed(this.precision); steppedValue = steppedValue.toFixed(this.precision);
return +steppedValue;
}, },
/** /**
...@@ -807,7 +823,7 @@ function throttle(func, wait, options) { ...@@ -807,7 +823,7 @@ function throttle(func, wait, options) {
*/ */
valueToOffset: function(val) valueToOffset: function(val)
{ {
return Math.round((Math.ceil(val) - this.minValue) * this.maxLeft / this.valueRange); return (val - this.minValue) * this.maxLeft / this.valueRange;
}, },
/** /**
...@@ -818,7 +834,7 @@ function throttle(func, wait, options) { ...@@ -818,7 +834,7 @@ function throttle(func, wait, options) {
*/ */
offsetToValue: function(offset) offsetToValue: function(offset)
{ {
return Math.ceil( (offset / this.maxLeft) * this.valueRange + this.minValue ); return (offset / this.maxLeft) * this.valueRange + this.minValue;
}, },
// Events // Events
...@@ -847,6 +863,9 @@ function throttle(func, wait, options) { ...@@ -847,6 +863,9 @@ function throttle(func, wait, options) {
*/ */
onStart: function (pointer, ref, event) onStart: function (pointer, ref, event)
{ {
var ehMove, ehEnd,
eventNames = this.getEventNames(event);
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
...@@ -859,16 +878,11 @@ function throttle(func, wait, options) { ...@@ -859,16 +878,11 @@ function throttle(func, wait, options) {
pointer.addClass('rz-active'); pointer.addClass('rz-active');
if(event.touches || (event.originalEvent !== undefined && event.originalEvent.touches)) ehMove = angular.bind(this, this.onMove, pointer);
{ ehEnd = angular.bind(this, this.onEnd, ehMove);
$document.on('touchmove', angular.bind(this, this.onMove, pointer));
$document.one('touchend', angular.bind(this, this.onEnd)); $document.on(eventNames.moveEvent, ehMove);
} $document.one(eventNames.endEvent, ehEnd);
else
{
$document.on('mousemove', angular.bind(this, this.onMove, pointer));
$document.one('mouseup', angular.bind(this, this.onEnd));
}
}, },
/** /**
...@@ -882,6 +896,8 @@ function throttle(func, wait, options) { ...@@ -882,6 +896,8 @@ function throttle(func, wait, options) {
{ {
var eventX, sliderLO, newOffset, newValue; var eventX, sliderLO, newOffset, newValue;
/* http://stackoverflow.com/a/12336075/282882 */
//noinspection JSLint
if('clientX' in event) if('clientX' in event)
{ {
eventX = event.clientX; eventX = event.clientX;
...@@ -922,6 +938,7 @@ function throttle(func, wait, options) { ...@@ -922,6 +938,7 @@ function throttle(func, wait, options) {
newValue = this.offsetToValue(newOffset); newValue = this.offsetToValue(newOffset);
newValue = this.roundStep(newValue); newValue = this.roundStep(newValue);
newOffset = this.valueToOffset(newValue);
if (this.range) if (this.range)
{ {
...@@ -954,26 +971,46 @@ function throttle(func, wait, options) { ...@@ -954,26 +971,46 @@ function throttle(func, wait, options) {
/** /**
* onEnd event handler * onEnd event handler
* *
* @param {Event} event The event * @param {Event} event The event
* @param {Function} ehMove The the bound move event handler
* @returns {undefined} * @returns {undefined}
*/ */
onEnd: function(event) onEnd: function(ehMove, event)
{ {
var moveEventName = this.getEventNames(event).moveEvent;
this.minH.removeClass('rz-active'); this.minH.removeClass('rz-active');
this.maxH.removeClass('rz-active'); this.maxH.removeClass('rz-active');
$document.off(moveEventName, ehMove);
this.scope.$emit('slideEnded');
this.tracking = '';
},
/**
* Get event names for move and event end
*
* @param {Event} event The event
*
* @return {{moveEvent: string, endEvent: string}}
*/
getEventNames: function(event)
{
var eventNames = {moveEvent: '', endEvent: ''};
if(event.touches || (event.originalEvent !== undefined && event.originalEvent.touches)) if(event.touches || (event.originalEvent !== undefined && event.originalEvent.touches))
{ {
$document.off('touchmove'); eventNames.moveEvent = 'touchmove';
eventNames.endEvent = 'touchend';
} }
else else
{ {
$document.off('mousemove'); eventNames.moveEvent = 'mousemove';
eventNames.endEvent = 'mouseup';
} }
this.scope.$emit('slideEnded'); return eventNames;
this.tracking = '';
} }
}; };
...@@ -995,7 +1032,8 @@ function throttle(func, wait, options) { ...@@ -995,7 +1032,8 @@ function throttle(func, wait, options) {
rzSliderHigh: '=?', rzSliderHigh: '=?',
rzSliderTranslate: '&', rzSliderTranslate: '&',
rzSliderHideLimitLabels: '=?', rzSliderHideLimitLabels: '=?',
rzSliderAlwaysShowBar: '=?' rzSliderAlwaysShowBar: '=?',
rzSliderPresentOnly: '@'
}, },
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment