-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdatetimePicker.js
267 lines (263 loc) · 7.16 KB
/
datetimePicker.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
/*
*datetime,date,custom
*日期:2017-3-24
*config参数说明
*---------
*container:必填项操作的DOM
*type:类型(date:日期,datetime:时间,custom:自定义数据)
*eventName:事件类型
*cols:数据
*selectedClass 有值时按钮的样式
*value:返回的数据
*connector:连接符号
*callback:返回的函数
*/
;(function($){
var today = new Date(),
initMonthes = ('01 02 03 04 05 06 07 08 09 10 11 12').split(' ');
/*年*/
function initYears(max,min) {
var arr = [];
for (var i = (min || 1950); i <= (max || 2030); i++) { arr.push(i); }
return arr;
};
/*天*/
function getDays(max){
var days = [];
for(var i=1;i <= (max || 31);i++){
days.push(i<10 ? "0"+i : i );
};
return days;
};
function getDaysByMonthAndYear(month,year){
var int_d = new Date(year, parseInt(month)+1-1, 1);
var d = new Date(int_d - 1);
return getDays(d.getDate());
};
/*格式化数字*/
function formatNumber(n){
return n < 10 ? "0" + n : n;
};
/*判断数组值得到相应的下标*/
function isHasElementOne(arr,value){
for(var i = 0,vlen = arr.length; i < vlen; i++){
if(arr[i] == value){
return i;
}
}
return -1;
};
var cols = [
{
value: initYears()
},
{
value: initMonthes
},
{
value: getDays()
},
{
value: (function () {
var arr = [];
for (var i = 0; i <= 23; i++) { arr.push(i < 10 ? '0' + i : i); }
return arr;
})()
},
{
value: (function () {
var arr = [];
for (var i = 0; i <= 59; i++) { arr.push(i < 10 ? '0' + i : i); }
return arr;
})()
}
];
function DatetimePicker(container , config){
if(!container && !(typeof container == 'string')){
alert('参数不对!');
return false;
};
this.container = container;
this.config = {
type:'date', //date,datetime,custom
eventName:'click',
cols: cols,
value: [],
selectedClass:'',
connector:'-',
callBack:function(){}
};
//默认参数扩展
if(config && $.isPlainObject(config)){
$.extend(this.config , config);
};
var _config = this.config;
switch (_config.type) {
case 'datetime':
_config.value = [today.getFullYear(), formatNumber(today.getMonth()+1), formatNumber(today.getDate()), formatNumber(today.getHours()), formatNumber(today.getMinutes())];
break;
case 'custom':
_config.value = [];
for(var i = 0; i< _config.cols.length;i++){
_config.value.push(_config.cols[i].value[0]);
}
break;
default:
_config.value = [today.getFullYear(), formatNumber(today.getMonth()+1), formatNumber(today.getDate())];
break;
}
console.log(_config.value);
var _this = this,
containerVal;
$(container).each(function(){
$(this).on(_this.config.eventName,function(e){
e.stopPropagation();
e.preventDefault();
_this.element = e.target;
if($(this).attr('value')){
containerVal = $(this).attr('value');
}
if(containerVal){
_this.config.value = containerVal.split(',');
}
_this.lock();
_this.init();
});
});
};
DatetimePicker.prototype = {
init: function(){
this.pickerWrap = $('<div class="ui-picker-wrap hide" style="display:none"></div>');
this.pickerMask = $('<div class="mask"></div>');
this.picker = $('<div class="ui-picker"></div>');
this.pickerHeader = $('<div class="ui-picker-hd ui-border-b"></div>');
this.cancel = $('<span class="ui-picker-cancel">取消</span>');
this.confirm = $('<span class="ui-picker-confirm">确定</span>');
this.pickerbody = $('<div class="ui-picker-bd"></div>');
this.pickerFooter = $('<div class="ui-picker-ft ui-border-tb"></div>');
this.pickerWrap.append(this.pickerMask);
this.pickerWrap.append(this.picker);
this.picker.append(this.pickerHeader);
this.pickerHeader.append(this.cancel);
this.pickerHeader.append(this.confirm);
this.picker.append(this.pickerbody);
this.picker.append(this.pickerFooter);
this.created();
this.show();
this.event();
},
event: function(){
var _this = this;
this.cancel.on('touchend',function(){
_this.hide();
});
this.pickerMask.on('touchend',function(){
_this.hide();
});
this.confirm.on('touchend',function(){
$(_this.element).attr('value',_this.config.value);
$(_this.element).text(_this.formatValue(_this.config.value));
if(_this.config.callBack){
_this.config.callBack(_this.formatValue(_this.config.value));
if(_this.config.selectedClass){
$(_this.element).addClass(_this.config.selectedClass);
}
}
_this.hide();
});
},
created: function(){
var _this = this;
_this._template();
},
_template: function(){
var _this = this,
parent = '',
$body = $('body'),
dateArr = [],
config = _this.config,
arr = config.value;
for(var i=0; i < arr.length; i++){
parent += '<div class="ui-picker-slot"id="picker-'+ i +'"><ul class="ui-picker-list">'+ _this.itemTemplate(i) +'</ul></div>';
};
_this.pickerbody.append(parent);
$body.append(this.pickerWrap);
for(var i=0; i<arr.length;i++){
(function(i){
dateArr[i] = new Swiper('#picker-'+i,{
wrapper : '.ui-picker-list',
slide: '.ui-picker-item',
direction:'vertical',
endFn:function(num){
config.value[i] = $('#picker-'+i).find('.swiper-slide-active').text();
if(config.type == 'date' || config.type == 'datetime'){
if(i < 2){
var len = getDaysByMonthAndYear(arr[1],arr[0]).length;
dateArr[2].len = len;
if(arr[2] && arr[2] > len-1 ){
dateArr[2]._initialSlide(len-1);
}
$('#picker-2 li').show();
$('#picker-2 li:gt('+ (len-1) +')').hide();
}
}
}
});
dateArr[i]._initialSlide(isHasElementOne(config.cols[i].value,config.value[i]));
})(i);
};
},
itemTemplate: function(num){
var html = '',
_this = this;
arr = _this.config.cols[num].value;
for(var i = 0; i<arr.length;i++){
html+= '<li class="ui-picker-item" index="' + i + '" value="' + arr[i] + '">'+arr[i]+'</li>';
};
return html;
},
formatValue: function (arr){
var _this = this,
connector = _this.config.connector,
type = _this.config.type;
switch ( type ) {
case 'date':
return arr.join(connector);
break;
case 'datetime':
return arr[0] + connector + arr[1] + connector + arr[2] + ' ' + arr[3] + ':' + arr[4];
break;
case 'custom':
return arr.join(connector);
break;
}
},
lock:function(){
$("body").on("touchmove",function(event){
event.preventDefault;
}, false);
},
unLock:function(){
$("body").off("touchmove");
},
show: function(){
var _this = this;
$('.ui-picker-wrap').show();
setTimeout(function(){
$('.ui-picker-wrap').removeClass('hide').addClass('show');
},100);
},
hide: function(){
var _this = this;
$('.ui-picker-wrap').removeClass('show').addClass('hide');
setTimeout(function(){
_this.unLock();
$('.ui-picker-wrap').remove();
},250);
}
}
window.DatetimePicker = DatetimePicker;
$.datetimePicker = function(config){
return new DatetimePicker(config);
}
})(window.jQuery || $);