var app = angular.module('plunker', []);
var app = angular.module('app', ['ui-rangeSlider']);
app.controller('controllerA', ['$scope', controllerA]);
function controllerA($scope) {
$scope.min = 10;
$scope.max = 25;
}
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link href="style.css" rel="stylesheet" />
<script data-semver="1.2.13" src="http://code.angularjs.org/1.2.13/angular.js" data-require="angular.js@1.2.x"></script>
<script src="app.js"></script>
<script src="angular.rangeslider.js"></script>
<link rel="stylesheet" href="angular.rangeslider.css" />
</head>
<body>
<div ng-controller="controllerA">
<h3>Slider</h3>
<div range-slider min="10" max="25" pin-handle="min" model-max="max"></div>
<div>{{min}}</div>
<div>{{max}}</div>
</div>
</body>
</html>
/* Put your css in here */
/**
* Angular RangeSlider SCSS
*
* Version: 0.0.13
*
* Author: Daniel Crisp, danielcrisp.com
*
* The rangeSlider has been styled to match the default styling
* of form elements styled using Twitter's Bootstrap
*
* Originally forked from https://github.com/leongersen/noUiSlider
*
This code is released under the MIT Licence - http://opensource.org/licenses/MIT
Copyright (c) 2013 Daniel Crisp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*------------------------------------*\
COMPASS IMPORTS
\*------------------------------------*/
/*------------------------------------*\
SETTINGS
\*------------------------------------*/
/*------------------------------------*\
THE CSS
\*------------------------------------*/
/* line 25, scss/_rangeSlider.scss */
.ngrs-range-slider {
position: relative;
margin: 10px 0 30px;
padding: 4px;
border: 1px solid #ccc;
background: #fff;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
-moz-transition: border 0.2s linear, box-shadow 0.2s linear;
-o-transition: border 0.2s linear, box-shadow 0.2s linear;
-webkit-transition: border 0.2s linear, box-shadow 0.2s linear;
transition: border 0.2s linear, box-shadow 0.2s linear;
-webkit-tap-highlight-color: transparent;
/*------------------------------------*\
RUNNER
\*------------------------------------*/
/*------------------------------------*\
JOIN (connects the two handles)
\*------------------------------------*/
/*------------------------------------*\
HANDLE
\*------------------------------------*/
/*------------------------------------*\
HANDLE SPECIFICS
\*------------------------------------*/
/*------------------------------------*\
VALUE LABELS
\*------------------------------------*/
/*------------------------------------*\
ATTACHED VALUE RUNNER
\*------------------------------------*/
/*------------------------------------*\
VERTICAL SLIDER
\*------------------------------------*/
/*------------------------------------*\
FOCUS STATE
\*------------------------------------*/
/*------------------------------------*\
DISABLED STATE
\*------------------------------------*/
}
/* line 28, scss/_rangeSlider.scss */
.ngrs-range-slider, .ngrs-range-slider * {
display: block;
cursor: default;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
-moz-user-select: -moz-none;
-ms-user-select: none;
-webkit-user-select: none;
user-select: none;
}
/* line 53, scss/_rangeSlider.scss */
.ngrs-range-slider .ngrs-runner {
position: relative;
margin: 0 9px;
height: 18px;
}
/* line 63, scss/_rangeSlider.scss */
.ngrs-range-slider .ngrs-join {
position: absolute;
z-index: 1;
top: 50%;
left: 0;
right: 100%;
height: 8px;
margin: -4px 0 0 0;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
background-color: #2f96b4;
background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4gPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGRlZnM+PGxpbmVhckdyYWRpZW50IGlkPSJncmFkIiBncmFkaWVudFVuaXRzPSJvYmplY3RCb3VuZGluZ0JveCIgeDE9IjAuNSIgeTE9IjAuMCIgeDI9IjAuNSIgeTI9IjEuMCI+PHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzViYzBkZSIvPjxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iIzJmOTZiNCIvPjwvbGluZWFyR3JhZGllbnQ+PC9kZWZzPjxyZWN0IHg9IjAiIHk9IjAiIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InVybCgjZ3JhZCkiIC8+PC9zdmc+IA==');
background-size: 100%;
background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #5bc0de), color-stop(100%, #2f96b4));
background-image: -moz-linear-gradient(#5bc0de, #2f96b4);
background-image: -webkit-linear-gradient(#5bc0de, #2f96b4);
background-image: linear-gradient(#5bc0de, #2f96b4);
}
/* line 81, scss/_rangeSlider.scss */
.ngrs-range-slider .ngrs-handle {
position: absolute;
z-index: 2;
height: 100%;
width: 18px;
margin: 0 0 0 -9px;
background: #efefef;
border: 1px solid #ccc;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
/*------------------------------------*\
HANDLE ICON
\*------------------------------------*/
}
/* line 95, scss/_rangeSlider.scss */
.ngrs-range-slider .ngrs-handle i {
display: block;
width: 100%;
height: 100%;
background: no-repeat -9999px -9999px;
cursor: pointer;
}
/* line 104, scss/_rangeSlider.scss */
.ngrs-range-slider .ngrs-handle.ngrs-over i {
background-position: 50% 50%;
}
/* line 109, scss/_rangeSlider.scss */
.ngrs-range-slider .ngrs-handle.ngrs-down {
-moz-box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
-webkit-box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
}
/* line 120, scss/_rangeSlider.scss */
.ngrs-range-slider .ngrs-handle-min i {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAFNJREFUeNpiYMAEXEDsA+OwoEnKALETEHOgK2AEYhMgNkQ3DqSAB6pLAot1DExIJmAFzED8C4hvQdnIppyFKYCBp0D8CohloVafxWUqN7I3AQIMAKw6B24pOi8lAAAAAElFTkSuQmCC");
}
/* line 127, scss/_rangeSlider.scss */
.ngrs-range-slider .ngrs-handle-max i {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAFdJREFUeNpiYEAAHyDmYkADzEhsByBWA+K3QPwJmwJjIGYBYlUgZgLi59gUwIAkEEsD8VMmBtyAkQFqJDZwAYjPAPE/dAU/gHg/ED/GpgvkTW50QYAAAwADfwrM5sqplgAAAABJRU5ErkJggg==");
}
/* line 137, scss/_rangeSlider.scss */
.ngrs-range-slider .ngrs-value {
position: absolute;
top: 100%;
left: 0;
padding: 5px 0 0 0;
font-size: 12px;
color: #999;
}
/* line 145, scss/_rangeSlider.scss */
.ngrs-range-slider .ngrs-value.ngrs-value-max {
left: auto;
right: 0;
text-align: right;
}
/* line 152, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-handle-min-down .ngrs-value-min, .ngrs-range-slider.ngrs-handle-max-down .ngrs-value-max {
color: #333;
}
/* line 160, scss/_rangeSlider.scss */
.ngrs-range-slider .ngrs-attached-handles {
margin: 0 9px;
position: relative;
/*------------------------------------*\
ATTACHED VALUE RUNNER LABELS
\*------------------------------------*/
}
/* line 167, scss/_rangeSlider.scss */
.ngrs-range-slider .ngrs-attached-handles .ngrs-value {
text-align: left;
}
/* line 172, scss/_rangeSlider.scss */
.ngrs-range-slider .ngrs-attached-handles .ngrs-value > div {
margin: 0 0 0 -50%;
}
/* line 181, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical {
width: 28px;
margin: 10px auto;
/*------------------------------------*\
RUNNER
\*------------------------------------*/
/*------------------------------------*\
ATTACHED VALUE RUNNER
\*------------------------------------*/
/*------------------------------------*\
JOIN
\*------------------------------------*/
/*------------------------------------*\
HANDLE
\*------------------------------------*/
/*------------------------------------*\
HANDLE SPECIFICS
\*------------------------------------*/
/*------------------------------------*\
VALUE LABELS
\*------------------------------------*/
/*------------------------------------*\
VERTICAL LEFT SLIDER
\*------------------------------------*/
/*------------------------------------*\
VERTICAL RIGHT SLIDER
\*------------------------------------*/
}
/* line 189, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical .ngrs-runner {
margin: 9px 0;
height: 300px;
width: 18px;
}
/* line 199, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical .ngrs-value-runner.ngrs-attached-handles {
position: absolute;
top: 0;
left: 100%;
bottom: 0;
margin: 9px 0;
}
/* line 212, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical .ngrs-join {
width: 8px;
height: auto;
top: 0;
bottom: 100%;
left: 50%;
right: auto;
margin: 0 0 0 -4px;
}
/* line 226, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical .ngrs-handle {
width: 100%;
height: 18px;
margin: -9px 0 0 0;
}
/* line 237, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical .ngrs-handle-min i {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAFFJREFUeNpiYEAFPFAMB0xIbEYgdoJiRpggM5ICUyBWhZoA0vgMWYEsENsg6ZQE4ldA/AmkkguIHZGNhQKQGBfIBHcgFmTABCxALMJAMQAIMAAcNgVQJ7t7JQAAAABJRU5ErkJggg==");
}
/* line 244, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical .ngrs-handle-max i {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAFZJREFUeNpiYKAUMAKxDxBL4ZB/xgQk9gHxDyySILF9zEDiNxC/A2JVNAW7gfgtM5TzCYhZgFgCyr8IxNdADGZk+4BYGoi/APEBIP6PzVE8UAwHAAEGAArIDvzRFIA6AAAAAElFTkSuQmCC");
}
/* line 254, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical .ngrs-value {
top: 0;
left: 100%;
padding: 0 0 0 5px;
}
/* line 259, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical .ngrs-value.ngrs-value-max {
top: auto;
bottom: 0;
right: auto;
text-align: left;
}
/* line 269, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical .ngrs-attached-handles .ngrs-value > div {
margin: -50% 0 0 0;
}
/* line 279, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical.ngrs-left {
margin: 10px 0;
}
/* line 287, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical.ngrs-right {
margin: 10px 0 10px auto;
/*------------------------------------*\
VALUE LABELS
\*------------------------------------*/
}
/* line 294, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical.ngrs-right .ngrs-value {
left: auto;
right: 100%;
padding: 0 5px 0 0;
text-align: right;
}
/* line 300, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical.ngrs-right .ngrs-value.ngrs-value-max {
text-align: right;
}
/* line 306, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-vertical.ngrs-right .ngrs-value-runner.ngrs-attached-handles {
left: 0;
}
/* line 318, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-focus {
border-color: rgba(82, 168, 236, 0.8);
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}
/* line 329, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-disabled, .ngrs-range-slider.ngrs-disabled.ngrs-focus {
border-color: #ddd;
-moz-box-shadow: none;
-webkit-box-shadow: none;
box-shadow: none;
}
/* line 335, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-disabled .ngrs-handle {
background: #fff;
border-color: #ddd;
}
/* line 339, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-disabled .ngrs-handle i {
background: none !important;
cursor: default;
}
/* line 345, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-disabled .ngrs-join {
background: #ddd;
}
/* line 349, scss/_rangeSlider.scss */
.ngrs-range-slider.ngrs-disabled .ngrs-value {
color: #ddd;
}
/*------------------------------------*\
TOUCH STATE
\*------------------------------------*/
/* line 361, scss/_rangeSlider.scss */
body.ngrs-touching {
-ms-touch-action: none;
}
/*
* Angular RangeSlider Directive
*
* Version: 0.0.13
*
* Author: Daniel Crisp, danielcrisp.com
*
* The rangeSlider has been styled to match the default styling
* of form elements styled using Twitter's Bootstrap
*
* Originally forked from https://github.com/leongersen/noUiSlider
*
This code is released under the MIT Licence - http://opensource.org/licenses/MIT
Copyright (c) 2013 Daniel Crisp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
(function() {
'use strict';
// check if we need to support legacy angular
var legacySupport = (angular.version.major === 1 && angular.version.minor === 0);
/**
* RangeSlider, allows user to define a range of values using a slider
* Touch friendly.
* @directive
*/
angular.module('ui-rangeSlider', [])
.directive('rangeSlider', ['$document', '$filter', '$log', function($document, $filter, $log) {
// test for mouse, pointer or touch
var eventNamespace = '.rangeSlider',
defaults = {
disabled: false,
orientation: 'horizontal',
step: 0,
decimalPlaces: 0,
showValues: true,
preventEqualMinMax: false,
attachHandleValues: false
},
// Determine the events to bind. IE11 implements pointerEvents without
// a prefix, which breaks compatibility with the IE10 implementation.
/** @const */
actions = window.navigator.pointerEnabled ? {
start: 'pointerdown',
move: 'pointermove',
end: 'pointerup',
over: 'pointerdown',
out: 'mouseout'
} : window.navigator.msPointerEnabled ? {
start: 'MSPointerDown',
move: 'MSPointerMove',
end: 'MSPointerUp',
over: 'MSPointerDown',
out: 'mouseout'
} : {
start: 'mousedown touchstart',
move: 'mousemove touchmove',
end: 'mouseup touchend',
over: 'mouseover touchstart',
out: 'mouseout'
},
onEvent = actions.start + eventNamespace,
moveEvent = actions.move + eventNamespace,
offEvent = actions.end + eventNamespace,
overEvent = actions.over + eventNamespace,
outEvent = actions.out + eventNamespace,
// get standarised clientX and clientY
client = function(f) {
try {
return [(f.clientX || f.originalEvent.clientX || f.originalEvent.touches[0].clientX), (f.clientY || f.originalEvent.clientY || f.originalEvent.touches[0].clientY)];
} catch (e) {
return ['x', 'y'];
}
},
restrict = function(value) {
// normalize so it can't move out of bounds
return (value < 0 ? 0 : (value > 100 ? 100 : value));
},
isNumber = function(n) {
// console.log(n);
return !isNaN(parseFloat(n)) && isFinite(n);
},
scopeOptions = {
disabled: '=?',
min: '=',
max: '=',
modelMin: '=?',
modelMax: '=?',
onHandleDown: '&', // calls optional function when handle is grabbed
onHandleUp: '&', // calls optional function when handle is released
orientation: '@', // options: horizontal | vertical | vertical left | vertical right
step: '@',
decimalPlaces: '@',
filter: '@',
filterOptions: '@',
showValues: '@',
pinHandle: '@',
preventEqualMinMax: '@',
attachHandleValues: '@'
};
if (legacySupport) {
// make optional properties required
scopeOptions.disabled = '=';
scopeOptions.modelMin = '=';
scopeOptions.modelMax = '=';
}
// if (EVENT < 4) {
// // some sort of touch has been detected
// angular.element('html').addClass('ngrs-touch');
// } else {
// angular.element('html').addClass('ngrs-no-touch');
// }
return {
restrict: 'A',
replace: true,
template: ['<div class="ngrs-range-slider">',
'<div class="ngrs-runner">',
'<div class="ngrs-handle ngrs-handle-min"><i></i></div>',
'<div class="ngrs-handle ngrs-handle-max"><i></i></div>',
'<div class="ngrs-join"></div>',
'</div>',
'<div class="ngrs-value-runner">',
'<div class="ngrs-value ngrs-value-min" ng-show="showValues"><div>{{filteredModelMin}}</div></div>',
'<div class="ngrs-value ngrs-value-max" ng-show="showValues"><div>{{filteredModelMax}}</div></div>',
'</div>',
'</div>'
].join(''),
scope: scopeOptions,
link: function(scope, element, attrs, controller) {
/**
* FIND ELEMENTS
*/
var $slider = angular.element(element),
handles = [element.find('.ngrs-handle-min'), element.find('.ngrs-handle-max')],
values = [element.find('.ngrs-value-min'), element.find('.ngrs-value-max')],
join = element.find('.ngrs-join'),
pos = 'left',
posOpp = 'right',
orientation = 0,
allowedRange = [0, 0],
range = 0,
down = false;
// filtered
scope.filteredModelMin = scope.modelMin;
scope.filteredModelMax = scope.modelMax;
/**
* FALL BACK TO DEFAULTS FOR SOME ATTRIBUTES
*/
attrs.$observe('disabled', function(val) {
if (!angular.isDefined(val)) {
scope.disabled = defaults.disabled;
}
scope.$watch('disabled', setDisabledStatus);
});
attrs.$observe('orientation', function(val) {
if (!angular.isDefined(val)) {
scope.orientation = defaults.orientation;
}
var classNames = scope.orientation.split(' '),
useClass;
for (var i = 0, l = classNames.length; i < l; i++) {
classNames[i] = 'ngrs-' + classNames[i];
}
useClass = classNames.join(' ');
// add class to element
$slider.addClass(useClass);
// update pos
if (scope.orientation === 'vertical' || scope.orientation === 'vertical left' || scope.orientation === 'vertical right') {
pos = 'top';
posOpp = 'bottom';
orientation = 1;
}
});
attrs.$observe('step', function(val) {
if (!angular.isDefined(val)) {
scope.step = defaults.step;
}
});
attrs.$observe('decimalPlaces', function(val) {
if (!angular.isDefined(val)) {
scope.decimalPlaces = defaults.decimalPlaces;
}
});
attrs.$observe('showValues', function(val) {
if (!angular.isDefined(val)) {
scope.showValues = defaults.showValues;
} else {
if (val === 'false') {
scope.showValues = false;
} else {
scope.showValues = true;
}
}
});
attrs.$observe('pinHandle', function(val) {
if (!angular.isDefined(val)) {
scope.pinHandle = null;
} else {
if (val === 'min' || val === 'max') {
scope.pinHandle = val;
} else {
scope.pinHandle = null;
}
}
scope.$watch('pinHandle', setPinHandle);
});
attrs.$observe('preventEqualMinMax', function(val) {
if (!angular.isDefined(val)) {
scope.preventEqualMinMax = defaults.preventEqualMinMax;
} else {
if (val === 'false') {
scope.preventEqualMinMax = false;
} else {
scope.preventEqualMinMax = true;
}
}
});
attrs.$observe('attachHandleValues', function(val) {
if (!angular.isDefined(val)) {
scope.attachHandleValues = defaults.attachHandleValues;
} else {
if (val === 'true' || val === '') {
// flag as true
scope.attachHandleValues = true;
// add class to runner
element.find('.ngrs-value-runner').addClass('ngrs-attached-handles');
} else {
scope.attachHandleValues = false;
}
}
});
// listen for changes to values
scope.$watch('min', setMinMax);
scope.$watch('max', setMinMax);
scope.$watch('modelMin', setModelMinMax);
scope.$watch('modelMax', setModelMinMax);
/**
* HANDLE CHANGES
*/
function setPinHandle(status) {
if (status === "min") {
angular.element(handles[0]).css('display', 'none');
angular.element(handles[1]).css('display', 'block');
} else if (status === "max") {
angular.element(handles[0]).css('display', 'block');
angular.element(handles[1]).css('display', 'none');
} else {
angular.element(handles[0]).css('display', 'block');
angular.element(handles[1]).css('display', 'block');
}
}
function setDisabledStatus(status) {
if (status) {
$slider.addClass('ngrs-disabled');
} else {
$slider.removeClass('ngrs-disabled');
}
}
function setMinMax() {
if (scope.min > scope.max) {
throwError('min must be less than or equal to max');
}
// only do stuff when both values are ready
if (angular.isDefined(scope.min) && angular.isDefined(scope.max)) {
// make sure they are numbers
if (!isNumber(scope.min)) {
throwError('min must be a number');
}
if (!isNumber(scope.max)) {
throwError('max must be a number');
}
range = scope.max - scope.min;
allowedRange = [scope.min, scope.max];
// update models too
setModelMinMax();
}
}
function setModelMinMax() {
if (scope.modelMin > scope.modelMax) {
throwWarning('modelMin must be less than or equal to modelMax');
// reset values to correct
scope.modelMin = scope.modelMax;
}
// only do stuff when both values are ready
if (
(angular.isDefined(scope.modelMin) || scope.pinHandle === 'min') &&
(angular.isDefined(scope.modelMax) || scope.pinHandle === 'max')
) {
// make sure they are numbers
if (!isNumber(scope.modelMin)) {
if (scope.pinHandle !== 'min') {
throwWarning('modelMin must be a number');
}
scope.modelMin = scope.min;
}
if (!isNumber(scope.modelMax)) {
if (scope.pinHandle !== 'max') {
throwWarning('modelMax must be a number');
}
scope.modelMax = scope.max;
}
var handle1pos = restrict(((scope.modelMin - scope.min) / range) * 100),
handle2pos = restrict(((scope.modelMax - scope.min) / range) * 100),
value1pos,
value2pos;
if (scope.attachHandleValues) {
value1pos = handle1pos;
value2pos = handle2pos;
}
// make sure the model values are within the allowed range
scope.modelMin = Math.max(scope.min, scope.modelMin);
scope.modelMax = Math.min(scope.max, scope.modelMax);
if (scope.filter && scope.filterOptions) {
scope.filteredModelMin = $filter(scope.filter)(scope.modelMin, scope.filterOptions);
scope.filteredModelMax = $filter(scope.filter)(scope.modelMax, scope.filterOptions);
} else if (scope.filter) {
var filterTokens = scope.filter.split(':'),
filterName = scope.filter.split(':')[0],
filterOptions = filterTokens.slice().slice(1),
modelMinOptions,
modelMaxOptions;
// properly parse string and number args
filterOptions = filterOptions.map(function (arg) {
if (isNumber(arg)) {
return +arg;
} else if ((arg[0] == "\"" && arg[arg.length-1] == "\"") || (arg[0] == "\'" && arg[arg.length-1] == "\'")) {
return arg.slice(1, -1);
}
});
modelMinOptions = filterOptions.slice();
modelMaxOptions = filterOptions.slice();
modelMinOptions.unshift(scope.modelMin);
modelMaxOptions.unshift(scope.modelMax);
scope.filteredModelMin = $filter(filterName).apply(null, modelMinOptions);
scope.filteredModelMax = $filter(filterName).apply(null, modelMaxOptions);
} else {
scope.filteredModelMin = scope.modelMin;
scope.filteredModelMax = scope.modelMax;
}
// check for no range
if (scope.min === scope.max && scope.modelMin == scope.modelMax) {
// reposition handles
angular.element(handles[0]).css(pos, '0%');
angular.element(handles[1]).css(pos, '100%');
if (scope.attachHandleValues) {
// reposition values
angular.element(values[0]).css(pos, '0%');
angular.element(values[1]).css(pos, '100%');
}
// reposition join
angular.element(join).css(pos, '0%').css(posOpp, '0%');
} else {
// reposition handles
angular.element(handles[0]).css(pos, handle1pos + '%');
angular.element(handles[1]).css(pos, handle2pos + '%');
if (scope.attachHandleValues) {
// reposition values
angular.element(values[0]).css(pos, value1pos + '%');
angular.element(values[1]).css(pos, value2pos + '%');
angular.element(values[1]).css(posOpp, 'auto');
}
// reposition join
angular.element(join).css(pos, handle1pos + '%').css(posOpp, (100 - handle2pos) + '%');
// ensure min handle can't be hidden behind max handle
if (handle1pos > 95) {
angular.element(handles[0]).css('z-index', 3);
}
}
}
}
function handleMove(index) {
var $handle = handles[index];
// on mousedown / touchstart
$handle.bind(onEvent + 'X', function(event) {
var handleDownClass = (index === 0 ? 'ngrs-handle-min' : 'ngrs-handle-max') + '-down',
//unbind = $handle.add($document).add('body'),
modelValue = (index === 0 ? scope.modelMin : scope.modelMax) - scope.min,
originalPosition = (modelValue / range) * 100,
originalClick = client(event),
previousClick = originalClick,
previousProposal = false;
if (angular.isFunction(scope.onHandleDown)) {
scope.onHandleDown();
}
// stop user accidentally selecting stuff
angular.element('body').bind('selectstart' + eventNamespace, function() {
return false;
});
// only do stuff if we are disabled
if (!scope.disabled) {
// flag as down
down = true;
// add down class
$handle.addClass('ngrs-down');
$slider.addClass('ngrs-focus ' + handleDownClass);
// add touch class for MS styling
angular.element('body').addClass('ngrs-touching');
// listen for mousemove / touchmove document events
$document.bind(moveEvent, function(e) {
// prevent default
e.preventDefault();
var currentClick = client(e),
movement,
proposal,
other,
per = (scope.step / range) * 100,
otherModelPosition = (((index === 0 ? scope.modelMax : scope.modelMin) - scope.min) / range) * 100;
if (currentClick[0] === "x") {
return;
}
// calculate deltas
currentClick[0] -= originalClick[0];
currentClick[1] -= originalClick[1];
// has movement occurred on either axis?
movement = [
(previousClick[0] !== currentClick[0]), (previousClick[1] !== currentClick[1])
];
// propose a movement
proposal = originalPosition + ((currentClick[orientation] * 100) / (orientation ? $slider.height() : $slider.width()));
// normalize so it can't move out of bounds
proposal = restrict(proposal);
if (scope.preventEqualMinMax) {
if (per === 0) {
per = (1 / range) * 100; // restrict to 1
}
if (index === 0) {
otherModelPosition = otherModelPosition - per;
} else if (index === 1) {
otherModelPosition = otherModelPosition + per;
}
}
// check which handle is being moved and add / remove margin
if (index === 0) {
proposal = proposal > otherModelPosition ? otherModelPosition : proposal;
} else if (index === 1) {
proposal = proposal < otherModelPosition ? otherModelPosition : proposal;
}
if (scope.step > 0) {
// only change if we are within the extremes, otherwise we get strange rounding
if (proposal < 100 && proposal > 0) {
proposal = Math.round(proposal / per) * per;
}
}
if (proposal > 95 && index === 0) {
$handle.css('z-index', 3);
} else {
$handle.css('z-index', '');
}
if (movement[orientation] && proposal != previousProposal) {
if (index === 0) {
// update model as we slide
scope.modelMin = parseFloat(parseFloat((((proposal * range) / 100) + scope.min)).toFixed(scope.decimalPlaces));
} else if (index === 1) {
scope.modelMax = parseFloat(parseFloat((((proposal * range) / 100) + scope.min)).toFixed(scope.decimalPlaces));
}
// update angular
scope.$apply();
previousProposal = proposal;
}
previousClick = currentClick;
}).bind(offEvent, function() {
if (angular.isFunction(scope.onHandleUp)) {
scope.onHandleUp();
}
// unbind listeners
$document.off(moveEvent);
$document.off(offEvent);
angular.element('body').removeClass('ngrs-touching');
// cancel down flag
down = false;
// remove down and over class
$handle.removeClass('ngrs-down');
$handle.removeClass('ngrs-over');
// remove active class
$slider.removeClass('ngrs-focus ' + handleDownClass);
});
}
}).on(overEvent, function () {
$handle.addClass('ngrs-over');
}).on(outEvent, function () {
if (!down) {
$handle.removeClass('ngrs-over');
}
});
}
function throwError(message) {
scope.disabled = true;
throw new Error('RangeSlider: ' + message);
}
function throwWarning(message) {
$log.warn(message);
}
/**
* DESTROY
*/
scope.$on('$destroy', function() {
// unbind event from slider
$slider.off(eventNamespace);
// unbind from body
angular.element('body').off(eventNamespace);
// unbind from document
$document.off(eventNamespace);
// unbind from handles
for (var i = 0, l = handles.length; i < l; i++) {
handles[i].off(eventNamespace);
handles[i].off(eventNamespace + 'X');
}
});
/**
* INIT
*/
$slider
// disable selection
.bind('selectstart' + eventNamespace, function(event) {
return false;
})
// stop propagation
.bind('click', function(event) {
event.stopPropagation();
});
// bind events to each handle
handleMove(0);
handleMove(1);
}
};
}]);
// requestAnimationFramePolyFill
// http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
// shim layer with setTimeout fallback
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
}());
###Demo of Rangeslider (c/o Daniel Crisp)
Having some issues with the non-display of value labels at either end of slider.
Seems to work fine in plunkr preview but absent in the embedded view.
Similarly doesn't show in my own app which is very close to this though does use UI-Router - can't see that as an issue.