salix/front/core/services/week-days.js

117 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-10-23 15:38:35 +00:00
import ngModule from '../module';
2019-11-18 15:31:37 +00:00
/**
* @property {Array} days Weekdays data array with the same indexes as Date.getDay()
* @property {Object} map Weekdays data map using weekday codes as key
* @property {Array} localeCodes Locale weekday codes indexes depend on current locale
* @property {Array} locale Weekday data array with indexes depending on current locale
*
* Weekday properties:
*
* @property {Number} index The weekday index acording to Date.getDay()
* @property {String} code The weekday code
* @property {String} name The weekday name
* @property {String} char The first weekday letter
* @property {String} abr The abreviated 3 letters weekday name
* @property {String} locale The weekday name in current locale
* @property {String} localeChar The first weekday letter in current locale
* @property {String} localeAbr The abreviated 3 letters weekday name in current locale
*/
2019-10-23 15:38:35 +00:00
class WeekDays {
constructor($translate) {
this.$translate = $translate;
this.days = [
{
code: 'sun',
name: 'Sunday'
}, {
code: 'mon',
name: 'Monday'
}, {
code: 'tue',
name: 'Tuesday'
}, {
code: 'wed',
name: 'Wednesday'
}, {
code: 'thu',
name: 'Thursday'
}, {
code: 'fri',
name: 'Friday'
}, {
code: 'sat',
name: 'Saturday'
}
];
this.map = {};
for (let i = 0; i < this.days.length; i++) {
let day = this.days[i];
day.index = i;
day.char = day.name.substr(0, 1);
day.abr = day.name.substr(0, 3);
this.map[day.code] = day;
}
this.getLocales();
}
getLocales() {
for (let day of this.days) {
let locale = this.$translate.instant(day.name);
Object.assign(day, {
locale,
localeChar: locale.substr(0, 1),
localeAbr: locale.substr(0, 3)
});
}
this.localeCodes = [
'mon',
'tue',
'wed',
'thu',
'fri',
'sat',
'sun'
];
this.locales = [];
for (let code of this.localeCodes)
this.locales.push(this.map[code]);
}
2019-11-18 15:31:37 +00:00
fromSet(weekDays) {
let wdays = [];
if (weekDays) {
let codes = weekDays.split(',');
for (let code of codes) {
let data = this.map[code];
if (data) wdays[data.index] = true;
}
}
return wdays;
}
toSet(wdays) {
let weekDays = [];
if (wdays) {
for (let i = 0; i < wdays.length; i++) {
if (!wdays[i]) continue;
let data = this.days[i];
if (data) weekDays.push(data.code);
}
}
return weekDays.join(',');
}
2019-10-23 15:38:35 +00:00
}
WeekDays.$inject = ['$translate'];
ngModule.service('vnWeekDays', WeekDays);