zui.calendar.js 24.5 KB
Newer Older
C
Catouse 已提交
1
/*!
C
Catouse 已提交
2
 * ZUI: 日历 - v1.8.1 - 2018-01-18
C
Catouse 已提交
3 4
 * http://zui.sexy
 * GitHub: https://github.com/easysoft/zui.git 
C
Catouse 已提交
5
 * Copyright (c) 2018 cnezsoft.com; Licensed MIT
C
Catouse 已提交
6 7
 */

8 9 10 11 12 13 14
/* ========================================================================
 * ZUI: calendar.js
 * http://zui.sexy
 * ========================================================================
 * Copyright (c) 2014 cnezsoft.com; Licensed MIT
 * ======================================================================== */

C
Catouse 已提交
15
(function($, window) {
C
Catouse 已提交
16
    'use strict';
C
Catouse 已提交
17
    var NAME = 'zui.calendar';
C
Catouse 已提交
18 19 20 21
    var NUMBER_TYPE_NAME = 'number';
    var STRING_TYPE_NAME = 'string';
    var UNDEFINED_TYPE_NAME = 'undefined';
    var presetColors = {
C
Catouse 已提交
22 23 24 25 26 27 28
        "primary": 1,
        "green": 2,
        "red": 3,
        "blue": 4,
        "yellow": 5,
        "brown": 6,
        "purple": 7
C
Catouse 已提交
29
    };
C
Catouse 已提交
30

C
Catouse 已提交
31
    var getNearbyLastWeekDay = function(date, lastWeek) {
C
Catouse 已提交
32 33 34
            lastWeek = lastWeek || 1;

            var d = date.clone();
C
Catouse 已提交
35
            while(d.getDay() != lastWeek) {
C
Catouse 已提交
36 37
                d.addDays(-1);
            }
C
Catouse 已提交
38
            d.clearTime();
C
Catouse 已提交
39 40 41
            return d;
        },

C
Catouse 已提交
42
        getFirstDayOfMonth = function(date) {
C
Catouse 已提交
43 44 45
            var d = date.clone();
            d.setDate(1);
            return d;
C
Catouse 已提交
46 47 48 49 50
        },

        calculateDays = function(start, end) {
            var s = start.clone().clearTime();
            var e = end.clone().clearTime();
C
Catouse 已提交
51
            return Math.round((e.getTime() - s.getTime()) / Date.ONEDAY_TICKS) + 1;
C
Catouse 已提交
52 53 54 55 56 57 58 59 60
        },

        everyDayTo = function(start, end, callback) {
            var a = start.clone();
            var i = 0;
            while(a <= end) {
                callback(a.clone(), i++);
                a.addDays(1);
            }
C
Catouse 已提交
61
        };
C
Catouse 已提交
62

C
Catouse 已提交
63
    var Calendar = function(element, options) {
C
Catouse 已提交
64
        this.name = NAME;
65
        this.$ = $(element);
C
Catouse 已提交
66
        this.id = this.$.attr('id') || (NAME + $.zui.uuid());
C
Catouse 已提交
67
        this.$.attr('id', this.id);
C
Catouse 已提交
68
        this.storeName = NAME + '.' + this.id;
C
Catouse 已提交
69 70 71 72 73

        this.getOptions(options);
        this.getLang();

        this.data = this.options.data;
C
Catouse 已提交
74 75
        this.addCalendars(this.data.calendars);
        this.addEvents(this.data.events);
C
Catouse 已提交
76 77
        this.sortEvents();

C
Catouse 已提交
78
        this.storeData = $.zui.store.pageGet(this.storeName, {
79 80 81
            date: 'today',
            view: 'month'
        });
C
Catouse 已提交
82

C
Catouse 已提交
83 84 85
        this.date = this.options.startDate || 'today';
        this.view = this.options.startView || 'month';

C
Catouse 已提交
86 87
        this.$.toggleClass('limit-event-title', options.limitEventTitle);

C
Catouse 已提交
88
        if(this.options.withHeader) {
C
Catouse 已提交
89
            var $header = this.$.children('.calender-header');
C
Catouse 已提交
90
            if(!$header.length) {
C
Catouse 已提交
91 92 93 94 95 96 97 98 99
                $header = $('<header><div class="btn-toolbar"><div class="btn-group"><button type="button" class="btn btn-today">{today}</button></div><div class="btn-group"><button type="button" class="btn btn-prev"><i class="icon-chevron-left"></i></button><button type="button" class="btn btn-next"><i class="icon-chevron-right"></i></button></div><div class="btn-group"><span class="calendar-caption"></span></div></div></header>'.format(this.lang));
                this.$.append($header);
            }
            this.$caption = $header.find('.calendar-caption');
            this.$todayBtn = $header.find('.btn-today');
            this.$header = $header;
        }

        var $views = this.$.children('.calendar-views');
C
Catouse 已提交
100
        if(!$views.length) {
C
Catouse 已提交
101 102 103 104 105 106 107 108 109 110 111 112
            $views = $('<div class="calendar-views"></div>');
            this.$.append($views);
        }
        this.$views = $views;
        this.$monthView = $views.children('.calendar-view.month');

        this.display();

        this.bindEvents();
    };

    // default options
113
    Calendar.DEFAULTS = {
C
Catouse 已提交
114 115
        langs: {
            zh_cn: {
C
Catouse 已提交
116 117 118 119 120 121
                weekNames: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
                monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
                today: '今天',
                year: '{0}年',
                month: '{0}月',
                yearMonth: '{0}年{1}月'
C
Catouse 已提交
122
            },
C
Catouse 已提交
123
            zh_tw: {
C
Catouse 已提交
124 125 126 127 128 129 130
                weekNames: ['週一', '週二', '週三', '週四', '週五', '週六', '週日'],
                monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
                today: '今天',
                year: '{0}年',
                month: '{0}月',
                yearMonth: '{0}年{1}月'
            },
C
Catouse 已提交
131
            en: {
C
Catouse 已提交
132 133 134 135 136 137
                weekNames: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
                monthNames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
                today: 'Today',
                year: '{0}',
                month: '{0}',
                yearMonth: '{2}, {0}'
C
Catouse 已提交
138 139
            }
        },
C
Catouse 已提交
140 141 142
        data: {
            calendars: {
                defaultCal: {
143 144
                    color: '#229F24'
                }
C
Catouse 已提交
145
            },
146
            events: []
C
Catouse 已提交
147 148 149 150 151 152
        },
        // startView: "month",  // default view when load complete
        // startDate: 'today',  // default date when load complete
        limitEventTitle: true,
        storage: true,
        withHeader: true,
C
Catouse 已提交
153
        dragThenDrop: true, // drag an event and drop at another day,
C
Catouse 已提交
154
        // hideEmptyWeekends: false // Auto hide empty weekends
C
Catouse 已提交
155 156 157
    };

    // Sort events by start datetime
C
Catouse 已提交
158
    Calendar.prototype.sortEvents = function() {
C
Catouse 已提交
159
        var events = this.events;
C
Catouse 已提交
160
        if(!$.isArray(events)) {
C
Catouse 已提交
161 162 163
            events = [];
        }

C
Catouse 已提交
164
        events.sort(function(a, b) {
C
Catouse 已提交
165
            return a.start < b.start ? 1 : (a.start > b.start ? (-1) : 0);
C
Catouse 已提交
166
        });
C
Catouse 已提交
167 168

        this.events = events;
C
Catouse 已提交
169
    };
C
Catouse 已提交
170

C
Catouse 已提交
171
    Calendar.prototype.bindEvents = function() {
C
Catouse 已提交
172 173 174
        var $e = this.$,
            self = this;

C
Catouse 已提交
175
        $e.on('click', '.btn-today', function() {
C
Catouse 已提交
176 177 178
            self.date = new Date();
            self.display();
            self.callEvent('clickTodayBtn');
C
Catouse 已提交
179 180
        }).on('click', '.btn-next', function() {
            if(self.view === 'month') {
C
Catouse 已提交
181 182 183 184
                self.date.addMonths(1);
            }
            self.display();
            self.callEvent('clickNextBtn');
C
Catouse 已提交
185 186
        }).on('click', '.btn-prev', function() {
            if(self.view === 'month') {
C
Catouse 已提交
187 188 189 190
                self.date.addMonths(-1);
            }
            self.display();
            self.callEvent('clickPrevBtn');
C
Catouse 已提交
191 192
        }).on('click', '.event', function(event) {
            self.callEvent('clickEvent', {
C
Catouse 已提交
193
                element: this,
194 195 196
                event: $(this).data('event'),
                events: self.events
            });
C
Catouse 已提交
197
            event.stopPropagation();
C
Catouse 已提交
198 199
        }).on('click', '.cell-day', function() {
            self.callEvent('clickCell', {
C
Catouse 已提交
200
                element: this,
201
                view: self.view,
C
Catouse 已提交
202
                date: new Date($(this).children('.day').attr('data-date')),
203 204
                events: self.events
            });
C
Catouse 已提交
205 206 207
        });
    };

C
Catouse 已提交
208
    Calendar.prototype.addCalendars = function(calendars, silence) {
209
        var that = this;
C
Catouse 已提交
210 211
        if(!that.calendars) that.calendars = {};

C
Catouse 已提交
212
        if($.isPlainObject(calendars)) {
C
Catouse 已提交
213 214
            calendars = [calendars];
        }
C
Catouse 已提交
215 216 217 218 219
        $.each(calendars, function(index, cal) {
            if(!silence && !that.callEvent('beforeAddCalendars', {
                    newCalendar: cal,
                    data: that.data
                })) {
C
Catouse 已提交
220
                return;
C
Catouse 已提交
221 222
            }

C
Catouse 已提交
223 224 225 226 227 228 229 230
            if(!cal.color) cal.color = 'primary';
            if(!presetColors[cal.color.toLowerCase()]) {
                var c = new $.zui.Color(cal.color);
                cal.textColor = c.contrast().hexStr();
            } else {
                cal.presetColor = true;
            }
            that.calendars[cal.name] = cal;
231
        });
C
Catouse 已提交
232 233 234

        if(!silence) {
            that.display();
C
Catouse 已提交
235
            that.callEvent('addCalendars', {
C
Catouse 已提交
236 237 238 239
                newCalendars: calendars,
                data: that.data
            });
        }
C
Catouse 已提交
240 241
    };

C
Catouse 已提交
242
    Calendar.prototype.addEvents = function(events, silence) {
243
        var that = this;
C
Catouse 已提交
244 245
        if(!that.events) that.events = [];

C
Catouse 已提交
246
        if($.isPlainObject(events)) {
C
Catouse 已提交
247 248
            events = [events];
        }
C
Catouse 已提交
249 250 251 252 253
        $.each(events, function(index, e) {
            if(!silence && !that.callEvent('beforeAddEvent', {
                    newEvent: e,
                    data: that.data
                })) {
C
Catouse 已提交
254 255 256 257 258
                return;
            }

            var startType = typeof e.start;
            var endType = typeof e.end;
C
Catouse 已提交
259
            if(startType === NUMBER_TYPE_NAME || startType === STRING_TYPE_NAME) {
C
Catouse 已提交
260 261
                e.start = new Date(e.start);
            }
C
Catouse 已提交
262
            if(endType === NUMBER_TYPE_NAME || endType === STRING_TYPE_NAME) {
C
Catouse 已提交
263 264
                e.end = new Date(e.end);
            }
C
Catouse 已提交
265
            if(typeof e.id === UNDEFINED_TYPE_NAME) {
C
Catouse 已提交
266
                e.id = $.zui.uuid();
C
Catouse 已提交
267 268
            }

C
Catouse 已提交
269 270 271 272 273 274 275 276
            if(e.allDay) {
                e.start.clearTime();
                e.end.clearTime().addDays(1).addMilliseconds(-1);
            }

            e.days = calculateDays(e.start, e.end);

            that.events.push(e);
277
        });
C
Catouse 已提交
278 279 280 281

        if(!silence) {
            that.sortEvents();
            that.display();
C
Catouse 已提交
282
            that.callEvent('addEvents', {
C
Catouse 已提交
283 284 285 286
                newEvents: events,
                data: that.data
            });
        }
C
Catouse 已提交
287 288
    };

C
Catouse 已提交
289
    Calendar.prototype.getEvent = function(id) {
C
Catouse 已提交
290
        var events = this.events;
C
Catouse 已提交
291 292
        for(var i = 0; i < events.length; i++) {
            if(events[i].id == id) {
C
Catouse 已提交
293 294 295
                return events[i];
            }
        }
C
Catouse 已提交
296
        return null;
C
Catouse 已提交
297 298
    };

C
Catouse 已提交
299
    Calendar.prototype.updateEvents = function(events) {
300
        var eventsParams = {
C
Catouse 已提交
301 302 303 304
                data: this.data,
                changes: []
            },
            that = this;
C
Catouse 已提交
305

C
Catouse 已提交
306
        if($.isPlainObject(events)) {
C
Catouse 已提交
307 308 309
            events = [events];
        }
        var event, chgs, eventParam;
C
Catouse 已提交
310
        $.each(events, function(index, changes) {
C
Catouse 已提交
311 312
            event = changes.event;
            chgs = changes.changes;
313 314 315 316
            eventParam = {
                event: event,
                changes: []
            };
C
Catouse 已提交
317
            if(typeof event === STRING_TYPE_NAME) {
318
                event = that.getEvent(event);
C
Catouse 已提交
319
            }
C
Catouse 已提交
320 321
            if(event) {
                if($.isPlainObject(chgs)) {
C
Catouse 已提交
322 323
                    chgs = [chgs];
                }
C
Catouse 已提交
324 325
                $.each(changes, function(idx, chge) {
                    if(that.callEvent('beforeChange', {
C
Catouse 已提交
326 327 328 329
                            event: event,
                            change: chge.change,
                            to: chge.to,
                            from: event[chge.change]
C
Catouse 已提交
330 331
                        })) {
                        eventParam.changes.push($.extend(true, {}, chge, {
332 333
                            from: event[chge.change]
                        }));
C
Catouse 已提交
334 335 336 337 338 339 340
                        event[chge.change] = chge.to;
                    }
                });
            }
            eventsParams.changes.push(eventParam);
        });

341 342 343
        that.sortEvents();
        that.display();
        that.callEvent('change', eventsParams);
C
Catouse 已提交
344 345
    };

C
Catouse 已提交
346 347
    Calendar.prototype.removeEvents = function(events) {
        if(!$.isArray(events)) {
C
Catouse 已提交
348 349
            events = [events];
        }
C
Catouse 已提交
350 351
        var id, event, idx, evts = this.events,
            that = this,
352
            removedEvents = [];
C
Catouse 已提交
353
        $.each(events, function(index, value) {
C
Catouse 已提交
354 355
            id = $.isPlainObject(value) ? value.id : value;
            idx = -1;
C
Catouse 已提交
356 357
            for(var i = 0; i < evts.length; i++) {
                if(evts[i].id == id) {
C
Catouse 已提交
358 359 360 361 362 363
                    idx = i;
                    event = evts[i];
                    break;
                }
            }

C
Catouse 已提交
364
            if(idx >= 0 && that.callEvent('beforeRemoveEvent', {
C
Catouse 已提交
365 366 367
                    event: event,
                    eventId: id,
                    data: that.data
C
Catouse 已提交
368
                })) {
C
Catouse 已提交
369 370 371 372 373
                evts.splice(idx, 1);
                removedEvents.push(event);
            }
        });

374 375
        that.sortEvents();
        that.display();
C
Catouse 已提交
376
        that.callEvent('removeEvents', {
377
            removedEvents: removedEvents,
378
            data: that.data
379
        });
C
Catouse 已提交
380 381
    };

C
Catouse 已提交
382 383
    Calendar.prototype.getOptions = function(options) {
        this.options = $.extend({}, Calendar.DEFAULTS, this.$.data(), options);
C
Catouse 已提交
384 385
    };

C
Catouse 已提交
386 387
    Calendar.prototype.getLang = function() {
        this.lang = this.options.langs[this.options.lang || ($.zui && $.zui.clientLang ? $.zui.clientLang() : 'zh-cn')];
C
Catouse 已提交
388 389
    };

C
Catouse 已提交
390
    Calendar.prototype.display = function(view, date) {
391
        var that = this;
C
Catouse 已提交
392 393 394
        var viewType = typeof view;
        var dateType = typeof date;

C
Catouse 已提交
395
        if(viewType === UNDEFINED_TYPE_NAME) {
396
            view = that.view;
C
Catouse 已提交
397
        } else {
398
            that.view = view;
C
Catouse 已提交
399 400
        }

C
Catouse 已提交
401
        if(dateType === UNDEFINED_TYPE_NAME) {
402
            date = that.date;
C
Catouse 已提交
403
        } else {
404
            that.date = date;
C
Catouse 已提交
405 406
        }

C
Catouse 已提交
407
        if(date === 'today') {
C
Catouse 已提交
408
            date = new Date();
409
            that.date = date;
C
Catouse 已提交
410
        }
C
Catouse 已提交
411

C
Catouse 已提交
412
        if(typeof date === STRING_TYPE_NAME) {
C
Catouse 已提交
413
            date = new Date(date);
414
            that.date = date;
C
Catouse 已提交
415 416
        }

C
Catouse 已提交
417 418
        if(that.options.storage) {
            $.zui.store.pageSet(that.storeName, {
419 420 421
                date: date,
                view: view
            });
C
Catouse 已提交
422 423
        }

424 425 426 427
        var eventPramas = {
            view: view,
            date: date
        };
C
Catouse 已提交
428 429
        if(that.callEvent('beforeDisplay', eventPramas)) {
            switch(view) {
C
Catouse 已提交
430
                case 'month':
431
                    that.displayMonth(date);
C
Catouse 已提交
432 433 434
                    break;
            }

435
            that.callEvent('display', eventPramas);
C
Catouse 已提交
436 437 438
        }
    };

C
Catouse 已提交
439
    Calendar.prototype.displayMonth = function(date) {
440
        var that = this;
C
Catouse 已提交
441
        date = date || that.date;
442 443
        var options = that.options,
            lang = that.lang,
C
Catouse 已提交
444
            i,
445 446
            $views = that.$views,
            $e = that.$;
C
Catouse 已提交
447

C
Catouse 已提交
448
        var $view = that.$monthView;
C
Catouse 已提交
449
        if(!$view.length) {
C
Catouse 已提交
450 451 452 453 454 455
            $view = $('<div class="calendar-view month"><table class="table table-bordered"><thead><tr class="week-head"></tr></thead><tbody class="month-days"></tbody></table></div>');

            var $weekHead = $view.find('.week-head'),
                $monthDays = $view.find('.month-days'),
                $tr;

C
Catouse 已提交
456
            for(i = 0; i < 7; i++) {
C
Catouse 已提交
457
                $('<th>' + lang.weekNames[i] + '</th>').toggleClass('weekend-head', i >= 5).appendTo($weekHead);
C
Catouse 已提交
458 459
            }

C
Catouse 已提交
460
            for(i = 0; i < 6; i++) {
C
Catouse 已提交
461
                $tr = $('<tr class="week-days"></tr>');
C
Catouse 已提交
462
                for(var j = 0; j < 7; j++) {
C
Catouse 已提交
463
                    $('<td class="cell-day"><div class="day"><div class="heading"><span class="month"></span> <span class="number"></span></div><div class="content"><div class="events"></div></div></div></td>').toggleClass('weekend-day', j >= 5).appendTo($tr);
C
Catouse 已提交
464 465 466 467 468
                }
                $monthDays.append($tr);
            }

            $views.append($view);
C
Catouse 已提交
469
            that.$monthView = $view;
C
Catouse 已提交
470 471 472
        }

        var $weeks = $view.find('.week-days'),
473
            $days = $view.find('.day'),
C
Catouse 已提交
474
            firstDayOfMonth = getFirstDayOfMonth(date),
C
Catouse 已提交
475
            // lastDayOfMonth = getLastDayOfMonth(date),
C
Catouse 已提交
476 477 478 479 480 481 482 483
            $week,
            $day,
            $cell,
            year,
            day,
            month,
            today = new Date();
        var firstDay = getNearbyLastWeekDay(firstDayOfMonth),
484 485
            thisYear = date.getFullYear(),
            thisMonth = date.getMonth(),
C
Catouse 已提交
486
            todayMonth = today.getMonth(),
487 488
            todayYear = today.getFullYear(),
            todayDate = today.getDate();
C
Catouse 已提交
489 490
        var lastDay = firstDay.clone().addDays(6 * 7).addMilliseconds(-1),
            printDate = firstDay.clone().addDays(1).addMilliseconds(-1);
C
Catouse 已提交
491
        var events = that.getEvents(firstDay, lastDay),
C
Catouse 已提交
492 493
            calendars = that.calendars,
            printDateId, isFirstDayOfWeek, $event, cal, $dayEvents;
C
Catouse 已提交
494

C
Catouse 已提交
495
        var isEmptyWeekends = true;
C
Catouse 已提交
496
        $weeks.each(function(weekIdx) {
C
Catouse 已提交
497
            $week = $(this);
C
Catouse 已提交
498
            $week.find('.day').each(function(dayIndex) {
C
Catouse 已提交
499
                isFirstDayOfWeek = dayIndex === 0;
C
Catouse 已提交
500 501 502 503 504
                $day = $(this);
                $cell = $day.closest('.cell-day');
                year = printDate.getFullYear();
                day = printDate.getDate();
                month = printDate.getMonth();
C
Catouse 已提交
505 506
                printDateId = printDate.toDateString();
                $day.attr('data-date', printDateId);
C
Catouse 已提交
507 508 509 510 511 512 513 514 515
                $day.find('.heading > .number').text(day);

                $day.find('.heading > .month')
                    .toggle((weekIdx === 0 && dayIndex === 0) || day === 1)
                    .text(((month === 0 && day === 1) ? (lang.year.format(year) + ' ') : '') + lang.monthNames[month]);
                $cell.toggleClass('current-month', month === thisMonth);
                $cell.toggleClass('current', (day === todayDate && month === todayMonth && year === todayYear));
                $cell.toggleClass('past', printDate < today);
                $cell.toggleClass('future', printDate > today);
C
Catouse 已提交
516 517 518
                $dayEvents = $day.find('.events').empty();

                var dayEvents = events[printDateId];
C
Catouse 已提交
519
                if(dayEvents) {
C
Catouse 已提交
520
                    var eventsMap = dayEvents.events,
C
Catouse 已提交
521 522 523
                        stripCount = 0,
                        e;
                    for(i = 0; i <= dayEvents.maxPos; ++i) {
C
Catouse 已提交
524 525 526 527 528
                        e = eventsMap[i];
                        if(!e || (e.placeholder && !isFirstDayOfWeek)) {
                            stripCount++;
                            continue;
                        }
C
Catouse 已提交
529 530 531
                        if (isEmptyWeekends && dayIndex >= 5) {
                            isEmptyWeekends = false;
                        }
C
Catouse 已提交
532 533 534 535 536
                        $event = $('<div data-id="' + e.id + '" class="event" title="' + e.desc + '"><span class="time">' + e.start.format('hh:mm') + '</span> <span class="title">' + e.title + '</span></div>');
                        $event.find('.time').toggle(!e.allDay);
                        $event.data('event', e);
                        $event.attr('data-days', e.days);

C
Catouse 已提交
537
                        if(e.calendar) {
C
Catouse 已提交
538
                            cal = calendars[e.calendar];
C
Catouse 已提交
539
                            if(cal) {
C
Catouse 已提交
540 541 542
                                if(cal.presetColor) {
                                    $event.addClass('color-' + cal.color);
                                } else {
C
Catouse 已提交
543 544 545 546
                                    $event.css({
                                        'background-color': cal.color,
                                        color: cal.textColor
                                    });
C
Catouse 已提交
547 548 549 550
                                }
                            }
                        }

C
Catouse 已提交
551 552
                        if(e.days) {
                            if(!e.placeholder) {
C
Catouse 已提交
553
                                $event.css('width', Math.min(7 - dayIndex, e.days) + '00%');
C
Catouse 已提交
554
                            } else if(isFirstDayOfWeek) {
C
Catouse 已提交
555 556 557 558 559 560 561 562 563 564 565 566
                                $event.css('width', Math.min(7, e.days - e.holderPos) + '00%');
                            }
                        }

                        if(stripCount > 0) {
                            $event.css('margin-top', stripCount * 22);
                            stripCount = 0;
                        }

                        $dayEvents.append($event);
                    }
                }
C
Catouse 已提交
567 568 569 570

                printDate.addDays(1);
            });
        });
C
Catouse 已提交
571 572 573
        if (options.hideEmptyWeekends) {
            $view.toggleClass('weekends-empty', isEmptyWeekends);
        }
C
Catouse 已提交
574

C
Catouse 已提交
575
        if(options.withHeader) {
C
Catouse 已提交
576
            that.$caption.text(lang.yearMonth.format(thisYear, thisMonth + 1, lang.monthNames[thisMonth]));
C
Catouse 已提交
577
            that.$todayBtn.toggleClass('disabled', thisMonth === todayMonth && thisYear === todayYear);
C
Catouse 已提交
578 579
        }

C
Catouse 已提交
580
        if(options.dragThenDrop) {
C
Catouse 已提交
581 582 583 584
            if(!$.fn.droppable) {
                return console.error('Calendar dragThenDrop option requires droppable.js');
            }
            if(!$view.data('zui.droppable')) {
C
Catouse 已提交
585 586
                $view.droppable($.extend({
                    target: '.cell-day',
C
Catouse 已提交
587
                    selector: '.event',
C
Catouse 已提交
588 589
                    flex: true,
                    start: function() {
C
Catouse 已提交
590
                        that.$.addClass('event-dragging');
C
Catouse 已提交
591 592 593
                    },
                    drop: function(e) {
                        var et = e.element.data('event'),
C
Catouse 已提交
594
                            newDate = e.target.children('.day').attr('data-date');
C
Catouse 已提交
595 596 597 598 599 600 601 602
                        if(!et || !newDate) return;
                        var startDate = et.start.clone();
                        if(startDate.toDateString() != newDate) {
                            newDate = new Date(newDate);
                            newDate.setHours(startDate.getHours());
                            newDate.setMinutes(startDate.getMinutes());
                            newDate.setSeconds(startDate.getSeconds());

C
Catouse 已提交
603
                            if(that.callEvent('beforeChange', {
604
                                    event: et,
C
Catouse 已提交
605 606 607 608 609 610 611 612 613 614
                                    change: 'start',
                                    to: newDate
                                })) {
                                var oldEnd = et.end.clone();

                                et.end.addMilliseconds(et.end.getTime() - startDate.getTime());
                                et.start = newDate;

                                that.display();

C
Catouse 已提交
615 616
                                that.callEvent('change', {
                                    data: that.data,
C
Catouse 已提交
617
                                    changes: [{
C
Catouse 已提交
618 619 620 621 622 623 624 625 626 627
                                        event: et,
                                        changes: [{
                                            change: 'start',
                                            from: startDate,
                                            to: et.start
                                        }, {
                                            change: 'end',
                                            from: oldEnd,
                                            to: et.end
                                        }]
628
                                    }]
C
Catouse 已提交
629 630
                                });
                            }
C
Catouse 已提交
631
                        }
C
Catouse 已提交
632 633
                    },
                    finish: function() {
C
Catouse 已提交
634
                        that.$.removeClass('event-dragging');
C
Catouse 已提交
635
                    }
C
Catouse 已提交
636
                }, ($.isPlainObject(options.dragThenDrop)) ? options.dragThenDrop : null));
C
Catouse 已提交
637
            }
C
Catouse 已提交
638 639 640
        }
    };

C
Catouse 已提交
641
    Calendar.prototype.getEvents = function(start, end) {
C
Catouse 已提交
642 643
        var events = {};
        var calendars = this.calendars;
C
Catouse 已提交
644
        var addEventToDay = function(day, event, position) {
C
Catouse 已提交
645 646
            var dayId = day.toDateString();
            var dayEvents = events[dayId];
C
Catouse 已提交
647 648 649 650 651
            if(!dayEvents) {
                dayEvents = {
                    maxPos: -1,
                    events: {}
                };
C
Catouse 已提交
652 653
            }

C
Catouse 已提交
654 655 656
            if(typeof position === 'undefined') {
                for(var i = 0; i < 100; ++i) {
                    if(!dayEvents.events[i]) {
C
Catouse 已提交
657 658 659 660 661 662 663 664 665 666 667
                        position = i;
                        break;
                    }
                }
            }

            dayEvents.maxPos = Math.max(position, dayEvents.maxPos);
            dayEvents.events[position] = event;
            events[dayId] = dayEvents;
            return position;
        };
C
Catouse 已提交
668 669
        $.each(this.events, function(index, e) {
            if(e.start >= start && e.start <= end) {
C
Catouse 已提交
670
                var position = addEventToDay(e.start, e);
C
Catouse 已提交
671 672 673 674 675 676 677 678
                if(e.days > 1) {
                    var placeholder = $.extend({
                        placeholder: true
                    }, e);
                    everyDayTo(e.start.clone().addDays(1), e.end, function(thisDay, i) {
                        addEventToDay(thisDay.clone(), $.extend({
                            holderPos: i
                        }, placeholder), position);
C
Catouse 已提交
679 680 681 682 683 684 685
                    });
                }
            }
        });
        return events;
    };

C
Catouse 已提交
686
    Calendar.prototype.callEvent = function(name, params) {
C
Catouse 已提交
687
        var result = this.$.callEvent(name + '.' + NAME, params, this);
C
Catouse 已提交
688
        return !(result.result !== undefined && (!result.result));
C
Catouse 已提交
689 690
    };

C
Catouse 已提交
691 692
    $.fn.calendar = function(option) {
        return this.each(function() {
693
            var $this = $(this);
C
Catouse 已提交
694
            var data = $this.data(NAME);
C
Catouse 已提交
695 696
            var options = typeof option == 'object' && option;

C
Catouse 已提交
697
            if(!data) $this.data(NAME, (data = new Calendar(this, options)));
C
Catouse 已提交
698

C
Catouse 已提交
699
            if(typeof option == STRING_TYPE_NAME) data[option]();
C
Catouse 已提交
700 701 702 703
        });
    };

    $.fn.calendar.Constructor = Calendar;
C
Catouse 已提交
704
}(jQuery, window));
C
Catouse 已提交
705