-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcron-selector.js
268 lines (265 loc) · 11.8 KB
/
cron-selector.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
(function($){
if(!$){
throw 'cron-selector : JQuery is needed'
}
var timeTypes = ['second','minute','hour','day','month','week','year'],
timeNames = ['秒','分','时','日/月','月','日/周','年'],
timeValues = ['0-59','0-59','0-23','1-31','1-12','1-7','1970-2099'],
localWeekSequence = ['2','3','4','5','6','7','1'],
weekChar = ['/','-','*',',','?','L','C','#'],
typeSelectorClass = 'typeSelector',
valueCheckBoxClass = 'valueCheckBox',
startInputClass = 'start',
endInputClass = 'end',
distanceInputClass = 'distance',
defaultOptions = {
containerClass:'',
locatedWeek:false
},
selectorTypeEnum = {
period:'period',
assign:'assign',
empty:'empty',
};
var typeToName = function (timeType) {
return timeNames[timeTypes.indexOf(timeType)];
};
var typeToValueScope = function (timeType) {
var timeValue = timeValues[timeTypes.indexOf(timeType)];
return timeValue ? timeValue.split('-') : timeValue;
};
$.cronSelector = function(options){
var options = this.options = $.extend(defaultOptions,options);
var container = this.container = $('<div class="pb-tag-box ' + this.options['containerClass'] + '"></div>').appendTo($(options['container']));
//初始化时间类型切换html
var renderTabHtml = function(){
var tabHtml = ['<div class="pb-tag">'];
var tabItemHtml = ['<div class="pb-tag-body">'];
for(var i = 0 ; i < timeTypes.length ; i++){
tabHtml.push('<div class="pb-tag-item" tabCode="'+timeTypes[i]+'">'+timeNames[i]+'</div>');
tabItemHtml.push('<div class="pb-tag-body-item" tabItemCode="'+timeTypes[i]+'" style="display: none"></div>');
}
tabHtml.push('</div>');
tabItemHtml.push('</div>');
container.append(tabHtml.join(''));
return container.append(tabItemHtml.join(''));
};
var getTimeItemContainer = function(timeType){
return $('div[tabItemCode='+timeType+']',container);
};
//初始化周期时间类型选择器
var renderPeriodHtml = function(timeType){
var timeContainer = getTimeItemContainer(timeType);
var typeName = typeToName(timeType);
var valueScope = typeToValueScope(timeType) || ['',''];
var min = valueScope[0];
var max = valueScope[1];
$(timeContainer).append('<div class="pb-form-item">\n' +
' <input class="'+typeSelectorClass+' pb-form-check" type="checkbox" name="'+selectorTypeEnum.period+'"> 从<input class="pb-form-input '+startInputClass+'" type="number" min="'+min+'" max="'+max+'">'+typeName+'开始,\n' +
' 到<input class="pb-form-input '+endInputClass+'" type="number" min="'+min+'" max="'+max+'">'+typeName+'结束,\n' +
' 每<input class="pb-form-input '+distanceInputClass+'" type="number" min="0" max="'+(max-min)+'">'+typeName+'执行一次\n' +
' </div>');
};
//初始化指定时间类型选择器
var renderAssignHtml = function(timeType){
var timeContainer = getTimeItemContainer(timeType);
var valueScope = typeToValueScope(timeType);
if(valueScope){
var timeValues = ['<div class="assignInput">\n' +
' <span class="pb-form-item">\n' +
' <input class="'+typeSelectorClass+' pb-form-check" type="checkbox" name="'+selectorTypeEnum.assign+'">指定时间' +
' </span>'];
for(var i = valueScope[0];i <= valueScope[1];i++){
timeValues.push('<span class="pb-form-item">\n' +
' <input class="pb-form-check '+valueCheckBoxClass+'" type="checkbox" value="'+i+'">' + i +
' </span>');
}
timeValues.push('</div>');
$(timeContainer).append(timeValues.join(''));
}
}
//初始化时间类型选择器
var renderTimeItemHtml = function(){
for(var i= 0;i<timeTypes.length;i++){
renderPeriodHtml(timeTypes[i]);
renderAssignHtml(timeTypes[i]);
}
};
//初始化html
var renderHtml = function(){
var tabItemContainer = renderTabHtml(container);
renderTimeItemHtml(tabItemContainer);
};
//初始化时间类型切换事件
var initTabChooseEvent = function(){
$('[tabcode]').click(function(){
var code = $(this).attr('tabcode');
$(this).addClass('active').siblings().removeClass('active');
$('div[tabitemcode=' + code + ']').show().siblings().hide();
});
};
//清空该时间类型下的所有输入
var clearInput = function(timeType){
var timeContainer = getTimeItemContainer(timeType);
timeContainer.find("input").prop("checked", false);
};
//初始化时间选择类型互斥事件
var initMutualEvent = function(){
$('div[tabItemCode]',container).each(function(){
var _this = $(this);
var timeType = $(this).attr('tabItemCode');
$('input.'+typeSelectorClass,_this).on('change',function(){
if($(this).prop('checked')) {
$('input.'+typeSelectorClass,_this).not($(this)).prop('checked',false);
if(timeType == 'week'){
clearInput('day');
}
if(timeType == 'day'){
clearInput('week');
}
}
});
});
};
//获得指定时间类型的值
var getTimeValue = function(timeType){
var checkedSelect = getTimeItemContainer(timeType).find('input.'+typeSelectorClass+':checked');
var selectorType = checkedSelect.length>0 ? checkedSelect.attr('name') : selectorTypeEnum.empty;
if(selectorType == selectorTypeEnum.period){
return getPeriodValue(timeType);
}else if(selectorType == selectorTypeEnum.assign){
return getAssignValue(timeType);
}else if(selectorType == selectorTypeEnum.empty){
return getEmptyValue(timeType);
}
};
//获得周期时间选择器的值
var getPeriodValue = function(timeType){
var timeContainer = getTimeItemContainer(timeType)
var start = $('input.'+startInputClass,timeContainer).val();
var end = $('input.'+endInputClass,timeContainer).val();
var distance = $('input.'+distanceInputClass,timeContainer).val();
var value = start || '';
if(end && start){
value += ('-' + end)
}
if(distance){
value += ('/' + distance);
}
return value || '*';
};
//获得指定时间选择器的值
var getAssignValue = function(timeType){
var timeContainer = getTimeItemContainer(timeType);
var selectedValues = [];
$('input.'+valueCheckBoxClass+':checked',timeContainer).each(function () {
selectedValues.push($(this).attr("value"));
});
return selectedValues.join(',') || '0';
};
//是否将日/周本地化
var translateWeek = function(weekTime,reverse){
if(!weekTime){
return weekTime;
}
var weekDays = weekTime.split('');
for(var i = 0;i < weekDays.length;i++){
if(weekChar.indexOf(weekDays[i]) == -1){
if(reverse){
weekDays[i] = localWeekSequence.indexOf(weekDays[i]) + 1;
}else {
weekDays[i] = localWeekSequence[parseInt(weekDays[i]) - 1];
}
}
}
return weekDays.join('');
}
//获得空值
var getEmptyValue = function(timeType){
if(timeType == 'week' || timeType == 'day'){
return '?'
}
return "*"
};
var isNumber = function(value){
var re = /^[0-9]+.?[0-9]*$/;
return re.test(value)
};
//根据值获取对应选择器类型
var getTimeValueSelectorType = function(value){
if(!value || value == '*' || value == '?'){
return selectorTypeEnum.empty;
}
if(value.indexOf('/') != -1 || value.indexOf('-') != -1){
return selectorTypeEnum.period;
}
if(value.indexOf(',') != -1 || isNumber(value)){
return selectorTypeEnum.assign;
}
};
//初始化周期时间
var initPeriodValue = function(value,timeType){
var timeContainer = getTimeItemContainer(timeType);
$('input[name="'+selectorTypeEnum.period+'"]',timeContainer).prop("checked",true);
var values = value.split('/');
var startAndEnd = values[0].split('-');
if(startAndEnd != '*') {
$('input.'+startInputClass, timeContainer).val(startAndEnd[0]);
if(startAndEnd.length > 1) {
$('input.'+endInputClass, timeContainer).val(startAndEnd[1]);
}
}
if(values.length > 1) {
var period = values[1];
$('input.'+distanceInputClass, timeContainer).val(period);
}
};
//初始化指定时间
var initAssignValue = function(value,timeType){
var timeContainer = getTimeItemContainer(timeType);
$('input[name="'+selectorTypeEnum.assign+'"]',timeContainer).prop("checked",true);
var selectedValues = value.split(',');
for(var i = 0;i < selectedValues.length;i++){
$('input[value='+selectedValues[i]+'].'+valueCheckBoxClass,timeContainer).prop('checked',true);
}
};
var initEvent = function(){
initTabChooseEvent();
initMutualEvent();
};
var init = function(){
renderHtml();
initEvent();
};
init();
this.val = function(){
var values = [];
for(var i = 0;i < timeTypes.length;i++){
values.push(getTimeValue(timeTypes[i]));
}
var weekValue = values[timeTypes.indexOf('week')];
if(weekValue == '?' && values[timeTypes.indexOf('day')] == '?'){
values[timeTypes.indexOf('day')] = '*'
}
if(this.options['locatedWeek']){
values[timeTypes.indexOf('week')] = translateWeek(weekValue);
}
return values.join(' ');
};
this.parseVal = function(value){
var timeValues = value.split(' ');
timeValues[timeTypes.indexOf("week")] = translateWeek(timeValues[timeTypes.indexOf("week")],true);
for(var i = 0;i < timeValues.length;i++){
var timeValue = timeValues[i];
var timeType = timeTypes[i];
var selectorType = getTimeValueSelectorType(timeValue);
if(selectorType == selectorTypeEnum.assign){
initAssignValue(timeValue,timeType);
}else if(selectorType == selectorTypeEnum.period){
initPeriodValue(timeValue,timeType);
}
}
};
return this;
}
})($)