Skip to content

Commit a57f7c9

Browse files
committed
Added a readOnly option.
Fixed a global scope leak.
1 parent 85d52a1 commit a57f7c9

File tree

3 files changed

+121
-45
lines changed

3 files changed

+121
-45
lines changed

cron/jquery-cron.js

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
var defaults = {
4444
initial : "* * * * *",
45+
readOnly: false,
4546
minuteOpts : {
4647
minWidth : 100, // Only applies if columns and itemWidth not set.
4748
itemWidth : 30,
@@ -91,7 +92,6 @@
9192
closeEffect : "slide",
9293
hideOnMouseOut : true
9394
},
94-
url_set : undefined,
9595
customValues : undefined,
9696
onChange: undefined
9797
};
@@ -100,7 +100,7 @@
100100
var str_opt_mih = "";
101101
for (var i = 0; i < 60; i++) {
102102
var j = (i < 10)? "0":"";
103-
str_opt_mih += "<option value='"+i+"'>" + j + i + "</option>\n";
103+
str_opt_mih += "<option value='" + i + "'>" + j + i + "</option>\n";
104104
}
105105

106106
// Options for hours in a day,
@@ -117,7 +117,7 @@
117117
else if (i == 2 || i == 22) { var suffix = "nd"; }
118118
else if (i == 3 || i == 23) { var suffix = "rd"; }
119119
else { var suffix = "th"; }
120-
str_opt_dom += "<option value='"+i+"'>" + i + suffix + "</option>\n";
120+
str_opt_dom += "<option value='" + i + "'>" + i + suffix + "</option>\n";
121121
}
122122

123123
// Options for months.
@@ -126,22 +126,22 @@
126126
"May", "June", "July", "August",
127127
"September", "October", "November", "December"];
128128
for (var i = 0; i < months.length; i++) {
129-
str_opt_month += "<option value='"+(i+1)+"'>" + months[i] + "</option>\n";
129+
str_opt_month += "<option value='" + (i + 1) + "'>" + months[i] + "</option>\n";
130130
}
131131

132132
// Options for day of week.
133133
var str_opt_dow = "";
134134
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
135135
"Friday", "Saturday"];
136136
for (var i = 0; i < days.length; i++) {
137-
str_opt_dow += "<option value='"+i+"'>" + days[i] + "</option>\n";
137+
str_opt_dow += "<option value='" + i + "'>" + days[i] + "</option>\n";
138138
}
139139

140140
// Options for period.
141141
var str_opt_period = "";
142142
var periods = ["minute", "hour", "day", "week", "month", "year"];
143143
for (var i = 0; i < periods.length; i++) {
144-
str_opt_period += "<option value='"+periods[i]+"'>" + periods[i] + "</option>\n";
144+
str_opt_period += "<option value='" + periods[i] + "'>" + periods[i] + "</option>\n";
145145
}
146146

147147
// Display matrix.
@@ -235,10 +235,13 @@
235235
return false;
236236
}
237237

