lyg
2025-03-04 72bbec1590f85974d369ce7aeaa05be8905672a0
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
mbui.define(['tool'], function (exports) {
    let tool = mbui.tool;
    var settings = {
        mode: "month",
        weekMode: ["一", "二", "三", "四", "五", "六", "日"],
        newDate: new Date(),
        width: null,
        height: null,
        shwoLunar: false,
        showModeBtn: true,
        showEvent: true,
        success: function (today) {
            console.log(today);
        },
        mark: function (start_date, end_date) {
            console.log(start_date);
            console.log(end_date);
        }
    };
    var Calendar = {
        init: function () {
            var me = this, el = me.el, opts = me.options;
            el.addClass("calendar");
            opts.width = el.width();
            opts.height = el.height();
 
            typeof (opts.newDate) == "string" ? opts.newDate = me._getDateByString(opts.newDate) : "";
            me._createCalendar();
 
            //绑定事件
            //changeMode
            el.on("click", ".calendar-mode-select .btn", function (e) {
                e.stopPropagation();
                var modeText = $(this).text();
                var mode = modeText == "月" ? "month" : "year";
                me.changeMode(mode);
            })
 
            //calendar-cell日期点击事件
            el.on("click", ".calendar-cell", function (e) {
                e.stopPropagation();
                $(".dropdown-month").removeClass("open");
                $(".dropdown-year").removeClass("open");
 
                var cellDate = $(this).attr("title");
                var viewData = me.viewData;
                var year = parseInt(cellDate.split("年")[0]);
                var month = parseInt(cellDate.split("年")[1].split("月")[0]) - 1;
                var date = parseInt(cellDate.split("年")[1].split("月")[1].split("日")[0]);
                if (opts.mode == "year") {
                    if (opts.cellClick) opts.cellClick(year + '-' + (month + 1))
                }
                else if (opts.mode == "month" && month == opts.newDate.getMonth()) {
                    if (me.options.cellClick) me.options.cellClick(year + '-' + (month + 1) + '-' + date);
                }
            })
 
            //年份下拉
            el.on("click", ".calendar-year-select", function (e) {
                e.stopPropagation();
                $(".dropdown-month").removeClass("open");
                $(".dropdown-year").toggleClass("open");
                //创建下拉数据
                var yearText = opts.newDate.getFullYear();
                var s = '';
                for (var i = 0; i < 21; i++) {
                    if (i == 10) {
                        s += '<li class="year-item active">'
                    }
                    else {
                        s += '<li class="year-item">'
                    }
                    s += '<span class="year-check">' + (yearText - 10 + i) + '</span>'
                    s += '<span >年</span>'
                    s += '</li>'
                }
                me.el.find(".dropdown-year").html(s);
            })
 
            //上一月
            el.on("click", ".calendar-pre", function (e) {
                e.stopPropagation();
                var year = opts.newDate.getFullYear(), month = opts.newDate.getMonth() + 1;
                month--;
                if (month < 1) {
                    year = year - 1;
                    month = 12;
                }
                opts.newDate.setFullYear(year);
                var beforeDate = opts.newDate.getDate();
                opts.newDate.setMonth(month - 1);
                var afterDate = opts.newDate.getDate();
 
                //处理日期30号,切换到2月不存在30号
                if (beforeDate != afterDate) {
                    opts.newDate.setDate(opts.newDate.getDate() - 1);
                }
                opts.mode == "month" ? me._refreshCalendar(opts.newDate) : me._refreshYearCalendar(opts.newDate);
                $(".calendar-month-text").text(month + "月");
                $(".calendar-year-text").text(year + "年");
            })
 
            //下一月
            el.on("click", ".calendar-next", function (e) {
                e.stopPropagation();
                var year = opts.newDate.getFullYear(), month = opts.newDate.getMonth() + 1;
                month++;
                if (month > 12) {
                    year = year + 1;
                    month = 1;
                }
                opts.newDate.setFullYear(year);
                var beforeDate = opts.newDate.getDate();
                opts.newDate.setMonth(month - 1);
                var afterDate = opts.newDate.getDate();
 
                //处理日期30号,切换到2月不存在30号
                if (beforeDate != afterDate) {
                    opts.newDate.setDate(opts.newDate.getDate() - 1);
                }
                opts.mode == "month" ? me._refreshCalendar(opts.newDate) : me._refreshYearCalendar(opts.newDate);
                $(".calendar-month-text").text(month + "月");
                $(".calendar-year-text").text(year + "年");
            })
 
            //年份改变
            el.on("click", ".year-item", function (e) {
                e.stopPropagation();
                $(".dropdown-year").removeClass("open");
                var yearText = $(this).text();
                var yearNum = yearText.split("年")[0];
                if (yearNum == opts.newDate.getFullYear()) return;
                opts.newDate.setFullYear(yearNum);
                opts.mode == "month" ? me._refreshCalendar(opts.newDate) : me._refreshYearCalendar(opts.newDate);
                $(".calendar-year-text").text(yearText);
            })
 
            //月份下拉
            el.on("click", ".calendar-month-select", function (e) {
                e.stopPropagation();
                $(".dropdown-year").removeClass("open");
                $(".dropdown-month").toggleClass("open");
            })
 
            //月份改变
            el.on("click", ".month-item", function (e) {
                e.stopPropagation();
                $(".dropdown-month").removeClass("open");
                var monthText = $(this).text();
                var monthNum = monthText.split("月")[0];
                if (monthNum == (opts.newDate.getMonth() + 1)) return;
 
                var beforeDate = opts.newDate.getDate();
                opts.newDate.setMonth(monthNum - 1);
                var afterDate = opts.newDate.getDate();
 
                //处理日期30号,切换到2月不存在30号
                if (beforeDate != afterDate) {
                    opts.newDate.setDate(opts.newDate.getDate() - 1);
                }
                me._refreshCalendar(opts.newDate);
                $(".calendar-month-text").text(monthText);
            })
 
            $(document.body).on("click", function (e) {
                $(".dropdown-month").removeClass("open");
                $(".dropdown-year").removeClass("open");
            })
        },
        //公开方法
        changeMode: function (mode) {
            var me = this;
            if (mode == me.options.mode) return;
            me.options.mode = mode;
            me._createCalendar();
        },
        getViewDate: function (viewDate) {
            var me = this, opts = me.options, mode = opts.mode, data = opts.data;
            if (!data || data.length == 0) return [];
            var viewData = {}, modeYear = viewDate.getFullYear(), modeMonth = null;
            if (mode == "month") { modeMonth = viewDate.getMonth() };
 
            //筛选视图数据并转化未对象 要不要转化为属性
            for (var i = 0; i < data.length; i++) {
                var item = data[i];
                var start = me._getDateByString(item.startDate);
                var year = start.getFullYear();
                var month = start.getMonth();
                var date = start.getDate();
                if (modeMonth && year == modeYear && modeMonth == month) {
                    if (!viewData[date]) viewData[date] = [];
                    viewData[date].push(item);
                }
                else if (!modeMonth && year == modeYear) {
                    if (!viewData[month]) viewData[month] = [];
                    viewData[month].push(item);
                }
            }
            return viewData;
 
        },
        _getDateByString: function (stringDate) {
            var me = this;
            var year = stringDate.split("-")[0];
            var month = parseInt(stringDate.split("-")[1]) - 1;
            var date = stringDate.split("-")[2];
            return new Date(year, month, date);
        },
        //私有方法
        _createCalendar: function () {
            var me = this;
            var dateMode = me.options.mode;
            me._createView();
        },
        _createView: function () {
            var me = this, el = me.el, opts = me.options, mode = opts.mode, newDate = opts.newDate, html = '';
            html += me._createToolbar();
            html += '<div class="calendar-body">';
            html += '<table class="calendar-table" cellspacing="0">'
            if (mode == "month") {
                html += me._createHeader();
            }
            html += me._createBody();
            html += '</table>'
            html += '</div>'
            el.html(html);
 
            if (mode == "month") {
                me._refreshCalendar(newDate);
            }
            else {
                me._refreshYearCalendar(newDate);
            }
            if (me.options.success) me.options.success(newDate);
        },
        _createToolbar: function () {
            var me = this, newDare = me.options.newDate, mode = me.options.mode, showModeBtn = me.options.showModeBtn, s = '';
            var year = newDare.getFullYear();
            var month = newDare.getMonth() + 1;
            s += '<div class="calendar-header">'
            s += '<div class="calendar-select calendar-year-select" >'
            s += '<span class="calendar-year-text"> ' + year + '年</span >'
            s += '<ul id="dropdown-year" class="dropdown-year">'
            s += '</ul>'
            s += '</div> '
 
            if (mode == "month") {
                s += '<div class="calendar-select  calendar-month-select">'
                s += '<span class="calendar-month-text"> ' + month + '月</span >'
                //创建月份下拉
                s += '<ul class="dropdown-month">'
                for (var i = 1; i <= 12; i++) {
                    s += '<li class="month-item">'
                    s += '<span class="month-check">' + i + '</span>'
                    s += '<span >月</span>'
                    s += '</li>'
                }
                s += '</ul>'
                s += '</div > '
            }
            if (showModeBtn) {
                s += '<div class="calendar-select calendar-mode-select">'
                s += '<div class="btn-group">'
                if (mode == "month") {
                    s += '<span  class="btn calendar-select-active">月</span>'
                    s += '<span class="btn">年</span>'
                }
                else {
                    s += '<span  class="btn">月</span>'
                    s += '<span class="btn calendar-select-active">年</span>'
                }
                s += '</div>'
                s += '</div>'
            }
            s += '<i class="fa-angle-left calendar-pre"></i><i class="fa-angle-right calendar-next"></i>'
            s += '</div>'
            return s;
        },
        _createHeader: function () {
            var me = this, opts = me.options, weekMode = opts.weekMode;
            var s = '<thead><tr>'
            weekMode.forEach(function (item) {
                s += ' <th class="calendar-column-header" title="周' + item + '"><span class="calendar-column-header-inner">' + item + '</span></th>'
            })
            s += '</thead></tr>'
            return s;
        },
        _createBody: function () {
            var me = this;
            var s = ' <tbody class="calendar-tbody">'
            s += '</tbody>'
            return s;
        },
        _refreshYearCalendar: function (newDate) {
            var me = this, showEvent = me.options.showEvent, s = '';
            //每次都重新获取会不会影响性能
            me.viewData = viewData = me.getViewDate(newDate);
            var year = newDate.getFullYear(), month = newDate.getMonth();
            //四行三列
            for (var i = 0; i < 4; i++) {
                s += '<tr>'
                for (var l = 0; l < 3; l++) {
                    renderMonth = i * 3 + l;
                    if (month == renderMonth) {
                        s += '<td title="' + year + '年' + (renderMonth + 1) + '月" class="calendar-cell calendar-thisMonth">';
                    }
                    else {
                        s += '<td title="' + year + '年' + (renderMonth + 1) + '月" class="calendar-cell">';
                    }
                    s += '<div class="calendar-date">';
                    s += '<div class="calendar-value">' + (renderMonth + 1) + '月';
                    if (showEvent && viewData[renderMonth]) {
                        if (viewData[renderMonth].length > 0) {
                            s += '<div class="calendar-event"></div>';
                        }
                    }
                    s += '</div>';
                    s += '</div></td>';
                }
                s += '</tr>'
            }
            me.el.find(".calendar-tbody").html(s);
        },
        _refreshCalendar: function (newDate) {
            var me = this, showEvent = me.options.showEvent, s = '';
            me.viewData = viewData = me.getViewDate(newDate);
            var _newDate = me._cloneDate(newDate);
            //当前date
            var nowNum = _newDate.getDate();
            //第一天周几
            _newDate.setDate(1);
            var weekDay = _newDate.getDay() == 0 ? 7 : _newDate.getDay();
            //视图第一天
            var viewDate = me._cloneDate(_newDate);
            viewDate.setDate(viewDate.getDate() - weekDay + 1);
            //当前第几周/行 (暂不处理)
            var spileDate = (newDate.getTime() - viewDate.getTime()) / (1000 * 60 * 60 * 24);
            renderDate = me._cloneDate(viewDate);
            var start_date, end_date;
            //固定六行
            for (var i = 0; i < 6; i++) {
                s += '<tr>'
                for (var l = 0; l < 7; l++) {
                    var year = renderDate.getFullYear();
                    var month = renderDate.getMonth() + 1;
                    var date = renderDate.getDate();
                    if (i == 0 && l == 0) {
                        start_date = year + '-' + month + '-' + date;
                    }
                    end_date = year + '-' + month + '-' + date;
                    if (renderDate.getMonth() < newDate.getMonth()) {
                        s += '<td title="' + year + '年' + month + '月' + date + '日" class="calendar-cell calendar-last-month-cell">';
                    }
                    else if (renderDate.getMonth() > newDate.getMonth()) {
                        s += '<td title="' + year + '年' + month + '月' + date + '日" class="calendar-cell calendar-next-month-cell">';
                    }
                    else if (date == nowNum) {
                        s += '<td title="' + year + '年' + month + '月' + date + '日" class="calendar-cell calendar-today">';
                    }
                    else {
                        s += '<td title="' + year + '年' + month + '月' + date + '日" class="calendar-cell">';
                    }
                    s += '<div class="calendar-date">';
                    s += '<div class="calendar-value">' + date;
                    if (showEvent && viewData[date] && renderDate.getMonth() == newDate.getMonth()) {
                        if (viewData[date].length > 0) {
                            s += '<div class="calendar-event"></div>';
                        }
                    }
                    s += '</div>';
                    s += '</div></td>';
                    renderDate.setDate(renderDate.getDate() + 1);
                }
                s += '</tr>'
            }
            me.el.find(".calendar-tbody").html(s);
            if (me.options.mark) me.options.mark(start_date, end_date);
        },
        _cloneDate: function (date) {
            return new Date(date.getFullYear(), date.getMonth(), date.getDate());
        }
    }
    var calendar = function (element, options) {
        Calendar.el = $('#' + element);
        Calendar.options = $.extend(true, {}, settings, options);
        Calendar.init();
    }
    // 输出接口
    exports('calendar', calendar);
});