[483] | 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"}); |
---|
| 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[0].pattern = this.yearPattern; |
---|
| 93 | p[1].pattern = this.monthPattern; |
---|
| 94 | p[2].pattern = this.dayPattern; |
---|
| 95 | this.reorderSlots(); |
---|
| 96 | }, |
---|
| 97 | |
---|
| 98 | reorderSlots: function(){ |
---|
| 99 | // summary: |
---|
| 100 | // Reorders the slots. |
---|
| 101 | if(this.slotOrder.length){ return; } |
---|
| 102 | var a = datelocale._parseInfo().bundle["dateFormat-short"].toLowerCase().split(/[^ymd]+/, 3); |
---|
| 103 | this.slotOrder = array.map(a, function(pat){ |
---|
| 104 | return {y:0, m:1, d:2}[pat.charAt(0)]; |
---|
| 105 | }); |
---|
| 106 | }, |
---|
| 107 | |
---|
| 108 | reset: function(){ |
---|
| 109 | // summary: |
---|
| 110 | // Goes to today. |
---|
| 111 | var now = new Date(); |
---|
| 112 | var v = array.map(this.slots, function(w){ return w.format(now); }); |
---|
| 113 | this.set("colors", v); |
---|
| 114 | this._disableEndDaysOfMonth(); |
---|
| 115 | if(this.value){ |
---|
| 116 | this.set("value", this.value); |
---|
| 117 | this.value = null; |
---|
| 118 | }else if(this.values){ |
---|
| 119 | this.set("values", this.values); |
---|
| 120 | this.values = null; |
---|
| 121 | }else{ |
---|
| 122 | this.set("values", v); |
---|
| 123 | } |
---|
| 124 | }, |
---|
| 125 | |
---|
| 126 | _onYearSet: function(){ |
---|
| 127 | // summary: |
---|
| 128 | // An internal handler called when the year value is changed. |
---|
| 129 | // tags: |
---|
| 130 | // private |
---|
| 131 | var slot = this.slots[0]; |
---|
| 132 | var newValue = slot.get("value"); |
---|
| 133 | if(!(slot._previousValue && newValue == slot._previousValue)){ // do nothing if the value is unchanged |
---|
| 134 | this._disableEndDaysOfMonth(); |
---|
| 135 | slot._previousValue = newValue; |
---|
| 136 | slot._set("value", newValue); |
---|
| 137 | this.onYearSet(); |
---|
| 138 | } |
---|
| 139 | }, |
---|
| 140 | |
---|
| 141 | onYearSet: function(){ |
---|
| 142 | // summary: |
---|
| 143 | // A handler called when the year value is changed. |
---|
| 144 | }, |
---|
| 145 | |
---|
| 146 | _onMonthSet: function(){ |
---|
| 147 | // summary: |
---|
| 148 | // An internal handler called when the month value is changed. |
---|
| 149 | // tags: |
---|
| 150 | // private |
---|
| 151 | var slot = this.slots[1]; |
---|
| 152 | var newValue = slot.get("value"); |
---|
| 153 | if(!(slot._previousValue && newValue == slot._previousValue)){ // do nothing if the value is unchanged |
---|
| 154 | this._disableEndDaysOfMonth(); |
---|
| 155 | slot._previousValue = newValue; |
---|
| 156 | slot._set("value", newValue); // notify watches |
---|
| 157 | this.onMonthSet(); |
---|
| 158 | } |
---|
| 159 | }, |
---|
| 160 | |
---|
| 161 | onMonthSet: function(){ |
---|
| 162 | // summary: |
---|
| 163 | // A handler called when the month value is changed. |
---|
| 164 | }, |
---|
| 165 | |
---|
| 166 | _onDaySet: function(){ |
---|
| 167 | // summary: |
---|
| 168 | // An internal handler called when the day value is changed. |
---|
| 169 | // tags: |
---|
| 170 | // private |
---|
| 171 | var slot = this.slots[2]; |
---|
| 172 | var newValue = slot.get("value"); |
---|
| 173 | if(!(slot._previousValue && newValue == slot._previousValue)){ // do nothing if the value is unchanged |
---|
| 174 | if(!this._disableEndDaysOfMonth()){ |
---|
| 175 | // If _disableEndDaysOfMonth has changed the day value, |
---|
| 176 | // skip notifications till next call of _onDaySet, to |
---|
| 177 | // avoid the extra notification for the (invalid) |
---|
| 178 | // intermediate value of the day. |
---|
| 179 | slot._previousValue = newValue; |
---|
| 180 | slot._set("value", newValue); // notify watches |
---|
| 181 | this.onDaySet(); |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | }, |
---|
| 185 | |
---|
| 186 | onDaySet: function(){ |
---|
| 187 | // summary: |
---|
| 188 | // A handler called when the day value is changed. |
---|
| 189 | }, |
---|
| 190 | |
---|
| 191 | _disableEndDaysOfMonth: function(){ |
---|
| 192 | // summary: |
---|
| 193 | // Disables the end days of the month to match the specified |
---|
| 194 | // number of days of the month. Returns true if the day value is changed. |
---|
| 195 | // tags: |
---|
| 196 | // private |
---|
| 197 | var pat = this.slots[0].pattern + "/" + this.slots[1].pattern, |
---|
| 198 | v = this.get("values"), |
---|
| 199 | date = datelocale.parse(v[0] + "/" + v[1], {datePattern:pat, selector:"date"}), |
---|
| 200 | daysInMonth = ddate.getDaysInMonth(date); |
---|
| 201 | var changedDay = false; |
---|
| 202 | if(daysInMonth < v[2]){ |
---|
| 203 | // day value is invalid for this month, change it |
---|
| 204 | changedDay = true; |
---|
| 205 | this.slots[2]._spinToValue(daysInMonth, false/*applyValue*/); |
---|
| 206 | } |
---|
| 207 | this.disableValues(daysInMonth); |
---|
| 208 | return changedDay; |
---|
| 209 | }, |
---|
| 210 | |
---|
| 211 | _getDateAttr: function(){ |
---|
| 212 | // summary: |
---|
| 213 | // Returns a Date object for the current values. |
---|
| 214 | // tags: |
---|
| 215 | // private |
---|
| 216 | var v = this.get("values"), // [year, month, day] |
---|
| 217 | s = this.slots, |
---|
| 218 | pat = s[0].pattern + "/" + s[1].pattern + "/" + s[2].pattern; |
---|
| 219 | return datelocale.parse(v[0] + "/" + v[1] + "/" + v[2], {datePattern:pat, selector:"date"}); |
---|
| 220 | }, |
---|
| 221 | |
---|
| 222 | _setValuesAttr: function(/*Array*/values){ |
---|
| 223 | // summary: |
---|
| 224 | // Sets the current date as an array of values. |
---|
| 225 | // description: |
---|
| 226 | // This method takes an array that consists of three values, |
---|
| 227 | // year, month, and day. If the values are integer, they are |
---|
| 228 | // formatted to locale-specific strings before setting them to |
---|
| 229 | // the slots. Month starts from 1 to 12 (Ex. 1 - Jan, 2 - Feb, etc.) |
---|
| 230 | // If the values are NOT integer, they are directly |
---|
| 231 | // passed to the setter of the slots without formatting. |
---|
| 232 | // |
---|
| 233 | // example: |
---|
| 234 | // | set("values", [2012, 1, 20]); // January 20, 2012 |
---|
| 235 | // tags: |
---|
| 236 | // private |
---|
| 237 | array.forEach(this.getSlots(), function(w, i){ |
---|
| 238 | var v = values[i]; |
---|
| 239 | if(typeof v == "number"){ |
---|
| 240 | var arr = [1970, 1, 1]; |
---|
| 241 | arr.splice(i, 1, v - 0); |
---|
| 242 | v = w.format(new Date(arr[0], arr[1] - 1, arr[2])); |
---|
| 243 | } |
---|
| 244 | w.set("value", v); |
---|
| 245 | }); |
---|
| 246 | }, |
---|
| 247 | |
---|
| 248 | _setValueAttr: function(/*String*/value){ |
---|
| 249 | // summary: |
---|
| 250 | // Sets the current date as an String formatted according to a subset of the ISO-8601 standard. |
---|
| 251 | // description: |
---|
| 252 | // This method first converts the value argument by calling the fromISOString method of |
---|
| 253 | // the dojo/date/stamp module, then sets the values of the picker according to the resulting |
---|
| 254 | // Date object. If the string cannot be parsed by fromISOString, the method does nothing. |
---|
| 255 | // value: |
---|
| 256 | // A string formatted as described in the dojo/date/stamp module. |
---|
| 257 | // example: |
---|
| 258 | // | set("value", "2012-1-20"); // January 20, 2012 |
---|
| 259 | // tags: |
---|
| 260 | // private |
---|
| 261 | var date = datestamp.fromISOString(value); |
---|
| 262 | this.set("values", array.map(this.slots, function(w){ return w.format(date); })); |
---|
| 263 | }, |
---|
| 264 | |
---|
| 265 | _getValueAttr: function(){ |
---|
| 266 | // summary: |
---|
| 267 | // Gets the current date as a String formatted according to a subset of the ISO-8601 standard. |
---|
| 268 | // returns: String |
---|
| 269 | // A string formatted as described in the dojo/date/stamp module. |
---|
| 270 | // tags: |
---|
| 271 | // private |
---|
| 272 | return datestamp.toISOString(this.get("date"), { selector: "date" }); |
---|
| 273 | } |
---|
| 274 | }); |
---|
| 275 | }); |
---|