1 | jQuery.fn.pwgDatepicker = function(options) { |
---|
2 | options = options || {}; |
---|
3 | |
---|
4 | return this.each(function() { |
---|
5 | var $this = jQuery(this), |
---|
6 | $target = jQuery('[name="'+ jQuery(this).data('datepicker') +'"]'), |
---|
7 | value = $target.val().split('-'); |
---|
8 | |
---|
9 | function set(date) { |
---|
10 | $this.datepicker('setDate', date); |
---|
11 | |
---|
12 | if ($this.data('datepicker-start')) { |
---|
13 | $start.datepicker('option', 'maxDate', date); |
---|
14 | } |
---|
15 | else if ($this.data('datepicker-end')) { |
---|
16 | $end.datepicker('option', 'minDate', date); |
---|
17 | } |
---|
18 | } |
---|
19 | |
---|
20 | // init picker |
---|
21 | $this.datepicker(jQuery.extend({ |
---|
22 | dateFormat: 'DD d MM yy', |
---|
23 | altField: $target, |
---|
24 | altFormat: 'yy-mm-dd', |
---|
25 | autoSize: true, |
---|
26 | changeMonth : true, |
---|
27 | changeYear: true |
---|
28 | }, options)); |
---|
29 | |
---|
30 | // attach linked picker (for ranges) |
---|
31 | if ($this.data('datepicker-start')) { |
---|
32 | var $start = jQuery('[data-datepicker="'+ jQuery(this).data('datepicker-start') +'"]'); |
---|
33 | |
---|
34 | $this.datepicker('option', 'onClose', function(date) { |
---|
35 | $start.datepicker('option', 'maxDate', date); |
---|
36 | }); |
---|
37 | } |
---|
38 | else if ($this.data('datepicker-end')) { |
---|
39 | var $end = jQuery('[data-datepicker="'+ jQuery(this).data('datepicker-end') +'"]'); |
---|
40 | |
---|
41 | $this.datepicker('option', 'onClose', function(date) { |
---|
42 | $end.datepicker('option', 'minDate', date); |
---|
43 | }); |
---|
44 | } |
---|
45 | |
---|
46 | // attach unset button |
---|
47 | if ($this.data('datepicker-unset')) { |
---|
48 | jQuery('#'+ $this.data('datepicker-unset')).on('click', function(e) { |
---|
49 | e.preventDefault(); |
---|
50 | |
---|
51 | $target.val(''); |
---|
52 | set(null); |
---|
53 | }); |
---|
54 | } |
---|
55 | |
---|
56 | // set value from linked input |
---|
57 | if (value.length == 3) { |
---|
58 | set(new Date(value[0], value[1]-1, value[2])); |
---|
59 | } |
---|
60 | }); |
---|
61 | }; |
---|