@@ -4,20 +4,25 @@ var DatePicker;
4
4
"use strict" ;
5
5
6
6
/**
7
- * 日历组件
8
- *
9
- * @param config {el: HTMLElement, default: '2017-04-11' | '2017-04'}
7
+ * 日历组件
8
+ * @param config {target: DOM, defaultDate: '2017-04-15', selectInterval: [2000, 2020]}
10
9
* @constructor
11
10
*/
12
11
function Calendar ( config ) {
13
12
14
13
this . target = config . el ;
15
14
this . defaultDate = config . default || 'today' ;
16
15
this . selectInterval = config . interval || [ 1970 , 2030 ] ;
16
+ // 当前日历数据
17
17
this . nonceYear = 0 ;
18
18
this . nonceMonth = 0 ;
19
19
this . nonceDay = 0 ;
20
20
21
+ // 选中的数据
22
+ this . selectedYear = 0 ;
23
+ this . selectedMonth = 0 ;
24
+ // 日期数组
25
+ this . renderDate = [ ] ;
21
26
this . init ( ) ;
22
27
}
23
28
@@ -46,7 +51,7 @@ var DatePicker;
46
51
addClass : function ( el , className ) {
47
52
48
53
var ol = el . className ;
49
- if ( ol . split ( ' ' ) . indexOf ( className ) == - 1 ) {
54
+ if ( ol . split ( ' ' ) . indexOf ( className ) === - 1 ) {
50
55
el . className = ol + " " + className ;
51
56
}
52
57
@@ -134,6 +139,9 @@ var DatePicker;
134
139
self . nonceDay = relate [ 2 ] || 1 ;
135
140
}
136
141
}
142
+ // 设置选中的默认年份与月份
143
+ self . selectedYear = self . nonceYear ;
144
+ self . selectedMonth = self . nonceMonth ;
137
145
self . _setDateList ( ) ;
138
146
139
147
@@ -186,6 +194,7 @@ var DatePicker;
186
194
} ;
187
195
renderList . push ( obj ) ;
188
196
}
197
+ this . renderDate = renderList ;
189
198
190
199
this . _doRender ( renderList ) ;
191
200
} ,
@@ -196,20 +205,25 @@ var DatePicker;
196
205
*/
197
206
_doRender : function ( arr ) {
198
207
199
- var date = "<ul class='weekTip'>" +
200
- "<li class='weekend'>日</li>" +
201
- "<li>一</li>" +
202
- "<li>二</li>" +
203
- "<li>三</li>" +
204
- "<li>四</li>" +
205
- "<li>五</li>" +
206
- "<li class='weekend'>六</li>" +
207
- "</ul>" ;
208
+ var self = this ,
209
+ date = "<ul class='weekTip'>" +
210
+ "<li class='weekend'>日</li>" +
211
+ "<li>一</li>" +
212
+ "<li>二</li>" +
213
+ "<li>三</li>" +
214
+ "<li>四</li>" +
215
+ "<li>五</li>" +
216
+ "<li class='weekend'>六</li>" +
217
+ "</ul>" ;
208
218
date += "<ul class='date'>" ;
209
219
arr . forEach ( function ( ele , ind ) {
210
220
var line = Math . floor ( ind / 6 ) ;
211
221
if ( ! ele . isInner )
212
222
date += "<li class='disabled'>" + ele . num + "</li>" ;
223
+ else if ( ele . num === self . nonceDay &&
224
+ self . nonceMonth === self . selectedMonth &&
225
+ self . nonceYear === self . selectedYear )
226
+ date += "<li class='abled active'>" + ele . num + "</li>" ;
213
227
else if ( ! ( ind % 7 ) || ind === line * 6 + line - 1 )
214
228
date += "<li class='weekend'>" + ele . num + "</li>" ;
215
229
else
@@ -313,9 +327,25 @@ var DatePicker;
313
327
function ( t ) {
314
328
// 父元素不是ul.date时跳出
315
329
if ( ! t . parentNode . classList . contains ( 'date' ) ) return ;
330
+
316
331
self . nonceDay = + ( t . innerHTML ) ;
317
- utils . removeClass ( t . parentNode . querySelectorAll ( 'li' ) , 'active' ) ;
318
- utils . addClass ( t , 'active' ) ;
332
+ // 点击的是非本月的日期
333
+ var clkId = [ ] . indexOf . call ( t . parentElement . children , t ) ;
334
+ if ( ! self . renderDate [ clkId ] . isInner ) {
335
+ if ( self . nonceDay > 15 ) {
336
+ self . selectedMonth = self . nonceMonth - 1 ;
337
+ self . _prevMonth ( ) ;
338
+ } else {
339
+ self . selectedMonth = self . nonceMonth + 1 ;
340
+ self . _nextMonth ( ) ;
341
+ }
342
+ } else {
343
+ self . selectedMonth = self . nonceMonth ;
344
+ }
345
+
346
+ self . selectedYear = self . nonceYear ;
347
+
348
+ self . _setDateList ( ) ;
319
349
} )
320
350
} ) ;
321
351
0 commit comments