forked from zpv/paper-range-slider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper-range-slider.html
1690 lines (1423 loc) · 59.3 KB
/
paper-range-slider.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--
@license
Copyright (C) 2016, Iftach Sadeh <[email protected]>
The MIT License (MIT)
See the file LICENSE.txt for further details.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../iron-form-element-behavior/iron-form-element-behavior.html">
<link rel="import" href="../iron-range-behavior/iron-range-behavior.html">
<link rel="import" href="../paper-behaviors/paper-inky-focus-behavior.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../paper-progress/paper-progress.html">
<link rel="import" href="../paper-styles/color.html">
<!--
# paper-range-slider
`paper-range-slider` allows user to select a range of values within a given
(possibly wider) range, by moving the position of two knobs, or by moving the
position of the distance spanned between the two knobs.
## Examples:
Basic use:
```html
<paper-range-slider></paper-range-slider>
```
See README.md for further details.
@element paper-range-slider
@demo demo/index.html
-->
<dom-module id="paper-range-slider">
<template>
<style>
/* local styles go here */
:host {
--paper-range-slider-width: 200px;
/*width: var(--paper-range-slider-width);*/
/*display: inline-block;*/
@apply --layout;
@apply --layout-justified;
@apply --layout-center;
--paper-range-slider-lower-color: var(--paper-grey-400);
--paper-range-slider-active-color: var(--primary-color);
--paper-range-slider-higher-color: var(--paper-grey-400);
--paper-range-slider-knob-color: var(--primary-color);
--paper-range-slider-pin-color: var(--primary-color);
--paper-range-slider-pin-start-color: var(--paper-grey-400);
--paper-range-slider-knob-start-color: transparent;
--paper-range-slider-knob-start-border-color: var(--paper-grey-400);
}
#sliderOuterDiv_0 {
display: inline-block;
width: var(--paper-range-slider-width);
}
#sliderOuterDiv_1 {
position: relative;
height: calc(30px + var(--paper-single-range-slider-height, 2px));
margin-left: 0;
margin-right: 0;
margin-top: 0;
margin-bottom: 0;
}
/* mimic the size of the #sliderKnob of paper-single-range-slider */
.sliderKnobMinMax {
position: absolute;
left: 0;
top: 0;
margin-left: calc(-15px - var(--paper-single-range-slider-height, 2px)/2);
width: calc(30px + var(--paper-single-range-slider-height, 2px));
height: calc(30px + var(--paper-single-range-slider-height, 2px));
/*background: #2196F3; opacity: 0.3;*/
}
.divSpanWidth {
position:absolute;
width:100%;
display:block;
top:0px;
}
#sliderMax {
line-height: normal;
--paper-single-range-slider-bar-color: transparent;
--paper-single-range-slider-knob-color: var(--paper-range-slider-knob-color);
--paper-single-range-slider-pin-color: var(--paper-range-slider-pin-color);
--paper-single-range-slider-active-color: var(--paper-range-slider-active-color);
--paper-single-range-slider-secondary-color: var(--paper-range-slider-higher-color);
--paper-single-range-slider-pin-start-color: var(--paper-range-slider-pin-start-color);
--paper-single-range-slider-knob-start-color: var(--paper-range-slider-knob-start-color);
--paper-single-range-slider-knob-start-border-color: var(--paper-range-slider-knob-start-border-color);
}
#sliderMin {
line-height: normal;
--paper-single-range-slider-active-color: var(--paper-range-slider-lower-color);
--paper-single-range-slider-secondary-color: transparent;
--paper-single-range-slider-knob-color: var(--paper-range-slider-knob-color);
--paper-single-range-slider-pin-color: var(--paper-range-slider-pin-color);
--paper-single-range-slider-pin-start-color: var(--paper-range-slider-pin-start-color);
--paper-single-range-slider-knob-start-color: var(--paper-range-slider-knob-start-color);
--paper-single-range-slider-knob-start-border-color: var(--paper-range-slider-knob-start-border-color);
}
</style>
<div id="sliderOuterDiv_0">
<div id="sliderOuterDiv_1">
<!-- for debugging, add: to style the following: "border: 1px solid red" -->
<div id='backDiv' class="divSpanWidth" on-down="_backDivDown" on-tap="_backDivTap"
on-up="_backDivUp" on-track="_backDivOnTrack" on-transitionend="_backDivTransEnd">
<!-- background: #2196F3; opacity:0.2; -->
<!-- must have this in order to force width/height -->
<div id='backDivInner_0' style="line-height:200%;"><br></div>
</div>
<!-- add a couple of invisible overlay divs, to improve the the toggle of slider knobs -->
<div id='sliderKnobMin' class="sliderKnobMinMax" on-down="_backDivDown"
on-up="_backDivUp" on-track="_sliderKnobMinOnTrack"></div>
<div id='sliderKnobMax' class="sliderKnobMinMax" on-down="_backDivDown"
on-up="_backDivUp" on-track="_sliderKnobMaxOnTrack"></div>
<div class="divSpanWidth" style="pointer-events: none;">
<paper-single-range-slider id='sliderMax' disabled$="[[disabled]]" on-down="_sliderMaxDown"
on-up="_sliderMaxUp" step="[[step]]" min="[[min]]" max="[[max]]"
value="[[valueMax]]" secondary-progress='[[max]]' style="width:100%;">
</paper-single-range-slider>
</div>
<div class="divSpanWidth" style="pointer-events: none;">
<paper-single-range-slider id='sliderMin' disabled$="[[disabled]]" on-down="_sliderMinDown"
on-up="_sliderMinUp" noink step="[[step]]" min="[[min]]" max="[[max]]"
value="[[valueMin]]" style="width:100%;">
</paper-single-range-slider>
</div>
<!-- now add one real line -->
<div id='backDivInner_1' style="line-height:100%;"><br></div>
</div>
</div>
</template>
<script>
Polymer({
is: 'paper-range-slider',
behaviors: [
Polymer.IronRangeBehavior,
],
properties: {
/**
* the width of the element in pixels.
*/
sliderWidth: {
type: String,
value: "",
notify: true,
reflectToAttribute: true
},
/**
* the minimal value (lower range) of the slider.
*/
min: {
type: Number,
value: 0,
notify: true,
reflectToAttribute: true
},
/**
* the maximal value (upper range) of the slider.
*/
max: {
type: Number,
value: 100,
notify: true,
reflectToAttribute: true
},
/**
* the current value of the lower range of the slider.
*/
valueMin: {
type: Number,
value: 0,
notify: true,
reflectToAttribute: true
},
/**
* the current value of the upper range of the slider.
*/
valueMax: {
type: Number,
value: 100,
notify: true,
reflectToAttribute: true
},
/**
* the minimal step-change of a knob on the slider
*/
step: {
type: Number,
value: 1,
notify: true,
reflectToAttribute: true
},
/**
* optional minimal value for the difference between valueMin and valueMax
* by default this is negative (valueDiffMin is ignored)
*/
valueDiffMin: {
type: Number,
value: 0,
notify: true,
reflectToAttribute: true
},
/**
* optional maximal value for the difference between valueMin and valueMax
* by default this is negative (valueDiffMax is ignored)
*/
valueDiffMax: {
type: Number,
value: 0,
notify: true,
reflectToAttribute: true
},
/**
* if true, pins with numeric value label are shown when the slider thumb
* is pressed. Use for settings for which users need to know the exact
* value of the setting.
*/
alwaysShowPin: {
type: Boolean,
value: false,
notify: true
},
/**
* if true, pins with numeric value label are shown when the slider thumb
* is pressed. Use for settings for which users need to know the exact
* value of the setting.
*/
pin: {
type: Boolean,
value: false,
notify: true
},
/**
* if true, the slider thumb snaps to tick marks evenly spaced based
* on the `step` property value.
*/
snaps: {
type: Boolean,
value: false,
notify: true
},
/**
* if true, the slider is disabled.
*/
disabled: {
type: Boolean,
value: false,
notify: true
},
/**
* an option to "revert" from the paper-range-slider to a single paper-single-range-slider.
*/
singleSlider: {
type: Boolean,
value: false,
notify: true
},
/**
* time-window (in msec) to keep the slider._setTransiting(true) for the
* two paper-single-range-slider elements, when using the setValues() method to change the
* selected range. This should be slightly higher than the transition time defined for the
* paper-single-range-slider (which, as of paper-single-range-slider-v1.0.11, is 0.08s/0.18s).
*/
transDuration: {
type: Number,
value: 250,
},
/**
* if set to true, tapping the slider below or above the selected range
* will update the selected range.
*/
tapValueExtend: {
type: Boolean,
value: true,
notify: true
},
/**
* if set to true, tapping the slider inside the selected range
* will update the selected range.
*/
tapValueReduce: {
type: Boolean,
value: false,
notify: true
},
/**
* if set to true, tapping the slider will update the selected range,
* while keeping the same difference between valueMin and valueMax.
* tapValueMove supersedes tapValueExtend and tapValueReduce
*/
tapValueMove: {
type: Boolean,
value: false,
notify: true
}
},
// initialization
ready: function() {
function _hasVar(varIn) { return (varIn != undefined && varIn != null); }
if(!_hasVar(this._nInitTries)) this._nInitTries = 0;
// it may take a little time for polymer to decide to render the paper-single-range-slider elements,
// we therefore wait until they're ready before we proceed with the initialization
setTimeout(function () {
var sliderContainer = this.shadowRoot.querySelector("#sliderMax").getEle('#sliderContainer');
if(_hasVar(sliderContainer)) { sliderContainer = (sliderContainer.offsetWidth > 0); }
if(_hasVar(sliderContainer)) { sliderContainer = this.shadowRoot.querySelector("#sliderMin").getEle('#sliderContainer'); }
if(_hasVar(sliderContainer)) { sliderContainer = (sliderContainer.offsetWidth > 0); }
// finally, may go through with the initialization
if(_hasVar(sliderContainer)) {
this._renderedReady();
}
// if not, try again in a few msec
else {
if(this._nInitTries < 1000) {
var this_ = this;
setTimeout(function() { this_.ready(); }, 10);
}
else {
console.error("could not properly initialize the underlying paper-single-range-slider elements ...");
}
this._nInitTries++;
}
}.bind(this), 0);
return;
},
// initialization once the paper-single-range-slider elements have been rendered
_renderedReady: function() {
// some initializations
this.init();
// set the padding for the parent container
this._setPadding();
// setup listeners for updating everything whenever a knob is affected
var this_ = this;
this.shadowRoot.querySelector("#sliderMin").addEventListener('immediate-value-change', function(customEvent) {
this_._setValueMinMax(this_._getValuesMinMax(this.immediateValue,null));
this_.$.sliderMin._expandKnob();
this_.$.sliderMax._expandKnob();
});
this.shadowRoot.querySelector("#sliderMax").addEventListener('immediate-value-change', function(customEvent) {
// console.log('immed-change..',this.immediateValue,this_.valueMax)
this_._setValueMinMax(this_._getValuesMinMax(null,this.immediateValue));
});
this.shadowRoot.querySelector("#sliderMin").addEventListener('change', function(customEvent) {
this_._setValueMinMax(this_._getValuesMinMax(this.immediateValue,null));
if(this_.alwaysShowPin) {
this_.$.sliderMin._expandKnob();
}
});
this.shadowRoot.querySelector("#sliderMax").addEventListener('change', function(customEvent) {
// console.log('final-change..',this.immediateValue,this_.valueMax)
this_._setValueMinMax(this_._getValuesMinMax(null,this.immediateValue));
if(this_.alwaysShowPin) {
this_.$.sliderMax._expandKnob();
}
});
return;
},
// set padding for the element
_setPadding: function() {
// the creation of the element can never be trusted to derive the offsetHeight
// (if e.g., it is in a collapsed container). We therefore add a fake element to the
// DOM to get the basic text-heigh, then we remove this element.
var beforeEle = document.createElement("div");
beforeEle.setAttribute("style","position:absolute; top:0px; opacity:0;");
beforeEle.innerHTML = "invisibleText"; // must have some content
document.body.insertBefore(beforeEle, document.body.children[0]);
var height = beforeEle.offsetHeight / 2; //console.log( 'qqq',beforeEle.offsetHeight)
this.$.sliderOuterDiv_0.style.paddingTop = height+"px"
this.$.sliderOuterDiv_0.style.paddingBottom = height+"px"
beforeEle.parentNode.removeChild(beforeEle);
return;
},
// internal variables for minimal/maximal difference between this.valueMin, this.valueMax
// each one is between zero and the maximal difference available in the range, and
// the this._valueDiffMin can not be larger than this._valueDiffMax
_setValueDiff: function() {
this._valueDiffMax = Math.max(this.valueDiffMax, 0);
this._valueDiffMin = Math.max(this.valueDiffMin, 0);
return;
},
// get a new set of min/max values, following predefined rules for overlap of the two
_getValuesMinMax: function(valueMin,valueMax) {
var hasMin = (valueMin != null && valueMin >= this.min && valueMin <= this.max);
var hasMax = (valueMax != null && valueMax >= this.min && valueMax <= this.max);
if(!hasMin && !hasMax) { return [this.valueMin,this.valueMax]; }
var valueNowMin = hasMin ? valueMin : this.valueMin;
var valueNowMax = hasMax ? valueMax : this.valueMax;
valueNowMin = Math.min(Math.max(valueNowMin, this.min), this.max)
valueNowMax = Math.min(Math.max(valueNowMax, this.min), this.max)
var diffNow = valueNowMax - valueNowMin;
// the anchor is the valueMin if it is explicitly provided
if(hasMin) {
if(diffNow < this._valueDiffMin) {
valueNowMax = Math.min(this.max, valueNowMin + this._valueDiffMin);
diffNow = valueNowMax - valueNowMin;
if(diffNow < this._valueDiffMin) {
valueNowMin = valueNowMax - this._valueDiffMin;
}
}
else if(diffNow > this._valueDiffMax && this._valueDiffMax > 0) {
valueNowMax = valueNowMin + this._valueDiffMax;
}
}
// if no valueMin given, decide the anchor is valueMax
else {
if(diffNow < this._valueDiffMin) {
valueNowMin = Math.max(this.min, valueNowMax - this._valueDiffMin);
diffNow = valueNowMax - valueNowMin;
if(diffNow < this._valueDiffMin) {
valueNowMax = valueNowMin + this._valueDiffMin;
}
}
else if(diffNow > this._valueDiffMax && this._valueDiffMax > 0) {
valueNowMin = valueNowMax - this._valueDiffMax;
}
}
return [valueNowMin, valueNowMax];
},
// set the value of the low edge of the selected range
_setValueMin: function(value) {
value = Math.max(value, this.min);
this.shadowRoot.querySelector("#sliderMin").value = value;
this.valueMin = value;
return;
},
// set the value of the high edge of the selected range
_setValueMax: function(value) {
value = Math.min(value, this.max);
this.shadowRoot.querySelector("#sliderMax").value = value;
this.valueMax = value;
return;
},
// set the values of the low/high edges of the selected range and broadcast the change
_setValueMinMax: function(valuesMinMax) {
this._setValueMin(valuesMinMax[0]);
this._setValueMax(valuesMinMax[1]);
// update the position of the overlay divs for the min/max knobs
this._updateSliderKnobMinMax();
// fire to indicate an update of this.valueMin and/or this.valueMax
this.updateValues();
return;
},
// set this.valueMin and/or this.valueMax (can input null values or out-of-range
// values in order to set only one of the two)
_setValues: function(valueMin,valueMax) {
// some sanity checks/changes
if(valueMin != null) {
if(valueMin < this.min || valueMin > this.max) valueMin = null;
}
if(valueMax != null) {
if(valueMax < this.min || valueMax > this.max) valueMax = null;
}
if(valueMin != null && valueMax != null) {
valueMin = Math.min(valueMin,valueMax);
}
// now update the values
this._setValueMinMax(this._getValuesMinMax(valueMin,valueMax));
return;
},
// update the position of the overlay divs for the min/max knobs
_updateSliderKnobMinMax: function() {
var _this = this;
var nTries = 0;
function try_updateSliderKnobMinMax() {
var width = _this.shadowRoot.querySelector("#sliderMax").getEle('#sliderContainer').offsetWidth;
if(width < 0.0001) {
if(nTries < 100) {
nTries++;
setTimeout(function() { try_updateSliderKnobMinMax(); }, 10);
}
return;
}
var leftMin = width*(_this.valueMin - _this.min)/(_this.max - _this.min)
+ 0.5 * _this.shadowRoot.querySelector("#sliderKnobMin").offsetWidth;
var leftMax = width*(_this.valueMax - _this.min)/(_this.max - _this.min)
+ 0.5 * _this.shadowRoot.querySelector("#sliderKnobMax").offsetWidth;
_this.shadowRoot.querySelector("#sliderKnobMin").style.left = leftMin+'px';
_this.shadowRoot.querySelector("#sliderKnobMax").style.left = leftMax+'px';
return;
}
try_updateSliderKnobMinMax();
return;
},
// interface for functions to control the draggable invisible div which
// spans the distance between the knobs
_backDivOnTrack: function(event) {
event.stopPropagation();
switch (event.detail.state) {
case 'start':
this._backDivTrackStart(event);
break;
case 'track':
this._backDivTrackDuring(event);
break;
case 'end':
this._backDivTrackEnd();
break;
}
return;
},
// place-holder function for possible later implementation
_backDivTrackStart: function(event) { return; },
// function to enable dragging both knobs by using the invisible
// div which spans the distance in between
_backDivTrackDuring: function(e) {
this._x1_Min = this._x0_Min / 100 + e.detail.dx;
var immediateValueMin = this._calcStep(this._getRatioPos(this.shadowRoot.querySelector("#sliderMin"), this._x1_Min/this._xWidth * 100));
this._x1_Max = this._x0_Max / 100 + e.detail.dx;
var immediateValueMax = this._calcStep(this._getRatioPos(this.shadowRoot.querySelector("#sliderMax"), this._x1_Max/this._xWidth * 100));
if(immediateValueMin >= this.min && immediateValueMax <= this.max) {
// console.log(immediateValueMin,immediateValueMax)
this._setValuesWithCurrentDiff(immediateValueMin, immediateValueMax, false);
}
return;
},
_setValuesWithCurrentDiff: function(valueMin, valueMax, useTrans) {
var diffMin = this._valueDiffMin;
var diffMax = this._valueDiffMax;
this._valueDiffMin = this.valueMax - this.valueMin;
this._valueDiffMax = this.valueMax - this.valueMin;
if(useTrans) this.setValues(valueMin, valueMax);
else this._setValues(valueMin, valueMax);
this._valueDiffMin = diffMin;
this._valueDiffMax = diffMax;
return;
},
// place-holder function for at the end of the dragging event, so the following
_backDivTrackEnd: function() { return; },
// functions to enable dragging each knob by using the invisible
// div which are overlaid on the respective knobs
_sliderKnobMinOnTrack: function(e) {
this._x1_Min = this._x0_Min / 100 + e.detail.dx;
var value = this._calcStep(this._getRatioPos(this.shadowRoot.querySelector("#sliderMin"), this._x1_Min/this._xWidth * 100));
this._setValues(value,null);
return;
},
_sliderKnobMaxOnTrack: function(e) {
this._x1_Max = this._x0_Max / 100 + e.detail.dx;
var value = this._calcStep(this._getRatioPos(this.shadowRoot.querySelector("#sliderMax"), this._x1_Max/this._xWidth * 100));
this._setValues(null,value);
return;
},
// _sliderMinDown, _sliderMaxDown, _sliderMinUp, _sliderMaxUp
// show/hide pins (if defined) for one knob, when the other knob is pressed
_sliderMinDown: function() {
this.shadowRoot.querySelector("#sliderMax")._expandKnob();
return;
},
_sliderMaxDown: function(event) {
if(!this.singleSlider) {
this.shadowRoot.querySelector("#sliderMin")._expandKnob();
}
else {
this._setValues(null,this._getEventValue(event));
}
return;
},
_sliderMinUp: function() {
if(this.alwaysShowPin) this.shadowRoot.querySelector("#sliderMin")._expandKnob();
else this.shadowRoot.querySelector("#sliderMax")._resetKnob();
return;
},
_sliderMaxUp: function() {
if(this.alwaysShowPin) this.shadowRoot.querySelector("#sliderMax")._expandKnob();
else {
this.shadowRoot.querySelector("#sliderMin")._resetKnob();
if(this.singleSlider) this.shadowRoot.querySelector("#sliderMax")._resetKnob();
}
return;
},
// function to calculate the value from an event
_getEventValue: function(event) {
var width = this.shadowRoot.querySelector("#sliderMax").getEle('#sliderContainer').offsetWidth;
var rect = this.shadowRoot.querySelector("#sliderMax").getEle('#sliderContainer').getBoundingClientRect();
var ratio = (event.detail.x - rect.left) / width;
var value = this.min + ratio * (this.max - this.min);
return value;
},
// set the value if tapping the slider below or above the selected range
_backDivTap: function(event) {
this._setValueNow = function(valueMin,valueMax) {
if(this.tapValueMove) { this._setValuesWithCurrentDiff(valueMin,valueMax,true); }
else { this.setValues(valueMin,valueMax); }
return;
}
var value = this._getEventValue(event);
if(value > this.valueMin && value < this.valueMax) {
if(this.tapValueReduce) {
var isLow = value < (this.valueMin + (this.valueMax - this.valueMin)/2);
if(isLow) { this._setValueNow(value,null); }
else { this._setValueNow(null,value); }
}
}
else if(this.tapValueExtend || this.tapValueMove) {
if(value < this.valueMin) { this._setValueNow(value,null); }
if(value > this.valueMax) { this._setValueNow(null,value); }
}
return;
},
// initialization before starting the dragging of the invisible
// div which spans the distance in between
_backDivDown: function(event) {
// show pins if defined
this._sliderMinDown();
this._sliderMaxDown();
// get the initial positions of knobs before dragging starts
this._xWidth = this.shadowRoot.querySelector("#sliderMin").getEle('#sliderBar').offsetWidth;
this._x0_Min = this.shadowRoot.querySelector("#sliderMin").ratio * this._xWidth;
this._x0_Max = this.shadowRoot.querySelector("#sliderMax").ratio * this._xWidth;
return;
},
// finalization after ending the dragging of the invisible
// div which spans the distance in between
_backDivUp: function() {
// hide pins if defined
this._sliderMinUp();
this._sliderMaxUp();
return;
},
// place-holder function for possible later implementation
_backDivTransEnd: function(event) { return; },
// // the current position of the knob for a given single slider
// _getPos: function(sliderIn) {
// return (sliderIn.ratio) * Number(sliderIn.style.width.replace('px',''));
// },
// the position of the knob for a given single slider, for a given ratio
_getRatioPos: function(slider, ratio) {
return Math.max(slider.min, Math.min(slider.max, (slider.max - slider.min) * ratio / 100 + slider.min));
},
/**
* initialize basic properties
* @method init
*/
init: function() {
//
this.setSingleSlider(this.singleSlider);
this.setDisabled(this.disabled);
// some basic properties
if(this.alwaysShowPin) { this.pin = true; }
this.shadowRoot.querySelector("#sliderMin").pin = this.pin;
this.shadowRoot.querySelector("#sliderMax").pin = this.pin;
this.shadowRoot.querySelector("#sliderMin").snaps = this.snaps;
this.shadowRoot.querySelector("#sliderMax").snaps = this.snaps;
if(this.sliderWidth != "") {
//this.updateStyles({'--paper-range-slider-width' : this.sliderWidth});
}
// since the two single sliders are overlaid, we need to remove foreground color
var sliderBar = this.shadowRoot.querySelector("#sliderMin").getEle('#sliderBar');
if(sliderBar) {
var progressContainer = sliderBar.shadowRoot.querySelector('#progressContainer');
if(progressContainer) progressContainer.style.background = "transparent";
}
// internal variable to prevent unneeded fire on updates
this._prevUpdateValues = [this.min, this.max]
// set internal variables to control the minimal and maximal difference between selected values
this._setValueDiff();
// initial setting after verifying this._valueDiffMin, this._valueDiffMax
this._setValueMinMax(this._getValuesMinMax(this.valueMin, this.valueMax));
// activate the pins, and never hide
if(this.alwaysShowPin) {
this.shadowRoot.querySelector("#sliderMin")._expandKnob();
this.shadowRoot.querySelector("#sliderMax")._expandKnob();
}
return;
},
/**
* set this.valueMin and/or this.valueMax (can input null values or out-of-range
* values in order to set only one of the two) - this is just a public
* wrapper for this._setValues(), but including transition animation
* @method setValues
*/
setValues: function(valueMin,valueMax) {
this.shadowRoot.querySelector("#sliderMin")._setTransiting(true);
this.shadowRoot.querySelector("#sliderMax")._setTransiting(true);
this._setValues(valueMin,valueMax);
var this_ = this;
setTimeout(function() {
this_.$.sliderMin._setTransiting(false);
this_.$.sliderMax._setTransiting(false);
}, this_.transDuration);
return;
},
/**
* fire whenever this.valueMin or this.valueMax are changed
* @method updateValues
*/
updateValues: function() {
if(this._prevUpdateValues[0] != this.valueMin || this._prevUpdateValues[1] != this.valueMax) {
this._prevUpdateValues = [this.valueMin, this.valueMax];
// fire the event
this.async(function() { this.fire('updateValues'); });
}
return;
},
/**
* set the minimal value (lower range) of the slider
* @method setMin
*/
setMin: function(minIn) {
// paper-single-range-slider needs a safety check that the min value we are going to set is
// not larger than the max value which is already set
if(this.max < minIn) this.max = minIn;
this.min = minIn;
this._prevUpdateValues = [this.min, this.max];
// update the selected value if it is now outside of the lower bound,
// or just update the position of the overlay divs for the min/max knobs
if(this.valueMin < this.min) this._setValues(this.min,null);
else this._updateSliderKnobMinMax();
return;
},
/**
* set the maximal value (upper range) of the slider
* @method setMax
*/
setMax: function(maxIn) {
// paper-single-range-slider needs a safety check that the min value we are going to set is
// not larger than the max value which is already set
if(this.min > maxIn) this.min = maxIn;
this.max = maxIn;
this._prevUpdateValues = [this.min, this.max];
// update the selected value if it is now outside of the upper bound,
// or just update the position of the overlay divs for the min/max knobs
if(this.valueMax > this.max) this._setValues(null,this.max);
else this._updateSliderKnobMinMax();
return;
},
/**
* set the minimal step-change of a knob on the slider
* @method setMax
*/
setStep: function(stepIn) {
this.step = stepIn;
return;
},
/**
* set the minimal difference between selected values
* @method setValueDiffMin
*/
setValueDiffMin: function(valueDiffMin) {
this._valueDiffMin = valueDiffMin;
return;
},
/**
* set the maximal difference between selected values
* @method setValueDiffMax
*/
setValueDiffMax: function(valueDiffMax) {
this._valueDiffMax = valueDiffMax;
return;
},
/**
* set the tapValueExtend property
* @method setValueDiffMax
*/
setTapValueExtend: function(isTapValueExtend) {
this.tapValueExtend = isTapValueExtend;
return;
},
/**
* set the tapValueReduce property
* @method setValueDiffMax
*/
setTapValueReduce: function(isTapValueReduce) {
this.tapValueReduce = isTapValueReduce;
return;
},
/**
* set the tapValueMove property
* @method setValueDiffMax
*/
setTapValueMove: function(isTapValueMove) {
this.tapValueMove = isTapValueMove;
return;
},
/**
* set the disabled parameter
* @method setValueDiffMax
*/
setDisabled: function(isDisabled) {
this.disabled = isDisabled;
var pointEvt = isDisabled ? "none" : "auto";
this.shadowRoot.querySelector("#sliderMax").getEle('#sliderKnob').style.pointerEvents = pointEvt;
this.shadowRoot.querySelector("#sliderMin").getEle('#sliderKnob').style.pointerEvents = pointEvt;
this.shadowRoot.querySelector("#sliderOuterDiv_1").style.pointerEvents = pointEvt;
this.shadowRoot.querySelector("#sliderKnobMin").style.pointerEvents = pointEvt;
this.shadowRoot.querySelector("#sliderKnobMax").style.pointerEvents = pointEvt;
return;
},
/**
* change the slider between the default paper-range-slider and a paper-single-range-slider
* @method setValueDiffMax
*/
setSingleSlider: function(isSingle) {
this.singleSlider = isSingle;
if(isSingle) {
this.shadowRoot.querySelector("#backDiv").style.display = 'none';
this.shadowRoot.querySelector("#sliderMax").style.pointerEvents = 'auto';
this.shadowRoot.querySelector("#sliderMax").style.display = '';
this.shadowRoot.querySelector("#sliderMin").style.display = 'none';
this.shadowRoot.querySelector("#sliderKnobMin").style.pointerEvents = 'none';
this.shadowRoot.querySelector("#sliderKnobMax").style.pointerEvents = 'none';
this.shadowRoot.querySelector("#backDiv").style.cursor = 'default';
this.shadowRoot.querySelector("#sliderKnobMin").style.cursor = 'default';
this.shadowRoot.querySelector("#sliderKnobMax").style.cursor = 'default';
this.shadowRoot.querySelector("#sliderMax").getEle('#sliderKnob').style.cursor = 'default';
this.shadowRoot.querySelector("#sliderMin").getEle('#sliderKnob').style.cursor = 'default';
}
else {
this.shadowRoot.querySelector("#backDiv").style.display = 'block';
this.shadowRoot.querySelector("#sliderMax").style.pointerEvents = 'none';
this.shadowRoot.querySelector("#sliderMax").style.display = '';
this.shadowRoot.querySelector("#sliderMin").style.display = '';
this.shadowRoot.querySelector("#sliderKnobMin").style.pointerEvents = 'auto';
this.shadowRoot.querySelector("#sliderKnobMax").style.pointerEvents = 'auto';
this.shadowRoot.querySelector("#backDiv").style.cursor = 'ew-resize';
this.shadowRoot.querySelector("#sliderKnobMin").style.cursor = 'col-resize';
this.shadowRoot.querySelector("#sliderKnobMax").style.cursor = 'col-resize';
this.shadowRoot.querySelector("#sliderMax").getEle('#sliderKnob').style.cursor = 'col-resize';
this.shadowRoot.querySelector("#sliderMin").getEle('#sliderKnob').style.cursor = 'col-resize';
}
// disable some of the interface of the two single sliders,
// but keep the knobs active if not disabled
this.shadowRoot.querySelector("#sliderMax").getEle('#sliderContainer').style.pointerEvents = this.singleSlider ? "auto" : "none";
this.shadowRoot.querySelector("#sliderMin").getEle('#sliderContainer').style.pointerEvents = "none";
return;
}
});
</script>
</dom-module>
<!-- paper-single-range-slider is a modified version of [paper-slider, v1.0.13] -->
<dom-module id="paper-single-range-slider">
<template strip-whitespace>
<style>
:host {
@apply --layout;
@apply --layout-justified;
@apply --layout-center;