1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-06-09 17:32:31 +02:00
TableFilter/src/modules/dateType.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-09-14 10:03:45 +02:00
import {Date as SugarDate} from 'sugar-date';
2016-09-13 16:30:18 +02:00
import 'sugar-date/locales';
2016-09-14 10:03:45 +02:00
import {isObj, isArray} from '../types';
2016-09-13 16:30:18 +02:00
export class DateType {
constructor(tf) {
this.tf = tf;
this.locale = tf.locale;
2016-09-14 10:03:45 +02:00
this.datetime = SugarDate;
2016-09-13 16:30:18 +02:00
this.emitter = tf.emitter;
}
init() {
2016-09-14 10:03:45 +02:00
if (this.initialized) {
return;
}
// Global locale
this.datetime.setLocale(this.locale);
// let locale = this.datetime.getLocale(this.locale);
2016-09-13 16:30:18 +02:00
2016-09-14 10:03:45 +02:00
// Add formats from column types configuration if any
2016-09-13 16:30:18 +02:00
this._addConfigFormats();
// locale.addFormat('{dd}/{MM}/{yyyy}');
// locale.addFormat('{MM}/{dd}/{yyyy}');
// locale.addFormat('{dd}-{months}-{yyyy|yy}');
// locale.addFormat('{dd}-{MM}-{yyyy|yy}');
2016-09-14 10:03:45 +02:00
this.initialized = true;
this.emitter.emit('date-type-initialized', this.tf, this);
2016-09-13 16:30:18 +02:00
}
2016-09-14 10:03:45 +02:00
parse(dateStr, localeCode) {
// console.log('parse', dateStr, localeCode,
// this.datetime.create(dateStr, localeCode));
return this.datetime.create(dateStr, localeCode);
2016-09-13 16:30:18 +02:00
}
isValid(dateStr, localeCode) {
2016-09-14 10:03:45 +02:00
// console.log(dateStr, localeCode, this.parse(dateStr, localeCode),
// this.datetime.isValid(this.parse(dateStr, localeCode)));
return this.datetime.isValid(this.parse(dateStr, localeCode));
2016-09-13 16:30:18 +02:00
}
getOptions(colIndex) {
let colType = this.tf.colTypes[colIndex];
return isObj(colType) ? colType : {};
}
_addConfigFormats() {
this.tf.colTypes.forEach((type, idx) => {
let options = this.getOptions(idx);
if (options.hasOwnProperty('format')) {
2016-09-14 10:03:45 +02:00
let locale = this.datetime.getLocale(
2016-09-13 16:30:18 +02:00
options.locale || this.locale
);
2016-09-14 10:03:45 +02:00
if (isArray(options.format)) {
options.format.forEach((format) => {
locale.addFormat(format);
});
} else {
locale.addFormat(options.format);
}
2016-09-13 16:30:18 +02:00
}
});
}
2016-09-14 10:03:45 +02:00
destroy() {
this.initialized = false;
}
2016-09-13 16:30:18 +02:00
}