Debian Codegraph @

dojox/mobile/_DatePickerMixin.js

raw symbols SCIP
1 define([
2 "dojo/_base/array",
3 "dojo/_base/declare",
4 "dojo/_base/lang",
5 "dojo/date",
6 "dojo/date/locale",
7 "dojo/date/stamp"
8 ], function(array, declare, lang, ddate, datelocale, datestamp){
9
10 // module:
11 // dojox/mobile/_DatePickerMixin
12
13 var slotMixin = {
14 format: function(/*Date*/d){
15 return datelocale.format(d, {datePattern:this.pattern, selector:"date",locale: this.picker.lang});
16 }
17 };
18
19 var yearSlotMixin = lang.mixin({
20 initLabels: function(){
21 this.labels = [];
22 if(this.labelFrom !== this.labelTo){
23 var d = new Date(this.labelFrom, 0, 1);
24 var i, idx;
25 for(i = this.labelFrom, idx = 0; i <= this.labelTo; i++, idx++){
26 d.setFullYear(i);
27 this.labels.push(this.format(d));
28 }
29 }
30 }
31 }, slotMixin);
32
33 var monthSlotMixin = lang.mixin({
34 initLabels: function(){
35 this.labels = [];
36 // On certain BlackBerry devices, we need to init to 16 not 1 to avoid some devices bugs (see #15677)
37 var d = new Date(2000, 0, 16);
38 for(var i = 0; i < 12; i++){
39 d.setMonth(i);
40 this.labels.push(this.format(d));
41 }
42 }
43 }, slotMixin);
44
45 var daySlotMixin = lang.mixin({
46 initLabels: function(){
47 this.labels = [];
48 var d = new Date(2000, 0, 1);
49 for(var i = 1; i <= 31; i++){
50 d.setDate(i);
51 this.labels.push(this.format(d));
52 }
53 }
54 }, slotMixin);
55
56 return declare("dojox.mobile._DatePickerMixin", null, {
57 // summary:
58 // A mixin for date picker widget.
59
60 // yearPattern: String
61 // A pattern to be used to format year.
62 yearPattern: "yyyy",
63
64 // monthPattern: String
65 // A pattern to be used to format month.
66 monthPattern: "MMM",
67
68 // dayPattern: String
69 // A pattern to be used to format day.
70 dayPattern: "d",
71
72 /*=====
73 // value: String
74 // A string representing the date value.
75 // The setter of this property first converts the value argument by calling
76 // the fromISOString method of the dojo/date/stamp module, then sets the
77 // values of the picker according to the resulting Date object.
78 // If the string cannot be parsed by fromISOString, the method does nothing.
79 // Example: set("value", "2012-1-20"); // January 20, 2012
80 // The getter returns the string formatted as described in the dojo/date/stamp
81 // module.
82 value: "",
83 =====*/
84
85 initSlots: function(){
86 // summary:
87 // Initializes the slots.
88 var c = this.slotClasses, p = this.slotProps;
89 c[0] = declare(c[0], yearSlotMixin);
90 c[1] = declare(c[1], monthSlotMixin);
91 c[2] = declare(c[2], daySlotMixin);
92 p[2].picker = p[1].picker = p[0].picker = this;
93 p[0].pattern = this.yearPattern;
94 p[1].pattern = this.monthPattern;
95 p[2].pattern = this.dayPattern;
96 this.reorderSlots();
97 },
98
99 reorderSlots: function(){
100 // summary:
101 // Reorders the slots.
102 if(this.slotOrder.length){ return; }
103 var a = datelocale._parseInfo({locale: this.lang}).bundle["dateFormat-short"].toLowerCase().split(/[^ymd]+/, 3);
104 this.slotOrder = array.map(a, function(pat){
105 return {y:0, m:1, d:2}[pat.charAt(0)];
106 });
107 },
108
109 reset: function(){
110 // summary:
111 // Goes to today.
112 var now = new Date();
113 var v = array.map(this.slots, function(w){ return w.format(now); });
114 this.set("colors", v);
115 this._disableEndDaysOfMonth();
116 if(this.value){
117 this.set("value", this.value);
118 this.value = null;
119 }else if(this.values){
120 this.set("values", this.values);
121 this.values = null;
122 }else{
123 this.set("values", v);
124 }
125 },
126
127 _onYearSet: function(){
128 // summary:
129 // An internal handler called when the year value is changed.
130 // tags:
131 // private
132 var slot = this.slots[0];
133 var newValue = slot.get("value");
134 if(!(slot._previousValue && newValue == slot._previousValue)){ // do nothing if the value is unchanged
135 this._disableEndDaysOfMonth();
136 slot._previousValue = newValue;
137 slot._set("value", newValue);
138 this.onYearSet();
139 }
140 },
141
142 onYearSet: function(){
143 // summary:
144 // A handler called when the year value is changed.
145 },
146
147 _onMonthSet: function(){
148 // summary:
149 // An internal handler called when the month value is changed.
150 // tags:
151 // private
152 var slot = this.slots[1];
153 var newValue = slot.get("value");
154 if(!(slot._previousValue && newValue == slot._previousValue)){ // do nothing if the value is unchanged
155 this._disableEndDaysOfMonth();
156 slot._previousValue = newValue;
157 slot._set("value", newValue); // notify watches
158 this.onMonthSet();
159 }
160 },
161
162 onMonthSet: function(){
163 // summary:
164 // A handler called when the month value is changed.
165 },
166
167 _onDaySet: function(){
168 // summary:
169 // An internal handler called when the day value is changed.
170 // tags:
171 // private
172 var slot = this.slots[2];
173 var newValue = slot.get("value");
174 if(!(slot._previousValue && newValue == slot._previousValue)){ // do nothing if the value is unchanged
175 if(!this._disableEndDaysOfMonth()){
176 // If _disableEndDaysOfMonth has changed the day value,
177 // skip notifications till next call of _onDaySet, to
178 // avoid the extra notification for the (invalid)
179 // intermediate value of the day.
180 slot._previousValue = newValue;
181 slot._set("value", newValue); // notify watches
182 this.onDaySet();
183 }
184 }
185 },
186
187 onDaySet: function(){
188 // summary:
189 // A handler called when the day value is changed.
190 },
191
192 _disableEndDaysOfMonth: function(){
193 // summary:
194 // Disables the end days of the month to match the specified
195 // number of days of the month. Returns true if the day value is changed.
196 // tags:
197 // private
198 var pat = this.slots[0].pattern + "/" + this.slots[1].pattern,
199 v = this.get("values"),
200 date = datelocale.parse(v[0] + "/" + v[1], {datePattern:pat, selector:"date",locale: this.lang}),
201 daysInMonth = ddate.getDaysInMonth(date);
202 var changedDay = false;
203 if(daysInMonth < v[2]){
204 // day value is invalid for this month, change it
205 changedDay = true;
206 this.slots[2]._spinToValue(daysInMonth, false/*applyValue*/);
207 }
208 this.disableValues(daysInMonth);
209 return changedDay;
210 },
211
212 _getDateAttr: function(){
213 // summary:
214 // Returns a Date object for the current values.
215 // tags:
216 // private
217 var v = this.get("values"), // [year, month, day]
218 s = this.slots,
219 pat = s[0].pattern + "/" + s[1].pattern + "/" + s[2].pattern;
220 return datelocale.parse(v[0] + "/" + v[1] + "/" + v[2], {datePattern:pat, selector:"date", locale: this.lang});
221 },
222
223 _setValuesAttr: function(/*Array*/values){
224 // summary:
225 // Sets the current date as an array of values.
226 // description:
227 // This method takes an array that consists of three values,
228 // year, month, and day. If the values are integer, they are
229 // formatted to locale-specific strings before setting them to
230 // the slots. Month starts from 1 to 12 (Ex. 1 - Jan, 2 - Feb, etc.)
231 // If the values are NOT integer, they are directly
232 // passed to the setter of the slots without formatting.
233 //
234 // example:
235 // | set("values", [2012, 1, 20]); // January 20, 2012
236 // tags:
237 // private
238 array.forEach(this.getSlots(), function(w, i){
239 var v = values[i];
240 if(typeof v == "number"){
241 var arr = [1970, 1, 1];
242 arr.splice(i, 1, v - 0);
243 v = w.format(new Date(arr[0], arr[1] - 1, arr[2]));
244 }
245 w.set("value", v);
246 });
247 },
248
249 _setValueAttr: function(/*String*/value){
250 // summary:
251 // Sets the current date as an String formatted according to a subset of the ISO-8601 standard.
252 // description:
253 // This method first converts the value argument by calling the fromISOString method of
254 // the dojo/date/stamp module, then sets the values of the picker according to the resulting
255 // Date object. If the string cannot be parsed by fromISOString, the method does nothing.
256 // value:
257 // A string formatted as described in the dojo/date/stamp module.
258 // example:
259 // | set("value", "2012-1-20"); // January 20, 2012
260 // tags:
261 // private
262 var date = datestamp.fromISOString(value);
263 this.set("values", array.map(this.slots, function(w){ return w.format(date); }));
264 },
265
266 _getValueAttr: function(){
267 // summary:
268 // Gets the current date as a String formatted according to a subset of the ISO-8601 standard.
269 // returns: String
270 // A string formatted as described in the dojo/date/stamp module.
271 // tags:
272 // private
273 return datestamp.toISOString(this.get("date"), { selector: "date" });
274 }
275 });
276 });