zui.calendar.js 23.9 KB
Newer Older
C
Catouse 已提交
1
/*!
C
Catouse 已提交
2
 * ZUI: 日历 - v1.7.0 - 2017-06-17
C
Catouse 已提交
3 4
 * http://zui.sexy
 * GitHub: https://github.com/easysoft/zui.git 
C
Catouse 已提交
5
 * Copyright (c) 2017 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,
153
        dragThenDrop: true // drag an event and drop at another day
C
Catouse 已提交
154 155 156
    };

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

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

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

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

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

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

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

C
Catouse 已提交
222 223 224 225 226 227 228 229
            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;
230
        });
C
Catouse 已提交
231 232 233

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

C
Catouse 已提交
447
        var $view = that.$monthView;
C
Catouse 已提交
448
        if(!$view.length) {
C
Catouse 已提交
449 450 451 452 453 454
            $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 已提交
455
            for(i = 0; i < 7; i++) {
C
Catouse 已提交
456 457 458
                $weekHead.append('<th>' + lang.weekNames[i] + '</th>');
            }

C
Catouse 已提交
459
            for(i = 0; i < 6; i++) {
C
Catouse 已提交
460
                $tr = $('<tr class="week-days"></tr>');
C
Catouse 已提交
461
                for(var j = 0; j < 7; j++) {
C
Catouse 已提交
462 463 464 465 466 467
                    $tr.append('<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>');
                }
                $monthDays.append($tr);
            }

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

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

C
Catouse 已提交
494
        $weeks.each(function(weekIdx) {
C
Catouse 已提交
495
            $week = $(this);
C
Catouse 已提交
496
            $week.find('.day').each(function(dayIndex) {
C
Catouse 已提交
497
                isFirstDayOfWeek = dayIndex === 0;
C
Catouse 已提交
498 499 500 501 502
                $day = $(this);
                $cell = $day.closest('.cell-day');
                year = printDate.getFullYear();
                day = printDate.getDate();
                month = printDate.getMonth();
C
Catouse 已提交
503 504
                printDateId = printDate.toDateString();
                $day.attr('data-date', printDateId);
C
Catouse 已提交
505 506 507 508 509 510 511 512 513
                $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 已提交
514 515 516
                $dayEvents = $day.find('.events').empty();

                var dayEvents = events[printDateId];
C
Catouse 已提交
517
                if(dayEvents) {
C
Catouse 已提交
518
                    var eventsMap = dayEvents.events,
C
Catouse 已提交
519 520 521
                        stripCount = 0,
                        e;
                    for(i = 0; i <= dayEvents.maxPos; ++i) {
C
Catouse 已提交
522 523 524 525 526 527 528 529 530 531
                        e = eventsMap[i];
                        if(!e || (e.placeholder && !isFirstDayOfWeek)) {
                            stripCount++;
                            continue;
                        }
                        $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 已提交
532
                        if(e.calendar) {
C
Catouse 已提交
533
                            cal = calendars[e.calendar];
C
Catouse 已提交
534
                            if(cal) {
C
Catouse 已提交
535 536 537
                                if(cal.presetColor) {
                                    $event.addClass('color-' + cal.color);
                                } else {
C
Catouse 已提交
538 539 540 541
                                    $event.css({
                                        'background-color': cal.color,
                                        color: cal.textColor
                                    });
C
Catouse 已提交
542 543 544 545
                                }
                            }
                        }

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

                printDate.addDays(1);
            });
        });

C
Catouse 已提交
567
        if(options.withHeader) {
C
Catouse 已提交
568
            that.$caption.text(lang.yearMonth.format(thisYear, thisMonth + 1, lang.monthNames[thisMonth]));
C
Catouse 已提交
569
            that.$todayBtn.toggleClass('disabled', thisMonth === todayMonth && thisYear === todayYear);
C
Catouse 已提交
570 571
        }

C
Catouse 已提交
572
        if(options.dragThenDrop) {
C
Catouse 已提交
573 574 575 576 577 578 579
            if(!$.fn.droppable) {
                return console.error('Calendar dragThenDrop option requires droppable.js');
            }
            if(!$view.data('zui.droppable')) {
                $view.droppable({
                    target: '.day',
                    selector: '.event',
C
Catouse 已提交
580 581
                    flex: true,
                    start: function() {
C
Catouse 已提交
582
                        that.$.addClass('event-dragging');
C
Catouse 已提交
583 584 585 586 587 588 589 590 591 592 593 594
                    },
                    drop: function(e) {
                        var et = e.element.data('event'),
                            newDate = e.target.attr('data-date');
                        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 已提交
595
                            if(that.callEvent('beforeChange', {
596
                                    event: et,
C
Catouse 已提交
597 598 599 600 601 602 603 604 605 606
                                    change: 'start',
                                    to: newDate
                                })) {
                                var oldEnd = et.end.clone();

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

                                that.display();

C
Catouse 已提交
607 608
                                that.callEvent('change', {
                                    data: that.data,
C
Catouse 已提交
609
                                    changes: [{
C
Catouse 已提交
610 611 612 613 614 615 616 617 618 619
                                        event: et,
                                        changes: [{
                                            change: 'start',
                                            from: startDate,
                                            to: et.start
                                        }, {
                                            change: 'end',
                                            from: oldEnd,
                                            to: et.end
                                        }]
620
                                    }]
C
Catouse 已提交
621 622
                                });
                            }
C
Catouse 已提交
623
                        }
C
Catouse 已提交
624 625
                    },
                    finish: function() {
C
Catouse 已提交
626
                        that.$.removeClass('event-dragging');
C
Catouse 已提交
627
                    }
C
Catouse 已提交
628 629
                });
            }
C
Catouse 已提交
630 631 632
        }
    };

C
Catouse 已提交
633
    Calendar.prototype.getEvents = function(start, end) {
C
Catouse 已提交
634 635
        var events = {};
        var calendars = this.calendars;
C
Catouse 已提交
636
        var addEventToDay = function(day, event, position) {
C
Catouse 已提交
637 638
            var dayId = day.toDateString();
            var dayEvents = events[dayId];
C
Catouse 已提交
639 640 641 642 643
            if(!dayEvents) {
                dayEvents = {
                    maxPos: -1,
                    events: {}
                };
C
Catouse 已提交
644 645
            }

C
Catouse 已提交
646 647 648
            if(typeof position === 'undefined') {
                for(var i = 0; i < 100; ++i) {
                    if(!dayEvents.events[i]) {
C
Catouse 已提交
649 650 651 652 653 654 655 656 657 658 659
                        position = i;
                        break;
                    }
                }
            }

            dayEvents.maxPos = Math.max(position, dayEvents.maxPos);
            dayEvents.events[position] = event;
            events[dayId] = dayEvents;
            return position;
        };
C
Catouse 已提交
660 661
        $.each(this.events, function(index, e) {
            if(e.start >= start && e.start <= end) {
C
Catouse 已提交
662
                var position = addEventToDay(e.start, e);
C
Catouse 已提交
663 664 665 666 667 668 669 670
                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 已提交
671 672 673 674 675 676 677
                    });
                }
            }
        });
        return events;
    };

C
Catouse 已提交
678
    Calendar.prototype.callEvent = function(name, params) {
C
Catouse 已提交
679
        var result = this.$.callEvent(name + '.' + NAME, params, this);
C
Catouse 已提交
680
        return !(result.result !== undefined && (!result.result));
C
Catouse 已提交
681 682
    };

C
Catouse 已提交
683 684
    $.fn.calendar = function(option) {
        return this.each(function() {
685
            var $this = $(this);
C
Catouse 已提交
686
            var data = $this.data(NAME);
C
Catouse 已提交
687 688
            var options = typeof option == 'object' && option;

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

C
Catouse 已提交
691
            if(typeof option == STRING_TYPE_NAME) data[option]();
C
Catouse 已提交
692 693 694 695
        });
    };

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