import ngModule from '../module'; /** * Stores useful information about a weekday. * * @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 */ 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} days Weekdays array with the same indexes as Date.getDay() * @property {Object} map Weekdays map using weekday codes as keys * @property {Array} locale Weekday data array with indexes depending on current locale * @property {Array} localeCodes Locale weekday codes with indexes depending on current locale */ export default class WeekDays { constructor($translate) { this.$translate = $translate; this.days = [ 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') ]; 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]); } /** * 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} Array with selected days set to %true */ 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; } /** * 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} wdays Array with selected days set to %true * @return {String} weekDays Weekday codes separated by commas */ 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);