marquex
2016-04-19 92a2c6816897ef287584aaa67cee2904eb31be22
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
var React = require('react'),
    moment = require('moment')
;
 
var DOM = React.DOM;
var DateTimePickerDays = React.createClass({
 
    render: function() {
        var footer = this.renderFooter(),
            date = this.props.viewDate,
            locale = date.localeData(),
            tableChildren
        ;
 
        tableChildren = [
            DOM.thead({ key: 'th'}, [
                DOM.tr({ key: 'h'},[
                    DOM.th({ key: 'p', className: 'rdtPrev' }, DOM.button({onClick: this.props.subtractTime(1, 'months'), type: 'button' }, '‹')),
                    DOM.th({ key: 's', className: 'rdtSwitch', onClick: this.props.showView('months'), colSpan: 5, 'data-value': this.props.viewDate.month() }, locale.months( date ) + ' ' + date.year() ),
                    DOM.th({ key: 'n', className: 'rdtNext' }, DOM.button({onClick: this.props.addTime(1, 'months'), type: 'button' }, '›'))
                ]),
                DOM.tr({ key: 'd'}, this.getDaysOfWeek( locale ).map( function( day, index ){ return DOM.th({ key: day + index, className: 'dow'}, day ); }) )
            ]),
            DOM.tbody({key: 'tb'}, this.renderDays())
        ];
 
        if( footer )
            tableChildren.push( footer );
 
        return DOM.div({ className: 'rdtDays' },
            DOM.table({}, tableChildren )
        );
    },
 
    /**
     * Get a list of the days of the week
     * depending on the current locale
     * @return {array} A list with the shortname of the days
     */
    getDaysOfWeek: function( locale ){
        var days = locale._weekdaysMin,
            first = locale.firstDayOfWeek(),
            dow = [],
            i = 0
        ;
 
        days.forEach( function( day ){
            dow[ (7 + (i++) - first) % 7 ] = day;
        });
 
        return dow;
    },
 
    renderDays: function() {
        var date = this.props.viewDate,
            selected = this.props.selectedDate && this.props.selectedDate.clone(),
            prevMonth = date.clone().subtract( 1, 'months' ),
            currentYear = date.year(),
            currentMonth = date.month(),
            weeks = [],
            days = [],
            renderer = this.props.renderDay || this.renderDay,
            isValid = this.props.isValidDate || this.isValidDate,
            classes, disabled, dayProps, currentDate
        ;
 
        // Go to the last week of the previous month
        prevMonth.date( prevMonth.daysInMonth() ).startOf('week');
        var lastDay = prevMonth.clone().add(42, 'd');
 
        while( prevMonth.isBefore( lastDay ) ){
            classes = 'rdtDay';
            currentDate = prevMonth.clone();
 
            if( ( prevMonth.year() == currentYear && prevMonth.month() < currentMonth ) || ( prevMonth.year() < currentYear ) )
                classes += ' rdtOld';
            else if( ( prevMonth.year() == currentYear && prevMonth.month() > currentMonth ) || ( prevMonth.year() > currentYear ) )
                classes += ' rdtNew';
 
            if( selected && prevMonth.isSame(selected, 'day') )
                classes += ' rdtActive';
 
            if (prevMonth.isSame(moment(), 'day') )
                classes += ' rdtToday';
 
            disabled = !isValid( currentDate, selected );
            if( disabled )
                classes += ' rdtDisabled';
 
            dayProps = {
                key: prevMonth.format('M_D'),
                'data-value': prevMonth.date(),
                className: classes
            };
            if( !disabled )
                dayProps.onClick = this.updateSelectedDate;
 
            days.push( renderer( dayProps, currentDate, selected ) );
 
            if( days.length == 7 ){
                weeks.push( DOM.tr( {key: prevMonth.format('M_D')}, days ) );
                days = [];
            }
 
            prevMonth.add( 1, 'd' );
        }
 
        return weeks;
    },
 
    updateSelectedDate: function( event ) {
        this.props.updateSelectedDate(event, true);
    },
 
    renderDay: function( props, currentDate, selectedDate ){
        return DOM.td( props, currentDate.date() );
    },
 
    renderFooter: function(){
        if( !this.props.timeFormat )
            return '';
 
        var date = this.props.selectedDate || this.props.viewDate;
        
        return DOM.tfoot({ key: 'tf'},
            DOM.tr({},
                DOM.td({ onClick: this.props.showView('time'), colSpan: 7, className: 'rdtTimeToggle'}, date.format( this.props.timeFormat ))
            )
        );
    },
    isValidDate: function(){ return 1; }
});
 
module.exports = DateTimePickerDays;