slider.js
64 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
/*
Copyright (c) 2011, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.com/yui/license.html
version: 2.9.0
*/
/**
* The Slider component is a UI control that enables the user to adjust
* values in a finite range along one or two axes. Typically, the Slider
* control is used in a web application as a rich, visual replacement
* for an input box that takes a number as input. The Slider control can
* also easily accommodate a second dimension, providing x,y output for
* a selection point chosen from a rectangular region.
*
* @module slider
* @title Slider Widget
* @namespace YAHOO.widget
* @requires yahoo,dom,dragdrop,event
* @optional animation
*/
(function () {
var getXY = YAHOO.util.Dom.getXY,
Event = YAHOO.util.Event,
_AS = Array.prototype.slice;
/**
* A DragDrop implementation that can be used as a background for a
* slider. It takes a reference to the thumb instance
* so it can delegate some of the events to it. The goal is to make the
* thumb jump to the location on the background when the background is
* clicked.
*
* @class Slider
* @extends YAHOO.util.DragDrop
* @uses YAHOO.util.EventProvider
* @constructor
* @param {String} id The id of the element linked to this instance
* @param {String} sGroup The group of related DragDrop items
* @param {SliderThumb} oThumb The thumb for this slider
* @param {String} sType The type of slider (horiz, vert, region)
*/
function Slider(sElementId, sGroup, oThumb, sType) {
Slider.ANIM_AVAIL = (!YAHOO.lang.isUndefined(YAHOO.util.Anim));
if (sElementId) {
this.init(sElementId, sGroup, true);
this.initSlider(sType);
this.initThumb(oThumb);
}
}
YAHOO.lang.augmentObject(Slider,{
/**
* Factory method for creating a horizontal slider
* @method YAHOO.widget.Slider.getHorizSlider
* @static
* @param {String} sBGElId the id of the slider's background element
* @param {String} sHandleElId the id of the thumb element
* @param {int} iLeft the number of pixels the element can move left
* @param {int} iRight the number of pixels the element can move right
* @param {int} iTickSize optional parameter for specifying that the element
* should move a certain number pixels at a time.
* @return {Slider} a horizontal slider control
*/
getHorizSlider :
function (sBGElId, sHandleElId, iLeft, iRight, iTickSize) {
return new Slider(sBGElId, sBGElId,
new YAHOO.widget.SliderThumb(sHandleElId, sBGElId,
iLeft, iRight, 0, 0, iTickSize), "horiz");
},
/**
* Factory method for creating a vertical slider
* @method YAHOO.widget.Slider.getVertSlider
* @static
* @param {String} sBGElId the id of the slider's background element
* @param {String} sHandleElId the id of the thumb element
* @param {int} iUp the number of pixels the element can move up
* @param {int} iDown the number of pixels the element can move down
* @param {int} iTickSize optional parameter for specifying that the element
* should move a certain number pixels at a time.
* @return {Slider} a vertical slider control
*/
getVertSlider :
function (sBGElId, sHandleElId, iUp, iDown, iTickSize) {
return new Slider(sBGElId, sBGElId,
new YAHOO.widget.SliderThumb(sHandleElId, sBGElId, 0, 0,
iUp, iDown, iTickSize), "vert");
},
/**
* Factory method for creating a slider region like the one in the color
* picker example
* @method YAHOO.widget.Slider.getSliderRegion
* @static
* @param {String} sBGElId the id of the slider's background element
* @param {String} sHandleElId the id of the thumb element
* @param {int} iLeft the number of pixels the element can move left
* @param {int} iRight the number of pixels the element can move right
* @param {int} iUp the number of pixels the element can move up
* @param {int} iDown the number of pixels the element can move down
* @param {int} iTickSize optional parameter for specifying that the element
* should move a certain number pixels at a time.
* @return {Slider} a slider region control
*/
getSliderRegion :
function (sBGElId, sHandleElId, iLeft, iRight, iUp, iDown, iTickSize) {
return new Slider(sBGElId, sBGElId,
new YAHOO.widget.SliderThumb(sHandleElId, sBGElId, iLeft, iRight,
iUp, iDown, iTickSize), "region");
},
/**
* Constant for valueChangeSource, indicating that the user clicked or
* dragged the slider to change the value.
* @property Slider.SOURCE_UI_EVENT
* @final
* @static
* @default 1
*/
SOURCE_UI_EVENT : 1,
/**
* Constant for valueChangeSource, indicating that the value was altered
* by a programmatic call to setValue/setRegionValue.
* @property Slider.SOURCE_SET_VALUE
* @final
* @static
* @default 2
*/
SOURCE_SET_VALUE : 2,
/**
* Constant for valueChangeSource, indicating that the value was altered
* by hitting any of the supported keyboard characters.
* @property Slider.SOURCE_KEY_EVENT
* @final
* @static
* @default 2
*/
SOURCE_KEY_EVENT : 3,
/**
* By default, animation is available if the animation utility is detected.
* @property Slider.ANIM_AVAIL
* @static
* @type boolean
*/
ANIM_AVAIL : false
},true);
YAHOO.extend(Slider, YAHOO.util.DragDrop, {
/**
* Tracks the state of the mouse button to aid in when events are fired.
*
* @property _mouseDown
* @type boolean
* @default false
* @private
*/
_mouseDown : false,
/**
* Override the default setting of dragOnly to true.
* @property dragOnly
* @type boolean
* @default true
*/
dragOnly : true,
/**
* Initializes the slider. Executed in the constructor
* @method initSlider
* @param {string} sType the type of slider (horiz, vert, region)
*/
initSlider: function(sType) {
/**
* The type of the slider (horiz, vert, region)
* @property type
* @type string
*/
this.type = sType;
//this.removeInvalidHandleType("A");
/**
* Event the fires when the value of the control changes. If
* the control is animated the event will fire every point
* along the way.
* @event change
* @param {int} newOffset|x the new offset for normal sliders, or the new
* x offset for region sliders
* @param {int} y the number of pixels the thumb has moved on the y axis
* (region sliders only)
*/
this.createEvent("change", this);
/**
* Event that fires at the beginning of a slider thumb move.
* @event slideStart
*/
this.createEvent("slideStart", this);
/**
* Event that fires at the end of a slider thumb move
* @event slideEnd
*/
this.createEvent("slideEnd", this);
/**
* Overrides the isTarget property in YAHOO.util.DragDrop
* @property isTarget
* @private
*/
this.isTarget = false;
/**
* Flag that determines if the thumb will animate when moved
* @property animate
* @type boolean
*/
this.animate = Slider.ANIM_AVAIL;
/**
* Set to false to disable a background click thumb move
* @property backgroundEnabled
* @type boolean
*/
this.backgroundEnabled = true;
/**
* Adjustment factor for tick animation, the more ticks, the
* faster the animation (by default)
* @property tickPause
* @type int
*/
this.tickPause = 40;
/**
* Enables the arrow, home and end keys, defaults to true.
* @property enableKeys
* @type boolean
*/
this.enableKeys = true;
/**
* Specifies the number of pixels the arrow keys will move the slider.
* Default is 20.
* @property keyIncrement
* @type int
*/
this.keyIncrement = 20;
/**
* moveComplete is set to true when the slider has moved to its final
* destination. For animated slider, this value can be checked in
* the onChange handler to make it possible to execute logic only
* when the move is complete rather than at all points along the way.
* Deprecated because this flag is only useful when the background is
* clicked and the slider is animated. If the user drags the thumb,
* the flag is updated when the drag is over ... the final onDrag event
* fires before the mouseup the ends the drag, so the implementer will
* never see it.
*
* @property moveComplete
* @type Boolean
* @deprecated use the slideEnd event instead
*/
this.moveComplete = true;
/**
* If animation is configured, specifies the length of the animation
* in seconds.
* @property animationDuration
* @type int
* @default 0.2
*/
this.animationDuration = 0.2;
/**
* Constant for valueChangeSource, indicating that the user clicked or
* dragged the slider to change the value.
* @property SOURCE_UI_EVENT
* @final
* @default 1
* @deprecated use static Slider.SOURCE_UI_EVENT
*/
this.SOURCE_UI_EVENT = 1;
/**
* Constant for valueChangeSource, indicating that the value was altered
* by a programmatic call to setValue/setRegionValue.
* @property SOURCE_SET_VALUE
* @final
* @default 2
* @deprecated use static Slider.SOURCE_SET_VALUE
*/
this.SOURCE_SET_VALUE = 2;
/**
* When the slider value changes, this property is set to identify where
* the update came from. This will be either 1, meaning the slider was
* clicked or dragged, or 2, meaning that it was set via a setValue() call.
* This can be used within event handlers to apply some of the logic only
* when dealing with one source or another.
* @property valueChangeSource
* @type int
* @since 2.3.0
*/
this.valueChangeSource = 0;
/**
* Indicates whether or not events will be supressed for the current
* slide operation
* @property _silent
* @type boolean
* @private
*/
this._silent = false;
/**
* Saved offset used to protect against NaN problems when slider is
* set to display:none
* @property lastOffset
* @type [int, int]
*/
this.lastOffset = [0,0];
},
/**
* Initializes the slider's thumb. Executed in the constructor.
* @method initThumb
* @param {YAHOO.widget.SliderThumb} t the slider thumb
*/
initThumb: function(t) {
var self = this;
/**
* A YAHOO.widget.SliderThumb instance that we will use to
* reposition the thumb when the background is clicked
* @property thumb
* @type YAHOO.widget.SliderThumb
*/
this.thumb = t;
t.cacheBetweenDrags = true;
if (t._isHoriz && t.xTicks && t.xTicks.length) {
this.tickPause = Math.round(360 / t.xTicks.length);
} else if (t.yTicks && t.yTicks.length) {
this.tickPause = Math.round(360 / t.yTicks.length);
}
// delegate thumb methods
t.onAvailable = function() {
return self.setStartSliderState();
};
t.onMouseDown = function () {
self._mouseDown = true;
return self.focus();
};
t.startDrag = function() {
self._slideStart();
};
t.onDrag = function() {
self.fireEvents(true);
};
t.onMouseUp = function() {
self.thumbMouseUp();
};
},
/**
* Executed when the slider element is available
* @method onAvailable
*/
onAvailable: function() {
this._bindKeyEvents();
},
/**
* Sets up the listeners for keydown and key press events.
*
* @method _bindKeyEvents
* @protected
*/
_bindKeyEvents : function () {
Event.on(this.id, "keydown", this.handleKeyDown, this, true);
Event.on(this.id, "keypress", this.handleKeyPress, this, true);
},
/**
* Executed when a keypress event happens with the control focused.
* Prevents the default behavior for navigation keys. The actual
* logic for moving the slider thumb in response to a key event
* happens in handleKeyDown.
* @param {Event} e the keypress event
*/
handleKeyPress: function(e) {
if (this.enableKeys) {
var kc = Event.getCharCode(e);
switch (kc) {
case 0x25: // left
case 0x26: // up
case 0x27: // right
case 0x28: // down
case 0x24: // home
case 0x23: // end
Event.preventDefault(e);
break;
default:
}
}
},
/**
* Executed when a keydown event happens with the control focused.
* Updates the slider value and display when the keypress is an
* arrow key, home, or end as long as enableKeys is set to true.
* @param {Event} e the keydown event
*/
handleKeyDown: function(e) {
if (this.enableKeys) {
var kc = Event.getCharCode(e),
t = this.thumb,
h = this.getXValue(),
v = this.getYValue(),
changeValue = true;
switch (kc) {
// left
case 0x25: h -= this.keyIncrement; break;
// up
case 0x26: v -= this.keyIncrement; break;
// right
case 0x27: h += this.keyIncrement; break;
// down
case 0x28: v += this.keyIncrement; break;
// home
case 0x24: h = t.leftConstraint;
v = t.topConstraint;
break;
// end
case 0x23: h = t.rightConstraint;
v = t.bottomConstraint;
break;
default: changeValue = false;
}
if (changeValue) {
if (t._isRegion) {
this._setRegionValue(Slider.SOURCE_KEY_EVENT, h, v, true);
} else {
this._setValue(Slider.SOURCE_KEY_EVENT,
(t._isHoriz ? h : v), true);
}
Event.stopEvent(e);
}
}
},
/**
* Initialization that sets up the value offsets once the elements are ready
* @method setStartSliderState
*/
setStartSliderState: function() {
this.setThumbCenterPoint();
/**
* The basline position of the background element, used
* to determine if the background has moved since the last
* operation.
* @property baselinePos
* @type [int, int]
*/
this.baselinePos = getXY(this.getEl());
this.thumb.startOffset = this.thumb.getOffsetFromParent(this.baselinePos);
if (this.thumb._isRegion) {
if (this.deferredSetRegionValue) {
this._setRegionValue.apply(this, this.deferredSetRegionValue);
this.deferredSetRegionValue = null;
} else {
this.setRegionValue(0, 0, true, true, true);
}
} else {
if (this.deferredSetValue) {
this._setValue.apply(this, this.deferredSetValue);
this.deferredSetValue = null;
} else {
this.setValue(0, true, true, true);
}
}
},
/**
* When the thumb is available, we cache the centerpoint of the element so
* we can position the element correctly when the background is clicked
* @method setThumbCenterPoint
*/
setThumbCenterPoint: function() {
var el = this.thumb.getEl();
if (el) {
/**
* The center of the slider element is stored so we can
* place it in the correct position when the background is clicked.
* @property thumbCenterPoint
* @type {"x": int, "y": int}
*/
this.thumbCenterPoint = {
x: parseInt(el.offsetWidth/2, 10),
y: parseInt(el.offsetHeight/2, 10)
};
}
},
/**
* Locks the slider, overrides YAHOO.util.DragDrop
* @method lock
*/
lock: function() {
this.thumb.lock();
this.locked = true;
},
/**
* Unlocks the slider, overrides YAHOO.util.DragDrop
* @method unlock
*/
unlock: function() {
this.thumb.unlock();
this.locked = false;
},
/**
* Handles mouseup event on the thumb
* @method thumbMouseUp
* @private
*/
thumbMouseUp: function() {
this._mouseDown = false;
if (!this.isLocked()) {
this.endMove();
}
},
onMouseUp: function() {
this._mouseDown = false;
if (this.backgroundEnabled && !this.isLocked()) {
this.endMove();
}
},
/**
* Returns a reference to this slider's thumb
* @method getThumb
* @return {SliderThumb} this slider's thumb
*/
getThumb: function() {
return this.thumb;
},
/**
* Try to focus the element when clicked so we can add
* accessibility features
* @method focus
* @private
*/
focus: function() {
this.valueChangeSource = Slider.SOURCE_UI_EVENT;
// Focus the background element if possible
var el = this.getEl();
if (el.focus) {
try {
el.focus();
} catch(e) {
// Prevent permission denied unhandled exception in FF that can
// happen when setting focus while another element is handling
// the blur. @TODO this is still writing to the error log
// (unhandled error) in FF1.5 with strict error checking on.
}
}
this.verifyOffset();
return !this.isLocked();
},
/**
* Event that fires when the value of the slider has changed
* @method onChange
* @param {int} firstOffset the number of pixels the thumb has moved
* from its start position. Normal horizontal and vertical sliders will only
* have the firstOffset. Regions will have both, the first is the horizontal
* offset, the second the vertical.
* @param {int} secondOffset the y offset for region sliders
* @deprecated use instance.subscribe("change") instead
*/
onChange: function (firstOffset, secondOffset) {
/* override me */
},
/**
* Event that fires when the at the beginning of the slider thumb move
* @method onSlideStart
* @deprecated use instance.subscribe("slideStart") instead
*/
onSlideStart: function () {
/* override me */
},
/**
* Event that fires at the end of a slider thumb move
* @method onSliderEnd
* @deprecated use instance.subscribe("slideEnd") instead
*/
onSlideEnd: function () {
/* override me */
},
/**
* Returns the slider's thumb offset from the start position
* @method getValue
* @return {int} the current value
*/
getValue: function () {
return this.thumb.getValue();
},
/**
* Returns the slider's thumb X offset from the start position
* @method getXValue
* @return {int} the current horizontal offset
*/
getXValue: function () {
return this.thumb.getXValue();
},
/**
* Returns the slider's thumb Y offset from the start position
* @method getYValue
* @return {int} the current vertical offset
*/
getYValue: function () {
return this.thumb.getYValue();
},
/**
* Provides a way to set the value of the slider in code.
*
* @method setValue
* @param {int} newOffset the number of pixels the thumb should be
* positioned away from the initial start point
* @param {boolean} skipAnim set to true to disable the animation
* for this move action (but not others).
* @param {boolean} force ignore the locked setting and set value anyway
* @param {boolean} silent when true, do not fire events
* @return {boolean} true if the move was performed, false if it failed
*/
setValue: function() {
var args = _AS.call(arguments);
args.unshift(Slider.SOURCE_SET_VALUE);
return this._setValue.apply(this,args);
},
/**
* Worker function to execute the value set operation. Accepts type of
* set operation in addition to the usual setValue params.
*
* @method _setValue
* @param source {int} what triggered the set (e.g. Slider.SOURCE_SET_VALUE)
* @param {int} newOffset the number of pixels the thumb should be
* positioned away from the initial start point
* @param {boolean} skipAnim set to true to disable the animation
* for this move action (but not others).
* @param {boolean} force ignore the locked setting and set value anyway
* @param {boolean} silent when true, do not fire events
* @return {boolean} true if the move was performed, false if it failed
* @protected
*/
_setValue: function(source, newOffset, skipAnim, force, silent) {
var t = this.thumb, newX, newY;
if (!t.available) {
this.deferredSetValue = arguments;
return false;
}
if (this.isLocked() && !force) {
return false;
}
if ( isNaN(newOffset) ) {
return false;
}
if (t._isRegion) {
return false;
}
this._silent = silent;
this.valueChangeSource = source || Slider.SOURCE_SET_VALUE;
t.lastOffset = [newOffset, newOffset];
this.verifyOffset();
this._slideStart();
if (t._isHoriz) {
newX = t.initPageX + newOffset + this.thumbCenterPoint.x;
this.moveThumb(newX, t.initPageY, skipAnim);
} else {
newY = t.initPageY + newOffset + this.thumbCenterPoint.y;
this.moveThumb(t.initPageX, newY, skipAnim);
}
return true;
},
/**
* Provides a way to set the value of the region slider in code.
* @method setRegionValue
* @param {int} newOffset the number of pixels the thumb should be
* positioned away from the initial start point (x axis for region)
* @param {int} newOffset2 the number of pixels the thumb should be
* positioned away from the initial start point (y axis for region)
* @param {boolean} skipAnim set to true to disable the animation
* for this move action (but not others).
* @param {boolean} force ignore the locked setting and set value anyway
* @param {boolean} silent when true, do not fire events
* @return {boolean} true if the move was performed, false if it failed
*/
setRegionValue : function () {
var args = _AS.call(arguments);
args.unshift(Slider.SOURCE_SET_VALUE);
return this._setRegionValue.apply(this,args);
},
/**
* Worker function to execute the value set operation. Accepts type of
* set operation in addition to the usual setValue params.
*
* @method _setRegionValue
* @param source {int} what triggered the set (e.g. Slider.SOURCE_SET_VALUE)
* @param {int} newOffset the number of pixels the thumb should be
* positioned away from the initial start point (x axis for region)
* @param {int} newOffset2 the number of pixels the thumb should be
* positioned away from the initial start point (y axis for region)
* @param {boolean} skipAnim set to true to disable the animation
* for this move action (but not others).
* @param {boolean} force ignore the locked setting and set value anyway
* @param {boolean} silent when true, do not fire events
* @return {boolean} true if the move was performed, false if it failed
* @protected
*/
_setRegionValue: function(source, newOffset, newOffset2, skipAnim, force, silent) {
var t = this.thumb, newX, newY;
if (!t.available) {
this.deferredSetRegionValue = arguments;
return false;
}
if (this.isLocked() && !force) {
return false;
}
if ( isNaN(newOffset) ) {
return false;
}
if (!t._isRegion) {
return false;
}
this._silent = silent;
this.valueChangeSource = source || Slider.SOURCE_SET_VALUE;
t.lastOffset = [newOffset, newOffset2];
this.verifyOffset();
this._slideStart();
newX = t.initPageX + newOffset + this.thumbCenterPoint.x;
newY = t.initPageY + newOffset2 + this.thumbCenterPoint.y;
this.moveThumb(newX, newY, skipAnim);
return true;
},
/**
* Checks the background position element position. If it has moved from the
* baseline position, the constraints for the thumb are reset
* @method verifyOffset
* @return {boolean} True if the offset is the same as the baseline.
*/
verifyOffset: function() {
var xy = getXY(this.getEl()),
t = this.thumb;
if (!this.thumbCenterPoint || !this.thumbCenterPoint.x) {
this.setThumbCenterPoint();
}
if (xy) {
if (xy[0] != this.baselinePos[0] || xy[1] != this.baselinePos[1]) {
// Reset background
this.setInitPosition();
this.baselinePos = xy;
// Reset thumb
t.initPageX = this.initPageX + t.startOffset[0];
t.initPageY = this.initPageY + t.startOffset[1];
t.deltaSetXY = null;
this.resetThumbConstraints();
return false;
}
}
return true;
},
/**
* Move the associated slider moved to a timeout to try to get around the
* mousedown stealing moz does when I move the slider element between the
* cursor and the background during the mouseup event
* @method moveThumb
* @param {int} x the X coordinate of the click
* @param {int} y the Y coordinate of the click
* @param {boolean} skipAnim don't animate if the move happend onDrag
* @param {boolean} midMove set to true if this is not terminating
* the slider movement
* @private
*/
moveThumb: function(x, y, skipAnim, midMove) {
var t = this.thumb,
self = this,
p,_p,anim;
if (!t.available) {
return;
}
t.setDelta(this.thumbCenterPoint.x, this.thumbCenterPoint.y);
_p = t.getTargetCoord(x, y);
p = [Math.round(_p.x), Math.round(_p.y)];
if (this.animate && t._graduated && !skipAnim) {
this.lock();
// cache the current thumb pos
this.curCoord = getXY(this.thumb.getEl());
this.curCoord = [Math.round(this.curCoord[0]), Math.round(this.curCoord[1])];
setTimeout( function() { self.moveOneTick(p); }, this.tickPause );
} else if (this.animate && Slider.ANIM_AVAIL && !skipAnim) {
this.lock();
anim = new YAHOO.util.Motion(
t.id, { points: { to: p } },
this.animationDuration,
YAHOO.util.Easing.easeOut );
anim.onComplete.subscribe( function() {
self.unlock();
if (!self._mouseDown) {
self.endMove();
}
});
anim.animate();
} else {
t.setDragElPos(x, y);
if (!midMove && !this._mouseDown) {
this.endMove();
}
}
},
_slideStart: function() {
if (!this._sliding) {
if (!this._silent) {
this.onSlideStart();
this.fireEvent("slideStart");
}
this._sliding = true;
this.moveComplete = false; // for backward compatibility. Deprecated
}
},
_slideEnd: function() {
if (this._sliding) {
// Reset state before firing slideEnd
var silent = this._silent;
this._sliding = false;
this.moveComplete = true; // for backward compatibility. Deprecated
this._silent = false;
if (!silent) {
this.onSlideEnd();
this.fireEvent("slideEnd");
}
}
},
/**
* Move the slider one tick mark towards its final coordinate. Used
* for the animation when tick marks are defined
* @method moveOneTick
* @param {int[]} the destination coordinate
* @private
*/
moveOneTick: function(finalCoord) {
var t = this.thumb,
self = this,
nextCoord = null,
tmpX, tmpY;
if (t._isRegion) {
nextCoord = this._getNextX(this.curCoord, finalCoord);
tmpX = (nextCoord !== null) ? nextCoord[0] : this.curCoord[0];
nextCoord = this._getNextY(this.curCoord, finalCoord);
tmpY = (nextCoord !== null) ? nextCoord[1] : this.curCoord[1];
nextCoord = tmpX !== this.curCoord[0] || tmpY !== this.curCoord[1] ?
[ tmpX, tmpY ] : null;
} else if (t._isHoriz) {
nextCoord = this._getNextX(this.curCoord, finalCoord);
} else {
nextCoord = this._getNextY(this.curCoord, finalCoord);
}
if (nextCoord) {
// cache the position
this.curCoord = nextCoord;
// move to the next coord
this.thumb.alignElWithMouse(t.getEl(), nextCoord[0] + this.thumbCenterPoint.x, nextCoord[1] + this.thumbCenterPoint.y);
// check if we are in the final position, if not make a recursive call
if (!(nextCoord[0] == finalCoord[0] && nextCoord[1] == finalCoord[1])) {
setTimeout(function() { self.moveOneTick(finalCoord); },
this.tickPause);
} else {
this.unlock();
if (!this._mouseDown) {
this.endMove();
}
}
} else {
this.unlock();
if (!this._mouseDown) {
this.endMove();
}
}
},
/**
* Returns the next X tick value based on the current coord and the target coord.
* @method _getNextX
* @private
*/
_getNextX: function(curCoord, finalCoord) {
var t = this.thumb,
thresh,
tmp = [],
nextCoord = null;
if (curCoord[0] > finalCoord[0]) {
thresh = t.tickSize - this.thumbCenterPoint.x;
tmp = t.getTargetCoord( curCoord[0] - thresh, curCoord[1] );
nextCoord = [tmp.x, tmp.y];
} else if (curCoord[0] < finalCoord[0]) {
thresh = t.tickSize + this.thumbCenterPoint.x;
tmp = t.getTargetCoord( curCoord[0] + thresh, curCoord[1] );
nextCoord = [tmp.x, tmp.y];
} else {
// equal, do nothing
}
return nextCoord;
},
/**
* Returns the next Y tick value based on the current coord and the target coord.
* @method _getNextY
* @private
*/
_getNextY: function(curCoord, finalCoord) {
var t = this.thumb,
thresh,
tmp = [],
nextCoord = null;
if (curCoord[1] > finalCoord[1]) {
thresh = t.tickSize - this.thumbCenterPoint.y;
tmp = t.getTargetCoord( curCoord[0], curCoord[1] - thresh );
nextCoord = [tmp.x, tmp.y];
} else if (curCoord[1] < finalCoord[1]) {
thresh = t.tickSize + this.thumbCenterPoint.y;
tmp = t.getTargetCoord( curCoord[0], curCoord[1] + thresh );
nextCoord = [tmp.x, tmp.y];
} else {
// equal, do nothing
}
return nextCoord;
},
/**
* Resets the constraints before moving the thumb.
* @method b4MouseDown
* @private
*/
b4MouseDown: function(e) {
if (!this.backgroundEnabled) {
return false;
}
this.thumb.autoOffset();
this.baselinePos = [];
},
/**
* Handles the mousedown event for the slider background
* @method onMouseDown
* @private
*/
onMouseDown: function(e) {
if (!this.backgroundEnabled || this.isLocked()) {
return false;
}
this._mouseDown = true;
var x = Event.getPageX(e),
y = Event.getPageY(e);
this.focus();
this._slideStart();
this.moveThumb(x, y);
},
/**
* Handles the onDrag event for the slider background
* @method onDrag
* @private
*/
onDrag: function(e) {
if (this.backgroundEnabled && !this.isLocked()) {
var x = Event.getPageX(e),
y = Event.getPageY(e);
this.moveThumb(x, y, true, true);
this.fireEvents();
}
},
/**
* Fired when the slider movement ends
* @method endMove
* @private
*/
endMove: function () {
this.unlock();
this.fireEvents();
this._slideEnd();
},
/**
* Resets the X and Y contraints for the thumb. Used in lieu of the thumb
* instance's inherited resetConstraints because some logic was not
* applicable.
* @method resetThumbConstraints
* @protected
*/
resetThumbConstraints: function () {
var t = this.thumb;
t.setXConstraint(t.leftConstraint, t.rightConstraint, t.xTickSize);
t.setYConstraint(t.topConstraint, t.bottomConstraint, t.xTickSize);
},
/**
* Fires the change event if the value has been changed. Ignored if we are in
* the middle of an animation as the event will fire when the animation is
* complete
* @method fireEvents
* @param {boolean} thumbEvent set to true if this event is fired from an event
* that occurred on the thumb. If it is, the state of the
* thumb dd object should be correct. Otherwise, the event
* originated on the background, so the thumb state needs to
* be refreshed before proceeding.
* @private
*/
fireEvents: function (thumbEvent) {
var t = this.thumb, newX, newY, newVal;
if (!thumbEvent) {
t.cachePosition();
}
if (! this.isLocked()) {
if (t._isRegion) {
newX = t.getXValue();
newY = t.getYValue();
if (newX != this.previousX || newY != this.previousY) {
if (!this._silent) {
this.onChange(newX, newY);
this.fireEvent("change", { x: newX, y: newY });
}
}
this.previousX = newX;
this.previousY = newY;
} else {
newVal = t.getValue();
if (newVal != this.previousVal) {
if (!this._silent) {
this.onChange( newVal );
this.fireEvent("change", newVal);
}
}
this.previousVal = newVal;
}
}
},
/**
* Slider toString
* @method toString
* @return {string} string representation of the instance
*/
toString: function () {
return ("Slider (" + this.type +") " + this.id);
}
});
YAHOO.lang.augmentProto(Slider, YAHOO.util.EventProvider);
YAHOO.widget.Slider = Slider;
})();
/**
* A drag and drop implementation to be used as the thumb of a slider.
* @class SliderThumb
* @extends YAHOO.util.DD
* @constructor
* @param {String} id the id of the slider html element
* @param {String} sGroup the group of related DragDrop items
* @param {int} iLeft the number of pixels the element can move left
* @param {int} iRight the number of pixels the element can move right
* @param {int} iUp the number of pixels the element can move up
* @param {int} iDown the number of pixels the element can move down
* @param {int} iTickSize optional parameter for specifying that the element
* should move a certain number pixels at a time.
*/
YAHOO.widget.SliderThumb = function(id, sGroup, iLeft, iRight, iUp, iDown, iTickSize) {
if (id) {
YAHOO.widget.SliderThumb.superclass.constructor.call(this, id, sGroup);
/**
* The id of the thumbs parent HTML element (the slider background
* element).
* @property parentElId
* @type string
*/
this.parentElId = sGroup;
}
/**
* Overrides the isTarget property in YAHOO.util.DragDrop
* @property isTarget
* @private
*/
this.isTarget = false;
/**
* The tick size for this slider
* @property tickSize
* @type int
* @private
*/
this.tickSize = iTickSize;
/**
* Informs the drag and drop util that the offsets should remain when
* resetting the constraints. This preserves the slider value when
* the constraints are reset
* @property maintainOffset
* @type boolean
* @private
*/
this.maintainOffset = true;
this.initSlider(iLeft, iRight, iUp, iDown, iTickSize);
/**
* Turns off the autoscroll feature in drag and drop
* @property scroll
* @private
*/
this.scroll = false;
};
YAHOO.extend(YAHOO.widget.SliderThumb, YAHOO.util.DD, {
/**
* The (X and Y) difference between the thumb location and its parent
* (the slider background) when the control is instantiated.
* @property startOffset
* @type [int, int]
*/
startOffset: null,
/**
* Override the default setting of dragOnly to true.
* @property dragOnly
* @type boolean
* @default true
*/
dragOnly : true,
/**
* Flag used to figure out if this is a horizontal or vertical slider
* @property _isHoriz
* @type boolean
* @private
*/
_isHoriz: false,
/**
* Cache the last value so we can check for change
* @property _prevVal
* @type int
* @private
*/
_prevVal: 0,
/**
* The slider is _graduated if there is a tick interval defined
* @property _graduated
* @type boolean
* @private
*/
_graduated: false,
/**
* Returns the difference between the location of the thumb and its parent.
* @method getOffsetFromParent
* @param {[int, int]} parentPos Optionally accepts the position of the parent
* @type [int, int]
*/
getOffsetFromParent0: function(parentPos) {
var myPos = YAHOO.util.Dom.getXY(this.getEl()),
ppos = parentPos || YAHOO.util.Dom.getXY(this.parentElId);
return [ (myPos[0] - ppos[0]), (myPos[1] - ppos[1]) ];
},
getOffsetFromParent: function(parentPos) {
var el = this.getEl(), newOffset,
myPos,ppos,l,t,deltaX,deltaY,newLeft,newTop;
if (!this.deltaOffset) {
myPos = YAHOO.util.Dom.getXY(el);
ppos = parentPos || YAHOO.util.Dom.getXY(this.parentElId);
newOffset = [ (myPos[0] - ppos[0]), (myPos[1] - ppos[1]) ];
l = parseInt( YAHOO.util.Dom.getStyle(el, "left"), 10 );
t = parseInt( YAHOO.util.Dom.getStyle(el, "top" ), 10 );
deltaX = l - newOffset[0];
deltaY = t - newOffset[1];
if (isNaN(deltaX) || isNaN(deltaY)) {
} else {
this.deltaOffset = [deltaX, deltaY];
}
} else {
newLeft = parseInt( YAHOO.util.Dom.getStyle(el, "left"), 10 );
newTop = parseInt( YAHOO.util.Dom.getStyle(el, "top" ), 10 );
newOffset = [newLeft + this.deltaOffset[0], newTop + this.deltaOffset[1]];
}
return newOffset;
},
/**
* Set up the slider, must be called in the constructor of all subclasses
* @method initSlider
* @param {int} iLeft the number of pixels the element can move left
* @param {int} iRight the number of pixels the element can move right
* @param {int} iUp the number of pixels the element can move up
* @param {int} iDown the number of pixels the element can move down
* @param {int} iTickSize the width of the tick interval.
*/
initSlider: function (iLeft, iRight, iUp, iDown, iTickSize) {
this.initLeft = iLeft;
this.initRight = iRight;
this.initUp = iUp;
this.initDown = iDown;
this.setXConstraint(iLeft, iRight, iTickSize);
this.setYConstraint(iUp, iDown, iTickSize);
if (iTickSize && iTickSize > 1) {
this._graduated = true;
}
this._isHoriz = (iLeft || iRight);
this._isVert = (iUp || iDown);
this._isRegion = (this._isHoriz && this._isVert);
},
/**
* Clear's the slider's ticks
* @method clearTicks
*/
clearTicks: function () {
YAHOO.widget.SliderThumb.superclass.clearTicks.call(this);
this.tickSize = 0;
this._graduated = false;
},
/**
* Gets the current offset from the element's start position in
* pixels.
* @method getValue
* @return {int} the number of pixels (positive or negative) the
* slider has moved from the start position.
*/
getValue: function () {
return (this._isHoriz) ? this.getXValue() : this.getYValue();
},
/**
* Gets the current X offset from the element's start position in
* pixels.
* @method getXValue
* @return {int} the number of pixels (positive or negative) the
* slider has moved horizontally from the start position.
*/
getXValue: function () {
if (!this.available) {
return 0;
}
var newOffset = this.getOffsetFromParent();
if (YAHOO.lang.isNumber(newOffset[0])) {
this.lastOffset = newOffset;
return (newOffset[0] - this.startOffset[0]);
} else {
return (this.lastOffset[0] - this.startOffset[0]);
}
},
/**
* Gets the current Y offset from the element's start position in
* pixels.
* @method getYValue
* @return {int} the number of pixels (positive or negative) the
* slider has moved vertically from the start position.
*/
getYValue: function () {
if (!this.available) {
return 0;
}
var newOffset = this.getOffsetFromParent();
if (YAHOO.lang.isNumber(newOffset[1])) {
this.lastOffset = newOffset;
return (newOffset[1] - this.startOffset[1]);
} else {
return (this.lastOffset[1] - this.startOffset[1]);
}
},
/**
* Thumb toString
* @method toString
* @return {string} string representation of the instance
*/
toString: function () {
return "SliderThumb " + this.id;
},
/**
* The onchange event for the handle/thumb is delegated to the YAHOO.widget.Slider
* instance it belongs to.
* @method onChange
* @private
*/
onChange: function (x, y) {
}
});
/**
* A slider with two thumbs, one that represents the min value and
* the other the max. Actually a composition of two sliders, both with
* the same background. The constraints for each slider are adjusted
* dynamically so that the min value of the max slider is equal or greater
* to the current value of the min slider, and the max value of the min
* slider is the current value of the max slider.
* Constructor assumes both thumbs are positioned absolutely at the 0 mark on
* the background.
*
* @namespace YAHOO.widget
* @class DualSlider
* @uses YAHOO.util.EventProvider
* @constructor
* @param {Slider} minSlider The Slider instance used for the min value thumb
* @param {Slider} maxSlider The Slider instance used for the max value thumb
* @param {int} range The number of pixels the thumbs may move within
* @param {Array} initVals (optional) [min,max] Initial thumb placement
*/
(function () {
var Event = YAHOO.util.Event,
YW = YAHOO.widget;
function DualSlider(minSlider, maxSlider, range, initVals) {
var self = this,
ready = { min : false, max : false },
minThumbOnMouseDown, maxThumbOnMouseDown;
/**
* A slider instance that keeps track of the lower value of the range.
* <strong>read only</strong>
* @property minSlider
* @type Slider
*/
this.minSlider = minSlider;
/**
* A slider instance that keeps track of the upper value of the range.
* <strong>read only</strong>
* @property maxSlider
* @type Slider
*/
this.maxSlider = maxSlider;
/**
* The currently active slider (min or max). <strong>read only</strong>
* @property activeSlider
* @type Slider
*/
this.activeSlider = minSlider;
/**
* Is the DualSlider oriented horizontally or vertically?
* <strong>read only</strong>
* @property isHoriz
* @type boolean
*/
this.isHoriz = minSlider.thumb._isHoriz;
minThumbOnMouseDown = this.minSlider.thumb.onMouseDown;
maxThumbOnMouseDown = this.maxSlider.thumb.onMouseDown;
this.minSlider.thumb.onMouseDown = function() {
self.activeSlider = self.minSlider;
minThumbOnMouseDown.apply(this,arguments);
};
this.maxSlider.thumb.onMouseDown = function () {
self.activeSlider = self.maxSlider;
maxThumbOnMouseDown.apply(this,arguments);
};
this.minSlider.thumb.onAvailable = function () {
minSlider.setStartSliderState();
ready.min = true;
if (ready.max) {
self.fireEvent('ready',self);
}
};
this.maxSlider.thumb.onAvailable = function () {
maxSlider.setStartSliderState();
ready.max = true;
if (ready.min) {
self.fireEvent('ready',self);
}
};
// dispatch mousedowns to the active slider
minSlider.onMouseDown =
maxSlider.onMouseDown = function(e) {
return this.backgroundEnabled && self._handleMouseDown(e);
};
// Fix the drag behavior so that only the active slider
// follows the drag
minSlider.onDrag =
maxSlider.onDrag = function(e) {
self._handleDrag(e);
};
// Likely only the minSlider's onMouseUp will be executed, but both are
// overridden just to be safe
minSlider.onMouseUp =
maxSlider.onMouseUp = function (e) {
self._handleMouseUp(e);
};
// Replace the _bindKeyEvents for the minSlider and remove that for the
// maxSlider since they share the same bg element.
minSlider._bindKeyEvents = function () {
self._bindKeyEvents(this);
};
maxSlider._bindKeyEvents = function () {};
// The core events for each slider are handled so we can expose a single
// event for when the event happens on either slider
minSlider.subscribe("change", this._handleMinChange, minSlider, this);
minSlider.subscribe("slideStart", this._handleSlideStart, minSlider, this);
minSlider.subscribe("slideEnd", this._handleSlideEnd, minSlider, this);
maxSlider.subscribe("change", this._handleMaxChange, maxSlider, this);
maxSlider.subscribe("slideStart", this._handleSlideStart, maxSlider, this);
maxSlider.subscribe("slideEnd", this._handleSlideEnd, maxSlider, this);
/**
* Event that fires when the slider is finished setting up
* @event ready
* @param {DualSlider} dualslider the DualSlider instance
*/
this.createEvent("ready", this);
/**
* Event that fires when either the min or max value changes
* @event change
* @param {DualSlider} dualslider the DualSlider instance
*/
this.createEvent("change", this);
/**
* Event that fires when one of the thumbs begins to move
* @event slideStart
* @param {Slider} activeSlider the moving slider
*/
this.createEvent("slideStart", this);
/**
* Event that fires when one of the thumbs finishes moving
* @event slideEnd
* @param {Slider} activeSlider the moving slider
*/
this.createEvent("slideEnd", this);
// Validate initial values
initVals = YAHOO.lang.isArray(initVals) ? initVals : [0,range];
initVals[0] = Math.min(Math.max(parseInt(initVals[0],10)|0,0),range);
initVals[1] = Math.max(Math.min(parseInt(initVals[1],10)|0,range),0);
// Swap initVals if min > max
if (initVals[0] > initVals[1]) {
initVals.splice(0,2,initVals[1],initVals[0]);
}
this.minVal = initVals[0];
this.maxVal = initVals[1];
// Set values so initial assignment when the slider thumbs are ready will
// use these values
this.minSlider.setValue(this.minVal,true,true,true);
this.maxSlider.setValue(this.maxVal,true,true,true);
}
DualSlider.prototype = {
/**
* The current value of the min thumb. <strong>read only</strong>.
* @property minVal
* @type int
*/
minVal : -1,
/**
* The current value of the max thumb. <strong>read only</strong>.
* @property maxVal
* @type int
*/
maxVal : -1,
/**
* Pixel distance to maintain between thumbs.
* @property minRange
* @type int
* @default 0
*/
minRange : 0,
/**
* Executed when one of the sliders fires the slideStart event
* @method _handleSlideStart
* @private
*/
_handleSlideStart: function(data, slider) {
this.fireEvent("slideStart", slider);
},
/**
* Executed when one of the sliders fires the slideEnd event
* @method _handleSlideEnd
* @private
*/
_handleSlideEnd: function(data, slider) {
this.fireEvent("slideEnd", slider);
},
/**
* Overrides the onDrag method for both sliders
* @method _handleDrag
* @private
*/
_handleDrag: function(e) {
YW.Slider.prototype.onDrag.call(this.activeSlider, e);
},
/**
* Executed when the min slider fires the change event
* @method _handleMinChange
* @private
*/
_handleMinChange: function() {
this.activeSlider = this.minSlider;
this.updateValue();
},
/**
* Executed when the max slider fires the change event
* @method _handleMaxChange
* @private
*/
_handleMaxChange: function() {
this.activeSlider = this.maxSlider;
this.updateValue();
},
/**
* Set up the listeners for the keydown and keypress events.
*
* @method _bindKeyEvents
* @protected
*/
_bindKeyEvents : function (slider) {
Event.on(slider.id,'keydown', this._handleKeyDown, this,true);
Event.on(slider.id,'keypress',this._handleKeyPress,this,true);
},
/**
* Delegate event handling to the active Slider. See Slider.handleKeyDown.
*
* @method _handleKeyDown
* @param e {Event} the mousedown DOM event
* @protected
*/
_handleKeyDown : function (e) {
this.activeSlider.handleKeyDown.apply(this.activeSlider,arguments);
},
/**
* Delegate event handling to the active Slider. See Slider.handleKeyPress.
*
* @method _handleKeyPress
* @param e {Event} the mousedown DOM event
* @protected
*/
_handleKeyPress : function (e) {
this.activeSlider.handleKeyPress.apply(this.activeSlider,arguments);
},
/**
* Sets the min and max thumbs to new values.
* @method setValues
* @param min {int} Pixel offset to assign to the min thumb
* @param max {int} Pixel offset to assign to the max thumb
* @param skipAnim {boolean} (optional) Set to true to skip thumb animation.
* Default false
* @param force {boolean} (optional) ignore the locked setting and set
* value anyway. Default false
* @param silent {boolean} (optional) Set to true to skip firing change
* events. Default false
*/
setValues : function (min, max, skipAnim, force, silent) {
var mins = this.minSlider,
maxs = this.maxSlider,
mint = mins.thumb,
maxt = maxs.thumb,
self = this,
done = { min : false, max : false };
// Clear constraints to prevent animated thumbs from prematurely
// stopping when hitting a constraint that's moving with the other
// thumb.
if (mint._isHoriz) {
mint.setXConstraint(mint.leftConstraint,maxt.rightConstraint,mint.tickSize);
maxt.setXConstraint(mint.leftConstraint,maxt.rightConstraint,maxt.tickSize);
} else {
mint.setYConstraint(mint.topConstraint,maxt.bottomConstraint,mint.tickSize);
maxt.setYConstraint(mint.topConstraint,maxt.bottomConstraint,maxt.tickSize);
}
// Set up one-time slideEnd callbacks to call updateValue when both
// thumbs have been set
this._oneTimeCallback(mins,'slideEnd',function () {
done.min = true;
if (done.max) {
self.updateValue(silent);
// Clean the slider's slideEnd events on a timeout since this
// will be executed from inside the event's fire
setTimeout(function () {
self._cleanEvent(mins,'slideEnd');
self._cleanEvent(maxs,'slideEnd');
},0);
}
});
this._oneTimeCallback(maxs,'slideEnd',function () {
done.max = true;
if (done.min) {
self.updateValue(silent);
// Clean both sliders' slideEnd events on a timeout since this
// will be executed from inside one of the event's fire
setTimeout(function () {
self._cleanEvent(mins,'slideEnd');
self._cleanEvent(maxs,'slideEnd');
},0);
}
});
// Must emit Slider slideEnd event to propagate to updateValue
mins.setValue(min,skipAnim,force,false);
maxs.setValue(max,skipAnim,force,false);
},
/**
* Set the min thumb position to a new value.
* @method setMinValue
* @param min {int} Pixel offset for min thumb
* @param skipAnim {boolean} (optional) Set to true to skip thumb animation.
* Default false
* @param force {boolean} (optional) ignore the locked setting and set
* value anyway. Default false
* @param silent {boolean} (optional) Set to true to skip firing change
* events. Default false
*/
setMinValue : function (min, skipAnim, force, silent) {
var mins = this.minSlider,
self = this;
this.activeSlider = mins;
// Use a one-time event callback to delay the updateValue call
// until after the slide operation is done
self = this;
this._oneTimeCallback(mins,'slideEnd',function () {
self.updateValue(silent);
// Clean the slideEnd event on a timeout since this
// will be executed from inside the event's fire
setTimeout(function () { self._cleanEvent(mins,'slideEnd'); }, 0);
});
mins.setValue(min, skipAnim, force);
},
/**
* Set the max thumb position to a new value.
* @method setMaxValue
* @param max {int} Pixel offset for max thumb
* @param skipAnim {boolean} (optional) Set to true to skip thumb animation.
* Default false
* @param force {boolean} (optional) ignore the locked setting and set
* value anyway. Default false
* @param silent {boolean} (optional) Set to true to skip firing change
* events. Default false
*/
setMaxValue : function (max, skipAnim, force, silent) {
var maxs = this.maxSlider,
self = this;
this.activeSlider = maxs;
// Use a one-time event callback to delay the updateValue call
// until after the slide operation is done
this._oneTimeCallback(maxs,'slideEnd',function () {
self.updateValue(silent);
// Clean the slideEnd event on a timeout since this
// will be executed from inside the event's fire
setTimeout(function () { self._cleanEvent(maxs,'slideEnd'); }, 0);
});
maxs.setValue(max, skipAnim, force);
},
/**
* Executed when one of the sliders is moved
* @method updateValue
* @param silent {boolean} (optional) Set to true to skip firing change
* events. Default false
* @private
*/
updateValue: function(silent) {
var min = this.minSlider.getValue(),
max = this.maxSlider.getValue(),
changed = false,
mint,maxt,dim,minConstraint,maxConstraint,thumbInnerWidth;
if (min != this.minVal || max != this.maxVal) {
changed = true;
mint = this.minSlider.thumb;
maxt = this.maxSlider.thumb;
dim = this.isHoriz ? 'x' : 'y';
thumbInnerWidth = this.minSlider.thumbCenterPoint[dim] +
this.maxSlider.thumbCenterPoint[dim];
// Establish barriers within the respective other thumb's edge, less
// the minRange. Limit to the Slider's range in the case of
// negative minRanges.
minConstraint = Math.max(max-thumbInnerWidth-this.minRange,0);
maxConstraint = Math.min(-min-thumbInnerWidth-this.minRange,0);
if (this.isHoriz) {
minConstraint = Math.min(minConstraint,maxt.rightConstraint);
mint.setXConstraint(mint.leftConstraint,minConstraint, mint.tickSize);
maxt.setXConstraint(maxConstraint,maxt.rightConstraint, maxt.tickSize);
} else {
minConstraint = Math.min(minConstraint,maxt.bottomConstraint);
mint.setYConstraint(mint.leftConstraint,minConstraint, mint.tickSize);
maxt.setYConstraint(maxConstraint,maxt.bottomConstraint, maxt.tickSize);
}
}
this.minVal = min;
this.maxVal = max;
if (changed && !silent) {
this.fireEvent("change", this);
}
},
/**
* A background click will move the slider thumb nearest to the click.
* Override if you need different behavior.
* @method selectActiveSlider
* @param e {Event} the mousedown event
* @private
*/
selectActiveSlider: function(e) {
var min = this.minSlider,
max = this.maxSlider,
minLocked = min.isLocked() || !min.backgroundEnabled,
maxLocked = max.isLocked() || !min.backgroundEnabled,
Ev = YAHOO.util.Event,
d;
if (minLocked || maxLocked) {
this.activeSlider = minLocked ? max : min;
} else {
if (this.isHoriz) {
d = Ev.getPageX(e)-min.thumb.initPageX-min.thumbCenterPoint.x;
} else {
d = Ev.getPageY(e)-min.thumb.initPageY-min.thumbCenterPoint.y;
}
this.activeSlider = d*2 > max.getValue()+min.getValue() ? max : min;
}
},
/**
* Delegates the onMouseDown to the appropriate Slider
*
* @method _handleMouseDown
* @param e {Event} mouseup event
* @protected
*/
_handleMouseDown: function(e) {
if (!e._handled && !this.minSlider._sliding && !this.maxSlider._sliding) {
e._handled = true;
this.selectActiveSlider(e);
return YW.Slider.prototype.onMouseDown.call(this.activeSlider, e);
} else {
return false;
}
},
/**
* Delegates the onMouseUp to the active Slider
*
* @method _handleMouseUp
* @param e {Event} mouseup event
* @protected
*/
_handleMouseUp : function (e) {
YW.Slider.prototype.onMouseUp.apply(
this.activeSlider, arguments);
},
/**
* Schedule an event callback that will execute once, then unsubscribe
* itself.
* @method _oneTimeCallback
* @param o {EventProvider} Object to attach the event to
* @param evt {string} Name of the event
* @param fn {Function} function to execute once
* @private
*/
_oneTimeCallback : function (o,evt,fn) {
var sub = function () {
// Unsubscribe myself
o.unsubscribe(evt, sub);
// Pass the event handler arguments to the one time callback
fn.apply({},arguments);
};
o.subscribe(evt,sub);
},
/**
* Clean up the slideEnd event subscribers array, since each one-time
* callback will be replaced in the event's subscribers property with
* null. This will cause memory bloat and loss of performance.
* @method _cleanEvent
* @param o {EventProvider} object housing the CustomEvent
* @param evt {string} name of the CustomEvent
* @private
*/
_cleanEvent : function (o,evt) {
var ce,i,len,j,subs,newSubs;
if (o.__yui_events && o.events[evt]) {
for (i = o.__yui_events.length; i >= 0; --i) {
if (o.__yui_events[i].type === evt) {
ce = o.__yui_events[i];
break;
}
}
if (ce) {
subs = ce.subscribers;
newSubs = [];
j = 0;
for (i = 0, len = subs.length; i < len; ++i) {
if (subs[i]) {
newSubs[j++] = subs[i];
}
}
ce.subscribers = newSubs;
}
}
}
};
YAHOO.lang.augmentProto(DualSlider, YAHOO.util.EventProvider);
/**
* Factory method for creating a horizontal dual-thumb slider
* @for YAHOO.widget.Slider
* @method YAHOO.widget.Slider.getHorizDualSlider
* @static
* @param {String} bg the id of the slider's background element
* @param {String} minthumb the id of the min thumb
* @param {String} maxthumb the id of the thumb thumb
* @param {int} range the number of pixels the thumbs can move within
* @param {int} iTickSize (optional) the element should move this many pixels
* at a time
* @param {Array} initVals (optional) [min,max] Initial thumb placement
* @return {DualSlider} a horizontal dual-thumb slider control
*/
YW.Slider.getHorizDualSlider =
function (bg, minthumb, maxthumb, range, iTickSize, initVals) {
var mint = new YW.SliderThumb(minthumb, bg, 0, range, 0, 0, iTickSize),
maxt = new YW.SliderThumb(maxthumb, bg, 0, range, 0, 0, iTickSize);
return new DualSlider(
new YW.Slider(bg, bg, mint, "horiz"),
new YW.Slider(bg, bg, maxt, "horiz"),
range, initVals);
};
/**
* Factory method for creating a vertical dual-thumb slider.
* @for YAHOO.widget.Slider
* @method YAHOO.widget.Slider.getVertDualSlider
* @static
* @param {String} bg the id of the slider's background element
* @param {String} minthumb the id of the min thumb
* @param {String} maxthumb the id of the thumb thumb
* @param {int} range the number of pixels the thumbs can move within
* @param {int} iTickSize (optional) the element should move this many pixels
* at a time
* @param {Array} initVals (optional) [min,max] Initial thumb placement
* @return {DualSlider} a vertical dual-thumb slider control
*/
YW.Slider.getVertDualSlider =
function (bg, minthumb, maxthumb, range, iTickSize, initVals) {
var mint = new YW.SliderThumb(minthumb, bg, 0, 0, 0, range, iTickSize),
maxt = new YW.SliderThumb(maxthumb, bg, 0, 0, 0, range, iTickSize);
return new YW.DualSlider(
new YW.Slider(bg, bg, mint, "vert"),
new YW.Slider(bg, bg, maxt, "vert"),
range, initVals);
};
YAHOO.widget.DualSlider = DualSlider;
})();
YAHOO.register("slider", YAHOO.widget.Slider, {version: "2.9.0", build: "2800"});