@ray-js/lamp-style-slider 0.0.3-beta-1 → 0.0.3-beta-3

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