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

128 lines
3.8 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
/**
2020-04-08 09:24:40 +00:00
* Stores useful information about a weekday.
2019-11-18 15:31:37 +00:00
*
* @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
*/
2020-04-08 09:24:40 +00:00
export class WeekDay {
constructor(code, name) {
this.code = code;
this.name = name;
}
}
/**
* Stores information about weekdays in many formats to be accessed in different
* ways depending on the context.
*
* @property {Array<WeekDay>} days Weekdays array with the same indexes as Date.getDay()
* @property {Object<WeekDay>} map Weekdays map using weekday codes as keys
* @property {Array<WeekDay>} locale Weekday data array with indexes depending on current locale
* @property {Array<String>} localeCodes Locale weekday codes with indexes depending on current locale
*/
export default class WeekDays {
2019-10-23 15:38:35 +00:00
constructor($translate) {
this.$translate = $translate;
this.days = [
2020-04-08 09:24:40 +00:00
new WeekDay('sun', 'Sunday'),
new WeekDay('mon', 'Monday'),
new WeekDay('tue', 'Tuesday'),
new WeekDay('wed', 'Wednesday'),
new WeekDay('thu', 'Thursday'),
new WeekDay('fri', 'Friday'),
new WeekDay('sat', 'Friday')
2019-10-23 15:38:35 +00:00
];
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
2020-04-08 09:24:40 +00:00
/**
* Transforms weekday set into an array whose indexes are weekday index
* with selected days set to %true.
*
* @param {String} weekDays Weekday codes separated by commas
* @return {Array<Boolean>} Array with selected days set to %true
*/
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;
}
2020-04-08 09:24:40 +00:00
/**
* Perform the inverse operation of fromSet() method. Transforms an
* array whose indexes are weekday index with selected days set to %true to
* weekday codes separated by commas.
*
* @param {Array<Boolean>} wdays Array with selected days set to %true
* @return {String} weekDays Weekday codes separated by commas
*/
2019-11-18 15:31:37 +00:00
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);