123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. import { Component, ElementRef, EventEmitter, HostListener, Input, Optional, Output, Renderer, ViewEncapsulation } from '@angular/core';
  2. import { NG_VALUE_ACCESSOR } from '@angular/forms';
  3. import { Config } from '../../config/config';
  4. import { PickerController } from '../picker/picker-controller';
  5. import { Form } from '../../util/form';
  6. import { BaseInput } from '../../util/base-input';
  7. import { Item } from '../item/item';
  8. import { clamp, isArray, isBlank, isObject, isPresent, isString } from '../../util/util';
  9. import { compareDates, convertDataToISO, convertFormatToKey, dateDataSortValue, dateSortValue, dateValueRange, daysInMonth, getValueFromFormat, parseDate, parseTemplate, renderDateTime, renderTextFormat, updateDate, } from '../../util/datetime-util';
  10. /**
  11. * @name DateTime
  12. * @description
  13. * The DateTime component is used to present an interface which makes it easy for
  14. * users to select dates and times. Tapping on `<ion-datetime>` will display a picker
  15. * interface that slides up from the bottom of the page. The picker then displays
  16. * scrollable columns that can be used to individually select years, months, days,
  17. * hours and minute values. The DateTime component is similar to the native
  18. * `<input type="datetime-local">` element, however, Ionic's DateTime component makes
  19. * it easy to display the date and time in a preferred format, and manage the datetime
  20. * values.
  21. *
  22. * ```html
  23. * <ion-item>
  24. * <ion-label>Date</ion-label>
  25. * <ion-datetime displayFormat="MM/DD/YYYY" [(ngModel)]="myDate"></ion-datetime>
  26. * </ion-item>
  27. * ```
  28. *
  29. *
  30. * ## Display and Picker Formats
  31. *
  32. * The DateTime component displays the values in two places: in the `<ion-datetime>`
  33. * component, and in the interface that is presented from the bottom of the screen.
  34. * The following chart lists all of the formats that can be used.
  35. *
  36. * | Format | Description | Example |
  37. * |---------|--------------------------------|-------------------------|
  38. * | `YYYY` | Year, 4 digits | `2018` |
  39. * | `YY` | Year, 2 digits | `18` |
  40. * | `M` | Month | `1` ... `12` |
  41. * | `MM` | Month, leading zero | `01` ... `12` |
  42. * | `MMM` | Month, short name | `Jan` |
  43. * | `MMMM` | Month, full name | `January` |
  44. * | `D` | Day | `1` ... `31` |
  45. * | `DD` | Day, leading zero | `01` ... `31` |
  46. * | `DDD` | Day, short name | `Fri` |
  47. * | `DDDD` | Day, full name | `Friday` |
  48. * | `H` | Hour, 24-hour | `0` ... `23` |
  49. * | `HH` | Hour, 24-hour, leading zero | `00` ... `23` |
  50. * | `h` | Hour, 12-hour | `1` ... `12` |
  51. * | `hh` | Hour, 12-hour, leading zero | `01` ... `12` |
  52. * | `a` | 12-hour time period, lowercase | `am` `pm` |
  53. * | `A` | 12-hour time period, uppercase | `AM` `PM` |
  54. * | `m` | Minute | `1` ... `59` |
  55. * | `mm` | Minute, leading zero | `01` ... `59` |
  56. * | `s` | Second | `1` ... `59` |
  57. * | `ss` | Second, leading zero | `01` ... `59` |
  58. * | `Z` | UTC Timezone Offset | `Z or +HH:mm or -HH:mm` |
  59. *
  60. * **Important**: See the [Month Names and Day of the Week Names](#month-names-and-day-of-the-week-names)
  61. * section below on how to use different names for the month and day.
  62. *
  63. * ### Display Format
  64. *
  65. * The `displayFormat` input property specifies how a datetime's value should be
  66. * printed, as formatted text, within the `ion-datetime` component.
  67. *
  68. * In the following example, the display in the `<ion-datetime>` will use the
  69. * month's short name, the numerical day with a leading zero, a comma and the
  70. * four-digit year. In addition to the date, it will display the time with the hours
  71. * in the 24-hour format and the minutes. Any character can be used as a separator.
  72. * An example display using this format is: `Jun 17, 2005 11:06`.
  73. *
  74. * ```html
  75. * <ion-item>
  76. * <ion-label>Date</ion-label>
  77. * <ion-datetime displayFormat="MMM DD, YYYY HH:mm" [(ngModel)]="myDate"></ion-datetime>
  78. * </ion-item>
  79. * ```
  80. *
  81. * ### Picker Format
  82. *
  83. * The `pickerFormat` input property determines which columns should be shown in the
  84. * interface, the order of the columns, and which format to use within each column.
  85. * If the `pickerFormat` input is not provided then it will default to the `displayFormat`.
  86. *
  87. * In the following example, the display in the `<ion-datetime>` will use the
  88. * `MM/YYYY` format, such as `06/2020`. However, the picker interface
  89. * will display two columns with the month's long name, and the four-digit year.
  90. *
  91. * ```html
  92. * <ion-item>
  93. * <ion-label>Date</ion-label>
  94. * <ion-datetime displayFormat="MM/YYYY" pickerFormat="MMMM YYYY" [(ngModel)]="myDate"></ion-datetime>
  95. * </ion-item>
  96. * ```
  97. *
  98. * ### Datetime Data
  99. *
  100. * Historically, handling datetime values within JavaScript, or even within HTML
  101. * inputs, has always been a challenge. Specifically, JavaScript's `Date` object is
  102. * notoriously difficult to correctly parse apart datetime strings or to format
  103. * datetime values. Even worse is how different browsers and JavaScript versions
  104. * parse various datetime strings differently, especially per locale.
  105. *
  106. * But no worries, all is not lost! Ionic's datetime input has been designed so
  107. * developers can avoid the common pitfalls, allowing developers to easily format
  108. * datetime values within the input, and give the user a simple datetime picker for a
  109. * great user experience.
  110. *
  111. * ##### ISO 8601 Datetime Format: YYYY-MM-DDTHH:mmZ
  112. *
  113. * Ionic uses the [ISO 8601 datetime format](https://www.w3.org/TR/NOTE-datetime)
  114. * for its value. The value is simply a string, rather than using JavaScript's `Date`
  115. * object. Additionally, when using the ISO datetime format, it makes it easier
  116. * to serialize and pass within JSON objects, and sending databases a standardized
  117. * format which it can be easily parsed if need be.
  118. *
  119. * To create an ISO datetime string for the current date and time, e.g. use `const currentDate = (new Date()).toISOString();`.
  120. *
  121. * An ISO format can be used as a simple year, or just the hour and minute, or get more
  122. * detailed down to the millisecond and timezone. Any of the ISO formats below can be used,
  123. * and after a user selects a new value, Ionic will continue to use the same ISO format
  124. * which datetime value was originally given as.
  125. *
  126. * | Description | Format | Datetime Value Example |
  127. * |----------------------|------------------------|------------------------------|
  128. * | Year | YYYY | 1994 |
  129. * | Year and Month | YYYY-MM | 1994-12 |
  130. * | Complete Date | YYYY-MM-DD | 1994-12-15 |
  131. * | Date and Time | YYYY-MM-DDTHH:mm | 1994-12-15T13:47 |
  132. * | UTC Timezone | YYYY-MM-DDTHH:mm:ssTZD | 1994-12-15T13:47:20.789Z |
  133. * | Timezone Offset | YYYY-MM-DDTHH:mm:ssTZD | 1994-12-15T13:47:20.789+5:00 |
  134. * | Hour and Minute | HH:mm | 13:47 |
  135. * | Hour, Minute, Second | HH:mm:ss | 13:47:20 |
  136. *
  137. * Note that the year is always four-digits, milliseconds (if it's added) is always
  138. * three-digits, and all others are always two-digits. So the number representing
  139. * January always has a leading zero, such as `01`. Additionally, the hour is always
  140. * in the 24-hour format, so `00` is `12am` on a 12-hour clock, `13` means `1pm`,
  141. * and `23` means `11pm`.
  142. *
  143. * It's also important to note that neither the `displayFormat` or `pickerFormat` can
  144. * set the datetime value's output, which is the value that is set by the component's
  145. * `ngModel`. The format's are merely for displaying the value as text and the picker's
  146. * interface, but the datetime's value is always persisted as a valid ISO 8601 datetime
  147. * string.
  148. *
  149. *
  150. * ## Min and Max Datetimes
  151. *
  152. * Dates are infinite in either direction, so for a user's selection there should be at
  153. * least some form of restricting the dates that can be selected. By default, the maximum
  154. * date is to the end of the current year, and the minimum date is from the beginning
  155. * of the year that was 100 years ago.
  156. *
  157. * To customize the minimum and maximum datetime values, the `min` and `max` component
  158. * inputs can be provided which may make more sense for the app's use-case, rather
  159. * than the default of the last 100 years. Following the same IS0 8601 format listed
  160. * in the table above, each component can restrict which dates can be selected by the
  161. * user. Below is an example of restricting the date selection between the beginning
  162. * of 2016, and October 31st of 2020:
  163. *
  164. * ```html
  165. * <ion-item>
  166. * <ion-label>Date</ion-label>
  167. * <ion-datetime displayFormat="MMMM YYYY" min="2016" max="2020-10-31" [(ngModel)]="myDate">
  168. * </ion-datetime>
  169. * </ion-item>
  170. * ```
  171. *
  172. *
  173. * ## Month Names and Day of the Week Names
  174. *
  175. * At this time, there is no one-size-fits-all standard to automatically choose the correct
  176. * language/spelling for a month name, or day of the week name, depending on the language
  177. * or locale. Good news is that there is an
  178. * [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat)
  179. * standard which *most* browsers have adopted. However, at this time the standard has not
  180. * been fully implemented by all popular browsers so Ionic is unavailable to take advantage
  181. * of it *yet*. Additionally, Angular also provides an internationalization service, but it
  182. * is still under heavy development so Ionic does not depend on it at this time.
  183. *
  184. * All things considered, the by far easiest solution is to just provide an array of names
  185. * if the app needs to use names other than the default English version of month and day
  186. * names. The month names and day names can be either configured at the app level, or
  187. * individual `ion-datetime` level.
  188. *
  189. * ### App Config Level
  190. *
  191. * ```ts
  192. * //app.module.ts
  193. * @NgModule({
  194. * ...,
  195. * imports: [
  196. * IonicModule.forRoot(MyApp, {
  197. * monthNames: ['janeiro', 'fevereiro', 'mar\u00e7o', ... ],
  198. * monthShortNames: ['jan', 'fev', 'mar', ... ],
  199. * dayNames: ['domingo', 'segunda-feira', 'ter\u00e7a-feira', ... ],
  200. * dayShortNames: ['dom', 'seg', 'ter', ... ],
  201. * })
  202. * ],
  203. * ...
  204. * })
  205. * ```
  206. *
  207. * ### Component Input Level
  208. *
  209. * ```html
  210. * <ion-item>
  211. * <ion-label>Período</ion-label>
  212. * <ion-datetime displayFormat="DDDD MMM D, YYYY" [(ngModel)]="myDate"
  213. * monthNames="janeiro, fevereiro, mar\u00e7o, ..."
  214. * monthShortNames="jan, fev, mar, ..."
  215. * dayNames="domingo, segunda-feira, ter\u00e7a-feira, ..."
  216. * dayShortNames="dom, seg, ter, ..."></ion-datetime>
  217. * </ion-item>
  218. * ```
  219. *
  220. *
  221. * ### Advanced Datetime Validation and Manipulation
  222. *
  223. * The datetime picker provides the simplicity of selecting an exact format, and persists
  224. * the datetime values as a string using the standardized
  225. * [ISO 8601 datetime format](https://www.w3.org/TR/NOTE-datetime).
  226. * However, it's important to note that `ion-datetime` does not attempt to solve all
  227. * situtations when validating and manipulating datetime values. If datetime values need
  228. * to be parsed from a certain format, or manipulated (such as adding 5 days to a date,
  229. * subtracting 30 minutes, etc.), or even formatting data to a specific locale, then we highly
  230. * recommend using [moment.js](http://momentjs.com/) to "Parse, validate, manipulate, and
  231. * display dates in JavaScript". [Moment.js](http://momentjs.com/) has quickly become
  232. * our goto standard when dealing with datetimes within JavaScript, but Ionic does not
  233. * prepackage this dependency since most apps will not require it, and its locale
  234. * configuration should be decided by the end-developer.
  235. *
  236. *
  237. * @usage
  238. * ```html
  239. * <ion-item>
  240. * <ion-label>Date</ion-label>
  241. * <ion-datetime displayFormat="MM/DD/YYYY" [(ngModel)]="myDate">
  242. * </ion-datetime>
  243. * </ion-item>
  244. * ```
  245. *
  246. *
  247. * @demo /docs/demos/src/datetime/
  248. */
  249. export class DateTime extends BaseInput {
  250. constructor(form, config, elementRef, renderer, item, _pickerCtrl) {
  251. super(config, elementRef, renderer, 'datetime', {}, form, item, null);
  252. this._pickerCtrl = _pickerCtrl;
  253. this._text = '';
  254. this._locale = {};
  255. /**
  256. * @input {string} The text to display on the picker's cancel button. Default: `Cancel`.
  257. */
  258. this.cancelText = 'Cancel';
  259. /**
  260. * @input {string} The text to display on the picker's "Done" button. Default: `Done`.
  261. */
  262. this.doneText = 'Done';
  263. /**
  264. * @input {any} Any additional options that the picker interface can accept.
  265. * See the [Picker API docs](../../picker/Picker) for the picker options.
  266. */
  267. this.pickerOptions = {};
  268. /**
  269. * @input {string} The text to display when there's no date selected yet.
  270. * Using lowercase to match the input attribute
  271. */
  272. this.placeholder = '';
  273. /**
  274. * @output {any} Emitted when the datetime selection was cancelled.
  275. */
  276. this.ionCancel = new EventEmitter();
  277. }
  278. /**
  279. * @hidden
  280. */
  281. ngAfterContentInit() {
  282. // first see if locale names were provided in the inputs
  283. // then check to see if they're in the config
  284. // if neither were provided then it will use default English names
  285. ['monthNames', 'monthShortNames', 'dayNames', 'dayShortNames'].forEach(type => {
  286. this._locale[type] = convertToArrayOfStrings(isPresent(this[type]) ? this[type] : this._config.get(type), type);
  287. });
  288. this._initialize();
  289. }
  290. /**
  291. * @hidden
  292. */
  293. _inputNormalize(val) {
  294. updateDate(this._value, val);
  295. return this._value;
  296. }
  297. /**
  298. * @hidden
  299. */
  300. _inputUpdated() {
  301. super._inputUpdated();
  302. this.updateText();
  303. }
  304. /**
  305. * @hidden
  306. */
  307. _inputShouldChange() {
  308. return true;
  309. }
  310. /**
  311. * TODO: REMOVE THIS
  312. * @hidden
  313. */
  314. _inputChangeEvent() {
  315. return this.value;
  316. }
  317. /**
  318. * @hidden
  319. */
  320. _inputNgModelEvent() {
  321. return convertDataToISO(this.value);
  322. }
  323. _click(ev) {
  324. ev.preventDefault();
  325. ev.stopPropagation();
  326. this.open();
  327. }
  328. _keyup() {
  329. this.open();
  330. }
  331. /**
  332. * @hidden
  333. */
  334. open() {
  335. if (this.isFocus() || this._disabled) {
  336. return;
  337. }
  338. (void 0) /* console.debug */;
  339. // the user may have assigned some options specifically for the picker
  340. const pickerOptions = Object.assign({}, this.pickerOptions);
  341. // Add a cancel and done button by default to the picker
  342. const defaultButtons = [{
  343. text: this.cancelText,
  344. role: 'cancel',
  345. handler: () => this.ionCancel.emit(this)
  346. }, {
  347. text: this.doneText,
  348. handler: (data) => this.value = data,
  349. }];
  350. pickerOptions.buttons = (pickerOptions.buttons || []).concat(defaultButtons);
  351. // Configure picker under the hood
  352. const picker = this._picker = this._pickerCtrl.create(pickerOptions);
  353. picker.ionChange.subscribe(() => {
  354. this.validate();
  355. picker.refresh();
  356. });
  357. // Update picker status before presenting
  358. this.generate();
  359. this.validate();
  360. // Present picker
  361. this._fireFocus();
  362. picker.present(pickerOptions);
  363. picker.onDidDismiss(() => {
  364. this._fireBlur();
  365. });
  366. }
  367. /**
  368. * @hidden
  369. */
  370. generate() {
  371. const picker = this._picker;
  372. // if a picker format wasn't provided, then fallback
  373. // to use the display format
  374. let template = this.pickerFormat || this.displayFormat || DEFAULT_FORMAT;
  375. if (isPresent(template)) {
  376. // make sure we've got up to date sizing information
  377. this.calcMinMax();
  378. // does not support selecting by day name
  379. // automaticallly remove any day name formats
  380. template = template.replace('DDDD', '{~}').replace('DDD', '{~}');
  381. if (template.indexOf('D') === -1) {
  382. // there is not a day in the template
  383. // replace the day name with a numeric one if it exists
  384. template = template.replace('{~}', 'D');
  385. }
  386. // make sure no day name replacer is left in the string
  387. template = template.replace(/{~}/g, '');
  388. // parse apart the given template into an array of "formats"
  389. parseTemplate(template).forEach(format => {
  390. // loop through each format in the template
  391. // create a new picker column to build up with data
  392. let key = convertFormatToKey(format);
  393. let values;
  394. // first see if they have exact values to use for this input
  395. if (isPresent(this[key + 'Values'])) {
  396. // user provide exact values for this date part
  397. values = convertToArrayOfNumbers(this[key + 'Values'], key);
  398. }
  399. else {
  400. // use the default date part values
  401. values = dateValueRange(format, this._min, this._max);
  402. }
  403. const column = {
  404. name: key,
  405. selectedIndex: 0,
  406. options: values.map(val => {
  407. return {
  408. value: val,
  409. text: renderTextFormat(format, val, null, this._locale),
  410. };
  411. })
  412. };
  413. // cool, we've loaded up the columns with options
  414. // preselect the option for this column
  415. const optValue = getValueFromFormat(this.getValueOrDefault(), format);
  416. const selectedIndex = column.options.findIndex(opt => opt.value === optValue);
  417. if (selectedIndex >= 0) {
  418. // set the select index for this column's options
  419. column.selectedIndex = selectedIndex;
  420. }
  421. // add our newly created column to the picker
  422. picker.addColumn(column);
  423. });
  424. // Normalize min/max
  425. const min = this._min;
  426. const max = this._max;
  427. const columns = this._picker.getColumns();
  428. ['month', 'day', 'hour', 'minute']
  429. .filter(name => !columns.find(column => column.name === name))
  430. .forEach(name => {
  431. min[name] = 0;
  432. max[name] = 0;
  433. });
  434. this.divyColumns();
  435. }
  436. }
  437. /**
  438. * @hidden
  439. */
  440. validateColumn(name, index, min, max, lowerBounds, upperBounds) {
  441. (void 0) /* assert */;
  442. (void 0) /* assert */;
  443. const column = this._picker.getColumn(name);
  444. if (!column) {
  445. return 0;
  446. }
  447. const lb = lowerBounds.slice();
  448. const ub = upperBounds.slice();
  449. const options = column.options;
  450. let indexMin = options.length - 1;
  451. let indexMax = 0;
  452. for (var i = 0; i < options.length; i++) {
  453. var opt = options[i];
  454. var value = opt.value;
  455. lb[index] = opt.value;
  456. ub[index] = opt.value;
  457. var disabled = opt.disabled = (value < lowerBounds[index] ||
  458. value > upperBounds[index] ||
  459. dateSortValue(ub[0], ub[1], ub[2], ub[3], ub[4]) < min ||
  460. dateSortValue(lb[0], lb[1], lb[2], lb[3], lb[4]) > max);
  461. if (!disabled) {
  462. indexMin = Math.min(indexMin, i);
  463. indexMax = Math.max(indexMax, i);
  464. }
  465. }
  466. let selectedIndex = column.selectedIndex = clamp(indexMin, column.selectedIndex, indexMax);
  467. opt = column.options[selectedIndex];
  468. if (opt) {
  469. return opt.value;
  470. }
  471. return 0;
  472. }
  473. /**
  474. * @private
  475. */
  476. validate() {
  477. const today = new Date();
  478. const minCompareVal = dateDataSortValue(this._min);
  479. const maxCompareVal = dateDataSortValue(this._max);
  480. const yearCol = this._picker.getColumn('year');
  481. (void 0) /* assert */;
  482. let selectedYear = today.getFullYear();
  483. if (yearCol) {
  484. // default to the first value if the current year doesn't exist in the options
  485. if (!yearCol.options.find(col => col.value === today.getFullYear())) {
  486. selectedYear = yearCol.options[0].value;
  487. }
  488. var yearOpt = yearCol.options[yearCol.selectedIndex];
  489. if (yearOpt) {
  490. // they have a selected year value
  491. selectedYear = yearOpt.value;
  492. }
  493. }
  494. const selectedMonth = this.validateColumn('month', 1, minCompareVal, maxCompareVal, [selectedYear, 0, 0, 0, 0], [selectedYear, 12, 31, 23, 59]);
  495. const numDaysInMonth = daysInMonth(selectedMonth, selectedYear);
  496. const selectedDay = this.validateColumn('day', 2, minCompareVal, maxCompareVal, [selectedYear, selectedMonth, 0, 0, 0], [selectedYear, selectedMonth, numDaysInMonth, 23, 59]);
  497. const selectedHour = this.validateColumn('hour', 3, minCompareVal, maxCompareVal, [selectedYear, selectedMonth, selectedDay, 0, 0], [selectedYear, selectedMonth, selectedDay, 23, 59]);
  498. this.validateColumn('minute', 4, minCompareVal, maxCompareVal, [selectedYear, selectedMonth, selectedDay, selectedHour, 0], [selectedYear, selectedMonth, selectedDay, selectedHour, 59]);
  499. }
  500. /**
  501. * @hidden
  502. */
  503. divyColumns() {
  504. const pickerColumns = this._picker.getColumns();
  505. let columnsWidth = [];
  506. let col;
  507. let width;
  508. for (var i = 0; i < pickerColumns.length; i++) {
  509. col = pickerColumns[i];
  510. columnsWidth.push(0);
  511. for (var j = 0; j < col.options.length; j++) {
  512. width = col.options[j].text.length;
  513. if (width > columnsWidth[i]) {
  514. columnsWidth[i] = width;
  515. }
  516. }
  517. }
  518. if (columnsWidth.length === 2) {
  519. width = Math.max(columnsWidth[0], columnsWidth[1]);
  520. pickerColumns[0].align = 'right';
  521. pickerColumns[1].align = 'left';
  522. pickerColumns[0].optionsWidth = pickerColumns[1].optionsWidth = `${width * 17}px`;
  523. }
  524. else if (columnsWidth.length === 3) {
  525. width = Math.max(columnsWidth[0], columnsWidth[2]);
  526. pickerColumns[0].align = 'right';
  527. pickerColumns[1].columnWidth = `${columnsWidth[1] * 17}px`;
  528. pickerColumns[0].optionsWidth = pickerColumns[2].optionsWidth = `${width * 17}px`;
  529. pickerColumns[2].align = 'left';
  530. }
  531. }
  532. /**
  533. * @hidden
  534. */
  535. updateText() {
  536. // create the text of the formatted data
  537. const template = this.displayFormat || this.pickerFormat || DEFAULT_FORMAT;
  538. this._text = renderDateTime(template, this.getValue(), this._locale);
  539. }
  540. /**
  541. * @hidden
  542. */
  543. getValue() {
  544. return this._value;
  545. }
  546. /**
  547. * @hidden
  548. */
  549. getValueOrDefault() {
  550. if (this.hasValue()) {
  551. return this._value;
  552. }
  553. const initialDateString = this.getDefaultValueDateString();
  554. const _default = {};
  555. updateDate(_default, initialDateString);
  556. return _default;
  557. }
  558. /**
  559. * Get the default value as a date string
  560. * @hidden
  561. */
  562. getDefaultValueDateString() {
  563. if (this.initialValue) {
  564. return this.initialValue;
  565. }
  566. const nowString = (new Date).toISOString();
  567. if (this.max) {
  568. const now = parseDate(nowString);
  569. const max = parseDate(this.max);
  570. let v;
  571. for (let i in max) {
  572. v = max[i];
  573. if (v === null) {
  574. max[i] = now[i];
  575. }
  576. }
  577. const diff = compareDates(now, max);
  578. // If max is before current time, return max
  579. if (diff > 0) {
  580. return this.max;
  581. }
  582. }
  583. return nowString;
  584. }
  585. /**
  586. * @hidden
  587. */
  588. hasValue() {
  589. const val = this._value;
  590. return isPresent(val)
  591. && isObject(val)
  592. && Object.keys(val).length > 0;
  593. }
  594. /**
  595. * @hidden
  596. */
  597. calcMinMax(now) {
  598. const todaysYear = (now || new Date()).getFullYear();
  599. if (isPresent(this.yearValues)) {
  600. var years = convertToArrayOfNumbers(this.yearValues, 'year');
  601. if (isBlank(this.min)) {
  602. this.min = Math.min.apply(Math, years);
  603. }
  604. if (isBlank(this.max)) {
  605. this.max = Math.max.apply(Math, years);
  606. }
  607. }
  608. else {
  609. if (isBlank(this.min)) {
  610. this.min = (todaysYear - 100).toString();
  611. }
  612. if (isBlank(this.max)) {
  613. this.max = todaysYear.toString();
  614. }
  615. }
  616. const min = this._min = parseDate(this.min);
  617. const max = this._max = parseDate(this.max);
  618. min.year = min.year || todaysYear;
  619. max.year = max.year || todaysYear;
  620. min.month = min.month || 1;
  621. max.month = max.month || 12;
  622. min.day = min.day || 1;
  623. max.day = max.day || 31;
  624. min.hour = min.hour || 0;
  625. max.hour = max.hour || 23;
  626. min.minute = min.minute || 0;
  627. max.minute = max.minute || 59;
  628. min.second = min.second || 0;
  629. max.second = max.second || 59;
  630. // Ensure min/max constraits
  631. if (min.year > max.year) {
  632. console.error('min.year > max.year');
  633. min.year = max.year - 100;
  634. }
  635. if (min.year === max.year) {
  636. if (min.month > max.month) {
  637. console.error('min.month > max.month');
  638. min.month = 1;
  639. }
  640. else if (min.month === max.month && min.day > max.day) {
  641. console.error('min.day > max.day');
  642. min.day = 1;
  643. }
  644. }
  645. }
  646. }
  647. DateTime.decorators = [
  648. { type: Component, args: [{
  649. selector: 'ion-datetime',
  650. template: '<div *ngIf="!_text" class="datetime-text datetime-placeholder">{{placeholder}}</div>' +
  651. '<div *ngIf="_text" class="datetime-text">{{_text}}</div>' +
  652. '<button aria-haspopup="true" ' +
  653. 'type="button" ' +
  654. '[id]="id" ' +
  655. 'ion-button="item-cover" ' +
  656. '[attr.aria-labelledby]="_labelId" ' +
  657. '[attr.aria-disabled]="_disabled" ' +
  658. 'class="item-cover">' +
  659. '</button>',
  660. host: {
  661. '[class.datetime-disabled]': '_disabled'
  662. },
  663. providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: DateTime, multi: true }],
  664. encapsulation: ViewEncapsulation.None,
  665. },] },
  666. ];
  667. /** @nocollapse */
  668. DateTime.ctorParameters = () => [
  669. { type: Form, },
  670. { type: Config, },
  671. { type: ElementRef, },
  672. { type: Renderer, },
  673. { type: Item, decorators: [{ type: Optional },] },
  674. { type: PickerController, decorators: [{ type: Optional },] },
  675. ];
  676. DateTime.propDecorators = {
  677. 'min': [{ type: Input },],
  678. 'max': [{ type: Input },],
  679. 'displayFormat': [{ type: Input },],
  680. 'initialValue': [{ type: Input },],
  681. 'pickerFormat': [{ type: Input },],
  682. 'cancelText': [{ type: Input },],
  683. 'doneText': [{ type: Input },],
  684. 'yearValues': [{ type: Input },],
  685. 'monthValues': [{ type: Input },],
  686. 'dayValues': [{ type: Input },],
  687. 'hourValues': [{ type: Input },],
  688. 'minuteValues': [{ type: Input },],
  689. 'monthNames': [{ type: Input },],
  690. 'monthShortNames': [{ type: Input },],
  691. 'dayNames': [{ type: Input },],
  692. 'dayShortNames': [{ type: Input },],
  693. 'pickerOptions': [{ type: Input },],
  694. 'placeholder': [{ type: Input },],
  695. 'ionCancel': [{ type: Output },],
  696. '_click': [{ type: HostListener, args: ['click', ['$event'],] },],
  697. '_keyup': [{ type: HostListener, args: ['keyup.space',] },],
  698. };
  699. /**
  700. * @hidden
  701. * Use to convert a string of comma separated numbers or
  702. * an array of numbers, and clean up any user input
  703. */
  704. function convertToArrayOfNumbers(input, type) {
  705. if (isString(input)) {
  706. // convert the string to an array of strings
  707. // auto remove any whitespace and [] characters
  708. input = input.replace(/\[|\]|\s/g, '').split(',');
  709. }
  710. let values;
  711. if (isArray(input)) {
  712. // ensure each value is an actual number in the returned array
  713. values = input
  714. .map((num) => parseInt(num, 10))
  715. .filter(isFinite);
  716. }
  717. if (!values || !values.length) {
  718. console.warn(`Invalid "${type}Values". Must be an array of numbers, or a comma separated string of numbers.`);
  719. }
  720. return values;
  721. }
  722. /**
  723. * @hidden
  724. * Use to convert a string of comma separated strings or
  725. * an array of strings, and clean up any user input
  726. */
  727. function convertToArrayOfStrings(input, type) {
  728. if (isPresent(input)) {
  729. if (isString(input)) {
  730. // convert the string to an array of strings
  731. // auto remove any [] characters
  732. input = input.replace(/\[|\]/g, '').split(',');
  733. }
  734. var values;
  735. if (isArray(input)) {
  736. // trim up each string value
  737. values = input.map((val) => val.trim());
  738. }
  739. if (!values || !values.length) {
  740. console.warn(`Invalid "${type}Names". Must be an array of strings, or a comma separated string.`);
  741. }
  742. return values;
  743. }
  744. }
  745. const DEFAULT_FORMAT = 'MMM D, YYYY';
  746. //# sourceMappingURL=datetime.js.map