Skip to content

Instantly share code, notes, and snippets.

@just-boris
Created May 14, 2015 10:29
Show Gist options
  • Select an option

  • Save just-boris/c03e8a51ec74c614d60d to your computer and use it in GitHub Desktop.

Select an option

Save just-boris/c03e8a51ec74c614d60d to your computer and use it in GitHub Desktop.
ionSlider.directive
angular.module('directive.ionSlider', []).directive('ionSlider', ['$document', function($document) {
"use strict";
return {
restrict: 'EA',
require: 'ngModel',
template: '<input type="text" />',
link: function(scope, element, attrs, ngModel) {
var input = element.find('input'),
isDouble = attrs.type === 'double';
input.ionRangeSlider({
grid: true,
hide_min_max: true,
values: scope.$eval(attrs.values),
min_prefix: attrs.minPrefix,
max_prefix: attrs.maxPrefix,
min_postfix: attrs.minPostfix,
max_postfix: attrs.maxPostfix,
min: +attrs.min,
max: +attrs.max,
type: attrs.type
});
var slider = input.data("ionRangeSlider");
ngModel.$render = function() {
var options;
if(isDouble) {
options = ngModel.$viewValue || {};
} else {
options = {
from: ngModel.$viewValue
};
}
slider.update(options);
};
input.on('change', function() {
ngModel.$setViewValue(isDouble ? {from: input.data("from"), to: input.data("to")} : input.data("from"));
});
element.on('mousedown', function() {
if(!scope.$eval(attrs.ngDisabled)) {
$document.one('mouseup', function() {
scope.$emit('sliderSlideFinished');
});
}
});
scope.$watch(attrs.ngDisabled, function(disabled) {
slider.update({
disable: disabled
});
});
scope.$on('$destroy', function() {
slider.destroy();
});
}
};
}]);
@lukeclifton
Copy link

lukeclifton commented Jun 9, 2016

Rather than using element.on('mousedown' as on line 38, its probably best to use the built in callback functions:

 input.ionRangeSlider({
   grid:           scope.settings.grid,
   hide_min_max:   true,
   values:         scope.settings.values,
   prefix:         scope.settings.prefix,
   postfix:        scope.settings.postfix,
   from:           scope.settings.from,
   to:             scope.settings.to,
   min:            scope.settings.min,
   max:            scope.settings.max,
   step:           scope.settings.step,
   grid_num:       scope.settings.gridnum,
   type:           scope.settings.sliderType,
   force_edges:    true,
   onChange: function() {
       ngModel.$setViewValue(isDouble ? {from: input.data("from"), to: input.data("to")} : input.data("from"));
   },
   onFinish: function() {
      scope.$emit('selectorControlChanged');
   }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment