Created
May 14, 2015 10:29
-
-
Save just-boris/c03e8a51ec74c614d60d to your computer and use it in GitHub Desktop.
ionSlider.directive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| }); | |
| } | |
| }; | |
| }]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rather than using
element.on('mousedown'as on line 38, its probably best to use the built in callback functions: