Skip to content
This repository was archived by the owner on May 25, 2019. It is now read-only.

Commit 4305239

Browse files
committed
Merge pull request #77 from jbarrus/master
added option to set upper and lower bounds that are higher/lower than the min/max
2 parents cd0539c + c048993 commit 4305239

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

demo/css/slider.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,14 @@
44
width: 2px;
55
height: 8px;
66
top: 16px;
7+
}
8+
9+
.ui-slider {
10+
position: relative;
11+
}
12+
13+
.ui-slider-out-of-bounds {
14+
position: absolute;
15+
background-color: #EEE;
16+
height: 100%;
717
}

demo/demo.html

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ <h1>AngularUI Slider demo</h1>
195195
<div ui-slider data-min="0" data-max="20" data-step="5" data-tick ng-model="demoVals.sliderExample13"></div>
196196
</div>
197197
</li>
198+
<li>
199+
<div class="sliderExample"><a name="ex14"></a>
200+
<strong>Slider with lower and upper bounds.</strong>
201+
<div ui-slider min="0" max="100" step="10" upper-bound="90" ng-model="demoVals.sliderExample14a">
202+
<div class="ui-slider-out-of-bounds" style="right:0; width: 10%"></div>
203+
</div>
204+
<input type="text" ng-model="demoVals.sliderExample14a" />
205+
<div ui-slider min="0" max="100" step="10" lower-bound="30" upper-bound="60" ng-model="demoVals.sliderExample14b">
206+
<div class="ui-slider-out-of-bounds" style="left:0; width: 30%"></div>
207+
<div class="ui-slider-out-of-bounds" style="right:0; width: 40%"></div>
208+
</div>
209+
<input type="text" ng-model="demoVals.sliderExample14b" />
210+
</div>
211+
</li>
212+
<li>
213+
<div class="sliderExample"><a name="ex15"></a>
214+
<strong>Slider - range values with lower and uppwer bounds</strong>
215+
<div ui-slider="{range: true}" min="0" max="100" step="0.01" lower-bound="20" upper-bound="90" use-decimals ng-model="demoVals.sliderExample15">
216+
<div class="ui-slider-out-of-bounds" style="left:0; width: 20%"></div>
217+
<div class="ui-slider-out-of-bounds" style="right:0; width: 10%"></div>
218+
</div>
219+
<input type="text" ng-model="demoVals.sliderExample15" />
220+
</div>
221+
</li>
198222
</ol>
199223
<script type="text/javascript" src="bower_components/jquery/dist/jquery.min.js"></script>
200224
<script type="text/javascript" src="bower_components/jquery-ui/ui/minified/jquery-ui.min.js"></script>
@@ -243,15 +267,18 @@ <h1>AngularUI Slider demo</h1>
243267
start: function (event, ui) { $log.info('Event: Slider start - set with slider options', event); },
244268
stop: function (event, ui) { $log.info('Event: Slider stop - set with slider options', event); }
245269
}
246-
}
270+
};
247271

248272
$scope.demoVals = {
249273
sliderExample3: 14,
250274
sliderExample4: 14,
251275
sliderExample5: 50,
252276
sliderExample8: 0.34,
253277
sliderExample9: [-0.52, 0.54],
254-
sliderExample10: -0.37
278+
sliderExample10: -0.37,
279+
sliderExample14a: 50,
280+
sliderExample14b: 50,
281+
sliderExample15: [30, 60]
255282
};
256283

257284
$scope.colorpicker = {

src/slider.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ angular.module('ui.slider', []).value('uiSliderConfig',{}).directive('uiSlider',
2020
};
2121

2222
// convenience properties
23-
var properties = ['min', 'max', 'step'];
23+
var properties = ['min', 'max', 'step', 'lowerBound', 'upperBound'];
2424
var useDecimals = (!angular.isUndefined(attrs.useDecimals)) ? true : false;
2525

2626
var init = function() {
@@ -75,8 +75,49 @@ angular.module('ui.slider', []).value('uiSliderConfig',{}).directive('uiSlider',
7575

7676
// Update model value from slider
7777
elm.bind('slide', function(event, ui) {
78+
var valuesChanged;
79+
80+
if (ui.values) {
81+
var boundedValues = ui.values.slice();
82+
83+
if (options.lowerBound && boundedValues[0] < options.lowerBound) {
84+
boundedValues[0] = Math.max(boundedValues[0], options.lowerBound);
85+
}
86+
if (options.upperBound && boundedValues[1] > options.upperBound) {
87+
boundedValues[1] = Math.min(boundedValues[1], options.upperBound);
88+
}
89+
90+
if (boundedValues[0] !== ui.values[0] || boundedValues[1] !== ui.values[1]) {
91+
valuesChanged = true;
92+
ui.values = boundedValues;
93+
}
94+
} else {
95+
var boundedValue = ui.value;
96+
97+
if (options.lowerBound && boundedValue < options.lowerBound) {
98+
boundedValue = Math.max(boundedValue, options.lowerBound);
99+
}
100+
if (options.upperBound && boundedValue > options.upperBound) {
101+
boundedValue = Math.min(boundedValue, options.upperBound);
102+
}
103+
104+
if (boundedValue !== ui.value) {
105+
valuesChanged = true;
106+
ui.value = boundedValue;
107+
}
108+
}
109+
110+
78111
ngModel.$setViewValue(ui.values || ui.value);
79112
scope.$apply();
113+
114+
if (valuesChanged) {
115+
setTimeout(function() {
116+
elm.slider('value', ui.values || ui.values);
117+
}, 0);
118+
119+
return false;
120+
}
80121
});
81122

82123
// Update slider from model value

0 commit comments

Comments
 (0)