Skip to content
This repository has been archived by the owner on Oct 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #105 from JulianWielga/patch-3
Browse files Browse the repository at this point in the history
better formatter
  • Loading branch information
seiyria committed Nov 12, 2015
2 parents 96f14c3 + b6d0239 commit 988a418
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"tests"
],
"dependencies": {
"seiyria-bootstrap-slider": "~5.2.6",
"angular": "~1.3.15||~1.4.7"
"seiyria-bootstrap-slider": ">= 5.2 < 6.0",
"angular": ">= 1.3 < 1.5"
}
}
46 changes: 26 additions & 20 deletions slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ angular.module('ui.bootstrap-slider', [])
link: function ($scope, element, attrs, ngModelCtrl, $compile) {
var ngModelDeregisterFn, ngDisabledDeregisterFn;

initSlider();
var slider = initSlider();

function initSlider() {
var options = {};
Expand Down Expand Up @@ -107,7 +107,11 @@ angular.module('ui.bootstrap-slider', [])
setFloatOption('value', $scope.value, 5);
}

if ($scope.formatter) options.formatter = $scope.$eval($scope.formatter);
if (attrs.formatter) {
options.formatter = function(value) {
return $scope.formatter({value: value});
}
}


// check if slider jQuery plugin exists
Expand Down Expand Up @@ -140,19 +144,18 @@ angular.module('ui.bootstrap-slider', [])
}
angular.forEach(updateEvent, function (sliderEvent) {
slider.on(sliderEvent, function (ev) {
ngModelCtrl.$setViewValue(ev);
$timeout(function () {
$scope.$apply();
ngModelCtrl.$setViewValue(ev);
});
});
});
slider.on('change', function (ev) {
ngModelCtrl.$setViewValue(ev.newValue);
$timeout(function () {
$scope.$apply();
ngModelCtrl.$setViewValue(ev.newValue);
});
});


// Event listeners
var sliderEvents = {
slideStart: 'onStartSlide',
Expand All @@ -164,15 +167,9 @@ angular.module('ui.bootstrap-slider', [])
slider.on(sliderEvent, function (ev) {
if ($scope[sliderEventAttr]) {

var callback = function () {
$timeout(function () {
fn($scope.$parent, { $event: ev, value: ev });
}

if ($rootScope.$$phase) {
$scope.$evalAsync(callback);
} else {
$scope.$apply(callback);
}
});
}
});
});
Expand All @@ -195,21 +192,30 @@ angular.module('ui.bootstrap-slider', [])
// deregister ngModel watcher to prevent memory leaks
if (angular.isFunction(ngModelDeregisterFn)) ngModelDeregisterFn();
ngModelDeregisterFn = $scope.$watch('ngModel', function (value) {
if($scope.range){
slider.setValue(value);
}else{
slider.setValue(parseFloat(value));
}
$timeout(function() {
if($scope.range){
slider.setValue(value);
}else{
slider.setValue(parseFloat(value));
}
slider.relayout();
});
}, true);

return slider;
}


var watchers = ['min', 'max', 'step', 'range', 'scale'];
angular.forEach(watchers, function (prop) {
$scope.$watch(prop, function () {
initSlider();
slider = initSlider();
});
});

$scope.$on('slider:relayout', function() {
slider.relayout();
});
}
};
}])
Expand Down
11 changes: 6 additions & 5 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<input id="sliderId" type="text" ng-model="testOptions.sliderId">

<label for="modelValue">Slider Value in ng-model:</label>
<input id="modelValue" type="range" ng-model="model.first" min="testOptions.min" step="testOptions.step"
max="testOptions.max">
<input type="number" ng-model="model.first" class="form-control">
<input id="modelValue" type="range" ng-model="model.first" min="{{testOptions.min}}" step="{{testOptions.step}}"
max="{{testOptions.max}}">
<input ng-model="model.first" class="form-control">

<label for="value">Value:</label>
<input id="value" type="number" ng-model="value.first" class="form-control">
Expand Down Expand Up @@ -71,7 +71,7 @@
max="testOptions.max"
step="testOptions.step"
range="testOptions.range"
precision="testOptions.precision"
precision="{{ testOptions.precision }}"
reversed="{{ testOptions.reversed }}"
orientation="{{ testOptions.orientation }}"
handle="{{ testOptions.handle }}"
Expand Down Expand Up @@ -122,14 +122,15 @@

<label for="formatter_sufffix">Formatter suffix:</label>
<input id="formatter_sufffix" type="text" ng-model="suffix" class="form-control">
<button ng-click="$broadcast('slider:relayout')">Relayout</button>
</p>

Slider with configurable tooltip<br>
<slider slider-id="tooltipSlider"
ng-model="model.third"
precision="{{ testOptions.precision }}"
tooltip="{{ testOptions.tooltip }}"
formatter="formatterFn"
formatter="formatterFn(value)"
>
</slider>
Model: {{model.third}}<br>
Expand Down

0 comments on commit 988a418

Please sign in to comment.