|
28 | 28 | firstDay: 0, // The first day of the week, Sun = 0, Mon = 1, ... |
29 | 29 | min: undefined, |
30 | 30 | max: undefined, |
31 | | - trigger: false, |
| 31 | + trigger: 0, |
| 32 | + editable: 0, |
32 | 33 |
|
33 | 34 | css: { |
34 | 35 |
|
|
135 | 136 |
|
136 | 137 | function parseDate(val) { |
137 | 138 |
|
138 | | - if (!val) { return; } |
| 139 | + if (val === undefined) { return; } |
139 | 140 | if (val.constructor == Date) { return val; } |
140 | 141 |
|
141 | 142 | if (typeof val == 'string') { |
|
166 | 167 | // variables |
167 | 168 | var self = this, |
168 | 169 | now = new Date(), |
| 170 | + yearNow = now.getFullYear(), |
169 | 171 | css = conf.css, |
170 | 172 | labels = LABELS[conf.lang], |
171 | 173 | root = $("#" + css.root), |
|
176 | 178 | value = input.attr("data-value") || conf.value || input.val(), |
177 | 179 | min = input.attr("min") || conf.min, |
178 | 180 | max = input.attr("max") || conf.max, |
179 | | - opened; |
| 181 | + opened, |
| 182 | + original; |
180 | 183 |
|
181 | 184 | // zero min is not undefined |
182 | 185 | if (min === 0) { min = "0"; } |
183 | 186 |
|
184 | 187 | // use sane values for value, min & max |
185 | | - value = parseDate(value) || now; |
186 | | - min = parseDate(min || conf.yearRange[0] * 365); |
187 | | - max = parseDate(max || conf.yearRange[1] * 365); |
| 188 | + value = parseDate(value) || now; |
| 189 | + |
| 190 | + min = parseDate(min || new Date(yearNow + conf.yearRange[0], 1, 1)); |
| 191 | + max = parseDate(max || new Date( yearNow + conf.yearRange[1]+ 1, 1, -1)); |
188 | 192 |
|
189 | 193 |
|
190 | 194 | // check that language exists |
191 | 195 | if (!labels) { throw "Dateinput: invalid language: " + conf.lang; } |
192 | 196 |
|
193 | 197 | // Replace built-in date input: NOTE: input.attr("type", "text") throws exception by the browser |
194 | 198 | if (input.attr("type") == 'date') { |
| 199 | + |
| 200 | + original = input.clone(); |
195 | 201 | var tmp = $("<input/>"); |
196 | 202 |
|
197 | | - $.each("class,disabled,id,maxlength,name,readonly,required,size,style,tabindex,title,value".split(","), function(i, attr) { |
| 203 | + $.each("class,disabled,id,maxlength,name,placeholder,readonly,required,size,style,tabindex,title,value".split(","), function(i, attr) { |
198 | 204 | tmp.attr(attr, input.attr(attr)); |
199 | | - }); |
| 205 | + }); |
| 206 | + |
200 | 207 | input.replaceWith(tmp); |
201 | 208 | input = tmp; |
202 | 209 | } |
|
303 | 310 | return self.hide(e); |
304 | 311 | } |
305 | 312 |
|
306 | | - // esc key |
307 | | - if (key == 27) { return self.hide(e); } |
| 313 | + // esc or tab key |
| 314 | + if (key == 27 || key == 9) { return self.hide(e); } |
308 | 315 |
|
309 | 316 | if ($(KEYS).index(key) >= 0) { |
310 | 317 |
|
|
614 | 621 |
|
615 | 622 | addYear: function(amount) { |
616 | 623 | return this.setValue(currYear + (amount || 1), currMonth, currDay); |
| 624 | + }, |
| 625 | + |
| 626 | + destroy: function() { |
| 627 | + input.add(document).unbind("click.d").unbind("keydown.d"); |
| 628 | + root.add(trigger).remove(); |
| 629 | + input.removeData("dateinput").removeClass(css.input); |
| 630 | + if (original) { input.replaceWith(original); } |
617 | 631 | }, |
618 | | - |
| 632 | + |
619 | 633 | hide: function(e) { |
620 | 634 |
|
621 | 635 | if (opened) { |
|
655 | 669 | } |
656 | 670 |
|
657 | 671 | }); |
658 | | - |
659 | | - // show dateinput & assign keyboard shortcuts |
660 | | - input.bind("focus click", self.show).keydown(function(e) { |
661 | 672 |
|
662 | | - var key = e.keyCode; |
663 | | - |
664 | | - // open dateinput with navigation keyw |
665 | | - if (!opened && $(KEYS).index(key) >= 0) { |
666 | | - self.show(e); |
667 | | - return e.preventDefault(); |
668 | | - } |
669 | | - |
670 | | - // allow tab |
671 | | - return e.shiftKey || e.ctrlKey || e.altKey || key == 9 ? true : e.preventDefault(); |
| 673 | + if (!conf.editable) { |
672 | 674 |
|
673 | | - }); |
| 675 | + // show dateinput & assign keyboard shortcuts |
| 676 | + input.bind("focus.d click.d", self.show).keydown(function(e) { |
| 677 | + |
| 678 | + var key = e.keyCode; |
| 679 | + |
| 680 | + // open dateinput with navigation keyw |
| 681 | + if (!opened && $(KEYS).index(key) >= 0) { |
| 682 | + self.show(e); |
| 683 | + return e.preventDefault(); |
| 684 | + } |
| 685 | + |
| 686 | + // allow tab |
| 687 | + return e.shiftKey || e.ctrlKey || e.altKey || key == 9 ? true : e.preventDefault(); |
| 688 | + |
| 689 | + }); |
| 690 | + } |
674 | 691 |
|
675 | 692 | // initial value |
676 | 693 | if (parseDate(input.val())) { |
|
0 commit comments