1 | import {Date as SugarDate} from 'sugar-date';
|
2 | import 'sugar-date/locales';
|
3 | import {Feature} from '../feature';
|
4 | import {isObj, isArray} from '../types';
|
5 | import {DATE} from '../const';
|
6 | import {root} from '../root';
|
7 |
|
8 | /**
|
9 | * Wrapper for Sugar Date module providing datetime helpers and locales
|
10 | * @export
|
11 | * @class DateType
|
12 | */
|
13 | export class DateType extends Feature {
|
14 |
|
15 | /**
|
16 | * Creates an instance of DateType
|
17 | * @param {TableFilter} tf TableFilter instance
|
18 | */
|
19 | constructor(tf) {
|
20 | super(tf, DateType);
|
21 |
|
22 | /**
|
23 | * Global locale
|
24 | * @type {String}
|
25 | */
|
26 | this.locale = tf.locale;
|
27 |
|
28 | /**
|
29 | * Sugar Date instance
|
30 | * @type {Object}
|
31 | */
|
32 | this.datetime = SugarDate;
|
33 |
|
34 | this.enable();
|
35 | }
|
36 |
|
37 | /**
|
38 | * Initialize DateType instance
|
39 | */
|
40 | init() {
|
41 | if (this.initialized) {
|
42 | return;
|
43 | }
|
44 |
|
45 | // Set global locale
|
46 | this.datetime.setLocale(this.locale);
|
47 |
|
48 | // Add formats from column types configuration if any
|
49 | this.addConfigFormats(this.tf.colTypes);
|
50 |
|
51 | this.emitter.on(
|
52 | ['add-date-type-formats'],
|
53 | (tf, types) => this.addConfigFormats(types)
|
54 | );
|
55 |
|
56 | // Broadcast date-type initialization
|
57 | this.emitter.emit('date-type-initialized', this.tf, this);
|
58 |
|
59 | /** @inherited */
|
60 | this.initialized = true;
|
61 | }
|
62 |
|
63 | /**
|
64 | * Parse a string representation of a date for a specified locale and return
|
65 | * a date object
|
66 | * @param {String} dateStr String representation of a date
|
67 | * @param {String} localeCode Locale code (ie 'en-us')
|
68 | * @returns {Date}
|
69 | */
|
70 | parse(dateStr, localeCode) {
|
71 | return this.datetime.create(dateStr, localeCode);
|
72 | }
|
73 |
|
74 | /**
|
75 | * Check string representation of a date for a specified locale is valid
|
76 | * @param {any} dateStr String representation of a date
|
77 | * @param {any} localeCode Locale code (ie 'en-us')
|
78 | * @returns {Boolean}
|
79 | */
|
80 | isValid(dateStr, localeCode) {
|
81 | return this.datetime.isValid(this.parse(dateStr, localeCode));
|
82 | }
|
83 |
|
84 | /**
|
85 | * Return the type object of a specified column as per configuration or
|
86 | * passed collection
|
87 | * @param {Number} colIndex Column index
|
88 | * @param {Array} types Collection of column types, optional
|
89 | * @returns {Object}
|
90 | */
|
91 | getOptions(colIndex, types) {
|
92 | types = types || this.tf.colTypes;
|
93 | let colType = types[colIndex];
|
94 | return isObj(colType) ? colType : {};
|
95 | }
|
96 |
|
97 | /**
|
98 | * Return the locale code for supplied column index as per configuration
|
99 | * or global setting
|
100 | * @param {Number} colIndex Column index
|
101 | * @returns {String} Locale code (ie: 'en-us')
|
102 | */
|
103 | getLocale(colIndex) {
|
104 | return this.getOptions(colIndex).locale || this.locale;
|
105 | }
|
106 |
|
107 | /**
|
108 | * Add date time format(s) to a locale as specified by the passed
|
109 | * collection of column types, ie:
|
110 | * [
|
111 | * 'string',
|
112 | * 'number',
|
113 | * { type: 'date', locale: 'en', format: ['{dd}/{MM}/{yyyy}']}
|
114 | * ]
|
115 | *
|
116 | * @param {Array} [types=[]] Collection of column types
|
117 | */
|
118 | addConfigFormats(types=[]) {
|
119 | types.forEach((type, idx) => {
|
120 | let options = this.getOptions(idx, types);
|
121 | if (options.type === DATE && options.hasOwnProperty('format')) {
|
122 | let locale = this.datetime.getLocale(
|
123 | options.locale || this.locale
|
124 | );
|
125 | let formats = isArray(options.format) ?
|
126 | options.format : [options.format];
|
127 |
|
128 | // Sugar date module throws exceptions with locale.addFormat
|
129 | try {
|
130 | formats.forEach((format) => {
|
131 | locale.addFormat(format);
|
132 | });
|
133 | } catch (ex) {
|
134 | root.console.error(ex);
|
135 | }
|
136 | }
|
137 | });
|
138 | }
|
139 |
|
140 | /**
|
141 | * Remove DateType instance
|
142 | */
|
143 | destroy() {
|
144 | if (!this.initialized) {
|
145 | return;
|
146 | }
|
147 |
|
148 | // TODO: remove added formats
|
149 |
|
150 | this.emitter.off(
|
151 | ['add-date-type-formats'],
|
152 | (tf, types) => this.addConfigFormats(types)
|
153 | );
|
154 |
|
155 | this.initialized = false;
|
156 | }
|
157 | }
|