@ray-js/lamp-saturation-slider 1.1.1 → 1.1.4-beta-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,837 +0,0 @@
1
- // @ts-check
2
-
3
- // props
4
- const THUMBS = ['start', 'end'];
5
-
6
- const isNumber = n => /\d+/.test(n);
7
- const getNumber = (n, def) => (isNumber(n) ? n : def);
8
-
9
- // state by instanceId in sjs singleton instance
10
- const getOst = (ownerInstance, instanceId) => {
11
- const ost = ownerInstance.getState() || {};
12
- return ost[instanceId] || {};
13
- };
14
- // set instance state
15
- const setOstValue = (ownerInstance, instanceId, key, value) => {
16
- const ost = ownerInstance.getState() || {};
17
- const state = ost[instanceId] || {};
18
- state[key] = value;
19
- ost[instanceId] = state;
20
- };
21
-
22
- // watch props
23
- const createPropObserver = prop => instanceId =>
24
- function (newValue, oldValue, ownerInstance) {
25
- const ost = getOst(ownerInstance, instanceId);
26
- const isUpdate = oldValue !== undefined;
27
- if (newValue === oldValue) return;
28
-
29
- if (prop === 'start') {
30
- setStart(ownerInstance, instanceId, getNumber(newValue, 0));
31
- }
32
- if (prop === 'watchstart' && isUpdate) {
33
- setStart(ownerInstance, instanceId, getNumber(newValue, 0));
34
- }
35
- if (prop === 'end') {
36
- setEnd(ownerInstance, instanceId, getNumber(newValue, 30));
37
- updateThumbBg({
38
- ownerInstance,
39
- instanceId,
40
- });
41
- }
42
- if (prop === 'watchend' && isUpdate) {
43
- setEnd(ownerInstance, instanceId, getNumber(newValue, 30));
44
- }
45
- setOstValue(ownerInstance, instanceId, '_startValueInPixels', null);
46
- setOstValue(ownerInstance, instanceId, '_endValueInPixels', null);
47
-
48
- setOstValue(ownerInstance, instanceId, 'rendered', false);
49
- if (!ost.rendered) {
50
- updateSlider(ownerInstance, instanceId);
51
- setOstValue(ownerInstance, instanceId, 'rendered', true);
52
- }
53
- };
54
-
55
- const getMin = (ownerInstance, instanceId) => {
56
- return getNumber(getProps(ownerInstance, instanceId).min, 0);
57
- };
58
-
59
- const getMax = (ownerInstance, instanceId) => {
60
- const max = getNumber(getProps(ownerInstance, instanceId).max, 100);
61
- const min = getMin(ownerInstance, instanceId);
62
- if (max > min) return max;
63
- return min + 100;
64
- };
65
-
66
- const getStep = (ownerInstance, instanceId) => {
67
- return getNumber(getProps(ownerInstance, instanceId).step, 1);
68
- };
69
- const getThumbStyleCalc = (ownerInstance, instanceId) => {
70
- return getProps(ownerInstance, instanceId).thumbstylecalc;
71
- };
72
-
73
- const getStart = (ownerInstance, instanceId) => {
74
- const ost = getOst(ownerInstance, instanceId);
75
- return ost._start;
76
- };
77
- const setStart = (ownerInstance, instanceId, value) => {
78
- setOstValue(ownerInstance, instanceId, '_start', setBoundries(ownerInstance, instanceId, value));
79
- };
80
- const getEnd = (ownerInstance, instanceId) => {
81
- const ost = getOst(ownerInstance, instanceId);
82
- return ost._end;
83
- };
84
- const setEnd = (ownerInstance, instanceId, value) => {
85
- setOstValue(ownerInstance, instanceId, '_end', setBoundries(ownerInstance, instanceId, value));
86
- };
87
- const getRangeValue = (ownerInstance, instanceId) => {
88
- return Math.abs(getEnd(ownerInstance, instanceId) - getStart(ownerInstance, instanceId));
89
- };
90
-
91
- const getProps = (ownerInstance, instanceId) => {
92
- const slider = queryComponent(ownerInstance, instanceId);
93
- return slider ? slider.getDataset() : {};
94
- };
95
-
96
- const isRangeMode = (ownerInstance, instanceId) => {
97
- const props = getProps(ownerInstance, instanceId);
98
- const isRangeMode = props.mode === 'range';
99
- return isRangeMode;
100
- };
101
-
102
- const isHideThumb = (ownerInstance, instanceId) => {
103
- const props = getProps(ownerInstance, instanceId);
104
- return !!props.hidethumb;
105
- };
106
-
107
- const isShowSteps = (ownerInstance, instanceId) => {
108
- const props = getProps(ownerInstance, instanceId);
109
- return !!props.showsteps;
110
- };
111
-
112
- const isVerticalMode = (ownerInstance, instanceId) => {
113
- const props = getProps(ownerInstance, instanceId);
114
- return props.direction === 'vertical';
115
- };
116
-
117
- const isEnableTouch = (ownerInstance, instanceId) => {
118
- const props = getProps(ownerInstance, instanceId);
119
- if (isHideThumb(ownerInstance, instanceId)) {
120
- return true;
121
- }
122
- return !!props.enabletouch;
123
- };
124
-
125
- const isEnableTouchBar = (ownerInstance, instanceId) => {
126
- const props = getProps(ownerInstance, instanceId);
127
- return !!props.enabletouchbar;
128
- };
129
-
130
- const isReverseMode = (ownerInstance, instanceId) => {
131
- const props = getProps(ownerInstance, instanceId);
132
- return !!props.reverse && !isRangeMode(ownerInstance, instanceId);
133
- };
134
-
135
- const queryComponent = (ownerInstance, instanceId, selector) => {
136
- const root = ownerInstance.selectComponent(`#${instanceId}`);
137
- return selector ? root.selectComponent(selector) : root;
138
- };
139
- const queryComponentAll = (ownerInstance, instanceId, selector) => {
140
- const root = queryComponent(ownerInstance, instanceId);
141
- return selector ? root.selectAllComponents(selector) : [];
142
- };
143
-
144
- const getThumbWidth = (ownerInstance, instanceId, type = 'width') => {
145
- const thumbStart = queryComponent(ownerInstance, instanceId, '.rayui-slider-thumb-start');
146
- const thumbStartWidth = thumbStart.getBoundingClientRect()[type];
147
- if (thumbStartWidth) {
148
- return thumbStartWidth;
149
- }
150
- const thumbEnd = queryComponent(ownerInstance, instanceId, '.rayui-slider-thumb-end');
151
- const thumbEndWidth = thumbEnd.getBoundingClientRect()[type];
152
- if (thumbEndWidth) {
153
- return thumbEndWidth;
154
- }
155
- };
156
-
157
- function updateSlider(ownerInstance, instanceId) {
158
- const ost = getOst(ownerInstance, instanceId);
159
-
160
- const ost_slider = queryComponent(ownerInstance, instanceId);
161
- setOstValue(ownerInstance, instanceId, 'slider', ost_slider);
162
-
163
- const ost_sliderRange = queryComponent(ownerInstance, instanceId, '.rayui-slider-bar');
164
- setOstValue(ownerInstance, instanceId, 'sliderRange', ost_sliderRange);
165
-
166
- if (ost_slider && ost_sliderRange) {
167
- const isRange = isRangeMode(ownerInstance, instanceId);
168
- const hideThumb = isHideThumb(ownerInstance, instanceId);
169
- const isVertical = isVerticalMode(ownerInstance, instanceId);
170
- const isRerverse = isReverseMode(ownerInstance, instanceId);
171
- const enableTouchBar = isEnableTouchBar(ownerInstance, instanceId);
172
-
173
- const thumbWidth = isVertical
174
- ? getThumbWidth(ownerInstance, instanceId, 'height')
175
- : getThumbWidth(ownerInstance, instanceId, 'width');
176
-
177
- const ost_thumbWidth = hideThumb ? 0 : thumbWidth;
178
- setOstValue(ownerInstance, instanceId, 'thumbWidth', ost_thumbWidth);
179
-
180
- const props = getProps(ownerInstance, instanceId);
181
-
182
- let ost_maxRange =
183
- (isVertical
184
- ? ost_slider.getBoundingClientRect().height
185
- : ost_slider.getBoundingClientRect().width) - ost_thumbWidth;
186
- if (!isRerverse && !isRange && !isVertical) {
187
- ost_maxRange -= ost_thumbWidth;
188
- }
189
- if (isVertical && isRerverse) {
190
- ost_maxRange -= ost_thumbWidth;
191
- }
192
-
193
- ost_maxRange += getNumber(props.maxrangeoffset, 0);
194
- setOstValue(ownerInstance, instanceId, 'maxRange', ost_maxRange);
195
-
196
- // 如果是单向,start就是 min
197
- const _startValueInPixels = isRange
198
- ? convertValueToPixels(ownerInstance, instanceId, getStart(ownerInstance, instanceId))
199
- : 0;
200
-
201
- setOstValue(ownerInstance, instanceId, '_startValueInPixels', _startValueInPixels);
202
-
203
- const _endValueInPixels = convertValueToPixels(
204
- ownerInstance,
205
- instanceId,
206
- getEnd(ownerInstance, instanceId)
207
- );
208
-
209
- setOstValue(ownerInstance, instanceId, '_endValueInPixels', _endValueInPixels);
210
-
211
- const reverseRange = isRerverse
212
- ? getMax(ownerInstance, instanceId) -
213
- getMin(ownerInstance, instanceId) -
214
- getRangeValue(ownerInstance, instanceId)
215
- : 0;
216
-
217
- setThumb(ownerInstance, instanceId, 'start', _startValueInPixels, {
218
- display: hideThumb ? 'none' : isRange ? 'block' : 'none', // 单向隐藏左按钮
219
- });
220
-
221
- const endValue = isRerverse
222
- ? convertValueToPixels(ownerInstance, instanceId, reverseRange)
223
- : _endValueInPixels;
224
- setThumb(ownerInstance, instanceId, 'end', endValue, {
225
- display: hideThumb ? 'none' : 'block', // 单向隐藏左按钮
226
- });
227
- setText(
228
- ownerInstance,
229
- instanceId,
230
- convertPixelsToValue(ownerInstance, instanceId, endValue, getStep(ownerInstance, instanceId))
231
- );
232
-
233
- setRange(
234
- ownerInstance,
235
- instanceId,
236
- _startValueInPixels,
237
- isRerverse ? convertValueToPixels(ownerInstance, instanceId, reverseRange) : _endValueInPixels
238
- );
239
-
240
- const showSteps = isShowSteps(ownerInstance, instanceId);
241
- if (showSteps) {
242
- const steps = queryComponent(ownerInstance, instanceId, '.rayui-slider-steps');
243
- if (steps) {
244
- steps.setStyle({
245
- padding: isVertical ? `${ost_thumbWidth / 2}px 0` : `0 ${ost_thumbWidth / 2}px`,
246
- });
247
- }
248
- setBarStepsWrap(ownerInstance, instanceId);
249
- }
250
-
251
- if (hideThumb || enableTouchBar) {
252
- const trackCurrentX =
253
- ost.trackCurrentX ||
254
- convertValueToPixels(
255
- ownerInstance,
256
- instanceId,
257
- isRerverse
258
- ? getMax(ownerInstance, instanceId) -
259
- getMin(ownerInstance, instanceId) -
260
- getEnd(ownerInstance, instanceId)
261
- : getEnd(ownerInstance, instanceId)
262
- );
263
- setOstValue(ownerInstance, instanceId, 'trackCurrentX', trackCurrentX);
264
- }
265
-
266
- const trackWidth = isVertical
267
- ? ost_slider.getBoundingClientRect().height
268
- : ost_slider.getBoundingClientRect().width;
269
-
270
- setOstValue(ownerInstance, instanceId, 'trackWidth', trackWidth);
271
- }
272
- }
273
-
274
- function renderTemplate(template, data) {
275
- return template.replace(/{{[\s\S]*?}}/g, slot => {
276
- const code = slot.replace(/{{|}}/g, '');
277
- let codeStr = code;
278
- for (const key in data) {
279
- codeStr = codeStr.replace(key, data[key]);
280
- }
281
- return eval(codeStr);
282
- });
283
- }
284
-
285
- function setText(ownerInstance, instanceId, text) {
286
- const props = getProps(ownerInstance, instanceId);
287
- if (props.showtext) {
288
- const content = props.texttemplate ? renderTemplate(props.texttemplate, { text }) : text;
289
- ownerInstance.callMethod('setText', { instanceId, content });
290
- }
291
- }
292
-
293
- /**
294
- * 百分比值转换滑动位置
295
- */
296
- function convertValueToPixels(ownerInstance, instanceId, value) {
297
- const ost = getOst(ownerInstance, instanceId);
298
- return parseFloat(
299
- (
300
- (value / (getMax(ownerInstance, instanceId) - getMin(ownerInstance, instanceId))) *
301
- ost.maxRange
302
- ).toFixed(2)
303
- );
304
- }
305
-
306
- /**
307
- * 滑动位置转换百分比值
308
- */
309
- function convertPixelsToValue(ownerInstance, instanceId, value, step = 1) {
310
- const ost = getOst(ownerInstance, instanceId);
311
-
312
- let _value = parseFloat(
313
- String(
314
- (value / ost.maxRange) *
315
- (getMax(ownerInstance, instanceId) - getMin(ownerInstance, instanceId))
316
- )
317
- );
318
- // round to step value
319
- _value = step > 0 ? Math.round(_value / step) * step : _value;
320
- return parseFloat(_value.toFixed(2));
321
- }
322
-
323
- function compileCalcStyle(ownerInstance, instanceId, valueInPixels) {
324
- const calcMap = getThumbStyleCalc(ownerInstance, instanceId);
325
- const style = {};
326
- if (calcMap) {
327
- for (const key in calcMap) {
328
- const str = calcMap[key];
329
- const code = renderTemplate(str, {
330
- text: convertPixelsToValue(
331
- ownerInstance,
332
- instanceId,
333
- valueInPixels,
334
- getStep(ownerInstance, instanceId)
335
- ),
336
- });
337
- style[key] = code;
338
- }
339
- }
340
- return style;
341
- }
342
-
343
- function setThumb(ownerInstance, instanceId, thumbName, valueInPixels, style) {
344
- const isVertical = isVerticalMode(ownerInstance, instanceId);
345
-
346
- const thumbs = queryComponentAll(ownerInstance, instanceId, '.rayui-slider-thumb');
347
- const renderThumbs = queryComponentAll(ownerInstance, instanceId, '.rayui-slider-thumb-render');
348
-
349
- const calcStyle = compileCalcStyle(ownerInstance, instanceId, valueInPixels);
350
- thumbs.forEach(thumb => {
351
- if (thumb.getDataset().name === thumbName) {
352
- if (isVertical) {
353
- thumb.setStyle({
354
- ...(style || {}),
355
- top: `${valueInPixels}px`,
356
- });
357
- } else {
358
- thumb.setStyle({
359
- ...(style || {}),
360
- left: `${valueInPixels}px`,
361
- });
362
- }
363
- }
364
- });
365
- renderThumbs.forEach(thumb => {
366
- if (thumb.getDataset().name === thumbName) {
367
- thumb.setStyle({
368
- ...(calcStyle || {}),
369
- });
370
- }
371
- });
372
- }
373
-
374
- function updateThumbZIndex(ownerInstance, instanceId) {
375
- const isRange = isRangeMode(ownerInstance, instanceId);
376
- if (!isRange) return;
377
- const start = getStart(ownerInstance, instanceId);
378
- const end = getEnd(ownerInstance, instanceId);
379
- const min = getMin(ownerInstance, instanceId);
380
- const max = getMax(ownerInstance, instanceId);
381
-
382
- const mid = Math.floor((max - min) / 2);
383
-
384
- // 左右重叠,右在上
385
- if (end < mid) {
386
- const start = queryComponent(ownerInstance, instanceId, '.rayui-slider-thumb-start');
387
- const end = queryComponent(ownerInstance, instanceId, '.rayui-slider-thumb-end');
388
-
389
- if (start && end) {
390
- start.setStyle({
391
- zIndex: 10,
392
- });
393
- end.setStyle({
394
- zIndex: 11,
395
- });
396
- }
397
- }
398
- // 左右重叠,左在上
399
- if (start > mid) {
400
- const start = queryComponent(ownerInstance, instanceId, '.rayui-slider-thumb-start');
401
- const end = queryComponent(ownerInstance, instanceId, '.rayui-slider-thumb-end');
402
-
403
- if (start && end) {
404
- start.setStyle({
405
- zIndex: 11,
406
- });
407
- end.setStyle({
408
- zIndex: 10,
409
- });
410
- }
411
- }
412
- }
413
-
414
- function setBarStepsWrap(ownerInstance, instanceId) {
415
- const ost_slider = queryComponent(ownerInstance, instanceId);
416
- const isVertical = isVerticalMode(ownerInstance, instanceId);
417
- const wrap = queryComponent(ownerInstance, instanceId, '.rayui-slider-bar-steps_wrap');
418
-
419
- let length = 0;
420
- if (isVertical) {
421
- length = ost_slider.getBoundingClientRect().height;
422
- } else {
423
- length = ost_slider.getBoundingClientRect().width;
424
- }
425
-
426
- wrap.setStyle({
427
- [isVertical ? 'height' : 'width']: `${length}px`,
428
- });
429
- }
430
-
431
- function setRange(ownerInstance, instanceId, start, end) {
432
- const ost = getOst(ownerInstance, instanceId);
433
- const isVertical = isVerticalMode(ownerInstance, instanceId);
434
- const isReverse = isReverseMode(ownerInstance, instanceId);
435
-
436
- const maxThumb = Math.max(start, end);
437
- const minThumb = Math.min(start, end);
438
- const width = Math.abs(maxThumb - minThumb);
439
- const thumbWidth = ost.thumbWidth;
440
-
441
- const props = getProps(ownerInstance, instanceId);
442
-
443
- const barPad = props.barpad;
444
-
445
- let rangeWidth = width + thumbWidth + barPad;
446
- if (isReverse) {
447
- rangeWidth = ost.trackWidth - rangeWidth;
448
- }
449
-
450
- ost.sliderRange.setStyle({
451
- [isVertical ? (isReverse ? 'bottom' : 'top') : isReverse ? 'right' : 'left']: `${
452
- minThumb - barPad / 2
453
- }px`,
454
- [isVertical ? 'height' : 'width']: `${rangeWidth}px`,
455
- });
456
- }
457
-
458
- function setBoundries(ownerInstance, instanceId, value) {
459
- const isReverse = isReverseMode(ownerInstance, instanceId);
460
- let _value = typeof value === 'number' ? value : parseFloat(value);
461
-
462
- _value = _value < 0 ? 0 : value; // MIN
463
-
464
- return _value > getMax(ownerInstance, instanceId) ? getMax(ownerInstance, instanceId) : _value; // MAX
465
- }
466
-
467
- const handleMouseDown = instanceId => (event, ownerInstance) => {
468
- const target = event.instance;
469
- const thumbId = target.getDataset().name;
470
- const isVertical = isVerticalMode(ownerInstance, instanceId);
471
- onStart(event, ownerInstance, instanceId);
472
-
473
- const ost = getOst(ownerInstance, instanceId);
474
-
475
- // allow move
476
- if (THUMBS.includes(thumbId)) {
477
- setOstValue(ownerInstance, instanceId, 'currentThumbName', thumbId);
478
- setOstValue(ownerInstance, instanceId, 'currentThumb', target);
479
- const startX = isVertical ? event.touches[0].pageY : event.touches[0].pageX;
480
-
481
- const currentThumbPositionX =
482
- startX -
483
- (isVertical
484
- ? ost.currentThumb.getBoundingClientRect().top
485
- : ost.currentThumb.getBoundingClientRect().left);
486
-
487
- setOstValue(ownerInstance, instanceId, 'currentThumbPositionX', currentThumbPositionX);
488
-
489
- toggleActiveThumb(ownerInstance, instanceId, true);
490
-
491
- setOstValue(ownerInstance, instanceId, 'isMoving', true);
492
- } else {
493
- return false;
494
- }
495
- };
496
-
497
- const handleTrackMouseDown = instanceId => (event, ownerInstance) => {
498
- const isRange = isRangeMode(ownerInstance, instanceId);
499
- const hideThumb = isHideThumb(ownerInstance, instanceId);
500
- const enableTouchBar = isEnableTouchBar(ownerInstance, instanceId);
501
-
502
- const ost = getOst(ownerInstance, instanceId);
503
- const isVertical = isVerticalMode(ownerInstance, instanceId);
504
- const startX = isVertical ? event.touches[0].clientY : event.touches[0].pageX;
505
-
506
- const trackStartX =
507
- startX -
508
- (isVertical ? ost.slider.getBoundingClientRect().top : ost.slider.getBoundingClientRect().left);
509
- setOstValue(ownerInstance, instanceId, 'trackStartX', trackStartX);
510
-
511
- onStart(event, ownerInstance, instanceId);
512
- setOstValue(ownerInstance, instanceId, 'currentThumbName', 'end');
513
-
514
- if (!isRange && !hideThumb && isEnableTouch(ownerInstance, instanceId) && !enableTouchBar) {
515
- const thumbEnd = queryComponent(ownerInstance, instanceId, '.rayui-slider-thumb-end');
516
-
517
- setOstValue(ownerInstance, instanceId, 'currentThumb', thumbEnd);
518
-
519
- const offset = isVertical
520
- ? thumbEnd.getBoundingClientRect().height / 2
521
- : thumbEnd.getBoundingClientRect().width / 2;
522
-
523
- if (thumbEnd) {
524
- let moveX = ost.trackStartX - offset;
525
-
526
- let moveValue = convertPixelsToValue(
527
- ownerInstance,
528
- instanceId,
529
- moveX,
530
- getStep(ownerInstance, instanceId)
531
- );
532
- // lock the thumb within the bounaries
533
- moveValue = setBoundries(ownerInstance, instanceId, moveValue);
534
-
535
- setOstValue(ownerInstance, instanceId, '_start', 0);
536
- setOstValue(ownerInstance, instanceId, '_end', moveValue);
537
-
538
- moveX = convertValueToPixels(ownerInstance, instanceId, moveValue);
539
-
540
- onChange(ownerInstance, instanceId, 'end');
541
- setThumb(ownerInstance, instanceId, 'end', moveX);
542
- setRange(ownerInstance, instanceId, 0, moveX);
543
- setOstValue(ownerInstance, instanceId, 'isMoving', true);
544
-
545
- const currentThumbPositionX =
546
- startX -
547
- (isVertical ? thumbEnd.getBoundingClientRect().top : thumbEnd.getBoundingClientRect().left);
548
- setOstValue(ownerInstance, instanceId, 'currentThumbPositionX', currentThumbPositionX);
549
- }
550
- }
551
- };
552
-
553
- function toggleActiveThumb(ownerInstance, instanceId, toggle = true) {
554
- const ost = getOst(ownerInstance, instanceId);
555
- }
556
-
557
- const onMouseMove = instanceId => (event, ownerInstance) => {
558
- const isVertical = isVerticalMode(ownerInstance, instanceId);
559
- const isReverse = isReverseMode(ownerInstance, instanceId);
560
- const ost = getOst(ownerInstance, instanceId);
561
- const hideThumb = isHideThumb(ownerInstance, instanceId);
562
- const enableTouchBar = isEnableTouchBar(ownerInstance, instanceId);
563
- // track mouse mouve only when toggle true
564
- if (ost.isMoving) {
565
- const currentX = isVertical ? event.touches[0].pageY : event.touches[0].pageX;
566
- let moveX =
567
- currentX -
568
- ost.currentThumbPositionX -
569
- (isVertical
570
- ? ost.slider.getBoundingClientRect().top
571
- : ost.slider.getBoundingClientRect().left);
572
-
573
- let moveValue = convertPixelsToValue(
574
- ownerInstance,
575
- instanceId,
576
- moveX,
577
- getStep(ownerInstance, instanceId)
578
- );
579
- // lock the thumb within the bounaries
580
- moveValue = setBoundries(ownerInstance, instanceId, moveValue);
581
- moveX = convertValueToPixels(ownerInstance, instanceId, moveValue);
582
-
583
- const props = getProps(ownerInstance, instanceId);
584
-
585
- switch (ost.currentThumbName) {
586
- case 'start':
587
- const _endValueInPixels = ost._endValueInPixels;
588
- // if (moveX > _endValueInPixels) return false;
589
- if (props.startmin && moveValue < props.startmin) return false;
590
- if (props.startmax && moveValue > props.startmax) return false;
591
- setOstValue(ownerInstance, instanceId, '_startValueInPixels', moveX);
592
- setOstValue(ownerInstance, instanceId, '_start', moveValue);
593
- onChange(ownerInstance, instanceId, 'start');
594
- break;
595
- case 'end':
596
- const _startValueInPixels = ost._startValueInPixels;
597
- // if (moveX < _startValueInPixels) return false;
598
- if (props.endmin && moveValue < props.endmin) return false;
599
- if (props.endmax && moveValue > props.endmax) return false;
600
- setOstValue(ownerInstance, instanceId, '_endValueInPixels', moveX);
601
- setOstValue(ownerInstance, instanceId, '_end', moveValue);
602
- onChange(ownerInstance, instanceId, 'end');
603
- break;
604
- }
605
- const _ost_ = getOst(ownerInstance, instanceId);
606
- setThumb(ownerInstance, instanceId, _ost_.currentThumbName, moveX);
607
- setRange(ownerInstance, instanceId, _ost_._endValueInPixels, _ost_._startValueInPixels);
608
- } else if (hideThumb || enableTouchBar) {
609
- // hideThumb mode
610
- const currentX = isVertical
611
- ? event.touches[0].clientY - ost.slider.getBoundingClientRect().top
612
- : event.touches[0].pageX;
613
-
614
- const delta = currentX - ost.trackStartX;
615
- const nextX = ost.trackCurrentX + delta;
616
-
617
- const trackNextX = nextX;
618
- setOstValue(ownerInstance, instanceId, 'trackNextX', trackNextX);
619
- if (trackNextX < 0) {
620
- setOstValue(ownerInstance, instanceId, 'trackNextX', 0);
621
- }
622
- if (trackNextX > ost.trackWidth) {
623
- setOstValue(ownerInstance, instanceId, 'trackNextX', ost.trackWidth);
624
- }
625
-
626
- let moveX = ost.trackNextX;
627
-
628
- let moveValue = convertPixelsToValue(
629
- ownerInstance,
630
- instanceId,
631
- moveX,
632
- getStep(ownerInstance, instanceId)
633
- );
634
- // lock the thumb within the bounaries
635
- moveValue = setBoundries(ownerInstance, instanceId, moveValue);
636
-
637
- setOstValue(ownerInstance, instanceId, '_start', 0);
638
- setOstValue(ownerInstance, instanceId, '_end', moveValue);
639
-
640
- moveX = convertValueToPixels(ownerInstance, instanceId, moveValue);
641
-
642
- setThumb(ownerInstance, instanceId, ost.currentThumbName, moveX);
643
- setRange(ownerInstance, instanceId, 0, moveX);
644
- onChange(ownerInstance, instanceId, 'end');
645
-
646
- return false;
647
- }
648
- };
649
-
650
- const limit = (number, min, max) => Math.min(max, Math.max(min, number));
651
-
652
- const hsvToRgb = (h = 0, s = 0, v = 0, a = 1) => {
653
- const hsb = [h, s, v].map((bit, i) => {
654
- let _bit = bit;
655
- if (_bit) _bit = parseFloat(_bit.toString());
656
- if (i === 0) {
657
- _bit %= 360;
658
- const res = _bit < 0 ? _bit + 360 : _bit;
659
- return res;
660
- }
661
- return limit(Math.round(bit), 0, 100);
662
- });
663
-
664
- const br = Math.round((hsb[2] / 100) * 255);
665
- if (hsb[1] === 0) return [br, br, br];
666
-
667
- const hue = hsb[0];
668
- const f = hue % 60;
669
- const p = Math.round(((hsb[2] * (100 - hsb[1])) / 10000) * 255);
670
- const q = Math.round(((hsb[2] * (6000 - hsb[1] * f)) / 600000) * 255);
671
- const t = Math.round(((hsb[2] * (6000 - hsb[1] * (60 - f))) / 600000) * 255);
672
-
673
- let rgb;
674
- switch (Math.floor(hue / 60)) {
675
- case 0:
676
- rgb = [br, t, p];
677
- break;
678
- case 1:
679
- rgb = [q, br, p];
680
- break;
681
- case 2:
682
- rgb = [p, br, t];
683
- break;
684
- case 3:
685
- rgb = [p, q, br];
686
- break;
687
- case 4:
688
- rgb = [t, p, br];
689
- break;
690
- default:
691
- rgb = [br, p, q];
692
- break;
693
- }
694
- if (a !== undefined) {
695
- rgb.push(limit(Number(a), 0, 1));
696
- }
697
- return rgb;
698
- };
699
-
700
- const updateThumbBg = ({ ownerInstance, instanceId }) => {
701
- // !!! thumb 内部颜色填充 ==== start
702
- const { hue } = getProps(ownerInstance, instanceId);
703
- const isRerverse = isReverseMode(ownerInstance, instanceId);
704
- const thumbEnd = ownerInstance.selectComponent('.rayui-slider-thumb-end-render');
705
- const reverseRange = isRerverse
706
- ? getMax(ownerInstance, instanceId) - getRangeValue(ownerInstance, instanceId)
707
- : 0;
708
- const end = isRerverse ? reverseRange : getEnd(ownerInstance, instanceId);
709
- const color = hsvToRgb(hue, Math.min(100, Math.round(end / 10)), 100);
710
- thumbEnd.setStyle({ background: `rgb(${color[0]}, ${color[1]}, ${color[2]})` });
711
- // !!! thumb 内部颜色填充 ==== end
712
- };
713
-
714
- const onChange = (ownerInstance, instanceId, position) => {
715
- const ost = getOst(ownerInstance, instanceId);
716
- const isRerverse = isReverseMode(ownerInstance, instanceId);
717
- const reverseRange = isRerverse
718
- ? getMax(ownerInstance, instanceId) - getRangeValue(ownerInstance, instanceId)
719
- : 0;
720
-
721
- const nextStart = getStart(ownerInstance, instanceId);
722
- const nextEnd = isRerverse ? reverseRange : getEnd(ownerInstance, instanceId);
723
- const nextRange = isRerverse ? reverseRange : getRangeValue(ownerInstance, instanceId);
724
-
725
- // 如果start、end、range 都没变
726
- if (nextStart === ost._last_start && nextEnd === ost._last_end && nextRange === ost._last_range) {
727
- return;
728
- }
729
- setText(ownerInstance, instanceId, nextEnd);
730
-
731
- setOstValue(ownerInstance, instanceId, '_last_start', nextStart);
732
- setOstValue(ownerInstance, instanceId, '_last_end', nextEnd);
733
- setOstValue(ownerInstance, instanceId, '_last_range', nextRange);
734
-
735
- updateThumbBg({
736
- ownerInstance,
737
- instanceId,
738
- });
739
-
740
- // publish
741
- ownerInstance.triggerEvent('move', {
742
- instanceId,
743
- start: nextStart,
744
- end: nextEnd,
745
- range: nextRange,
746
- position,
747
- });
748
- };
749
-
750
- const onEnd = (event, ownerInstance, instanceId) => {
751
- const target = event.instance;
752
- const isRerverse = isReverseMode(ownerInstance, instanceId);
753
- const reverseRange = isRerverse
754
- ? getMax(ownerInstance, instanceId) - getRangeValue(ownerInstance, instanceId)
755
- : 0;
756
-
757
- const nextStart = getStart(ownerInstance, instanceId);
758
- const nextEnd = isRerverse ? reverseRange : getEnd(ownerInstance, instanceId);
759
- const nextRange = isRerverse ? reverseRange : getRangeValue(ownerInstance, instanceId);
760
- setText(ownerInstance, instanceId, nextEnd);
761
-
762
- setOstValue(ownerInstance, instanceId, '_last_start', null);
763
- setOstValue(ownerInstance, instanceId, '_last_end', null);
764
- setOstValue(ownerInstance, instanceId, '_last_range', null);
765
- updateThumbBg({
766
- ownerInstance,
767
- instanceId,
768
- });
769
-
770
- // publish
771
- ownerInstance.triggerEvent('end', {
772
- instanceId,
773
- start: nextStart,
774
- end: nextEnd,
775
- range: nextRange,
776
- position: target.getDataset().name || 'end',
777
- });
778
- };
779
- const onStart = (event, ownerInstance, instanceId) => {
780
- const target = event.instance;
781
- const isRerverse = isReverseMode(ownerInstance, instanceId);
782
- const reverseRange = isRerverse
783
- ? getMax(ownerInstance, instanceId) - getRangeValue(ownerInstance, instanceId)
784
- : 0;
785
-
786
- const nextStart = getStart(ownerInstance, instanceId);
787
- const nextEnd = isRerverse ? reverseRange : getEnd(ownerInstance, instanceId);
788
- const nextRange = isRerverse ? reverseRange : getRangeValue(ownerInstance, instanceId);
789
-
790
- setText(ownerInstance, instanceId, nextEnd);
791
- updateThumbBg({
792
- ownerInstance,
793
- instanceId,
794
- });
795
-
796
- // publish
797
- ownerInstance.triggerEvent('start', {
798
- instanceId,
799
- start: nextStart,
800
- end: nextEnd,
801
- range: nextRange,
802
- position: target.getDataset().name || 'end',
803
- });
804
- };
805
-
806
- const onMouseUp = instanceId => (event, ownerInstance) => {
807
- const ost = getOst(ownerInstance, instanceId);
808
-
809
- setOstValue(ownerInstance, instanceId, 'isMoving', false);
810
- toggleActiveThumb(ownerInstance, instanceId, false);
811
- onEnd(event, ownerInstance, instanceId);
812
-
813
- updateThumbZIndex(ownerInstance, instanceId);
814
-
815
- return false;
816
- };
817
-
818
- const onTrackMouseUp = instanceId => (event, ownerInstance) => {
819
- const hideThumb = isHideThumb(ownerInstance, instanceId);
820
- const ost = getOst(ownerInstance, instanceId);
821
- setOstValue(ownerInstance, instanceId, 'trackCurrentX', ost.trackNextX);
822
- onEnd(event, ownerInstance, instanceId);
823
- setOstValue(ownerInstance, instanceId, 'isMoving', false);
824
- updateThumbZIndex(ownerInstance, instanceId);
825
- };
826
-
827
- module.exports = {
828
- startPropObserver: createPropObserver('start'),
829
- endPropObserver: createPropObserver('end'),
830
- watchstartPropObserver: createPropObserver('watchstart'),
831
- watchenddPropObserver: createPropObserver('watchend'),
832
- handleMouseDown,
833
- onMouseMove,
834
- onMouseUp,
835
- onTrackMouseUp,
836
- handleTrackMouseDown,
837
- };