Skip to content

Commit bef27d9

Browse files
committed
Added customValues option to specify custom cron entries.
1 parent 5c8b6cd commit bef27d9

File tree

2 files changed

+92
-18
lines changed

2 files changed

+92
-18
lines changed

cron/jquery-cron.js

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
hideOnMouseOut : true
9393
},
9494
url_set : undefined,
95+
customValues : undefined,
9596
onChange: undefined // callback function each time value changes
9697
};
9798

@@ -156,20 +157,24 @@
156157
};
157158

158159
var combinations = {
159-
"minute" : /^(\*\s){4}\*$/, // "* * * * *"
160-
"hour" : /^\d{1,2}\s(\*\s){3}\*$/, // "? * * * *"
161-
"day" : /^(\d{1,2}\s){2}(\*\s){2}\*$/, // "? ? * * *"
160+
"minute" : /^(\*\s){4}\*$/, // "* * * * *"
161+
"hour" : /^\d{1,2}\s(\*\s){3}\*$/, // "? * * * *"
162+
"day" : /^(\d{1,2}\s){2}(\*\s){2}\*$/, // "? ? * * *"
162163
"week" : /^(\d{1,2}\s){2}(\*\s){2}\d{1,2}$/, // "? ? * * ?"
163-
"month" : /^(\d{1,2}\s){3}\*\s\*$/, // "? ? ? * *"
164-
"year" : /^(\d{1,2}\s){4}\*$/ // "? ? ? ? *"
164+
"month" : /^(\d{1,2}\s){3}\*\s\*$/, // "? ? ? * *"
165+
"year" : /^(\d{1,2}\s){4}\*$/ // "? ? ? ? *"
165166
};
166-
167+
167168
// ------------------ internal functions ---------------
168169
function defined(obj) {
169170
if (typeof obj == "undefined") { return false; }
170171
else { return true; }
171172
}
172173

174+
function undefinedOrObject(obj) {
175+
return (!defined(obj) || typeof obj == "object")
176+
}
177+
173178
function getCronType(cron_str) {
174179
// check format of initial cron value
175180
var valid_cron = /^((\d{1,2}|\*)\s){4}(\d{1,2}|\*)$/
@@ -203,14 +208,18 @@
203208

204209
function hasError(c, o) {
205210
if (!defined(getCronType(o.initial))) { return true; }
211+
if (!undefinedOrObject(o.customValues)) { return true; }
206212
return false;
207213
}
208214

209215
function getCurrentValue(c) {
210216
var b = c.data("block");
211217
var min = hour = day = month = dow = "*";
212-
213-
switch (b["period"].find("select").val()) {
218+
var selectedPeriod = b["period"].find("select").val();
219+
switch (selectedPeriod) {
220+
case "minute":
221+
break;
222+
214223
case "hour":
215224
min = b["mins"].find("select").val();
216225
break;
@@ -238,6 +247,10 @@
238247
day = b["dom"].find("select").val();
239248
month = b["month"].find("select").val();
240249
break;
250+
251+
default:
252+
// we assume this only happens when customValues is set
253+
return selectedPeriod;
241254
}
242255
return [min, hour, day, month, dow].join(" ");
243256
}
@@ -264,10 +277,15 @@
264277

265278
// ---- define select boxes in the right order -----
266279

267-
var block = []
280+
var block = [], custom_periods = "", cv = o.customValues;
281+
if (defined(cv)) { // prepend custom values if specified
282+
for (var key in cv) {
283+
custom_periods += "<option value='" + cv[key] + "'>" + key + "</option>\n";
284+
}
285+
}
268286
block["period"] = $("<span class='cron-period'>"
269-
+ "Every <select name='cron-period'>" + str_opt_period
270-
+ "</select> </span>")
287+
+ "Every <select name='cron-period'>" + custom_periods
288+
+ str_opt_period + "</select> </span>")
271289
.appendTo(this)
272290
.find("select")
273291
.bind("change.cron", event_handlers.periodChanged)
@@ -395,12 +413,15 @@
395413
var event_handlers = {
396414
periodChanged : function() {
397415
var root = $(this).data("root");
398-
var block = root.data("block");
399-
var b = toDisplay[$(this).val()];
400-
401-
root.find("span.cron-block").hide(); // first, hide all blocks
402-
for (var i = 0; i < b.length; i++) {
403-
block[b[i]].show();
416+
var block = root.data("block"),
417+
opt = root.data("options");
418+
var period = $(this).val();
419+
root.find("span.cron-block").hide(); // first, hide all blocks
420+
if (toDisplay.hasOwnProperty(period)) { // not custom value
421+
var b = toDisplay[$(this).val()];
422+
for (var i = 0; i < b.length; i++) {
423+
block[b[i]].show();
424+
}
404425
}
405426
},
406427

index.html

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141
openSpeed: "slow"
4242
}
4343
});
44+
$('#example3').cron({
45+
initial: "42 3 * * 5",
46+
onChange: function() {
47+
$('#example3-val').text($(this).cron("value"));
48+
},
49+
customValues: {
50+
"5 Minutes" : "*/5 * * * *",
51+
"2 Hours on Weekends" : "0 */2 * * 5,6"
52+
}
53+
});
4454
});
4555
</script>
4656
</head>
@@ -66,7 +76,13 @@ <h2 id='intro'>Introduction</h2>
6676
</div>
6777

6878
<p>
69-
There is much room for improvements, and we welcome contributions
79+
In addition, the <a href='#option-customvalues'><tt>customValues</tt></a>
80+
option can be used to provide a list of canned cron entries. This allow users to
81+
cater for common use cases as well as enable more complex cron entries.
82+
</p>
83+
84+
<p>
85+
There is much room for improvements and we welcome contributions
7086
and bug fixes. Feel free to fork <a href='https://github.com/shawnchin/jquery-cron'>the project</a>
7187
and send us pull requests!
7288
</p>
@@ -255,6 +271,43 @@ <h3 id='option-each-element'>Customising individual select boxes</h3>
255271
</pre>
256272

257273
</p>
274+
275+
<h3 id='option-customvalues'>Adding custom values</h3>
276+
<p>
277+
Additional entries can be included in the period selection using the <tt>customValues</tt>
278+
option. This allows you to specify more complex cron entries that is not currently expressible
279+
using the current setup.
280+
</p>
281+
<p>
282+
For example, the following adds two additional entries to the selection:
283+
<pre>
284+
$('#selector').cron({
285+
customValues: {
286+
"5 Minutes" : "*/5 * * * *",
287+
"2 Hours on Weekends" : "0 */2 * * 5,6"
288+
}
289+
});
290+
</pre>
291+
</p>
292+
293+
<div class='example'>
294+
<div id='example3'></div>
295+
<p>Generated cron entry: <span class='example-text' id='example3-val'></span></p>
296+
</div>
297+
298+
<p>
299+
<b>Caveats:</b>
300+
<ul>
301+
<li>
302+
At present, cron entries specified in <tt>customValues</tt> are not validated.
303+
It is down to the implementer to ensure that provided values are as required.
304+
</li>
305+
<li>
306+
The <a href='#method-value'><tt>value</tt></a> method can be used to retrieve
307+
custom cron values. However, it cannot be used to set custom values.
308+
</li>
309+
</ul>
310+
</p>
258311
<h2>Methods</h2>
259312
<h3 id='method-value'>value</h3>
260313
<p>

0 commit comments

Comments
 (0)