-
Notifications
You must be signed in to change notification settings - Fork 52
/
cronGen.js
357 lines (307 loc) · 17.2 KB
/
cronGen.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
349
350
351
352
353
354
355
356
(function ($) {
var resultsName = "";
var inputElement;
var displayElement;
$.fn.extend({
cronGen: function () {
//create top menu
var cronContainer = $("<div/>", { id: "CronContainer", style: "display:none;width:300px;height:300px;" });
var mainDiv = $("<div/>", { id: "CronGenMainDiv", style: "width:480px;height:270px;" });
var topMenu = $("<ul/>", { "class": "nav nav-tabs", id: "CronGenTabs" });
$('<li/>', { 'class': 'active' }).html($('<a id="MinutesTab" href="#Minutes">Minutes</a>')).appendTo(topMenu);
$('<li/>').html($('<a id="HourlyTab" href="#Hourly">Hourly</a>')).appendTo(topMenu);
$('<li/>').html($('<a id="DailyTab" href="#Daily">Daily</a>')).appendTo(topMenu);
$('<li/>').html($('<a id="WeeklyTab" href="#Weekly">Weekly</a>')).appendTo(topMenu);
$('<li/>').html($('<a id="MonthlyTab" href="#Monthly">Monthly</a>')).appendTo(topMenu);
$('<li/>').html($('<a id="YearlyTab" href="#Yearly">Yearly</a>')).appendTo(topMenu);
$(topMenu).appendTo(mainDiv);
//create what's inside the tabs
var container = $("<div/>", { "class": "container-fluid", "style": "margin-top: 10px" });
var row = $("<div/>", { "class": "row-fluid" });
var span12 = $("<div/>", { "class": "span12" });
var tabContent = $("<div/>", { "class": "tab-content" });
//creating the minutesTab
var minutesTab = $("<div/>", { "class": "tab-pane active", id: "Minutes" });
$(minutesTab).append("Every ");
$("<input/>", { id: "MinutesInput", type: "text", value: "1", style: "width: 40px" }).appendTo(minutesTab);
$(minutesTab).append(" minute(s)");
$(minutesTab).appendTo(tabContent);
//creating the hourlyTab
var hourlyTab = $("<div/>", { "class": "tab-pane", id: "Hourly" });
var hourlyOption1 = $("<div/>", { "class": "well well-small" });
$("<input/>", { type: "radio", value: "1", name: "HourlyRadio", checked: "checked" }).appendTo(hourlyOption1);
$(hourlyOption1).append(" Every ");
$("<input/>", { id: "HoursInput", type: "text", value: "1", style: "width: 40px" }).appendTo(hourlyOption1);
$(hourlyOption1).append(" hour(s)");
$(hourlyOption1).appendTo(hourlyTab);
var hourlyOption2 = $("<div/>", { "class": "well well-small" });
$("<input/>", { type: "radio", value: "2", name: "HourlyRadio" }).appendTo(hourlyOption2);
$(hourlyOption2).append(" At ");
$(hourlyOption2).append('<select id="AtHours" class="hours" style="width: 60px"></select>');
$(hourlyOption2).append('<select id="AtMinutes" class="minutes" style="width: 60px"></select>');
$(hourlyOption2).appendTo(hourlyTab);
$(hourlyTab).appendTo(tabContent);
//craeting the dailyTab
var dailyTab = $("<div/>", { "class": "tab-pane", id: "Daily" });
var dailyOption1 = $("<div/>", { "class": "well well-small" });
$("<input/>", { type: "radio", value: "1", name: "DailyRadio", checked: "checked" }).appendTo(dailyOption1);
$(dailyOption1).append(" Every ");
$("<input/>", { id: "DaysInput", type: "text", value: "1", style: "width: 40px" }).appendTo(dailyOption1);
$(dailyOption1).append(" day(s)");
$(dailyOption1).appendTo(dailyTab);
var dailyOption2 = $("<div/>", { "class": "well well-small" });
$("<input/>", { type: "radio", value: "2", name: "DailyRadio" }).appendTo(dailyOption2);
$(dailyOption2).append(" Every week day ");
$(dailyOption2).appendTo(dailyTab);
$(dailyTab).append("Start time ");
$(dailyTab).append('<select id="DailyHours" class="hours" style="width: 60px"></select>');
$(dailyTab).append('<select id="DailyMinutes" class="minutes" style="width: 60px"></select>');
$(dailyTab).appendTo(tabContent);
//craeting the weeklyTab
var weeklyTab = $("<div/>", { "class": "tab-pane", id: "Weekly" });
var weeklyWell = $("<div/>", { "class": "well well-small" });
var span31 = $("<div/>", { "class": "span6 col-sm-6" });
$("<input/>", { type: "checkbox", value: "MON" }).appendTo(span31);
$(span31).append(" Monday<br />");
$("<input/>", { type: "checkbox", value: "WED" }).appendTo(span31);
$(span31).append(" Wednesday<br />");
$("<input/>", { type: "checkbox", value: "FRI" }).appendTo(span31);
$(span31).append(" Friday<br />");
$("<input/>", { type: "checkbox", value: "SUN" }).appendTo(span31);
$(span31).append(" Sunday");
var span32 = $("<div/>", { "class": "span6 col-sm-6" });
$("<input/>", { type: "checkbox", value: "TUE" }).appendTo(span32);
$(span32).append(" Tuesday<br />");
$("<input/>", { type: "checkbox", value: "THU" }).appendTo(span32);
$(span32).append(" Thursday<br />");
$("<input/>", { type: "checkbox", value: "SAT" }).appendTo(span32);
$(span32).append(" Saturday");
$(span31).appendTo(weeklyWell);
$(span32).appendTo(weeklyWell);
//Hack to fix the well box
$("<br /><br /><br /><br />").appendTo(weeklyWell);
$(weeklyWell).appendTo(weeklyTab);
$(weeklyTab).append("Start time ");
$(weeklyTab).append('<select id="WeeklyHours" class="hours" style="width: 60px"></select>');
$(weeklyTab).append('<select id="WeeklyMinutes" class="minutes" style="width: 60px"></select>');
$(weeklyTab).appendTo(tabContent);
//craeting the monthlyTab
var monthlyTab = $("<div/>", { "class": "tab-pane", id: "Monthly" });
var monthlyOption1 = $("<div/>", { "class": "well well-small" });
$("<input/>", { type: "radio", value: "1", name: "MonthlyRadio", checked: "checked" }).appendTo(monthlyOption1);
$(monthlyOption1).append(" Day ");
$("<input/>", { id: "DayOfMOnthInput", type: "text", value: "1", style: "width: 40px" }).appendTo(monthlyOption1);
$(monthlyOption1).append(" of every ");
$("<input/>", { id: "MonthInput", type: "text", value: "1", style: "width: 40px" }).appendTo(monthlyOption1);
$(monthlyOption1).append(" month(s)");
$(monthlyOption1).appendTo(monthlyTab);
var monthlyOption2 = $("<div/>", { "class": "well well-small" });
$("<input/>", { type: "radio", value: "2", name: "MonthlyRadio" }).appendTo(monthlyOption2);
$(monthlyOption2).append(" ");
$(monthlyOption2).append('<select id="WeekDay" class="day-order-in-month" style="width: 80px"></select>');
$(monthlyOption2).append('<select id="DayInWeekOrder" class="week-days" style="width: 100px"></select>');
$(monthlyOption2).append(" of every ");
$("<input/>", { id: "EveryMonthInput", type: "text", value: "1", style: "width: 40px" }).appendTo(monthlyOption2);
$(monthlyOption2).append(" month(s)");
$(monthlyOption2).appendTo(monthlyTab);
$(monthlyTab).append("Start time ");
$(monthlyTab).append('<select id="MonthlyHours" class="hours" style="width: 60px"></select>');
$(monthlyTab).append('<select id="MonthlyMinutes" class="minutes" style="width: 60px"></select>');
$(monthlyTab).appendTo(tabContent);
//craeting the yearlyTab
var yearlyTab = $("<div/>", { "class": "tab-pane", id: "Yearly" });
var yearlyOption1 = $("<div/>", { "class": "well well-small" });
$("<input/>", { type: "radio", value: "1", name: "YearlyRadio", checked: "checked" }).appendTo(yearlyOption1);
$(yearlyOption1).append(" Every ");
$(yearlyOption1).append('<select id="MonthsOfYear" class="months" style="width: 150px"></select>');
$(yearlyOption1).append(" in day ");
$("<input/>", { id: "YearInput", type: "text", value: "1", style: "width: 40px" }).appendTo(yearlyOption1);
$(yearlyOption1).appendTo(yearlyTab);
var yearlyOption2 = $("<div/>", { "class": "well well-small" });
$("<input/>", { type: "radio", value: "2", name: "YearlyRadio" }).appendTo(yearlyOption2);
$(yearlyOption2).append(" The ");
$(yearlyOption2).append('<select id="DayOrderInYear" class="day-order-in-month" style="width: 80px"></select>');
$(yearlyOption2).append('<select id="DayWeekForYear" class="week-days" style="width: 100px"></select>');
$(yearlyOption2).append(" of ");
$(yearlyOption2).append('<select id="MonthsOfYear2" class="months" style="width: 110px"></select>');
$(yearlyOption2).appendTo(yearlyTab);
$(yearlyTab).append("Start time ");
$(yearlyTab).append('<select id="YearlyHours" class="hours" style="width: 60px"></select>');
$(yearlyTab).append('<select id="YearlyMinutes" class="minutes" style="width: 60px"></select>');
$(yearlyTab).appendTo(tabContent);
$(tabContent).appendTo(span12);
//creating the button and results input
resultsName = $(this).prop("id");
$(this).prop("name", resultsName);
$(span12).appendTo(row);
$(row).appendTo(container);
$(container).appendTo(mainDiv);
$(cronContainer).append(mainDiv);
var that = $(this);
// Hide the original input
that.hide();
// Replace the input with an input group
var $g = $("<div>").addClass("input-group");
// Add an input
var $i = $("<input>", { type: 'text', placeholder: 'Cron trigger', readonly: 'readonly' }).addClass("form-control").val($(that).val());
$i.appendTo($g);
// Add the button
var $b = $("<button class=\"btn btn-default\"><i class=\"icon-edit\"></i></button>");
// Put button inside span
var $s = $("<span>").addClass("input-group-btn");
$b.appendTo($s);
$s.appendTo($g);
$(this).before($g);
inputElement = that;
displayElement = $i;
$b.popover({
html: true,
content: function () {
return $(cronContainer).html();
},
template: '<div class="popover" style="max-width:500px !important; width:500px"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><div class="popover-content"><p></p></div></div></div>',
placement: 'bottom'
}).on('click', function (e) {
e.preventDefault();
fillDataOfMinutesAndHoursSelectOptions();
fillDayWeekInMonth();
fillInWeekDays();
fillInMonths();
$('#CronGenTabs a').click(function (e) {
e.preventDefault();
$(this).tab('show');
//generate();
});
$("#CronGenMainDiv select, #CronGenMainDiv input").change(function (e) {
generate();
});
});
return;
}
});
var fillInMonths = function () {
var days = [
{ text: "January", val: "1" },
{ text: "February", val: "2" },
{ text: "March", val: "3" },
{ text: "April", val: "4" },
{ text: "May", val: "5" },
{ text: "June", val: "6" },
{ text: "July", val: "7" },
{ text: "August", val: "8" },
{ text: "September", val: "9" },
{ text: "October", val: "10" },
{ text: "Novermber", val: "11" },
{ text: "December", val: "12" }
];
$(".months").each(function () {
fillOptions(this, days);
});
};
var fillOptions = function (elements, options) {
for (var i = 0; i < options.length; i++)
$(elements).append("<option value='" + options[i].val + "'>" + options[i].text + "</option>");
};
var fillDataOfMinutesAndHoursSelectOptions = function () {
for (var i = 0; i < 60; i++) {
if (i < 24) {
$(".hours").each(function () { $(this).append(timeSelectOption(i)); });
}
$(".minutes").each(function () { $(this).append(timeSelectOption(i)); });
}
};
var fillInWeekDays = function () {
var days = [
{ text: "Monday", val: "MON" },
{ text: "Tuesday", val: "TUE" },
{ text: "Wednesday", val: "WED" },
{ text: "Thursday", val: "THU" },
{ text: "Friday", val: "FRI" },
{ text: "Saturday", val: "SAT" },
{ text: "Sunday", val: "SUN" }
];
$(".week-days").each(function () {
fillOptions(this, days);
});
};
var fillDayWeekInMonth = function () {
var days = [
{ text: "First", val: "1" },
{ text: "Second", val: "2" },
{ text: "Third", val: "3" },
{ text: "Fourth", val: "4" }
];
$(".day-order-in-month").each(function () {
fillOptions(this, days);
});
};
var displayTimeUnit = function (unit) {
if (unit.toString().length == 1)
return "0" + unit;
return unit;
};
var timeSelectOption = function (i) {
return "<option id='" + i + "'>" + displayTimeUnit(i) + "</option>";
};
var generate = function () {
var activeTab = $("ul#CronGenTabs li.active a").prop("id");
var results = "";
switch (activeTab) {
case "MinutesTab":
results = "0 0/" + $("#MinutesInput").val() + " * 1/1 * ? *";
break;
case "HourlyTab":
switch ($("input:radio[name=HourlyRadio]:checked").val()) {
case "1":
results = "0 0 0/" + $("#HoursInput").val() + " 1/1 * ? *";
break;
case "2":
results = "0 " + Number($("#AtMinutes").val()) + " " + Number($("#AtHours").val()) + " 1/1 * ? *";
break;
}
break;
case "DailyTab":
switch ($("input:radio[name=DailyRadio]:checked").val()) {
case "1":
results = "0 " + Number($("#DailyMinutes").val()) + " " + Number($("#DailyHours").val()) + " 1/" + $("#DaysInput").val() + " * ? *";
break;
case "2":
results = "0 " + Number($("#DailyMinutes").val()) + " " + Number($("#DailyHours").val()) + " ? * MON-FRI *";
break;
}
break;
case "WeeklyTab":
var selectedDays = "";
$("#Weekly input:checkbox:checked").each(function () { selectedDays += $(this).val() + ","; });
if (selectedDays.length > 0)
selectedDays = selectedDays.substr(0, selectedDays.length - 1);
results = "0 " + Number($("#WeeklyMinutes").val()) + " " + Number($("#WeeklyHours").val()) + " ? * " + selectedDays + " *";
break;
case "MonthlyTab":
switch ($("input:radio[name=MonthlyRadio]:checked").val()) {
case "1":
results = "0 " + Number($("#MonthlyMinutes").val()) + " " + Number($("#MonthlyHours").val()) + " " + $("#DayOfMOnthInput").val() + " 1/" + $("#MonthInput").val() + " ? *";
break;
case "2":
results = "0 " + Number($("#MonthlyMinutes").val()) + " " + Number($("#MonthlyHours").val()) + " ? 1/" + Number($("#EveryMonthInput").val()) + " " + $("#DayInWeekOrder").val() + "#" + $("#WeekDay").val() + " *";
break;
}
break;
case "YearlyTab":
switch ($("input:radio[name=YearlyRadio]:checked").val()) {
case "1":
results = "0 " + Number($("#YearlyMinutes").val()) + " " + Number($("#YearlyHours").val()) + " " + $("#YearInput").val() + " " + $("#MonthsOfYear").val() + " ? *";
break;
case "2":
results = "0 " + Number($("#YearlyMinutes").val()) + " " + Number($("#YearlyHours").val()) + " ? " + $("#MonthsOfYear2").val() + " " + $("#DayWeekForYear").val() + "#" + $("#DayOrderInYear").val() + " *";
break;
}
break;
}
// Update original control
inputElement.val(results).change();
// Update display
displayElement.val(results);
};
})(jQuery);