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

117 lines
3.1 KiB
JavaScript

import ngModule from '../module';
/**
* @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
*/
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]);
}
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(',');
}
}
WeekDays.$inject = ['$translate'];
ngModule.service('vnWeekDays', WeekDays);