source: trunk/themes/default/js/datepicker.js @ 23384

Last change on this file since 23384 was 21798, checked in by rvelices, 11 years ago

fix compatibility with jquery 1.9

  • Property svn:eol-style set to LF
File size: 5.2 KB
Line 
1// initialize controls
2// buttonImageName: Directory and name of calendar picture
3// day, month, year: selectors of visible date controls
4// linked_date: selector of hidden linked dates control
5// checked_on_change: selector of control to change "checked" attribut
6// min_linked_date: selector of hidden linked date control witch give min value
7// max_linked_date: selector of hidden linked date control witch give max value
8function pwg_common_initialization_datepicker(buttonImageName, day, month, year, linked_date, checked_on_change, min_linked_date, max_linked_date)
9{
10  // return formated date with control values
11  function pwg_get_fmt_from_ctrls()
12  {
13    return $(year).val() + "-" + $(month).val() + "-" + $(day).val();
14  }
15
16  // return if linked_date is valid date
17  function is_valid_linked_value(linked_date_name)
18  {
19                var array_date = $(linked_date_name).val().split('-');
20                return (
21                        (array_date.length == 3) &&
22                        (array_date[0].length) &&
23                        (array_date[1].length) && (array_date[1] != "0") &&
24                        (array_date[2].length) && (array_date[2] != "0")
25                        )
26  }
27 
28  // Action on change date value
29  function pwg_on_date_change()
30  {
31    pwg_check_date();
32    if (checked_on_change != null)
33    {
34                        $(checked_on_change).prop("checked", true);
35    }
36  }
37
38  // In order to desable element of list
39  function pwg_disabled_selection()
40  {
41                var array_date = $(linked_date).val().split('-')
42                        , y = array_date[0]
43                        , m = array_date[1];
44
45    // Init list
46    $(day + " option").removeAttr("disabled");
47    $(month + " option").removeAttr("disabled");
48
49    var daysInMonth = 32 - new Date(y, m - 1, 32).getDate();
50    $(day + " option:gt(" + (daysInMonth) +")").attr("disabled", "disabled");
51
52    if ((min_linked_date != null) && (is_valid_linked_value(min_linked_date) == true))
53    {
54      date_cmp = min_linked_date;
55      op_cmp = "lt";
56    }
57    else if ((max_linked_date != null) && (is_valid_linked_value(max_linked_date) == true))
58    {
59      date_cmp = max_linked_date;
60      op_cmp = "gt";
61    }
62    else
63    {
64      date_cmp = null;
65      op_cmp = null;
66    }
67
68    if (op_cmp != null)
69    {
70      array_date = $(date_cmp).val().split('-');
71      y_cmp = array_date[0];
72      m_cmp = array_date[1];
73      d_cmp = array_date[2];
74     
75      if (y == y_cmp)
76      {
77        $(month + " option:" + op_cmp + "(" + (m_cmp) +")").attr("disabled", "disabled");
78        if (op_cmp ==  "lt")
79        {
80            $(month + " option:eq(" + (0) +")").removeAttr("disabled");
81        }
82
83        if (m == m_cmp)
84        {
85          $(day + " option:" + op_cmp + "(" + (d_cmp) +")").attr("disabled", "disabled");
86          if (op_cmp ==  "lt")
87          {
88            $(day + " option:eq(" + (0) +")").removeAttr("disabled");
89          }
90        }
91      }
92    }
93  }
94
95  // Prevent selection of invalid dates through the select controls
96  function pwg_check_date()
97  {
98                var last_date = $(linked_date).val()
99                        , cancel=false;
100
101    $(linked_date).val(pwg_get_fmt_from_ctrls());
102
103    if ((min_linked_date != null) && (is_valid_linked_value(min_linked_date)))
104    {
105      cancel = ($(min_linked_date).datepicker("getDate") > $(linked_date).datepicker("getDate"));
106    }
107    else if ((max_linked_date != null) && (is_valid_linked_value(max_linked_date)))
108    {
109      cancel = ($(max_linked_date).datepicker("getDate") < $(linked_date).datepicker("getDate"));
110    }
111
112    if (cancel)
113    {
114      var array_date = last_date.split('-');
115      $(year).val(array_date[0]);
116      $(month).val(array_date[1]);
117      $(day).val(array_date[2]);
118      // check again
119      pwg_check_date();
120    }
121  }
122
123  jQuery().ready(function(){
124    // Init hidden value
125    $(linked_date).val(pwg_get_fmt_from_ctrls());
126
127    // Init Datepicker
128    jQuery(linked_date).datepicker({
129      dateFormat:'yy-m-d',
130      beforeShow:
131        // Prepare to show a date picker linked to three select controls
132        function readLinked(input) { 
133            if (min_linked_date != null)
134            {
135              return {minDate: $(min_linked_date).datepicker("getDate")};
136            }
137            else if (max_linked_date != null)
138            {
139              return {maxDate: $(max_linked_date).datepicker("getDate")};
140            }
141            else
142            {
143              return {};
144            }
145        },
146      onSelect:
147        // Update three select controls to match a date picker selection
148        function updateLinked(date) {
149            if (date.length == 0)
150            {
151              $(year).val("");
152              $(month).val("0");
153              $(day).val("0");
154            }
155            else
156            {
157              var array_date = date.split('-');
158              $(year).val(array_date[0]);
159              $(month).val(array_date[1]);
160              $(day).val(array_date[2]);
161            }
162            pwg_on_date_change();
163        },
164      showOn: "both",
165      buttonImage: buttonImageName,
166      buttonImageOnly: true,
167      buttonText: ""
168      });
169
170    // Check showed controls
171    jQuery(day + ", " + month + ", " + year).change(
172      function ()
173      {
174        pwg_on_date_change();
175      });
176
177    // Check showed controls
178    jQuery(day + ", " + month + ", " + year).focus(
179      function ()
180      {
181        pwg_disabled_selection();
182      });
183
184    // In order to init linked input
185    pwg_check_date();
186   });
187
188}
Note: See TracBrowser for help on using the repository browser.