238-
function getCurrentValue(c) {
239-
var block = c.data("block");
240-
var min = hour = day = month = dow = "*";
238+
function getCurrentValue($el) {
239+
var min, hour, day, month, dow;
240+
min = hour = day = month = dow = "*";
241+
242+
var block = $el.data("block");
241243
var selectedPeriod = block["period"].find("select").val();
244+
242245
switch (selectedPeriod) {
243246
case "minute":
244247
break;
@@ -286,16 +289,17 @@
286289
var methods = {
287290
init : function(opts) {
288291
// Init options.
289-
var options = opts ? opts : {}; // Default to empty obj.
292+
var options = opts ? opts : {};
290293
var o = $.extend([], defaults, options);
291-
var eo = $.extend({}, defaults.effectOpts, options.effectOpts);
294+
var ro = { readOnly: o.readOnly };
295+
var eo = $.extend({}, defaults.effectOpts, options.effectOpts, ro);
292296
$.extend(o, {
293-
minuteOpts : $.extend({}, defaults.minuteOpts, eo, options.minuteOpts),
294-
domOpts : $.extend({}, defaults.domOpts, eo, options.domOpts),
295-
monthOpts : $.extend({}, defaults.monthOpts, eo, options.monthOpts),
296-
dowOpts : $.extend({}, defaults.dowOpts, eo, options.dowOpts),
297-
timeHourOpts : $.extend({}, defaults.timeHourOpts, eo, options.timeHourOpts),
298-
timeMinuteOpts : $.extend({}, defaults.timeMinuteOpts, eo, options.timeMinuteOpts)
297+
minuteOpts : $.extend({}, defaults.minuteOpts, eo, ro, options.minuteOpts),
298+
domOpts : $.extend({}, defaults.domOpts, eo, ro, options.domOpts),
299+
monthOpts : $.extend({}, defaults.monthOpts, eo, ro, options.monthOpts),
300+
dowOpts : $.extend({}, defaults.dowOpts, eo, ro, options.dowOpts),
301+
timeHourOpts : $.extend({}, defaults.timeHourOpts, eo, ro, options.timeHourOpts),
302+
timeMinuteOpts : $.extend({}, defaults.timeMinuteOpts, eo, ro, options.timeMinuteOpts)
299303
});
300304

301305
// Prepend custom values if specified.
@@ -412,9 +416,9 @@
412416
this.data("block", block);
413417
this.data("blockBefore", blockBefore);
414418
this.data("blockAfter", blockAfter);
415-
this.data("current_value", o.initial); // Remember base value to detect changes.
419+
this.data("initialized", false);
416420

417-
return methods["value"].call(this, o.initial); // Set initial value.
421+
return methods.value.call(this, o.initial); // Set initial value.
418422
},
419423

420424
value : function(cron_str) {
@@ -424,6 +428,11 @@
424428
var t = getCronType(cron_str);
425429
if (!defined(t)) { return false; }
426430

431+
// Do not set the value if read-only unless the cron has not yet been initialized.
432+
var initialized = this.data('initialized');
433+
var opts = this.data('options');
434+
if (initialized && opts.readOnly) { return false; }
435+
427436
var block = this.data('block');
428437
var d = cron_str.split(' ');
429438
var v = {
@@ -474,18 +483,26 @@
474483
.gentleSelect('update')
475484
.trigger('change');
476485

486+
// Cron has been initialized.
487+
this.data("initialized", true);
488+
477489
return this;
478490
}
479491
};
480492

481493
var event_handlers = {
482494
periodChanged : function() {
483-
var root = $(this).data("root");
495+
var root = $(this).data('root');
496+
var initialized = root.data('initialized');
497+
var opts = root.data('options');
498+
499+
if (initialized && opts.readOnly) { return; }
500+
484501
var blockBefore = root.data("blockBefore");
485502
var blockAfter = root.data("blockAfter");
486503
var block = root.data("block");
487-
var opt = root.data("options");
488504
var period = $(this).val();
505+
489506
root.find(".cron-block").hide(); // First, hide all blocks.
490507
if (toDisplay.hasOwnProperty(period)) {
491508
// Only if not a custom value.

gentleSelect/jquery-gentleSelect.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
* <select id='myselect'><options> ... </options></select>
2121
*/
2222
(function($) {
23-
2423
var defaults = {
2524
minWidth : 100, // Only applies if columns and itemWidth not set.
2625
itemWidth : undefined,
@@ -187,13 +186,13 @@
187186
.data("label", label)
188187
.data("root", this);
189188
this.data("dialog", dialog);
190-
189+
191190
// If to be displayed in columns.
192191
if (defined(o.columns) || defined(o.rows)) {
193192
// Update CSS.
194193
ul.css("float", "left")
195194
.find("li").width(o.itemWidth).css("float", "left");
196-
195+
197196
var f = ul.find("li:first");
198197
var actualWidth = o.itemWidth
199198
+ parseInt(f.css("padding-left"))
@@ -297,7 +296,7 @@
297296
$.data(document.body, "activeDialog", dialog[0]);
298297
}
299298
},
300-
299+
301300
dialogHoverOut : function() {
302301
var $this = $(this);
303302
if ($this.data("root").data("options").hideOnMouseOut) {
@@ -329,21 +328,23 @@
329328
// TODO:
330329
// Animate the clicked element being inserted or removed.
331330

332-
if ($this.data("root").attr("multiple")) {
333-
clicked.toggleClass("selected");
334-
var s = $this.find("li.selected");
335-
label.html(getSelectedAsText(root, s, opts));
336-
var v = s.map(function() {
337-
return $(this).data("value");
338-
});
339-
// Update actual selectbox and trigger change event.
340-
root.val(v.get()).trigger("change");
341-
} else {
342-
$this.find("li.selected").removeClass("selected");
343-
clicked.addClass("selected");
344-
label.text(clicked.data("name"));
345-
// Update actual selectbox and trigger change event.
346-
root.val(value).trigger("change");
331+
if (!opts.readOnly) {
332+
if ($this.data("root").attr("multiple")) {
333+
clicked.toggleClass("selected");
334+
var s = $this.find("li.selected");
335+
label.html(getSelectedAsText(root, s, opts));
336+
var v = s.map(function() {
337+
return $(this).data("value");
338+
});
339+
// Update actual selectbox and trigger change event.
340+
root.val(v.get()).trigger("change");
341+
} else {
342+
$this.find("li.selected").removeClass("selected");
343+
clicked.addClass("selected");
344+
label.text(clicked.data("name"));
345+
// Update actual selectbox and trigger change event.
346+
root.val(value).trigger("change");
347+
}
347348
}
348349
}
349350
},
@@ -359,7 +360,6 @@
359360

360361
if (e.currentTarget == activeDialog) {
361362
// If they clicked on a dialog box.
362-
363363
e.stopPropagation();
364364
$('.gentleselect-dialog').each(function(key, val) {
365365
if ($(val).attr('display') != 'none' && val != e.currentTarget) {

test/test.js

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,13 @@
7676
});
7777

7878
it('should initialize a "minute" cron', function() {
79-
var $cron = getCurrCronUiEl().cron({
79+
var $cronUiEl = getCurrCronUiEl();
80+
var $cronValEl = getCurrCronValEl();
81+
82+
var $cron = $cronUiEl.cron({
8083
initial: '* * * * *',
8184
onChange: function() {
82-
var cronVal = getCurrCronValEl().text($(this).cron("value"));
85+
$cronValEl.text($(this).cron('value'));
8386
}
8487
});
8588

@@ -120,10 +123,61 @@
120123
});
121124

122125
it('should initialize a "week" cron', function() {
123-
var $cron = getCurrCronUiEl().cron({
126+
var $cronUiEl = getCurrCronUiEl();
127+
var $cronValEl = getCurrCronValEl();
128+
129+
var $cron = $cronUiEl.cron({
130+
initial: '0,15,30 5 * * 1,2',
131+
onChange: function() {
132+
$cronValEl.text($(this).cron('value'));
133+
}
134+
});
135+
136+
var $cronPeriodEl = $cron.find('.cron-period');
137+
$cronPeriodEl.should.have.length(1);
138+
$cronPeriodEl.css('display').should.not.equal('none');
139+
$cronPeriodEl.find('.gentleselect-label').text().should.equal('week');
140+
141+
var $cronBlockDomEl = $cron.find('.cron-block-dom');
142+
$cronBlockDomEl.should.have.length(1);
143+
$cronBlockDomEl.css('display').should.equal('none');
144+
$cronBlockDomEl.find('.gentleselect-label').text().should.equal('1st');
145+
146+
var $cronBlockMonthEl = $cron.find('.cron-block-month');
147+
$cronBlockMonthEl.should.have.length(1);
148+
$cronBlockMonthEl.css('display').should.equal('none');
149+
$cronBlockMonthEl.find('.gentleselect-label').text().should.equal('January');
150+
151+
var $cronBlockMinsEl = $cron.find('.cron-block-mins');
152+
$cronBlockMinsEl.should.have.length(1);
153+
$cronBlockMinsEl.css('display').should.equal('none');
154+
$cronBlockMinsEl.find('.gentleselect-label').text().should.equal('00');
155+
156+
var $cronBlockDowEl = $cron.find('.cron-block-dow');
157+
$cronBlockDowEl.should.have.length(1);
158+
$cronBlockDowEl.css('display').should.not.equal('none');
159+
$cronBlockDowEl.find('.gentleselect-label').text().should.equal('MondayTuesday');
160+
161+
var $cronBlockTimeHourEl = $cron.find('.cron-time-hour');
162+
$cronBlockTimeHourEl.should.have.length(1);
163+
$cronBlockTimeHourEl.css('display').should.not.equal('none');
164+
$cronBlockTimeHourEl.find('.gentleselect-label').text().should.equal('05');
165+
166+
var $cronBlockTimeMinEl = $cron.find('.cron-time-min');
167+
$cronBlockTimeMinEl.should.have.length(1);
168+
$cronBlockTimeMinEl.css('display').should.not.equal('none');
169+
$cronBlockTimeMinEl.find('.gentleselect-label').text().should.equal('001530');
170+
});
171+
172+
it('should initialize a read-only "week" cron', function() {
173+
var $cronUiEl = getCurrCronUiEl();
174+
var $cronValEl = getCurrCronValEl();
175+
176+
var $cron = $cronUiEl.cron({
124177
initial: '0,15,30 5 * * 1,2',
178+
readOnly: true,
125179
onChange: function() {
126-
var cronVal = getCurrCronValEl().text($(this).cron("value"));
180+
$cronValEl.text($(this).cron('value'));
127181
}
128182
});
129183

@@ -164,4 +218,9 @@
164218
});
165219
});
166220
});
221+
222+
223+
describe('test jquery-gentleSelect', function() {
224+
225+
});
167226
}).call(this);

0 commit comments

Comments
 (0)