|
1 | 1 | /*! angularjs-slider - v7.0.1 -
|
2 | 2 | (c) Rafal Zajac <[email protected]>, Valentin Hervieu <[email protected]>, Jussi Saarivirta <[email protected]>, Angelin Sirbu <[email protected]> -
|
3 | 3 | https://github.com/angular-slider/angularjs-slider -
|
4 |
| - 2021-09-07 */ |
| 4 | + 2022-05-26 */ |
5 | 5 | /*jslint unparam: true */
|
6 | 6 | /*global angular: false, console: false, define, module */
|
7 | 7 | ;(function(root, factory) {
|
|
37 | 37 | minRange: null,
|
38 | 38 | maxRange: null,
|
39 | 39 | restrictedRange: null,
|
| 40 | + skipRestrictedRangesWithArrowKeys: null, |
40 | 41 | pushRange: false,
|
41 | 42 | minLimit: null,
|
42 | 43 | maxLimit: null,
|
|
520 | 521 | },
|
521 | 522 |
|
522 | 523 | /*
|
523 |
| - * Reflow the slider when the low handle changes (called with throttle) |
524 |
| - */ |
| 524 | + * Reflow the slider when the low handle changes (called with throttle) |
| 525 | + */ |
525 | 526 | onLowHandleChange: function() {
|
526 | 527 | this.syncLowValue()
|
527 | 528 | if (this.range) this.syncHighValue()
|
|
536 | 537 | },
|
537 | 538 |
|
538 | 539 | /*
|
539 |
| - * Reflow the slider when the high handle changes (called with throttle) |
540 |
| - */ |
| 540 | + * Reflow the slider when the high handle changes (called with throttle) |
| 541 | + */ |
541 | 542 | onHighHandleChange: function() {
|
542 | 543 | this.syncLowValue()
|
543 | 544 | this.syncHighValue()
|
|
665 | 666 | }
|
666 | 667 | },
|
667 | 668 |
|
| 669 | + /** |
| 670 | + * Check if the restrictedRange option using multiple or not |
| 671 | + * |
| 672 | + * Run only once during initialization and only in case 4 |
| 673 | + * |
| 674 | + * @returns {undefined} |
| 675 | + */ |
| 676 | + |
| 677 | + ensureRestrictedBarIsArray: function(elem) { |
| 678 | + var jElem = angular.element(elem) |
| 679 | + this.restrictedBar = [] |
| 680 | + if (this.options.restrictedRange) { |
| 681 | + // this.options.restrictedRange converting to an array even if it's not entered as array. |
| 682 | + this.options.restrictedRange = !Array.isArray( |
| 683 | + this.options.restrictedRange |
| 684 | + ) |
| 685 | + ? [this.options.restrictedRange] |
| 686 | + : this.options.restrictedRange |
| 687 | + this.restrictedBar[0] = jElem |
| 688 | + var mainDiv = elem.parentElement |
| 689 | + for (var i = 1; i < this.options.restrictedRange.length; i++) { |
| 690 | + var sp = document.createElement('span') |
| 691 | + sp.setAttribute('class', 'rz-bar-wrapper') |
| 692 | + sp.innerHTML = |
| 693 | + '<span class="rz-bar rz-restricted" ng-style="restrictionStyle"></span>' |
| 694 | + mainDiv.appendChild(sp) |
| 695 | + this.restrictedBar[i] = angular.element(sp) |
| 696 | + } |
| 697 | + } else { |
| 698 | + elem.style.visibility = 'hidden' |
| 699 | + this.restrictedBar = null |
| 700 | + } |
| 701 | + }, |
| 702 | + |
668 | 703 | /**
|
669 | 704 | * Set the slider children to variables for easy access
|
670 | 705 | *
|
|
693 | 728 | this.selBar = jElem
|
694 | 729 | break
|
695 | 730 | case 4:
|
696 |
| - this.restrictedBar = jElem |
| 731 | + this.ensureRestrictedBarIsArray(elem) |
697 | 732 | break
|
698 | 733 | case 5:
|
699 | 734 | this.minH = jElem
|
|
773 | 808 | this.leftOutSelBar,
|
774 | 809 | !this.range || !this.options.showOuterSelectionBars
|
775 | 810 | )
|
776 |
| - this.alwaysHide(this.restrictedBar, !this.options.restrictedRange) |
| 811 | + |
| 812 | + // this.restrictedBar is everytime an array |
| 813 | + for (var r in this.restrictedBar) { |
| 814 | + if (this.restrictedBar[r]) |
| 815 | + this.alwaysHide( |
| 816 | + this.restrictedBar[r], |
| 817 | + !this.options.restrictedRange[r] |
| 818 | + ) |
| 819 | + } |
| 820 | + |
777 | 821 | this.alwaysHide(
|
778 | 822 | this.rightOutSelBar,
|
779 | 823 | !this.range || !this.options.showOuterSelectionBars
|
|
1358 | 1402 | var position = 0,
|
1359 | 1403 | dimension = 0
|
1360 | 1404 | if (this.options.restrictedRange) {
|
1361 |
| - var from = this.valueToPosition(this.options.restrictedRange.from), |
1362 |
| - to = this.valueToPosition(this.options.restrictedRange.to) |
1363 |
| - dimension = Math.abs(to - from) |
1364 |
| - position = this.options.rightToLeft |
1365 |
| - ? to + this.handleHalfDim |
1366 |
| - : from + this.handleHalfDim |
1367 |
| - this.setDimension(this.restrictedBar, dimension) |
1368 |
| - this.setPosition(this.restrictedBar, position) |
| 1405 | + this.options.restrictedRange = !Array.isArray( |
| 1406 | + this.options.restrictedRange |
| 1407 | + ) |
| 1408 | + ? [this.options.restrictedRange] |
| 1409 | + : this.options.restrictedRange |
| 1410 | + for (var i in this.options.restrictedRange) { |
| 1411 | + var from = this.valueToPosition( |
| 1412 | + this.options.restrictedRange[i].from |
| 1413 | + ), |
| 1414 | + to = this.valueToPosition(this.options.restrictedRange[i].to) |
| 1415 | + dimension = Math.abs(to - from) |
| 1416 | + position = this.options.rightToLeft |
| 1417 | + ? to + this.handleHalfDim |
| 1418 | + : from + this.handleHalfDim |
| 1419 | + this.setDimension(this.restrictedBar[i], dimension) |
| 1420 | + this.setPosition(this.restrictedBar[i], position) |
| 1421 | + } |
1369 | 1422 | }
|
1370 | 1423 | },
|
1371 | 1424 |
|
|
1447 | 1500 | ? 'bottom'
|
1448 | 1501 | : 'top'
|
1449 | 1502 | : reversed
|
1450 |
| - ? 'left' |
1451 |
| - : 'right' |
| 1503 | + ? 'left' |
| 1504 | + : 'right' |
1452 | 1505 | this.scope.barStyle = {
|
1453 | 1506 | backgroundImage:
|
1454 | 1507 | 'linear-gradient(to ' +
|
|
2183 | 2236 | }
|
2184 | 2237 | },
|
2185 | 2238 |
|
| 2239 | + /** |
| 2240 | + * Skip restricted range function when arrow keys use |
| 2241 | + * |
| 2242 | + * @param {number} currentValue value of the slider |
| 2243 | + * @param {number} key arrow key used |
| 2244 | + * |
| 2245 | + * @returns {number} currentValue value of the slider |
| 2246 | + */ |
| 2247 | + |
| 2248 | + skipRestrictedRanges: function(key, currentValue) { |
| 2249 | + if ( |
| 2250 | + this.options.restrictedRange && |
| 2251 | + Array.isArray(this.options.restrictedRange) |
| 2252 | + ) { |
| 2253 | + for (var i in this.options.restrictedRange) { |
| 2254 | + var range = this.options.restrictedRange[i] |
| 2255 | + // if it first or last value |
| 2256 | + if ( |
| 2257 | + (range.from === 0 && |
| 2258 | + currentValue === 0 && |
| 2259 | + [37, 40].includes(key)) || // LEFT or DOWN |
| 2260 | + (range.to >= |
| 2261 | + this.options.restrictedRange[ |
| 2262 | + this.options.restrictedRange.length - 1 |
| 2263 | + ].to && |
| 2264 | + currentValue >= |
| 2265 | + this.options.restrictedRange[ |
| 2266 | + this.options.restrictedRange.length - 1 |
| 2267 | + ].to && |
| 2268 | + [38, 39].includes(key)) // UP or RIGHT |
| 2269 | + ) { |
| 2270 | + return currentValue |
| 2271 | + } |
| 2272 | + if (range.to > currentValue && currentValue > range.from) { |
| 2273 | + if ( |
| 2274 | + Math.abs(range.to - currentValue) > |
| 2275 | + Math.abs(range.from - currentValue) |
| 2276 | + ) { |
| 2277 | + currentValue = range.to |
| 2278 | + } else { |
| 2279 | + currentValue = range.from |
| 2280 | + } |
| 2281 | + } |
| 2282 | + } |
| 2283 | + } |
| 2284 | + |
| 2285 | + return currentValue |
| 2286 | + }, |
| 2287 | + |
2186 | 2288 | /**
|
2187 | 2289 | * Key actions helper function
|
2188 | 2290 | *
|
|
2228 | 2330 | },
|
2229 | 2331 |
|
2230 | 2332 | onKeyboardEvent: function(event) {
|
2231 |
| - var currentValue = this[this.tracking], |
2232 |
| - keyCode = event.keyCode || event.which, |
2233 |
| - keys = { |
| 2333 | + var keyCode = event.keyCode || event.which |
| 2334 | + var currentValue = this[this.tracking] |
| 2335 | + var keys = { |
2234 | 2336 | 38: 'UP',
|
2235 | 2337 | 40: 'DOWN',
|
2236 | 2338 | 37: 'LEFT',
|
|
2254 | 2356 | var self = this
|
2255 | 2357 | $timeout(function() {
|
2256 | 2358 | var newValue = self.roundStep(self.sanitizeValue(action))
|
| 2359 | + newValue = self.options.skipRestrictedRangesWithArrowKeys |
| 2360 | + ? self.skipRestrictedRanges(keyCode, newValue) |
| 2361 | + : newValue |
2257 | 2362 | if (!self.options.draggableRangeOnly) {
|
2258 | 2363 | self.positionTrackingHandle(newValue)
|
2259 | 2364 | } else {
|
|
2548 | 2653 | },
|
2549 | 2654 |
|
2550 | 2655 | applyRestrictedRange: function(newValue) {
|
2551 |
| - if ( |
2552 |
| - this.options.restrictedRange != null && |
2553 |
| - newValue > this.options.restrictedRange.from && |
2554 |
| - newValue < this.options.restrictedRange.to |
2555 |
| - ) { |
2556 |
| - var halfWidth = |
2557 |
| - (this.options.restrictedRange.to - |
2558 |
| - this.options.restrictedRange.from) / |
2559 |
| - 2 |
2560 |
| - if (this.tracking === 'lowValue') { |
2561 |
| - return newValue > this.options.restrictedRange.from + halfWidth |
2562 |
| - ? this.options.restrictedRange.to |
2563 |
| - : this.options.restrictedRange.from |
2564 |
| - } |
2565 |
| - if (this.tracking === 'highValue') { |
2566 |
| - return newValue < this.options.restrictedRange.to - halfWidth |
2567 |
| - ? this.options.restrictedRange.from |
2568 |
| - : this.options.restrictedRange.to |
| 2656 | + for (var i in this.options.restrictedRange) { |
| 2657 | + if ( |
| 2658 | + this.options.restrictedRange[i] != null && |
| 2659 | + newValue > this.options.restrictedRange[i].from && |
| 2660 | + newValue < this.options.restrictedRange[i].to |
| 2661 | + ) { |
| 2662 | + var halfWidth = |
| 2663 | + (this.options.restrictedRange[i].to - |
| 2664 | + this.options.restrictedRange[i].from) / |
| 2665 | + 2 |
| 2666 | + if (this.tracking === 'lowValue') { |
| 2667 | + return newValue > |
| 2668 | + this.options.restrictedRange[i].from + halfWidth |
| 2669 | + ? this.options.restrictedRange[i].to |
| 2670 | + : this.options.restrictedRange[i].from |
| 2671 | + } |
| 2672 | + if (this.tracking === 'highValue') { |
| 2673 | + return newValue < this.options.restrictedRange[i].to - halfWidth |
| 2674 | + ? this.options.restrictedRange[i].from |
| 2675 | + : this.options.restrictedRange[i].to |
| 2676 | + } |
2569 | 2677 | }
|
2570 | 2678 | }
|
| 2679 | + |
2571 | 2680 | return newValue
|
2572 | 2681 | },
|
2573 | 2682 |
|
|
0 commit comments