calendar.js 3.41 KB
/**
 * 星潮教室 亲密度·日期
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/4/11
 */

var $ = require('jquery');

var CalendarHandler = {
    currentYear: 0,
    currentMonth: 0,
    initialize: function() {
        var $calendarItem;

        $calendarItem = this.createCalendar(0, 0);
        $('#Container').append($calendarItem);
    },
    isRuiYear: function(aDate) {
        return 0 === aDate % 4 && (aDate % 100 !== 0 || aDate % 400 === 0);
    },
    calculateWeek: function(y, m, d) {
        var arr, vDay, week;

        arr = '7123456'.split('');
        vDay = parseInt(d, 10);
        week = arr[new Date(y, m - 1, vDay).getDay()];
        return week;
    },
    calculateMonthDays: function(m, y) {
        var mDay = 0;
        var isRn;

        if (m === 0 || m === 1 || m === 3 || m === 5 || m === 7 || m === 8 || m === 10 || m === 12) {
            mDay = 31;
        } else {
            if (m === 2) {

                // 判断是否为芮年
                isRn = this.isRuiYear(y);
                if (isRn === true) {
                    mDay = 29;
                } else {
                    mDay = 28;
                }
            } else {
                mDay = 30;
            }
        }
        return mDay;
    },
    createCalendar: function(y, m) {
        var $dayItem, nowDate, nowYear, nowMonth, nowDaysNub, nowWeek, lastMonthDaysNub, i, hasCreateDaysNub;

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

        // var signDay = $('.sign-day').val();

        $dayItem = $('<div class="dayItem"></div>');

        // 获取当前月份的天数
        nowDate = new Date();

        nowYear = y === 0 ? nowDate.getFullYear() : y;
        this.currentYear = nowYear;
        nowMonth = m === 0 ? nowDate.getMonth() + 1 : m;
        this.currentMonth = nowMonth;
        nowDaysNub = this.calculateMonthDays(nowMonth, nowYear);

        window.nowDaysNub = nowDaysNub;

        $('.cur-year').html(nowYear);
        $('.cur-month').html(months[nowMonth - 1]);

        // 获取当月第一天是星期几
        nowWeek = parseInt(this.calculateWeek(nowYear, nowMonth, 1));

        // 获取上个月的天数
        lastMonthDaysNub = this.calculateMonthDays((nowMonth - 1), nowYear);

        if (nowWeek !== 0) {

            // 生成上月剩下的日期
            for (i = (lastMonthDaysNub - nowWeek); i < lastMonthDaysNub; i++) {
                $dayItem.append('<div class="item lastItem">' + (i + 1) + '</div>');
            }
        }

        // 生成当月的日期
        for (i = 1; i <= nowDaysNub; i++) {

            // if (signDay.indexOf(i) > -1) {
            //     $dayItem.append('<div class="item signItem">' + i + '</div>');
            // } else {
            //     $dayItem.append('<div class="item">' + i + '</div>');
            // }

            $dayItem.append('<div class="item now-days">' + i + '</div>');
        }

        // 获取总共已经生成的天数
        hasCreateDaysNub = nowWeek + nowDaysNub;

        // 如果小于42,往下个月推算
        if (hasCreateDaysNub < 41) {
            for (i = 0; i <= (41 - hasCreateDaysNub); i++) {
                $dayItem.append('<div class="item lastItem">' + (i + 1) + '</div>');
            }
        }

        return $dayItem;
    }
};

$(document).ready(function() {
    CalendarHandler.initialize();
});