ui.datepicker.js 18.0 KB
Newer Older
Hzp_D's avatar
Hzp_D 已提交
1 2 3 4
'use strict';

var $ = require('jquery');
var UI = require('./core');
Hzp_D's avatar
Hzp_D 已提交
5
var $doc = $(document);
Hzp_D's avatar
Hzp_D 已提交
6 7 8 9 10 11 12 13 14 15

/**
 * bootstrap-datepicker.js
 * @via http://www.eyecon.ro/bootstrap-datepicker
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

// Picker object

var Datepicker = function(element, options) {
Hzp_D's avatar
Hzp_D 已提交
16 17 18 19 20 21 22 23 24
  this.$element = $(element);
  this.options = $.extend({}, Datepicker.DEFAULTS,
      $.fn.datepicker.defaults, options);

  this.format = DPGlobal.parseFormat(this.options.format);
  this.$element.data('am-date', this.options.date);
  this.language = this.getLocale(this.options.locale);
  this.theme = this.options.theme;
  this.$picker = $(DPGlobal.template)
Hzp_D's avatar
Hzp_D 已提交
25 26 27 28 29 30
                  .appendTo('body')
                  .on({
                    click: $.proxy(this.click, this)
                    // mousedown: $.proxy(this.mousedown, this)
                  });

Hzp_D's avatar
Hzp_D 已提交
31 32 33
  this.isInput = this.$element.is('input');
  this.component = this.$element.is('.am-datepicker-date') ?
                     this.$element.find('.am-datepicker-add-on') : false;
Hzp_D's avatar
Hzp_D 已提交
34
  if (this.isInput) {
Hzp_D's avatar
Hzp_D 已提交
35
    this.$element.on({
Hzp_D's avatar
Hzp_D 已提交
36
      'click.datepicker.amui': $.proxy(this.open, this),
Hzp_D's avatar
Hzp_D 已提交
37
      // blur: $.proxy(this.close, this),
Hzp_D's avatar
Hzp_D 已提交
38
      'keyup.datepicker.amui': $.proxy(this.update, this)
Hzp_D's avatar
Hzp_D 已提交
39 40 41
    });
  } else {
    if (this.component) {
Hzp_D's avatar
Hzp_D 已提交
42
      this.component.on('click.datepicker.amui', $.proxy(this.open, this));
Hzp_D's avatar
Hzp_D 已提交
43
    } else {
Hzp_D's avatar
Hzp_D 已提交
44
      this.$element.on('click.datepicker.amui', $.proxy(this.open, this));
Hzp_D's avatar
Hzp_D 已提交
45 46 47
    }
  }

Hzp_D's avatar
Hzp_D 已提交
48
  this.minViewMode = this.options.minViewMode;
Hzp_D's avatar
Hzp_D 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

  if (typeof this.minViewMode === 'string') {
    switch (this.minViewMode) {
      case 'months':
        this.minViewMode = 1;
        break;
      case 'years':
        this.minViewMode = 2;
        break;
      default:
        this.minViewMode = 0;
        break;
    }
  }

Hzp_D's avatar
Hzp_D 已提交
64
  this.viewMode = this.options.viewMode;
Hzp_D's avatar
Hzp_D 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

  if (typeof this.viewMode === 'string') {
    switch (this.viewMode) {
      case 'months':
        this.viewMode = 1;
        break;
      case 'years':
        this.viewMode = 2;
        break;
      default:
        this.viewMode = 0;
        break;
    }
  }

  this.startViewMode = this.viewMode;
Hzp_D's avatar
Hzp_D 已提交
81
  this.weekStart = this.options.weekStart;
Hzp_D's avatar
Hzp_D 已提交
82
  this.weekEnd = this.weekStart === 0 ? 6 : this.weekStart - 1;
Hzp_D's avatar
Hzp_D 已提交
83
  this.onRender = this.options.onRender;
Hzp_D's avatar
Hzp_D 已提交
84

Hzp_D's avatar
Hzp_D 已提交
85
  this.addTheme();
Hzp_D's avatar
Hzp_D 已提交
86 87 88 89 90 91 92 93 94
  this.fillDow();
  this.fillMonths();
  this.update();
  this.showMode();
};

Datepicker.prototype = {
  constructor: Datepicker,

Hzp_D's avatar
Hzp_D 已提交
95 96 97 98 99
  open: function(e) {
    this.$picker.show();
    this.height = this.component ?
        this.component.outerHeight() : this.$element.outerHeight();

Hzp_D's avatar
Hzp_D 已提交
100
    this.place();
Hzp_D's avatar
Hzp_D 已提交
101
    $(window).on('resize.datepicker.amui', $.proxy(this.place, this));
Hzp_D's avatar
Hzp_D 已提交
102 103 104 105 106
    if (e) {
      e.stopPropagation();
      e.preventDefault();
    }
    var that = this;
Hzp_D's avatar
Hzp_D 已提交
107
    $(document).on('click.datepicker.amui', function(ev) {
Hzp_D's avatar
Hzp_D 已提交
108
      if ($(ev.target).closest('.am-datepicker').length == 0) {
Hzp_D's avatar
Hzp_D 已提交
109
        that.close();
Hzp_D's avatar
Hzp_D 已提交
110 111
      }
    });
Hzp_D's avatar
Hzp_D 已提交
112 113
    this.$element.trigger({
      type: 'open',
Hzp_D's avatar
Hzp_D 已提交
114 115 116 117
      date: this.date
    });
  },

Hzp_D's avatar
Hzp_D 已提交
118 119
  close: function() {
    this.$picker.hide();
Hzp_D's avatar
Hzp_D 已提交
120
    $(window).off('resize.datepicker.amui', this.place);
Hzp_D's avatar
Hzp_D 已提交
121 122 123
    this.viewMode = this.startViewMode;
    this.showMode();
    if (!this.isInput) {
Hzp_D's avatar
Hzp_D 已提交
124
      $(document).off('click.datepicker.amui', this.close);
Hzp_D's avatar
Hzp_D 已提交
125 126
    }
    // this.set();
Hzp_D's avatar
Hzp_D 已提交
127 128
    this.$element.trigger({
      type: 'close',
Hzp_D's avatar
Hzp_D 已提交
129 130 131 132 133 134 135 136
      date: this.date
    });
  },

  set: function() {
    var formated = DPGlobal.formatDate(this.date, this.format);
    if (!this.isInput) {
      if (this.component) {
Hzp_D's avatar
Hzp_D 已提交
137
        this.$element.find('input').prop('value', formated);
Hzp_D's avatar
Hzp_D 已提交
138
      }
Hzp_D's avatar
Hzp_D 已提交
139
      this.$element.data('am-date', formated);
Hzp_D's avatar
Hzp_D 已提交
140
    } else {
Hzp_D's avatar
Hzp_D 已提交
141
      this.$element.prop('value', formated);
Hzp_D's avatar
Hzp_D 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    }
  },

  setValue: function(newDate) {
    if (typeof newDate === 'string') {
      this.date = DPGlobal.parseDate(newDate, this.format);
    } else {
      this.date = new Date(newDate);
    }
    this.set();

    this.viewDate = new Date(this.date.getFullYear(),
                  this.date.getMonth(), 1, 0, 0, 0, 0);

    this.fill();
  },

  place: function() {
Hzp_D's avatar
Hzp_D 已提交
160
    var offset = this.component ?
Hzp_D's avatar
Hzp_D 已提交
161
                 this.component.offset() : this.$element.offset();
Hzp_D's avatar
Hzp_D 已提交
162
    var $width = this.component ?
Hzp_D's avatar
Hzp_D 已提交
163
        this.component.width() : this.$element.width();
Hzp_D's avatar
Hzp_D 已提交
164 165 166 167
    var top = offset.top + this.height;
    var left = offset.left;
    var right = $doc.width() - offset.left - $width;
    var isOutView = this.isOutView();
Hzp_D's avatar
Hzp_D 已提交
168 169
    this.$picker.removeClass('am-datepicker-right');
    this.$picker.removeClass('am-datepicker-up');
Hzp_D's avatar
Hzp_D 已提交
170 171
    if ($doc.width() > 640) {
      if (isOutView.outRight) {
Hzp_D's avatar
Hzp_D 已提交
172 173
        this.$picker.addClass('am-datepicker-right');
        this.$picker.css({
Hzp_D's avatar
Hzp_D 已提交
174 175 176 177 178 179 180
          top: top,
          left: 'auto',
          right: right
        });
        return
      }
      if (isOutView.outBottom) {
Hzp_D's avatar
Hzp_D 已提交
181 182
        this.$picker.addClass('am-datepicker-up');
        top = offset.top - this.$picker.outerHeight(true);
Hzp_D's avatar
Hzp_D 已提交
183 184 185 186
      }
    } else {
      left = 0;
    }
Hzp_D's avatar
Hzp_D 已提交
187
    this.$picker.css({
Hzp_D's avatar
Hzp_D 已提交
188 189
      top: top,
      left: left
Hzp_D's avatar
Hzp_D 已提交
190 191 192 193 194
    });
  },

  update: function(newDate) {
    this.date = DPGlobal.parseDate(
Hzp_D's avatar
Hzp_D 已提交
195
            typeof newDate === 'string' ? newDate : (this.isInput ?
Hzp_D's avatar
Hzp_D 已提交
196
                this.$element.prop('value') : this.$element.data('am-date')),
Hzp_D's avatar
Hzp_D 已提交
197 198 199 200 201 202 203 204 205 206 207
        this.format
    );
    this.viewDate = new Date(this.date.getFullYear(),
        this.date.getMonth(), 1, 0, 0, 0, 0);
    this.fill();
  },

  fillDow: function() {
    var dowCnt = this.weekStart;
    var html = '<tr>';
    while (dowCnt < this.weekStart + 7) {
Hzp_D's avatar
Hzp_D 已提交
208 209 210
      html += '<th class="am-datepicker-dow">' +
          Datepicker.locales[this.language].daysMin[(dowCnt++) % 7] +
          '</th>';
Hzp_D's avatar
Hzp_D 已提交
211 212
    }
    html += '</tr>';
Hzp_D's avatar
Hzp_D 已提交
213
    this.$picker.find('.am-datepicker-days thead').append(html);
Hzp_D's avatar
Hzp_D 已提交
214 215 216 217 218 219
  },

  fillMonths: function() {
    var html = '';
    var i = 0;
    while (i < 12) {
Hzp_D's avatar
Hzp_D 已提交
220 221
      html += '<span class="am-datepicker-month">' +
          Datepicker.locales[this.language].monthsShort[i++] + '</span>';
Hzp_D's avatar
Hzp_D 已提交
222
    }
Hzp_D's avatar
Hzp_D 已提交
223
    this.$picker.find('.am-datepicker-months td').append(html);
Hzp_D's avatar
Hzp_D 已提交
224 225 226 227 228 229 230 231 232
  },

  fill: function() {
    var d = new Date(this.viewDate);
    var year = d.getFullYear();
    var month = d.getMonth();
    var currentDate = this.date.valueOf();

    var prevMonth = new Date(year, month - 1, 28, 0, 0, 0, 0);
Hzp_D's avatar
Hzp_D 已提交
233 234 235 236 237
    var day = DPGlobal
        .getDaysInMonth(prevMonth.getFullYear(), prevMonth.getMonth());

    var daysSelect = this.$picker
        .find('.am-datepicker-days .am-datepicker-select');
Hzp_D's avatar
Hzp_D 已提交
238

Hzp_D's avatar
Hzp_D 已提交
239
    if (this.language === 'zh_CN') {
Hzp_D's avatar
Hzp_D 已提交
240 241
      daysSelect.text(year + Datepicker.locales[this.language].year[0] +
          ' ' + Datepicker.locales[this.language].months[month]);
Hzp_D's avatar
Hzp_D 已提交
242
    } else {
Hzp_D's avatar
Hzp_D 已提交
243 244
      daysSelect.text(Datepicker.locales[this.language].months[month] +
          ' ' + year);
Hzp_D's avatar
Hzp_D 已提交
245 246 247 248 249 250 251 252 253 254
    }

    prevMonth.setDate(day);
    prevMonth.setDate(day - (prevMonth.getDay() - this.weekStart + 7) % 7);

    var nextMonth = new Date(prevMonth);
    nextMonth.setDate(nextMonth.getDate() + 42);
    nextMonth = nextMonth.valueOf();
    var html = [];

Hzp_D's avatar
Hzp_D 已提交
255 256 257
    var className;
    var prevY;
    var prevM;
Hzp_D's avatar
Hzp_D 已提交
258 259 260 261 262

    while (prevMonth.valueOf() < nextMonth) {
      if (prevMonth.getDay() === this.weekStart) {
        html.push('<tr>');
      }
M
Minwe 已提交
263
      className = this.onRender(prevMonth);
Hzp_D's avatar
Hzp_D 已提交
264 265 266
      prevY = prevMonth.getFullYear();
      prevM = prevMonth.getMonth();
      if ((prevM < month &&  prevY === year) ||  prevY < year) {
M
Minwe 已提交
267
        className += ' am-datepicker-old';
Hzp_D's avatar
Hzp_D 已提交
268
      } else if ((prevM > month && prevY === year) || prevY > year) {
M
Minwe 已提交
269
        className += ' am-datepicker-new';
Hzp_D's avatar
Hzp_D 已提交
270 271
      }
      if (prevMonth.valueOf() === currentDate) {
M
Minwe 已提交
272
        className += ' am-active';
Hzp_D's avatar
Hzp_D 已提交
273
      }
Hzp_D's avatar
Hzp_D 已提交
274 275 276
      html.push('<td class="am-datepicker-day ' +
          className + '">' + prevMonth.getDate() + '</td>');

Hzp_D's avatar
Hzp_D 已提交
277 278 279 280 281 282 283
      if (prevMonth.getDay() === this.weekEnd) {
        html.push('</tr>');
      }
      prevMonth.setDate(prevMonth.getDate() + 1);

    }

Hzp_D's avatar
Hzp_D 已提交
284 285 286
    this.$picker.find('.am-datepicker-days tbody')
        .empty().append(html.join(''));

Hzp_D's avatar
Hzp_D 已提交
287 288
    var currentYear = this.date.getFullYear();

Hzp_D's avatar
Hzp_D 已提交
289
    var months = this.$picker.find('.am-datepicker-months')
Hzp_D's avatar
Hzp_D 已提交
290 291 292 293 294 295 296 297 298 299 300
        .find('.am-datepicker-select')
        .text(year);
    months = months.end()
              .find('span').removeClass('am-active');

    if (currentYear === year) {
      months.eq(this.date.getMonth()).addClass('am-active');
    }

    html = '';
    year = parseInt(year / 10, 10) * 10;
Hzp_D's avatar
Hzp_D 已提交
301
    var yearCont = this.$picker
Hzp_D's avatar
Hzp_D 已提交
302 303 304 305 306 307 308 309
        .find('.am-datepicker-years')
        .find('.am-datepicker-select')
        .text(year + '-' + (year + 9))
        .end()
        .find('td');

    year -= 1;
    for (var i = -1; i < 11; i++) {
Hzp_D's avatar
Hzp_D 已提交
310 311 312
      html += '<span class="' +
          (i === -1 || i === 10 ? ' am-datepicker-old' : '') +
          (currentYear === year ? ' am-active' : '') + '">' + year + '</span>';
Hzp_D's avatar
Hzp_D 已提交
313 314 315 316 317 318 319 320 321
      year += 1;
    }
    yearCont.html(html);

  },

  click: function(event) {
    event.stopPropagation();
    event.preventDefault();
Hzp_D's avatar
Hzp_D 已提交
322 323
    var month;
    var year;
Hzp_D's avatar
Hzp_D 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336

    var target = $(event.target).closest('span, td, th');
    if (target.length === 1) {
      switch (target[0].nodeName.toLowerCase()) {
        case 'th':
          switch (target[0].className) {
            case 'am-datepicker-switch':
              this.showMode(1);
              break;
            case 'am-datepicker-prev':
            case 'am-datepicker-next':
              this.viewDate['set' + DPGlobal.modes[this.viewMode].navFnc].call(
                  this.viewDate,
Hzp_D's avatar
Hzp_D 已提交
337 338 339 340 341
                      this.viewDate
                          ['get' + DPGlobal.modes[this.viewMode].navFnc]
                          .call(this.viewDate) +
                      DPGlobal.modes[this.viewMode].navStep *
                      (target[0].className === 'am-datepicker-prev' ? -1 : 1)
Hzp_D's avatar
Hzp_D 已提交
342 343 344 345 346 347 348 349
              );
              this.fill();
              this.set();
              break;
          }
          break;
        case 'span':
          if (target.is('.am-datepicker-month')) {
Hzp_D's avatar
Hzp_D 已提交
350
            month = target.parent().find('span').index(target);
Hzp_D's avatar
Hzp_D 已提交
351 352
            this.viewDate.setMonth(month);
          } else {
Hzp_D's avatar
Hzp_D 已提交
353
            year = parseInt(target.text(), 10) || 0;
Hzp_D's avatar
Hzp_D 已提交
354 355 356 357
            this.viewDate.setFullYear(year);
          }
          if (this.viewMode !== 0) {
            this.date = new Date(this.viewDate);
Hzp_D's avatar
Hzp_D 已提交
358
            this.$element.trigger({
Hzp_D's avatar
Hzp_D 已提交
359
              type: 'changeDate.datepicker.amui',
Hzp_D's avatar
Hzp_D 已提交
360 361 362 363 364 365 366 367 368 369 370
              date: this.date,
              viewMode: DPGlobal.modes[this.viewMode].clsName
            });
          }
          this.showMode(-1);
          this.fill();
          this.set();
          break;
        case 'td':
          if (target.is('.am-datepicker-day') && !target.is('.am-disabled')) {
            var day = parseInt(target.text(), 10) || 1;
Hzp_D's avatar
Hzp_D 已提交
371
            month = this.viewDate.getMonth();
Hzp_D's avatar
Hzp_D 已提交
372 373 374 375 376
            if (target.is('.am-datepicker-old')) {
              month -= 1;
            } else if (target.is('.am-datepicker-new')) {
              month += 1;
            }
Hzp_D's avatar
Hzp_D 已提交
377
            year = this.viewDate.getFullYear();
Hzp_D's avatar
Hzp_D 已提交
378 379 380 381
            this.date = new Date(year, month, day, 0, 0, 0, 0);
            this.viewDate = new Date(year, month, Math.min(28, day), 0,0,0,0);
            this.fill();
            this.set();
Hzp_D's avatar
Hzp_D 已提交
382
            this.$element.trigger({
Hzp_D's avatar
Hzp_D 已提交
383
              type: 'changeDate.datepicker.amui',
Hzp_D's avatar
Hzp_D 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
              date: this.date,
              viewMode: DPGlobal.modes[this.viewMode].clsName
            });
          }
          break;
      }
    }
  },

  mousedown: function(event) {
    event.stopPropagation();
    event.preventDefault();
  },

  showMode: function(dir) {
    if (dir) {
      this.viewMode = Math.max(this.minViewMode,
          Math.min(2, this.viewMode + dir));
    }

Hzp_D's avatar
Hzp_D 已提交
404
    this.$picker
Hzp_D's avatar
Hzp_D 已提交
405 406 407 408
        .find('>div')
        .hide()
        .filter('.am-datepicker-' + DPGlobal.modes[this.viewMode].clsName)
        .show();
Hzp_D's avatar
Hzp_D 已提交
409 410 411 412
  },

  isOutView: function() {
    var offset = this.component ?
Hzp_D's avatar
Hzp_D 已提交
413
        this.component.offset() : this.$element.offset();
Hzp_D's avatar
Hzp_D 已提交
414 415 416 417
    var isOutView = {
      outRight: false,
      outBottom: false
    };
Hzp_D's avatar
Hzp_D 已提交
418
    var $picker = this.$picker;
Hzp_D's avatar
Hzp_D 已提交
419
    var width = offset.left + $picker.outerWidth(true);
Hzp_D's avatar
Hzp_D 已提交
420 421
    var height = offset.top + $picker.outerHeight(true) +
        this.$element.innerHeight();
Hzp_D's avatar
Hzp_D 已提交
422 423 424 425 426 427 428 429 430 431

    if (width > $doc.width()) {
      isOutView.outRight = true;
    }
    if (height > $doc.height()) {
      isOutView.outBottom = true;
    }
    return isOutView;
  },

M
Minwe 已提交
432 433 434 435 436
  getLocale: function(locale) {
    if (!locale) {
      locale = navigator.language && navigator.language.split('-');
      locale[1] = locale[1].toUpperCase();
      locale = locale.join('_');
Hzp_D's avatar
Hzp_D 已提交
437
    }
M
Minwe 已提交
438 439 440 441 442

    if (!Datepicker.locales[locale]) {
      locale = 'en_US';
    }
    return locale;
Hzp_D's avatar
Hzp_D 已提交
443 444 445 446
  },

  addTheme: function() {
    if (this.theme) {
Hzp_D's avatar
Hzp_D 已提交
447
      this.$picker.addClass('am-datepicker-' + this.theme);
Hzp_D's avatar
Hzp_D 已提交
448
    }
Hzp_D's avatar
Hzp_D 已提交
449 450 451 452
  }
};

Datepicker.DEFAULTS = {
M
Minwe 已提交
453 454
  locale: 'zh_CN',
  format: 'yyyy-mm-dd',
Hzp_D's avatar
Hzp_D 已提交
455 456
  weekStart: 0,
  viewMode: 0,
Hzp_D's avatar
Hzp_D 已提交
457 458 459
  minViewMode: 0,
  date: '',
  theme: ''
Hzp_D's avatar
Hzp_D 已提交
460 461
};

Hzp_D's avatar
Hzp_D 已提交
462 463 464 465
// Datepicker locales

Datepicker.locales = {
  en_US: {
Hzp_D's avatar
Hzp_D 已提交
466 467 468 469 470 471 472 473 474 475 476
    days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
      'Thursday', 'Friday', 'Saturday'],

    daysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
    daysMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],

    months: ['January', 'February', 'March', 'April', 'May', 'June',
      'July', 'August', 'September', 'October', 'November', 'December'],

    monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
      'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
Hzp_D's avatar
Hzp_D 已提交
477
  },
Hzp_D's avatar
Hzp_D 已提交
478
  zh_CN: {
Hzp_D's avatar
Hzp_D 已提交
479 480 481 482 483 484 485 486 487
    days: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
    daysShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
    daysMin: ['', '', '', '', '', '', ''],

    months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月',
      '八月', '九月', '十月', '十一月', '十二月'],

    monthsShort: ['一月', '二月', '三月', '四月', '五月', '六月',
      '七月', '八月', '九月', '十月', '十一月', '十二月'],
Hzp_D's avatar
Hzp_D 已提交
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
    year: ['']
  }
};

var DPGlobal = {
  modes: [
    {
      clsName: 'days',
      navFnc: 'Month',
      navStep: 1
    },
    {
      clsName: 'months',
      navFnc: 'FullYear',
      navStep: 1
    },
    {
      clsName: 'years',
      navFnc: 'FullYear',
      navStep: 10
    }
  ],
  isLeapYear: function(year) {
    return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0))
  },
  getDaysInMonth: function(year, month) {
Hzp_D's avatar
Hzp_D 已提交
514 515
    return [31, (DPGlobal.isLeapYear(year) ? 29 : 28),
      31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
Hzp_D's avatar
Hzp_D 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
  },
  parseFormat: function(format) {
    var separator = format.match(/[.\/\-\s].*?/);
    var parts = format.split(/\W+/);

    if (!separator || !parts || parts.length === 0) {
      throw new Error('Invalid date format.');
    }
    return {
      separator: separator,
      parts: parts
    };
  },
  parseDate: function(date, format) {
    var parts = date.split(format.separator);
    var val;
Hzp_D's avatar
Hzp_D 已提交
532
    date = new Date();
Hzp_D's avatar
Hzp_D 已提交
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

    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);
    date.setMilliseconds(0);

    if (parts.length === format.parts.length) {
      var year = date.getFullYear();
      var day = date.getDate();
      var month = date.getMonth();

      for (var i = 0, cnt = format.parts.length; i < cnt; i++) {
        val = parseInt(parts[i], 10) || 1;
        switch (format.parts[i]) {
          case 'dd':
          case 'd':
            day = val;
            date.setDate(val);
            break;
          case 'mm':
          case 'm':
            month = val - 1;
            date.setMonth(val - 1);
            break;
          case 'yy':
            year = 2000 + val;
            date.setFullYear(2000 + val);
            break;
          case 'yyyy':
            year = val;
            date.setFullYear(val);
            break;
        }
      }
      date = new Date(year, month, day, 0, 0, 0);
    }
    return date;
  },
  formatDate: function(date, format) {
    var val = {
      d: date.getDate(),
      m: date.getMonth() + 1,
      yy: date.getFullYear().toString().substring(2),
      yyyy: date.getFullYear()
    };
Hzp_D's avatar
Hzp_D 已提交
578
    var dateArray = [];
Hzp_D's avatar
Hzp_D 已提交
579 580 581 582 583

    val.dd = (val.d < 10 ? '0' : '') + val.d;
    val.mm = (val.m < 10 ? '0' : '') + val.m;

    for (var i = 0, cnt = format.parts.length; i < cnt; i++) {
Hzp_D's avatar
Hzp_D 已提交
584
      dateArray.push(val[format.parts[i]]);
Hzp_D's avatar
Hzp_D 已提交
585
    }
Hzp_D's avatar
Hzp_D 已提交
586
    return dateArray.join(format.separator);
Hzp_D's avatar
Hzp_D 已提交
587 588 589
  },
  headTemplate: '<thead>' +
      '<tr class="am-datepicker-header">' +
Hzp_D's avatar
Hzp_D 已提交
590 591 592 593 594 595
      '<th class="am-datepicker-prev">' +
      '<i class="am-datepicker-prev-icon"></i></th>' +
      '<th colspan="5" class="am-datepicker-switch">' +
      '<div class="am-datepicker-select"></div></th>' +
      '<th class="am-datepicker-next"><i class="am-datepicker-next-icon"></i>' +
      '</th></tr></thead>',
Hzp_D's avatar
Hzp_D 已提交
596 597 598
  contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>'
};

Hzp_D's avatar
Hzp_D 已提交
599 600
DPGlobal.template = '<div class="am-datepicker am-datepicker-dropdown">' +
    '<div class="am-datepicker-caret"></div>' +
Hzp_D's avatar
Hzp_D 已提交
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
    '<div class="am-datepicker-days">' +
    '<table class="am-datepicker-table">' +
    DPGlobal.headTemplate +
    '<tbody></tbody>' +
    '</table>' +
    '</div>' +
    '<div class="am-datepicker-months">' +
    '<table class="am-datepicker-table">' +
    DPGlobal.headTemplate +
    DPGlobal.contTemplate +
    '</table>' +
    '</div>' +
    '<div class="am-datepicker-years">' +
    '<table class="am-datepicker-table">' +
    DPGlobal.headTemplate +
    DPGlobal.contTemplate +
    '</table>' +
    '</div>' +
    '</div>';

$.fn.datepicker = function(option, val) {
  return this.each(function() {
    var $this = $(this);
    var data = $this.data('amui.datepicker');

    var options = $.extend({}, Datepicker.DEFAULTS,
        UI.utils.options($this.attr('data-am-datepicker')),
            typeof option === 'object' && option);

    if (!data) {
      $this.data('amui.datepicker', (data = new Datepicker(this,
          $.extend({}, $.fn.datepicker.defaults, options))));
    }
    if (typeof option === 'string') {
      data[option](val);
    }
  });
};

$.fn.datepicker.defaults = {
  onRender: function(date) {
    return '';
  }
};

$.fn.datepicker.Constructor = Datepicker;

// Init code
$(document).on('ready', function(e) {
  $('[data-am-datepicker]').datepicker();
});

$.AMUI.datepicker = Datepicker;

module.exports = Datepicker;

// TODO: 1. 载入动画
Hzp_D's avatar
Hzp_D 已提交
658
//       2. less 优化