|
32 | 32 | dropdownSelector: null,
|
33 | 33 | minuteStep: 5,
|
34 | 34 | minView: 'minute',
|
| 35 | + modelType: 'Date', |
| 36 | + parseFormat: 'YYYY-MM-DDTHH:mm:ss.SSSZZ', |
35 | 37 | renderOn: null,
|
36 | 38 | startView: 'day'
|
37 | 39 | })
|
|
67 | 69 | 'dropdownSelector',
|
68 | 70 | 'minuteStep',
|
69 | 71 | 'minView',
|
| 72 | + 'modelType', |
| 73 | + 'parseFormat', |
70 | 74 | 'renderOn',
|
71 | 75 | 'startView'
|
72 | 76 | ];
|
|
111 | 115 | if (configuration.renderOn !== null && configuration.renderOn.length < 1) {
|
112 | 116 | throw ('renderOn must not be an empty string');
|
113 | 117 | }
|
| 118 | + if (configuration.modelType !== null && !angular.isString(configuration.modelType)) { |
| 119 | + throw ('modelType must be a string'); |
| 120 | + } |
| 121 | + if (configuration.modelType !== null && configuration.modelType.length < 1) { |
| 122 | + throw ('modelType must not be an empty string'); |
| 123 | + } |
| 124 | + if (configuration.modelType !== 'Date' && configuration.modelType !== 'moment' && configuration.modelType !== 'milliseconds') { |
| 125 | + // modelType contains string format, overriding parseFormat with modelType; |
| 126 | + configuration.parseFormat = configuration.modelType; |
| 127 | + } |
114 | 128 | if (configuration.dropdownSelector !== null && !angular.isString(configuration.dropdownSelector)) {
|
115 | 129 | throw ('dropdownSelector must be a string');
|
116 | 130 | }
|
|
187 | 201 | return moment.utc(unixDate).year(startYear).startOf('year');
|
188 | 202 | };
|
189 | 203 |
|
| 204 | + var formatValue = function formatValue(timeValue, formatString) { |
| 205 | + if (timeValue) { |
| 206 | + return getMoment(timeValue).format(formatString); |
| 207 | + } else { |
| 208 | + return ''; |
| 209 | + } |
| 210 | + }; |
| 211 | + |
| 212 | + /** |
| 213 | + * Converts a time value into a moment. |
| 214 | + * |
| 215 | + * This function is now necessary because moment logs a warning when parsing a string without a format. |
| 216 | + * @param modelValue |
| 217 | + * a time value in any of the supported formats (Date, moment, milliseconds, and string) |
| 218 | + * @returns {moment} |
| 219 | + * representing the specified time value. |
| 220 | + */ |
| 221 | + |
| 222 | + var getMoment = function getMoment(modelValue) { |
| 223 | + return moment(modelValue, angular.isString(modelValue) ? configuration.parseFormat : undefined); |
| 224 | + }; |
| 225 | + |
| 226 | + /** |
| 227 | + * Converts a time value to UCT/GMT time. |
| 228 | + * @param modelValue |
| 229 | + * a time value in any of the supported formats (Date, moment, milliseconds, and string) |
| 230 | + * @returns {number} |
| 231 | + * number of milliseconds since 1/1/1970 |
| 232 | + */ |
| 233 | + |
| 234 | + var getUTCTime = function getUTCTime(modelValue) { |
| 235 | + var tempDate = new Date(); |
| 236 | + if (modelValue) { |
| 237 | + var tempMoment = getMoment(modelValue); |
| 238 | + if (tempMoment.isValid()) { |
| 239 | + tempDate = tempMoment.toDate(); |
| 240 | + } else { |
| 241 | + throw 'Invalid date: ' + modelValue; |
| 242 | + } |
| 243 | + } |
| 244 | + return tempDate.getTime() - (tempDate.getTimezoneOffset() * 60000); |
| 245 | + }; |
| 246 | + |
190 | 247 | var dataFactory = {
|
191 | 248 | year: function year(unixDate) {
|
192 | 249 | var selectedDate = moment.utc(unixDate).startOf('year');
|
|
196 | 253 | var startDecade = (parseInt(selectedDate.year() / 10, 10) * 10);
|
197 | 254 | var startDate = moment.utc(startOfDecade(unixDate)).subtract(1, 'year').startOf('year');
|
198 | 255 |
|
199 |
| - var activeYear = ngModelController.$modelValue ? moment(ngModelController.$modelValue).year() : 0; |
| 256 | + var activeYear = formatValue(ngModelController.$modelValue, 'YYYY'); |
200 | 257 |
|
201 | 258 | var result = {
|
202 | 259 | 'currentView': 'year',
|
|
217 | 274 | 'display': yearMoment.format('YYYY'),
|
218 | 275 | 'past': yearMoment.year() < startDecade,
|
219 | 276 | 'future': yearMoment.year() > startDecade + 9,
|
220 |
| - 'active': yearMoment.year() === activeYear |
| 277 | + 'active': yearMoment.format('YYYY') === activeYear |
221 | 278 | };
|
222 | 279 |
|
223 | 280 | result.dates.push(new DateObject(dateValue));
|
|
230 | 287 |
|
231 | 288 | var startDate = moment.utc(unixDate).startOf('year');
|
232 | 289 | var previousViewDate = startOfDecade(unixDate);
|
233 |
| - var activeDate = ngModelController.$modelValue ? moment(ngModelController.$modelValue).format('YYYY-MMM') : 0; |
| 290 | + var activeDate = formatValue(ngModelController.$modelValue, 'YYYY-MMM'); |
234 | 291 |
|
235 | 292 | var result = {
|
236 | 293 | 'previousView': 'year',
|
|
268 | 325 |
|
269 | 326 | var startDate = moment.utc(startOfMonth).subtract(Math.abs(startOfMonth.weekday()), 'days');
|
270 | 327 |
|
271 |
| - var activeDate = ngModelController.$modelValue ? moment(ngModelController.$modelValue).format('YYYY-MMM-DD') : ''; |
| 328 | + var activeDate = formatValue(ngModelController.$modelValue, 'YYYY-MMM-DD'); |
272 | 329 |
|
273 | 330 | var result = {
|
274 | 331 | 'previousView': 'month',
|
|
312 | 369 | var selectedDate = moment.utc(unixDate).startOf('day');
|
313 | 370 | var previousViewDate = moment.utc(selectedDate).startOf('month');
|
314 | 371 |
|
315 |
| - var activeFormat = ngModelController.$modelValue ? moment(ngModelController.$modelValue).format('YYYY-MM-DD H') : ''; |
| 372 | + var activeFormat = formatValue(ngModelController.$modelValue, 'YYYY-MM-DD H'); |
316 | 373 |
|
317 | 374 | var result = {
|
318 | 375 | 'previousView': 'day',
|
|
344 | 401 | minute: function minute(unixDate) {
|
345 | 402 | var selectedDate = moment.utc(unixDate).startOf('hour');
|
346 | 403 | var previousViewDate = moment.utc(selectedDate).startOf('day');
|
347 |
| - var activeFormat = ngModelController.$modelValue ? moment(ngModelController.$modelValue).format('YYYY-MM-DD H:mm') : ''; |
| 404 | + var activeFormat = formatValue(ngModelController.$modelValue, 'YYYY-MM-DD H:mm'); |
348 | 405 |
|
349 | 406 | var result = {
|
350 | 407 | 'previousView': 'hour',
|
|
379 | 436 | var tempDate = new Date(unixDate);
|
380 | 437 | var newDate = new Date(tempDate.getUTCFullYear(), tempDate.getUTCMonth(), tempDate.getUTCDate(), tempDate.getUTCHours(), tempDate.getUTCMinutes(), tempDate.getUTCSeconds(), tempDate.getUTCMilliseconds());
|
381 | 438 |
|
| 439 | + switch (configuration.modelType) { |
| 440 | + case 'Date': |
| 441 | + // No additional work needed |
| 442 | + break; |
| 443 | + case 'moment': |
| 444 | + newDate = moment(newDate); |
| 445 | + break; |
| 446 | + case 'milliseconds': |
| 447 | + newDate = unixDate; |
| 448 | + break; |
| 449 | + default: // It is assumed that the modelType is a formatting string. |
| 450 | + newDate = moment(newDate).format(configuration.modelType); |
| 451 | + } |
| 452 | + |
382 | 453 | var oldDate = ngModelController.$modelValue;
|
383 | 454 | ngModelController.$setViewValue(newDate);
|
384 | 455 |
|
|
392 | 463 | }
|
393 | 464 | };
|
394 | 465 |
|
395 |
| - var getUTCTime = function getUTCTime(modelValue) { |
396 |
| - var tempDate = (modelValue ? moment(modelValue).toDate() : new Date()); |
397 |
| - return tempDate.getTime() - (tempDate.getTimezoneOffset() * 60000); |
398 |
| - }; |
399 |
| - |
400 | 466 | scope.changeView = function changeView(viewName, dateObject, event) {
|
401 | 467 | if (event) {
|
402 | 468 | event.stopPropagation();
|
|
0 commit comments