/
index.js
1161 lines (1147 loc) · 32.8 KB
/
index.js
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
let info;
let currentPage = {};
function getSystemInfo() {
if (info) return info;
info = wx.getSystemInfoSync();
return info;
}
function isIos() {
const sys = getSystemInfo();
return /iphone|ios/i.test(sys.platform);
}
/**
* 获取当前页面实例
*/
function getCurrentPage() {
const pages = getCurrentPages();
const last = pages.length - 1;
return pages[last];
}
/**
* new Date 区分平台
* @param {number} year
* @param {number} month
* @param {number} day
*/
function newDate(year, month, day) {
let cur = `${year}-${month}-${day}`;
if (isIos()) {
cur = `${year}/${month}/${day}`;
}
return new Date(cur);
}
/**
* todo 数组去重
* @param {array} array todo 数组
*/
function uniqueTodoLabels(array = []) {
let uniqueObject = {};
let uniqueArray = [];
array.forEach(item => {
uniqueObject[`${item.year}-${item.month}-${item.day}`] = item;
});
for (let i in uniqueObject) {
uniqueArray.push(uniqueObject[i]);
}
return uniqueArray;
}
/**
* 上滑
* @param {object} e 事件对象
* @returns {boolean} 布尔值
*/
export function isUpSlide(e) {
const { startX, startY } = this.data.gesture;
if (this.slideLock) {
const t = e.touches[0];
const deltaX = t.clientX - startX;
const deltaY = t.clientY - startY;
if (deltaY < -60 && deltaX < 20 && deltaX > -20) {
this.slideLock = false;
return true;
} else {
return false;
}
}
}
/**
* 下滑
* @param {object} e 事件对象
* @returns {boolean} 布尔值
*/
export function isDownSlide(e) {
const { startX, startY } = this.data.gesture;
if (this.slideLock) {
const t = e.touches[0];
const deltaX = t.clientX - startX;
const deltaY = t.clientY - startY;
if (deltaY > 60 && deltaX < 20 && deltaX > -20) {
this.slideLock = false;
return true;
} else {
return false;
}
}
}
/**
* 左滑
* @param {object} e 事件对象
* @returns {boolean} 布尔值
*/
export function isLeftSlide(e) {
const { startX, startY } = this.data.gesture;
if (this.slideLock) {
const t = e.touches[0];
const deltaX = t.clientX - startX;
const deltaY = t.clientY - startY;
if (deltaX < -60 && deltaY < 20 && deltaY > -20) {
this.slideLock = false;
return true;
} else {
return false;
}
}
}
/**
* 右滑
* @param {object} e 事件对象
* @returns {boolean} 布尔值
*/
export function isRightSlide(e) {
const { startX, startY } = this.data.gesture;
if (this.slideLock) {
const t = e.touches[0];
const deltaX = t.clientX - startX;
const deltaY = t.clientY - startY;
if (deltaX > 60 && deltaY < 20 && deltaY > -20) {
this.slideLock = false;
return true;
} else {
return false;
}
}
}
// function info(msg) {
// console.log('%cInfo: %c' + msg, 'color:#FF0080;font-weight:bold', 'color: #FF509B');
// }
function warn(msg) {
console.log(
'%cWarn: %c' + msg,
'color:#FF6600;font-weight:bold',
'color: #FF9933'
);
}
function tips(msg) {
console.log(
'%cTips: %c' + msg,
'color:#00B200;font-weight:bold',
'color: #00CC33'
);
}
const conf = {
/**
* 计算指定月份共多少天
* @param {number} year 年份
* @param {number} month 月份
*/
getThisMonthDays(year, month) {
return new Date(year, month, 0).getDate();
},
/**
* 计算指定月份第一天星期几
* @param {number} year 年份
* @param {number} month 月份
*/
getFirstDayOfWeek(year, month) {
return new Date(Date.UTC(year, month - 1, 1)).getDay();
},
/**
* 计算指定日期星期几
* @param {number} year 年份
* @param {number} month 月份
* @param {number} date 日期
*/
getDayOfWeek(year, month, date) {
return new Date(Date.UTC(year, month - 1, date)).getDay();
},
/**
* 渲染日历
* @param {number} curYear
* @param {number} curMonth
* @param {number} curDate
*/
renderCalendar(curYear, curMonth, curDate) {
conf.calculateEmptyGrids.call(this, curYear, curMonth);
conf.calculateDays.call(this, curYear, curMonth, curDate);
const { todoLabels } = this.data.calendar || {};
const { afterCalendarRender } = this.config;
if (todoLabels && todoLabels instanceof Array) {
conf.setTodoLabels.call(this);
}
if (
afterCalendarRender &&
typeof afterCalendarRender === 'function' &&
!this.firstRender
) {
afterCalendarRender();
this.firstRender = true;
}
},
/**
* 计算当前月份前后两月应占的格子
* @param {number} year 年份
* @param {number} month 月份
*/
calculateEmptyGrids(year, month) {
conf.calculatePrevMonthGrids.call(this, year, month);
conf.calculateNextMonthGrids.call(this, year, month);
},
/**
* 计算上月应占的格子
* @param {number} year 年份
* @param {number} month 月份
*/
calculatePrevMonthGrids(year, month) {
let empytGrids = [];
const prevMonthDays = conf.getThisMonthDays(year, month - 1);
const firstDayOfWeek = conf.getFirstDayOfWeek(year, month);
if (firstDayOfWeek > 0) {
const len = prevMonthDays - firstDayOfWeek;
for (let i = prevMonthDays; i > len; i--) {
empytGrids.push(i);
}
this.setData({
'calendar.empytGrids': empytGrids.reverse()
});
} else {
this.setData({
'calendar.empytGrids': null
});
}
},
/**
* 计算下月应占的格子
* @param {number} year 年份
* @param {number} month 月份
*/
calculateNextMonthGrids(year, month) {
let lastEmptyGrids = [];
const thisMonthDays = conf.getThisMonthDays(year, month);
const lastDayWeek = conf.getDayOfWeek(year, month, thisMonthDays);
if (+lastDayWeek !== 6) {
const len = 7 - (lastDayWeek + 1);
for (let i = 1; i <= len; i++) {
lastEmptyGrids.push(i);
}
this.setData({
'calendar.lastEmptyGrids': lastEmptyGrids
});
} else {
this.setData({
'calendar.lastEmptyGrids': null
});
}
},
/**
* 设置日历面板数据
* @param {number} year 年份
* @param {number} month 月份
*/
calculateDays(year, month, curDate) {
let days = [];
const {
todayTimestamp,
disableDays = [],
enableArea = []
} = this.data.calendar;
const thisMonthDays = conf.getThisMonthDays(year, month);
let selectedDay = [];
if (this.config.defaultDay !== undefined && !this.config.defaultDay) {
selectedDay = [];
this.config.defaultDay = undefined;
} else {
selectedDay = curDate
? [
{
day: curDate,
choosed: true,
year,
month
}
]
: this.data.calendar.selectedDay;
}
for (let i = 1; i <= thisMonthDays; i++) {
days.push({
day: i,
choosed: false,
year,
month
});
}
const selectedDayCol = selectedDay.map(
d => `${d.year}-${d.month}-${d.day}`
);
const disableDaysCol = disableDays.map(
d => `${d.year}-${d.month}-${d.day}`
);
days.map(item => {
const cur = `${item.year}-${item.month}-${item.day}`;
if (selectedDayCol.indexOf(cur) !== -1) item.choosed = true;
if (disableDaysCol.indexOf(cur) !== -1) item.disable = true;
const timestamp = newDate(item.year, item.month, item.day).getTime();
if (+enableArea[0] > +timestamp || +timestamp > +enableArea[1]) {
item.disable = true;
item.choosed = false;
}
if (
this.config.disablePastDay &&
timestamp - todayTimestamp < 0 &&
!item.disable
) {
item.disable = true;
}
});
const tmp = { 'calendar.days': days };
if (curDate) {
tmp['calendar.selectedDay'] = selectedDay;
}
this.setData(tmp);
},
/**
* 当改变月份时触发
* @param {object} param
*/
whenChangeMonth({ curYear, curMonth, newYear, newMonth }) {
const { whenChangeMonth } = this.config || {};
if (typeof whenChangeMonth === 'function') {
whenChangeMonth(
{
year: curYear,
month: curMonth
},
{
year: newYear,
month: newMonth
}
);
}
},
/**
* 选择上一月
*/
choosePrevMonth() {
const { curYear, curMonth } = this.data.calendar;
let newYear = curYear;
let newMonth = curMonth - 1;
if (newMonth < 1) {
newYear = curYear - 1;
newMonth = 12;
}
conf.whenChangeMonth.call(this, { curYear, curMonth, newYear, newMonth });
this.setData({
'calendar.curYear': newYear,
'calendar.curMonth': newMonth
});
conf.renderCalendar.call(this, newYear, newMonth);
},
/**
* 选择下一月
*/
chooseNextMonth() {
const { curYear, curMonth } = this.data.calendar;
let newYear = curYear;
let newMonth = curMonth + 1;
if (newMonth > 12) {
newYear = curYear + 1;
newMonth = 1;
}
conf.whenChangeMonth.call(this, { curYear, curMonth, newYear, newMonth });
this.setData({
'calendar.curYear': newYear,
'calendar.curMonth': newMonth
});
conf.renderCalendar.call(this, newYear, newMonth);
},
/**
* 日期点击事件
* @param {!object} e 事件对象
*/
tapDayItem(e) {
const { idx, disable } = e.currentTarget.dataset;
if (disable) return;
let currentSelected = {}; // 当前选中日期
let { days, selectedDay: selectedDays } = this.data.calendar || []; // 所有选中日期
const config = this.config;
const { multi, onTapDay } = config;
const opts = {
e,
idx,
onTapDay,
currentSelected,
selectedDays,
days: days.slice()
};
if (multi) {
conf.whenMulitSelect.call(this, opts);
} else {
conf.whenSingleSelect.call(this, opts);
}
},
/**
* 点击日期后触发事件
* @param {object} currentSelected 当前选择的日期
* @param {array} selectedDays 多选状态下选中的日期
*/
afterTapDay(currentSelected, selectedDays) {
const config = this.config;
const { multi, afterTapDay } = config;
if (afterTapDay && typeof afterTapDay === 'function') {
if (!multi) {
config.afterTapDay(currentSelected);
} else {
config.afterTapDay(currentSelected, selectedDays);
}
}
},
/**
* 多选
* @param {object} opts
*/
whenMulitSelect(opts = {}) {
let { currentSelected, selectedDays } = opts;
const { days, idx, onTapDay, e } = opts;
days[idx].choosed = !days[idx].choosed;
if (!days[idx].choosed) {
days[idx].cancel = true; // 点击事件是否是取消日期选择
currentSelected = days[idx];
selectedDays = selectedDays.filter(item => item.day !== days[idx].day);
} else {
currentSelected = days[idx];
selectedDays.push(currentSelected);
}
if (onTapDay && typeof onTapDay === 'function') {
return this.config.onTapDay(currentSelected, e);
}
this.setData({
'calendar.days': days,
'calendar.selectedDay': selectedDays
});
conf.afterTapDay.call(this, currentSelected, selectedDays);
},
/**
* 单选
* @param {object} opts
*/
whenSingleSelect(opts = {}) {
let { currentSelected, selectedDays = [] } = opts;
let shouldMarkerTodoDay = [];
const { days, idx, onTapDay, e } = opts;
const { month: sMonth, year: sYear } = selectedDays[0] || {};
const { month: dMonth, year: dYear } = days[0] || {};
const { calendar = {} } = this.data;
if (sMonth === dMonth && sYear === dYear && !this.weekMode) {
days[selectedDays[0].day - 1].choosed = false;
}
if (this.weekMode) {
days.map((item, idx) => {
if (item.day === selectedDays[0].day) days[idx].choosed = false;
});
}
if (calendar.todoLabels) {
// 过滤所有待办日期中当月有待办事项的日期
shouldMarkerTodoDay = calendar.todoLabels.filter(
item => +item.year === dYear && +item.month === dMonth
);
}
shouldMarkerTodoDay.forEach(item => {
// hasTodo 是否有待办事项
if (this.weekMode) {
days.map((_item, idx) => {
if (+_item.day === +item.day) {
days[idx].hasTodo = true;
if (
selectedDays &&
selectedDays.length &&
+selectedDays[0].day === +item.day
) {
days[idx].showTodoLabel = true;
}
}
});
} else {
days[item.day - 1].hasTodo = true;
// showTodoLabel 是否显示待办标记
if (
selectedDays &&
selectedDays.length &&
+selectedDays[0].day === +item.day
) {
days[selectedDays[0].day - 1].showTodoLabel = true;
}
}
});
if (days[idx].showTodoLabel) days[idx].showTodoLabel = false;
days[idx].choosed = true;
currentSelected = days[idx];
if (onTapDay && typeof onTapDay === 'function') {
return this.config.onTapDay(currentSelected, e);
}
this.setData({
'calendar.days': days,
'calendar.selectedDay': [currentSelected]
});
conf.afterTapDay.call(this, currentSelected);
},
/**
* 设置代办事项标志
* @param {object} options 代办事项配置
*/
setTodoLabels(options = {}) {
const { calendar } = this.data;
if (!calendar || !calendar.days) {
return warn('请等待日历初始化完成后再调用该方法');
}
const days = calendar.days.slice();
const { curYear, curMonth } = calendar;
const { days: todoDays = [], pos = 'bottom', dotColor = '' } = options;
const { todoLabels = [], todoLabelPos, todoLabelColor } = calendar;
const shouldMarkerTodoDay = todoDays.filter(
item => +item.year === curYear && +item.month === curMonth
);
let currentMonthTodoLabels = todoLabels.filter(
item => +item.year === curYear && +item.month === curMonth
);
shouldMarkerTodoDay.concat(currentMonthTodoLabels).forEach(item => {
let target = {};
if (this.weekMode) {
target = days.find(d => +d.day === +item.day);
} else {
target = days[item.day - 1];
}
if (target) target.showTodoLabel = !target.choosed;
});
const o = {
'calendar.days': days,
'calendar.todoLabels': uniqueTodoLabels(todoDays.concat(todoLabels))
};
if (pos && pos !== todoLabelPos) o['calendar.todoLabelPos'] = pos;
if (dotColor && dotColor !== todoLabelColor) {
o['calendar.todoLabelColor'] = dotColor;
}
this.setData(o);
},
/**
* 筛选待办事项
* @param {array} todos 需要删除待办标记的日期
*/
filterTodos(todos) {
const { todoLabels } = this.data.calendar;
const deleteTodo = todos.map(
item => `${item.year}-${item.month}-${item.day}`
);
return todoLabels.filter(
item =>
deleteTodo.indexOf(`${item.year}-${item.month}-${item.day}`) === -1
);
},
/**
* 删除指定日期的待办标识
* @param {array} todos 需要删除待办标记的日期
*/
deleteTodoLabels(todos) {
if (!(todos instanceof Array) || !todos.length) return;
const todoLabels = conf.filterTodos.call(this, todos);
const { days, curYear, curMonth } = this.data.calendar;
const currentMonthTodoLabels = todoLabels.filter(
item => curYear === +item.year && curMonth === +item.month
);
days.map(item => {
item.showTodoLabel = false;
});
currentMonthTodoLabels.forEach(item => {
days[item.day - 1].showTodoLabel = !days[item.day - 1].choosed;
});
this.setData({
'calendar.days': days,
'calendar.todoLabels': todoLabels
});
},
/**
* 清空所有日期的待办标识
*/
clearTodoLabels() {
const { days = [] } = this.data.calendar;
const _days = [].concat(days);
_days.map(item => {
item.showTodoLabel = false;
});
this.setData({
'calendar.days': _days,
'calendar.todoLabels': []
});
},
/**
* 跳转至今天
*/
jumpToToday() {
const date = new Date();
const curYear = date.getFullYear();
const curMonth = date.getMonth() + 1;
const curDate = date.getDate();
const timestamp = newDate(curYear, curMonth, curDate).getTime();
this.setData({
'calendar.curYear': curYear,
'calendar.curMonth': curMonth,
'calendar.selectedDay': [
{
day: curDate,
choosed: true,
year: curYear,
month: curMonth
}
],
'calendar.todayTimestamp': timestamp
});
conf.renderCalendar.call(this, curYear, curMonth, curDate);
},
/**
* 日历滑动开始
* @param {object} e
*/
calendarTouchstart(e) {
const t = e.touches[0];
const startX = t.clientX;
const startY = t.clientY;
currentPage.slideLock = true; // 滑动事件加锁
currentPage.setData({
'gesture.startX': startX,
'gesture.startY': startY
});
},
/**
* 日历滑动中
* @param {object} e
*/
calendarTouchmove(e) {
const self = currentPage;
if (isLeftSlide.call(self, e)) {
if (self.weekMode) return conf.calculateNextWeekDays.call(self);
conf.chooseNextMonth.call(self);
}
if (isRightSlide.call(self, e)) {
if (self.weekMode) return conf.calculatePrevWeekDays.call(self);
conf.choosePrevMonth.call(self);
}
},
/**
* 更新当前年月
*/
updateCurrYearAndMonth(type) {
let { days, curYear, curMonth } = this.data.calendar;
const { month: firstMonth } = days[0];
const { month: lastMonth } = days[days.length - 1];
const lastDayOfThisMonth = conf.getThisMonthDays(curYear, curMonth);
const lastDayOfThisWeek = days[days.length - 1];
const firstDayOfThisWeek = days[0];
if (
(lastDayOfThisWeek.day + 7 > lastDayOfThisMonth ||
(curMonth === firstMonth && firstMonth !== lastMonth)) &&
type === 'next'
) {
curMonth = curMonth + 1;
if (curMonth > 12) {
curYear = curYear + 1;
curMonth = 1;
}
} else if (
(+firstDayOfThisWeek.day <= 7 ||
(curMonth === lastMonth && firstMonth !== lastMonth)) &&
type === 'prev'
) {
curMonth = curMonth - 1;
if (curMonth <= 0) {
curYear = curYear - 1;
curMonth = 12;
}
}
return {
Uyear: curYear,
Umonth: curMonth
};
},
/**
* 计算周视图下当前这一周和当月的最后一天
*/
calculateLastDay() {
const { days, curYear, curMonth } = this.data.calendar;
const lastDayInThisWeek = days[days.length - 1].day;
const lastDayInThisMonth = conf.getThisMonthDays(curYear, curMonth);
return { lastDayInThisWeek, lastDayInThisMonth };
},
/**
* 计算周视图下当前这一周第一天
*/
calculateFirstDay() {
const { days } = this.data.calendar;
const firstDayInThisWeek = days[0].day;
return { firstDayInThisWeek };
},
/**
* 当月第一周所有日期范围
* @param {number} year
* @param {number} month
*/
firstWeekInMonth(year, month) {
const firstDay = conf.getDayOfWeek(year, month, 1);
const firstWeekDays = [1, 1 + (6 - firstDay)];
const { days } = this.data.calendar;
const daysCut = days.slice(firstWeekDays[0] - 1, firstWeekDays[1]);
return daysCut;
},
/**
* 当月最后一周所有日期范围
* @param {number} year
* @param {number} month
*/
lastWeekInMonth(year, month) {
const lastDay = conf.getThisMonthDays(year, month);
const lastDayWeek = conf.getDayOfWeek(year, month, lastDay);
const lastWeekDays = [lastDay - lastDayWeek, lastDay];
const { days } = this.data.calendar;
const daysCut = days.slice(lastWeekDays[0] - 1, lastWeekDays[1]);
return daysCut;
},
/**
* 渲染日期之前初始化已选日期
* @param {array} days 当前日期数组
*/
initSelectedDay(days) {
const daysCopy = days.slice();
const { selectedDay = [], todoLabels = [] } = this.data.calendar;
const selectedDayStr = selectedDay.map(
item => `${item.year}+${item.month}+${item.day}`
);
const todoLabelsCol = todoLabels.map(d => `${d.year}-${d.month}-${d.day}`);
daysCopy.map(item => {
if (
selectedDayStr.indexOf(`${item.year}+${item.month}+${item.day}`) !== -1
) {
item.choosed = true;
} else {
item.choosed = false;
}
if (
todoLabelsCol.indexOf(`${item.year}-${item.month}-${item.day}`) !== -1
) {
item.showTodoLabel = !item.choosed;
}
});
return daysCopy;
},
/**
* 周视图下设置可选日期范围
* @param {object} days 当前展示的日期
*/
setEnableAreaOnWeekMode(days) {
let { todayTimestamp, enableArea } = this.data.calendar;
days.map(item => {
const timestamp = newDate(item.year, item.month, item.day).getTime();
if (+enableArea[0] > +timestamp || +timestamp > +enableArea[1]) {
item.disable = true;
item.choosed = false;
}
if (
this.config.disablePastDay &&
timestamp - todayTimestamp < 0 &&
!item.disable
) {
item.disable = true;
}
});
},
/**
* 计算下一周的日期
*/
calculateNextWeekDays() {
let { lastDayInThisWeek, lastDayInThisMonth } = conf.calculateLastDay.call(
this
);
let { curYear, curMonth } = this.data.calendar;
let days = [];
if (lastDayInThisMonth - lastDayInThisWeek >= 7) {
const { Uyear, Umonth } = conf.updateCurrYearAndMonth.call(this, 'next');
curYear = Uyear;
curMonth = Umonth;
for (let i = lastDayInThisWeek + 1; i <= lastDayInThisWeek + 7; i++) {
days.push({
year: curYear,
month: curMonth,
day: i
});
}
} else {
for (let i = lastDayInThisWeek + 1; i <= lastDayInThisMonth; i++) {
days.push({
year: curYear,
month: curMonth,
day: i
});
}
const { Uyear, Umonth } = conf.updateCurrYearAndMonth.call(this, 'next');
curYear = Uyear;
curMonth = Umonth;
for (let i = 1; i <= 7 - (lastDayInThisMonth - lastDayInThisWeek); i++) {
days.push({
year: curYear,
month: curMonth,
day: i
});
}
}
days = conf.initSelectedDay.call(this, days);
conf.setEnableAreaOnWeekMode.call(this, days);
this.setData({
'calendar.curYear': curYear,
'calendar.curMonth': curMonth,
'calendar.days': days
});
},
/**
* 计算上一周的日期
*/
calculatePrevWeekDays() {
let { firstDayInThisWeek } = conf.calculateFirstDay.call(this);
let { curYear, curMonth } = this.data.calendar;
let days = [];
if (firstDayInThisWeek - 7 > 0) {
const { Uyear, Umonth } = conf.updateCurrYearAndMonth.call(this, 'prev');
curYear = Uyear;
curMonth = Umonth;
for (let i = firstDayInThisWeek - 7; i < firstDayInThisWeek; i++) {
days.push({
year: curYear,
month: curMonth,
day: i
});
}
} else {
let temp = [];
for (let i = 1; i < firstDayInThisWeek; i++) {
temp.push({
year: curYear,
month: curMonth,
day: i
});
}
const { Uyear, Umonth } = conf.updateCurrYearAndMonth.call(this, 'prev');
curYear = Uyear;
curMonth = Umonth;
const prevMonthDays = conf.getThisMonthDays(curYear, curMonth);
for (
let i = prevMonthDays - Math.abs(firstDayInThisWeek - 7);
i <= prevMonthDays;
i++
) {
days.push({
year: curYear,
month: curMonth,
day: i
});
}
days = days.concat(temp);
}
days = conf.initSelectedDay.call(this, days);
conf.setEnableAreaOnWeekMode.call(this, days);
this.setData({
'calendar.curYear': curYear,
'calendar.curMonth': curMonth,
'calendar.days': days
});
},
/**
* 计算当前选中日期所在周,并重新渲染日历
* @param {object} currentDay 当前选择日期
*/
selectedDayWeekAllDays(currentDay) {
let { days, curYear, curMonth } = this.data.calendar;
let { year, month, day } = currentDay;
let lastWeekDays = conf.lastWeekInMonth.call(this, year, month);
let empytGrids = [];
let lastEmptyGrids = [];
const firstWeekDays = conf.firstWeekInMonth.call(this, year, month);
// 判断选中日期的月份是否与当前月份一致
if (curYear !== year || curMonth !== month) day = 1;
if (curYear !== year) year = curYear;
if (curMonth !== month) month = curMonth;
if (firstWeekDays.find(item => item.day === day)) {
// 当前选择的日期为该月第一周
let temp = [];
const lastDayInThisMonth = conf.getThisMonthDays(year, month - 1);
const { Uyear, Umonth } = conf.updateCurrYearAndMonth.call(this, 'prev');
curYear = Uyear;
curMonth = Umonth;
for (
let i = lastDayInThisMonth - (7 - firstWeekDays.length) + 1;
i <= lastDayInThisMonth;
i++
) {
temp.push({
year: curYear,
month: curMonth,
day: i
});
}
days = temp.concat(firstWeekDays);
} else if (lastWeekDays.find(item => item.day === day)) {
// 当前选择的日期为该月最后一周
const temp = [];
if (lastWeekDays && lastWeekDays.length < 7) {
const { Uyear, Umonth } = conf.updateCurrYearAndMonth.call(
this,
'next'
);
curYear = Uyear;
curMonth = Umonth;
for (let i = 1, len = 7 - lastWeekDays.length; i <= len; i++) {
temp.push({
year: curYear,
month: curMonth,
day: i
});
}
}
days = lastWeekDays.concat(temp);
} else {
const week = conf.getDayOfWeek(year, month, day);
const range = [day - week, day + (6 - week)];
days = days.slice(range[0] - 1, range[1]);
}
days = conf.initSelectedDay.call(this, days);
this.setData({
'calendar.days': days,
'calendar.empytGrids': empytGrids,
'calendar.lastEmptyGrids': lastEmptyGrids
});
},
/**
* 周、月视图切换
* @param {string} view 视图 [week, month]
*/
switchWeek(view) {
if (this.config.multi) return warn('多选模式不能切换周月视图');
const { selectedDay = [], curYear, curMonth } = this.data.calendar;
if (!selectedDay.length) return;
const currentDay = selectedDay[0];
if (view === 'week') {
if (this.weekMode) return;
this.weekMode = true;
conf.selectedDayWeekAllDays.call(this, currentDay);
} else {
this.weekMode = false;
let { year, month, day } = currentDay;
if (curYear !== year || curMonth !== month) day = 1;
conf.renderCalendar.call(this, curYear, curMonth, day);
}
},
/**
* 禁用指定日期
* @param {array} days 禁用
*/
disableDays(data) {
const { disableDays = [], days } = this.data.calendar;
if (Object.prototype.toString.call(data) !== '[object Array]') {
return warn('disableDays 参数为数组');
}
const _disableDays = data.concat(disableDays);
const disableDaysCol = _disableDays.map(
d => `${d.year}-${d.month}-${d.day}`
);
days.map(item => {
const cur = `${item.year}-${item.month}-${item.day}`;
if (disableDaysCol.indexOf(cur) !== -1) item.disable = true;
});
this.setData({
'calendar.days': days,
'calendar.disableDays': _disableDays
});
}
};
/**
* 获取已选择的日期
*/
export const getSelectedDay = () => {
return currentPage && currentPage.data.calendar.selectedDay;
};
/**
* 跳转至指定日期
*/
export const jump = (year, month, day) => {
const self = currentPage;
const { selectedDay } = self.data.calendar;
if (
selectedDay &&
+selectedDay[0].year === +year &&
+selectedDay[0].month === +month &&
+selectedDay[0].day === +day
) {