-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathCalendar.js
348 lines (306 loc) · 11 KB
/
Calendar.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* ESUI (Enterprise Simple UI)
* Copyright 2013 Baidu Inc. All rights reserved.
*
* @ignore
* @file 日历
* @author dbear, otakustay
*/
define(
function (require) {
require('./MonthView');
var u = require('underscore');
var moment = require('moment');
var lib = require('./lib');
var ui = require('./main');
var InputControl = require('./InputControl');
var Layer = require('./Layer');
/**
* 日历用浮层
*
* @extends Layer
* @ignore
* @constructor
*/
function CalendarLayer() {
Layer.apply(this, arguments);
}
lib.inherits(CalendarLayer, Layer);
CalendarLayer.prototype.render = function (element) {
document.body.appendChild(element);
element.innerHTML = '<div data-ui-type="MonthView" '
+ 'data-ui-child-name="monthView"></div>';
var calendar = this.control;
calendar.helper.initChildren(element);
var monthView = calendar.getChild('monthView');
monthView.setProperties(
{ 'rawValue': calendar.rawValue, 'range': calendar.range });
monthView.on('change', syncMonthViewValue, calendar);
if (calendar.autoHideLayer) {
monthView.on(
'itemclick',
u.bind(calendar.layer.toggle, calendar.layer)
);
}
};
CalendarLayer.prototype.toggle = function () {
var element = this.getElement();
if (!element
|| this.control.helper.isPart(element, 'layer-hidden')
) {
// 展示之前先跟main同步
var calendar = this.control;
var monthView = calendar.getChild('monthView');
monthView.setProperties(
{ 'rawValue': calendar.rawValue, 'range': calendar.range }
);
this.show();
}
else {
this.hide();
}
};
/**
* 日历控件
*
* 日历控件是由一个按钮和一个浮层组成的,如果你只需要显示一个月日期的效果,
* 请使用{@link MonthView}控件
*
* @extends InputControl
* @requires MonthView
* @constructor
*/
function Calendar() {
InputControl.apply(this, arguments);
this.layer = new CalendarLayer(this);
}
/**
* 更新显示
*
* @param {MonthView} monthView MonthView控件实例
* @ignore
*/
function syncMonthViewValue() {
var monthView = this.getChild('monthView');
var date = monthView.getRawValue();
if (!date) {
return;
}
this.rawValue = date;
updateDisplayText(this);
/**
* @event change
*
* 值发生变化时触发
*
* @member Calendar
*/
this.fire('change');
}
/**
* 更新显示的文字
*
* @param {Calendar} calendar 控件实例
* @ignore
*/
function updateDisplayText(calendar) {
// 更新主显示
var textHolder = calendar.helper.getPart('text');
textHolder.innerHTML = u.escape(calendar.getValue());
}
Calendar.prototype = {
/**
* 控件类型,始终为`"Calendar"`
*
* @type {string}
* @readonly
* @override
*/
type: 'Calendar',
/**
* 初始化参数
*
* @param {Object} [options] 构造函数传入的参数
* @protected
* @override
*/
initOptions: function (options) {
var now = new Date();
var properties = {
range: {
begin: new Date(1983, 8, 3),
end: new Date(2046, 10, 4)
},
/**
* @property {string} [dateFormat="YYYY-MM-DD"]
*
* 输出的日期格式,用于{@link Calendar#getValue}返回时格式化
*
* 具体的日期格式参考
* [moment文档](http://momentjs.com/docs/#/displaying/format/)
*/
dateFormat: 'YYYY-MM-DD',
/**
* @property {string} [paramFormat="YYYY-MM-DD"]
*
* 输入的日期格式,用于{@link Calendar#setValue}时格式化
*
* 具体的日期格式参考
* [moment文档](http://momentjs.com/docs/#/displaying/format/)
*/
paramFormat: 'YYYY-MM-DD',
/**
* @property {Date} [rawValue]
*
* 控件的原始值,为`Date`类型,默认为当天
*
* @override
*/
rawValue: now,
// 是否点击自动关闭弹层
autoHideLayer: false
};
u.extend(properties, options);
if (options.autoHideLayer === 'false') {
properties.autoHideLayer = false;
}
if (lib.isInput(this.main)) {
this.helper.extractOptionsFromInput(this.main, properties);
}
// parseValue 需要用到 paramFormat
this.paramFormat = properties.paramFormat;
if (options.value) {
properties.rawValue = this.parseValue(properties.value);
}
// 类型如果是string
var range = properties.range;
if (typeof range === 'string') {
var beginAndEnd = range.split(',');
var begin = this.parseValue(beginAndEnd[0]);
var end = this.parseValue(beginAndEnd[1]);
properties.range = { begin: begin, end: end };
}
this.setProperties(properties);
},
/**
* 初始化DOM结构
*
* @protected
* @override
*/
initStructure: function () {
// 如果主元素是输入元素,替换成`<div>`
// 如果输入了非块级元素,则不负责
if (lib.isInput(this.main)) {
this.helper.replaceMain();
}
var template = [
'<div class="${classes}" id="${id}">${value}</div>',
'<div class="${arrow}"></div>'
];
this.main.innerHTML = lib.format(
template.join(''),
{
classes: this.helper.getPartClassName('text'),
id: this.helper.getId('text'),
arrow: this.helper.getPartClassName('arrow')
}
);
},
/**
* 初始化事件交互
*
* @protected
* @override
*/
initEvents: function () {
this.helper.addDOMEvent(this.main, 'click', u.bind(this.layer.toggle, this.layer));
},
/**
* 重渲染
*
* @method
* @protected
* @override
*/
repaint: require('./painters').createRepaint(
InputControl.prototype.repaint,
{
/**
* @property {meta.DateRange} range
*
* 指定控件可选的时间段
*/
name: ['rawValue', 'range'],
paint: function (calendar, rawValue, range) {
updateDisplayText(calendar);
var monthView = calendar.getChild('monthView');
if (monthView) {
monthView.setProperties({
rawValue: rawValue,
range: range
});
}
}
},
{
name: ['disabled', 'hidden', 'readOnly'],
paint: function (calendar, disabled, hidden, readOnly) {
if (disabled || hidden || readOnly) {
calendar.layer.hide();
}
}
}
),
/**
* 设置日期可选区间
*
* @param {meta.DateRange} range 日期可选区间
*/
setRange: function (range) {
this.setProperties({ 'range': range });
},
/**
* 将值从原始格式转换成字符串,复杂类型的输入控件需要重写此接口
*
* @param {Date} rawValue 原始值
* @return {string}
* @protected
* @override
*/
stringifyValue: function (rawValue) {
return moment(rawValue).format(this.dateFormat) || '';
},
/**
* 将字符串类型的值转换成原始格式,复杂类型的输入控件需要重写此接口
*
* @param {string} value 字符串值
* @return {Date}
* @protected
* @override
*/
parseValue: function (value) {
var date = moment(value, this.paramFormat).toDate();
return date;
},
/**
* 销毁
*
* @override
*/
dispose: function () {
if (this.helper.isInStage('DISPOSED')) {
return;
}
if (this.layer) {
this.layer.dispose();
this.layer = null;
}
InputControl.prototype.dispose.apply(this, arguments);
}
};
lib.inherits(Calendar, InputControl);
ui.register(Calendar);
return Calendar;
}
);