UNPKG

@factory-utils/tools

Version:

Set of genericised angular2+ components used in most of Advanced SCHEMA - Factory's projects.

1,283 lines (1,272 loc) 433 kB
import * as i0 from '@angular/core'; import { Injectable, NgModule, EventEmitter, Component, Input, Output, Directive, HostListener, ViewChild, LOCALE_ID } from '@angular/core'; import { marked } from 'marked'; import TurndownService from 'turndown'; import * as i1 from '@ngx-translate/core'; import { TranslateModule, TranslateService } from '@ngx-translate/core'; import moment from 'moment/moment'; import numeral from 'numeral'; import * as i1$1 from '@angular/common'; import { CommonModule } from '@angular/common'; import { Subject } from 'rxjs'; import { takeUntil, debounceTime, distinctUntilChanged, tap } from 'rxjs/operators'; import * as i4 from '@angular/forms'; import { FormsModule } from '@angular/forms'; import * as i3 from 'ngx-quill'; import { QuillModule } from 'ngx-quill'; import { BrowserModule } from '@angular/platform-browser'; import * as i5 from 'ngx-scrollbar'; import { NgScrollbarModule } from 'ngx-scrollbar'; import { NgScrollReached } from 'ngx-scrollbar/reached-event'; class StringUtil { static equals(arg1, arg2) { return (StringUtil.compare(arg1, arg2) === 0); } static compare(arg1, arg2, ignoreSpecialChars = false, ignoreCase = false) { if (ignoreCase) { arg1 = arg1.toLocaleLowerCase(); arg2 = arg2.toLocaleLowerCase(); } if (ignoreSpecialChars) { arg1 = StringUtil.removeSpecialChars(arg1); arg2 = StringUtil.removeSpecialChars(arg2); } return arg1 === arg2 ? 0 : arg1 > arg2 ? 1 : -1; } static removeSpecialChars(arg1) { return arg1.normalize('NFD').replace(/([\u0300-\u036f]|[^0-9a-zA-Z])/g, ''); } static { this.ɵfac = function StringUtil_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || StringUtil)(); }; } static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: StringUtil, factory: StringUtil.ɵfac }); } } (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(StringUtil, [{ type: Injectable }], null, null); })(); /* eslint-disable @typescript-eslint/no-explicit-any */ class ObjectUtil { /** * Return true if arg is an Object * */ static isObject(arg) { try { if (arg !== null && typeof arg === 'object' && arg.constructor.toString().indexOf('Array') < 0) { return true; } return false; } catch (e) { // IE8,9 Will throw exceptions on certain host objects #9897 return false; } } /** * Return true when obj given in parameter is empty. * Return false otherwise. * */ static isEmpty(obj) { return Object.keys(obj).length === 0; } /** * Return number attributes. * */ static size(obj, ignoredValue) { let size = 0; for (const key in obj) { if (obj.hasOwnProperty(key) && (typeof ignoredValue === 'undefined' || !ArrayUtil.contains(ignoredValue, key))) { size++; } } return size; } /** * Return a copy of an object. */ static copy(obj) { const out = Array.isArray(obj) ? [] : {}; for (const key in obj) { const v = obj[key]; out[key] = (typeof v === 'object') ? ObjectUtil.copy(v) : v; } return out; } /** * Return a copy of an object. */ static deepCopy(block) { let out = null; if (Array.isArray(block)) { out = []; for (const elem of block) { const subArray = elem; out.push((typeof subArray === 'object') ? ObjectUtil.deepCopy(subArray) : subArray); } } else { out = {}; for (const key in block) { const subObject = block[key]; out[key] = (typeof subObject === 'object') ? ObjectUtil.deepCopy(subObject) : subObject; } } return out; } /** * Compare an object with the current object. * Return true if the two elements are identical. * Otherwise return false. * */ static equals(obj1, obj2) { return (ObjectUtil.compare(obj1, obj2) === 0); } static compare(obj1, obj2) { const size1 = ObjectUtil.size(obj1); const size2 = ObjectUtil.size(obj2); if (size1 !== size2) { return size1 - size2; } for (const key in obj1) { const value1 = obj1[key]; const value2 = obj2[key]; if (!AllTypeUtil.equals(value1, value2)) { return AllTypeUtil.compare(value1, value2); } } return 0; } /** * Returns all entries matching searchedValue in obj and all its children * */ static deepSearch(obj, hierarchicalField, searchedField, searchedValue, skipChildren = true, skipReferences = true, ignoreSpecialChars = false, ignoreCase = false, multipleSearchedValues = false) { let currValue = obj[searchedField]; let cleanedObj = []; function subSearch(searchedValue, currValue) { let subCleanedObj = []; if (typeof currValue === 'string') { if (ignoreCase) { currValue = currValue.toLocaleLowerCase(); searchedValue = searchedValue.toLocaleLowerCase(); } if (ignoreSpecialChars) { currValue = StringUtil.removeSpecialChars(currValue); searchedValue = StringUtil.removeSpecialChars(searchedValue); } if (currValue.indexOf(searchedValue) > -1) { if (skipReferences) { subCleanedObj.push(ObjectUtil.deepCopy(obj)); } else { subCleanedObj.push(obj); } if (skipChildren) { delete subCleanedObj[0][hierarchicalField]; } } else if (obj[hierarchicalField] && obj[hierarchicalField].length) { let deepRes = ArrayUtil.deepSearch(obj[hierarchicalField], hierarchicalField, searchedField, searchedValue, skipChildren, skipReferences, ignoreSpecialChars, ignoreCase, multipleSearchedValues); return deepRes; } } else if (obj[searchedField] === searchedValue) { if (skipReferences) { subCleanedObj.push(ObjectUtil.deepCopy(obj)); } else { subCleanedObj.push(obj); } if (skipChildren) { delete subCleanedObj[0][hierarchicalField]; } } else if (obj[hierarchicalField] && obj[hierarchicalField].length) { let deepRes = ArrayUtil.deepSearch(obj[hierarchicalField], hierarchicalField, searchedField, searchedValue, skipChildren, skipReferences, ignoreSpecialChars, ignoreCase, multipleSearchedValues); return deepRes; } return subCleanedObj; } if (multipleSearchedValues && typeof searchedValue === 'string') { const searchedValues = searchedValue.replace(/(\s|\t)+/g, ' ').split(' '); for (let searchedValue of searchedValues) { let res = subSearch(searchedValue, currValue); for (let element of res) { if (cleanedObj.findIndex((objElement) => element[searchedField] === objElement[searchedField]) === -1) { cleanedObj.push(element); } } } } else { cleanedObj = subSearch(searchedValue, currValue); } return cleanedObj; } /** * Fills an object with the given object, without changing its reference. * */ static fill(target, source) { for (const elemIndex in source) { if (target[elemIndex] && ObjectUtil.isObject(target[elemIndex])) { ObjectUtil.fill(target[elemIndex], source[elemIndex]); } else { target[elemIndex] = source[elemIndex]; } } } /** * Get the key of an object with specific value. * */ static getKeyByValue(obj, value) { for (const key in obj) { if (obj.hasOwnProperty(key) && obj[key] === value) { return key; } } } /** * Clear an object * */ static clear(obj, ignoredValue) { for (const key in obj) { if (obj.hasOwnProperty(key) && (typeof ignoredValue === 'undefined' || !ArrayUtil.contains(ignoredValue, key))) { delete obj[key]; } } } /** * Copy all obj1 values in obj2 and keep obj2 reference * */ static applyDiff(obj1, obj2) { const typesAllowCopy = ['string', 'number']; for (const key2 in obj2) { if (key2 in obj1) { if (((ObjectUtil.isObject(obj1[key2]) && ObjectUtil.isObject(obj2[key2])) || (ArrayUtil.isArray(obj1[key2]) && ArrayUtil.isArray(obj2[key2]))) && !ObjectUtil.equals(obj1[key2], obj2[key2])) { ObjectUtil.applyDiff(obj1[key2], obj2[key2]); } else if (typesAllowCopy.indexOf(typeof obj1[key2]) !== -1 && typesAllowCopy.indexOf(typeof obj2[key2]) !== -1 && obj1[key2] !== obj2[key2]) { obj2[key2] = obj1[key2]; } } else if (!(key2 in obj1)) { delete obj2[key2]; } } for (const key1 in obj1) { if (!(key1 in obj2)) { obj2[key1] = obj1[key1]; } } } static { this.ɵfac = function ObjectUtil_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || ObjectUtil)(); }; } static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: ObjectUtil, factory: ObjectUtil.ɵfac }); } } (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ObjectUtil, [{ type: Injectable }], null, null); })(); class ArrayUtil { /** * Return true if arg is an Array * */ static isArray(arg) { return Object.prototype.toString.call(arg) === '[object Array]'; } /** * Return the first occurrence. * */ static front(array) { return array[0]; } /** * Return the last occurrence. * */ static back(array) { return array[array.length - 1]; } /** * Return a copy of an array. * */ static copy(array) { return (JSON.parse(JSON.stringify(array))); } static equals(array1, array2) { return (ArrayUtil.compare(array1, array2) === 0); } static compare(array1, array2) { if (array1.length !== array2.length) { return array1.length - array2.length; } array1.sort(); array2.sort(); array1.forEach(function (value, index) { const value2 = array2[index]; if (!AllTypeUtil.equals(value, value2)) { return AllTypeUtil.compare(value, value2); } }); return 0; } /** * Fills an array with the given array, without changing its reference. * */ static fill(target, source) { target.length = 0; for (const elem of source) { target.push(elem); } } /** * Check if an element exist. * Return true is element is found. * Otherwise return false. * */ static contains(array, elem) { for (const el of array) { if (el === elem) { return true; } } return false; } /** * Returns all entries matching searchedValue in array and all its children * */ static deepSearch(arr, hierarchicalField, searchedField, searchedValue, skipChildren = true, skipReferences = true, ignoreSpecialChars = false, ignoreCase = false, multipleSearchedValues = false) { let res = []; for (const child of arr) { if (child) { let objRes = ObjectUtil.deepSearch(child, hierarchicalField, searchedField, searchedValue, skipChildren, skipReferences, ignoreSpecialChars, ignoreCase, multipleSearchedValues); if (objRes && objRes.length) { res.push(...objRes); } } } return res; } /** * Clear an array * */ static clear(array) { while (array.length > 0) { array.pop(); } } /** * Initialize a new array * if defaultItem is set, it pushes size (default 0) defaultItem items in the array * */ static init(defaultItem, size) { const newArray = []; if (typeof defaultItem !== 'undefined') { if (typeof size === 'undefined') { size = 0; } for (let i = 0; i < size; ++i) { if (ArrayUtil.isArray(defaultItem)) { newArray.push(ArrayUtil.copy(defaultItem)); } else if (ObjectUtil.isObject(defaultItem)) { newArray.push(ObjectUtil.copy(defaultItem)); } else { newArray.push(defaultItem); } } } return newArray; } /** * Find an element in Array * If array given in parameter is an object's array, you can specify a key * */ static find(array, value, key) { for (const el of array) { if (typeof key === 'undefined' && el === value) { return el; } else if (typeof key !== 'undefined' && ObjectUtil.isObject(el) && el[key] === value) { return el; } } return null; } /** * Find all elements in Array * If array given in parameter is an object's array, you can specify a key * */ static findAll(array, value, key) { const res = []; for (const el of array) { if (typeof key === 'undefined' && el === value) { res.push(el); } else if (typeof key !== 'undefined' && ObjectUtil.isObject(el) && el[key] === value) { res.push(el); } } return res; } /** * Find index of the given value * */ static findIndex(array, value) { for (let i = 0; i < array.length; i++) { if (array[i] === value) { return i; } } return -1; } /** * Find object of the given object * */ static findObject(array, obj) { for (const entry of array) { if (ObjectUtil.equals(entry, obj)) { return entry; } } return null; } /** * Find an element in Array * If array given in parameter is an object's array, you can specify a key * */ static findObjIndex(array, value, key) { for (let i = 0; i < array.length; i++) { if (typeof key === 'undefined' && array[i] === value) { return i; } else if (typeof key !== 'undefined' && ObjectUtil.isObject(array[i]) && array[i][key] === value) { return i; } } return -1; } /** * Insert value at the specified index * */ static insert(array, index, value) { array.splice(index, 0, value); } /** * Remove specific index * */ static removeIndex(array, index) { array.splice(index, 1); } static { this.ɵfac = function ArrayUtil_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || ArrayUtil)(); }; } static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: ArrayUtil, factory: ArrayUtil.ɵfac }); } } (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ArrayUtil, [{ type: Injectable }], null, null); })(); class AllTypeUtil { static equals(arg1, arg2) { return (AllTypeUtil.compare(arg1, arg2) === 0); } static compare(arg1, arg2) { const typeofArg1 = typeof arg1; const typeofArg2 = typeof arg2; if (typeofArg1 !== typeofArg2) { return -1; } if (typeofArg1 === 'string') { return StringUtil.compare(arg1, arg2); } else if (ArrayUtil.isArray(arg1)) { return ArrayUtil.compare(arg1, arg2); } else if (ObjectUtil.isObject(arg1)) { return ObjectUtil.compare(arg1, arg2); } else { return arg1 - arg2; } } static { this.ɵfac = function AllTypeUtil_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || AllTypeUtil)(); }; } static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: AllTypeUtil, factory: AllTypeUtil.ɵfac }); } } (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(AllTypeUtil, [{ type: Injectable }], null, null); })(); class ChangesUtil { static hasChanged(changes, property, member) { if (typeof property === 'undefined') { let somethingHasChanged = false; for (const attrKey in changes) { if (ChangesUtil.hasChanged(changes, attrKey)) { somethingHasChanged = true; } } return somethingHasChanged; } else if (changes[property]) { let member1 = changes[property].previousValue; let member2 = changes[property].currentValue; if (!member1) { return true; } if (member) { member1 = changes[property].previousValue[member]; member2 = changes[property].currentValue[member]; } return !AllTypeUtil.equals(member1, member2); } return false; } static resetWrongInputs(changes, defaultValues, component) { for (const key in defaultValues) { if (changes[key] && (typeof component[key] === 'undefined' || component[key] === null)) { component[key] = defaultValues[key]; changes[key].currentValue = defaultValues[key]; } } } static { this.ɵfac = function ChangesUtil_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || ChangesUtil)(); }; } static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: ChangesUtil, factory: ChangesUtil.ɵfac }); } } (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ChangesUtil, [{ type: Injectable }], null, null); })(); class DateUtil { static { this.quarterLabels = [ { shortLabel: 'Q1', longLabel: '1st quarter' }, { shortLabel: 'Q2', longLabel: '2nd quarter' }, { shortLabel: 'Q3', longLabel: '3rd quarter' }, { shortLabel: 'Q4', longLabel: '4th quarter' } ]; } static { this.monthLabels = [ { shortLabel: 'jan', longLabel: 'january' }, { shortLabel: 'feb', longLabel: 'february' }, { shortLabel: 'mar', longLabel: 'march' }, { shortLabel: 'apr', longLabel: 'april' }, { shortLabel: 'may', longLabel: 'may' }, { shortLabel: 'jun', longLabel: 'june' }, { shortLabel: 'jul', longLabel: 'july' }, { shortLabel: 'aug', longLabel: 'august' }, { shortLabel: 'sep', longLabel: 'september' }, { shortLabel: 'oct', longLabel: 'october' }, { shortLabel: 'nov', longLabel: 'november' }, { shortLabel: 'dec', longLabel: 'december' } ]; } static dayDiff(d1, d2) { const timeDiff = Math.abs(d2.getTime() - d1.getTime()); const diffDays = timeDiff / (1000 * 3600 * 24); return Math.floor(diffDays); } static monthDiff(d1, d2) { const year1 = d1.getFullYear(); const year2 = d2.getFullYear(); let month1 = d1.getMonth(); let month2 = d2.getMonth(); if (month1 === 0) { month1++; month2++; } return (year2 - year1) * 12 + (month2 - month1); } static quarterDiff(d1, d2) { const quarterDiff = DateUtil.monthDiff(d1, d2) / 3; const sign = quarterDiff < 0 ? -1 : 1; return Math.floor(Math.abs(quarterDiff)) * sign; } static yearDiff(d1, d2) { const year1 = d1.getFullYear(); const year2 = d2.getFullYear(); return (year2 - year1); } static dayFirstDate(date) { const year = date.getFullYear(); const month = date.getMonth(); const day = date.getDate(); return new Date(year, month, day, 0); } static dayLastDate(date) { const year = date.getFullYear(); const month = date.getMonth(); const day = date.getDate(); return new Date(year, month, day, 23, 59, 59); } static monthFirstDate(date) { const year = date.getFullYear(); const month = date.getMonth(); return new Date(year, month, 1); } static monthLastDate(date) { const year = date.getFullYear(); const month = date.getMonth(); return new Date(year, month + 1, 0); } static quarterFirstDate(date) { const year = date.getFullYear(); const month = date.getMonth(); const quarter = Math.floor(month / 3); return new Date(year, quarter * 3, 1); } static quarterLastDate(date) { const year = date.getFullYear(); const month = date.getMonth(); const quarter = Math.ceil(month / 3); return new Date(year, quarter * 3, 0); } static yearFirstDate(date) { const year = date.getFullYear(); return new Date(year.toString()); } static yearLastDate(date) { const year = date.getFullYear(); return new Date(year, 11, 31); } static incrementDay(date) { const newDate = new Date(date); newDate.setDate(date.getDate() + 1); return newDate; } static incrementMonth(date) { const newDate = new Date(date); newDate.setMonth(date.getMonth() + 1); return newDate; } static incrementQuarter(date) { const newDate = new Date(date); newDate.setMonth(date.getMonth() + 3); return newDate; } static incrementYear(date) { const newDate = new Date(date); newDate.setFullYear(date.getFullYear() + 1); return newDate; } static displayDate(date) { let display; if (typeof date === 'number') { date = new Date(date); } display = `${DateUtil.displayDay(date)} ${DateUtil.displayMonth(date)} ${DateUtil.displayYear(date)}`; return display; } static displayDay(date) { let display; if (typeof date === 'number') { date = new Date(date); } display = date.getDate().toString(); return display; } static displayMonth(date) { let display; if (typeof date === 'number') { date = new Date(date); } display = DateUtil.monthLabels[date.getMonth()].shortLabel; return display; } static displayQuarter(date) { let quarter; let display; if (typeof date === 'number') { date = new Date(date); } quarter = Math.floor(date.getMonth() / 3); display = DateUtil.quarterLabels[quarter].shortLabel; return display; } static displayYear(date) { let display; if (typeof date === 'number') { date = new Date(date); } display = date.getFullYear().toString(); return display; } } class TextParserUtil { static htmlToMarkdown(input) { input = input .replace(/<span class="ql-cursor">.*?<\/span>/, '') // remove quill cursor .replace(/<(\w*?)[\s]*[^>]*?(?:class="([\w-]*)"[\s]*)?[^>]*?(?:style="color: rgb\(([\d, ]*?)\);"[\s]*)?[^>]*?(?:class="([\w-]*)"[\s]*)?[^>]*?>(.*?)(<\/\1>)/gi, (match, p1, p2, p3, p4, p5) => { let r = p5; r = (p3 ? '¦S' + p3.replace(/ /g, '') + '¸' + r + '¦' : r); // quill style color r = (p2 ? '¦C' + p2.replace(/ql-size-(.).*/, '$1') + '¸' + r + '¦' : r); // quill size class r = (p4 ? '¦C' + p4.replace(/ql-size-(.).*/, '$1') + '¸' + r + '¦' : r); // quill size class r = (p1 !== 'span' ? '<' + p1 + '>' + r + '</' + p1 + '>' : r); // don't output empty span return r; }) // replace style and class .replace(/<ol>/gi, '¦#') // markdown doesn't handle well lists .replace(/<\/ol>/gi, '·') .replace(/<ul>/gi, '¦°') // markdown doesn't handle well lists .replace(/<\/ul>/gi, '·') .replace(/<li>/gi, '¦¯') // markdown doesn't handle well lists .replace(/<\/li>/gi, '·') .replace(/<u>/gi, '¦_') // markdown doesn't handle underline .replace(/<\/u>/gi, '¸') .replace(/<s>/gi, '¦-') // mardown doesn't handle crossed-word .replace(/<\/s>/gi, '¸') .replace(/<p><br><\/p>/gi, '¦BR'); return new TurndownService().turndown(input); } static markdownToHtml(input) { let md = marked.setOptions({}); input = input .replace(/¦\#(.*?·)·/gi, '<ol>$1</ol>') .replace(/¦\°(.*?·)·/gi, '<ul>$1</ul>') .replace(/¦¯¦C(ql-indent.*?)¸/gi, (m, p1) => { return `<li class="${p1}">`; }) .replace(/¦¯(.*?)·/gi, '<li>$1</li>') .replace(/¦C(.*?)¸/gi, (m, p1) => { if (m === '¦Cl¸') { p1 = p1.replace(/l/, 'ql-size-large'); } else if (m === '¦Cs¸') { p1 = p1.replace(/s/, 'ql-size-small'); } return `<span class="${p1}">`; }) .replace(/¦S(.*?)¸/gi, '<span style="color: rgb($1);">') .replace(/¦_(.*?)¸/gi, '<u>$1</u>') .replace(/¦-(.*?)¸/gi, '<s>$1</s>') .replace(/¦BR/gi, '<p><br></p>') .replace(/¦·/gi, '</li>') .replace(/¦/gi, '</span>') .replace(/\*\*\*\*/g, '**&zwnj;**') // bug in marked lib: '**bold1****bold2**' gives '<p><strong>bold1**</strong>bold2**</p>' &zwnj; is Zero-width non-joiner .replace(/\_\_/g, '_&zwnj;_'); // bug in marked lib: '_italic1__italic2_' gives '<p><em>italic1__italic2<em></p>' &zwnj; is Zero-width non-joiner return md.parse(input); } } /* eslint-disable @typescript-eslint/no-explicit-any */ class NumberFormatService { constructor() { this._numeral = numeral; this._registerFrenchLocale(); } get numeral() { return this._numeral; } _registerFrenchLocale() { if (typeof numeral.locales['fr'] !== 'undefined') { return; } numeral.register('locale', 'fr', { delimiters: { thousands: ' ', decimal: ',' }, abbreviations: { thousand: 'k', million: 'm', billion: 'b', trillion: 't' }, ordinal: value => (value === 1 ? 'er' : 'ème'), currency: { symbol: '€' } }); } static { this.ɵfac = function NumberFormatService_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || NumberFormatService)(); }; } static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: NumberFormatService, factory: NumberFormatService.ɵfac }); } } (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(NumberFormatService, [{ type: Injectable }], () => [], null); })(); class TranslateTestingModule { static { this.ɵfac = function TranslateTestingModule_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || TranslateTestingModule)(); }; } static { this.ɵmod = /*@__PURE__*/ i0.ɵɵdefineNgModule({ type: TranslateTestingModule }); } static { this.ɵinj = /*@__PURE__*/ i0.ɵɵdefineInjector({ imports: [TranslateModule.forRoot(), TranslateModule] }); } } (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(TranslateTestingModule, [{ type: NgModule, args: [{ imports: [ TranslateModule.forRoot() ], exports: [ TranslateModule ] }] }], null, null); })(); (function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(TranslateTestingModule, { imports: [i1.TranslateModule], exports: [TranslateModule] }); })(); class LocaleService { constructor(_numberFormatService, _translateService) { this._numberFormatService = _numberFormatService; this._translateService = _translateService; this._moment = moment; if (this._translateService) { this.currentLanguage = this._translateService.currentLang; } else { this.currentLanguage = navigator.language; } } get currentLanguage() { return this._currentLanguage; } get moment() { return this._moment; } set currentLanguage(currentLanguage) { this._currentLanguage = currentLanguage; this.moment.locale(currentLanguage); if (this._numberFormatService && this._numberFormatService.numeral) { this._numberFormatService.numeral.locale(currentLanguage); } if (this._translateService) { this._translateService.use(currentLanguage); } } getMoment() { return this._moment; } setLanguage(language) { this.currentLanguage = language; } addLanguage(language, lang) { if (this._translateService) { this._translateService.setTranslation(language, lang, true); } } setDefaultLanguage(language) { this.moment.locale(language); if (this._numberFormatService && this._numberFormatService.numeral) { this._numberFormatService.numeral.locale(language); } if (this._translateService) { this._translateService.setDefaultLang(language); this._translateService.use(language); } } static { this.ɵfac = function LocaleService_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || LocaleService)(i0.ɵɵinject(NumberFormatService), i0.ɵɵinject(i1.TranslateService)); }; } static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: LocaleService, factory: LocaleService.ɵfac }); } } (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(LocaleService, [{ type: Injectable }], () => [{ type: NumberFormatService }, { type: i1.TranslateService }], null); })(); function CalendarComponent_span_9_Template(rf, ctx) { if (rf & 1) { const _r1 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "span", 28); i0.ɵɵlistener("click", function CalendarComponent_span_9_Template_span_click_0_listener() { i0.ɵɵrestoreView(_r1); const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.switchToMonth()); }); i0.ɵɵtext(1); i0.ɵɵelementEnd(); } if (rf & 2) { const ctx_r1 = i0.ɵɵnextContext(); i0.ɵɵadvance(); i0.ɵɵtextInterpolate(ctx_r1.localeService.moment(ctx_r1.displayedDate).format("MMMM")); } } function CalendarComponent_span_10_Template(rf, ctx) { if (rf & 1) { const _r3 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "span", 28); i0.ɵɵlistener("click", function CalendarComponent_span_10_Template_span_click_0_listener() { i0.ɵɵrestoreView(_r3); const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.switchToMonth()); }); i0.ɵɵtext(1); i0.ɵɵelementEnd(); } if (rf & 2) { const ctx_r1 = i0.ɵɵnextContext(); i0.ɵɵadvance(); i0.ɵɵtextInterpolate2("", ctx_r1.localeService.moment(ctx_r1.displayedDate).format("MMMM"), " ", ctx_r1.localeService.moment(ctx_r1.displayedDate).format("YYYY"), ""); } } function CalendarComponent_div_20_Template(rf, ctx) { if (rf & 1) { const _r4 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "div", 29); i0.ɵɵlistener("click", function CalendarComponent_div_20_Template_div_click_0_listener() { const i_r5 = i0.ɵɵrestoreView(_r4).index; const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.selectYear(i_r5)); }); i0.ɵɵelementStart(1, "span"); i0.ɵɵtext(2); i0.ɵɵelementEnd()(); } if (rf & 2) { const year_r6 = ctx.$implicit; const i_r5 = ctx.index; const ctx_r1 = i0.ɵɵnextContext(); i0.ɵɵclassProp("today", ctx_r1.isYearToday(i_r5))("theme-bg", ctx_r1.isYearSelected(i_r5))("disabled", year_r6 < ctx_r1.startDateYear || year_r6 > ctx_r1.endDateYear); i0.ɵɵadvance(2); i0.ɵɵtextInterpolate(year_r6); } } function CalendarComponent_div_23_Template(rf, ctx) { if (rf & 1) { const _r7 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "div", 30); i0.ɵɵlistener("mouseover", function CalendarComponent_div_23_Template_div_mouseover_0_listener() { const monthIndex_r8 = i0.ɵɵrestoreView(_r7).index; const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.hoverMonth(monthIndex_r8)); })("mouseout", function CalendarComponent_div_23_Template_div_mouseout_0_listener() { i0.ɵɵrestoreView(_r7); const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.outMonth()); })("click", function CalendarComponent_div_23_Template_div_click_0_listener() { const monthIndex_r8 = i0.ɵɵrestoreView(_r7).index; const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.selectMonth(monthIndex_r8)); }); i0.ɵɵelementStart(1, "span"); i0.ɵɵtext(2); i0.ɵɵelementEnd()(); } if (rf & 2) { const month_r9 = ctx.$implicit; const monthIndex_r8 = ctx.index; const ctx_r1 = i0.ɵɵnextContext(); i0.ɵɵclassProp("today", ctx_r1.isMonthToday(monthIndex_r8))("theme-bg", ctx_r1.isMonthSelected(monthIndex_r8) || ctx_r1.isMonthRanged(monthIndex_r8))("range-start", ctx_r1.isRanged() && ctx_r1.isMonthSelected(monthIndex_r8))("range-end", ctx_r1.isRanged() && ctx_r1.isMonthRanged(monthIndex_r8))("theme-highlight", ctx_r1.isMonthInRange(monthIndex_r8))("disabled", ctx_r1.isItemDisabled(ctx_r1.displayedDate.getFullYear(), monthIndex_r8)); i0.ɵɵadvance(2); i0.ɵɵtextInterpolate(month_r9); } } function CalendarComponent_div_24_div_1_Template(rf, ctx) { if (rf & 1) { const _r10 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "div", 33); i0.ɵɵlistener("click", function CalendarComponent_div_24_div_1_Template_div_click_0_listener() { const quarterIndex_r11 = i0.ɵɵrestoreView(_r10).$implicit; const ctx_r1 = i0.ɵɵnextContext(2); return i0.ɵɵresetView(ctx_r1.selectMonth((quarterIndex_r11 - 1) * 3)); }); i0.ɵɵelementStart(1, "span"); i0.ɵɵtext(2); i0.ɵɵelementEnd()(); } if (rf & 2) { const quarterIndex_r11 = ctx.$implicit; const ctx_r1 = i0.ɵɵnextContext(2); i0.ɵɵclassProp("theme-bg", ctx_r1.isCurrentYearDisplayed && quarterIndex_r11 === ctx_r1.getSelectedQuarter())("disabled", ctx_r1.isItemDisabled(ctx_r1.displayedDate.getFullYear(), (quarterIndex_r11 - 1) * 3)); i0.ɵɵadvance(2); i0.ɵɵtextInterpolate1("Q", quarterIndex_r11, ""); } } function CalendarComponent_div_24_Template(rf, ctx) { if (rf & 1) { i0.ɵɵelementStart(0, "div", 31); i0.ɵɵtemplate(1, CalendarComponent_div_24_div_1_Template, 3, 5, "div", 32); i0.ɵɵelementEnd(); } if (rf & 2) { const ctx_r1 = i0.ɵɵnextContext(); i0.ɵɵclassProp("disabled", ctx_r1.editedSection === "month"); i0.ɵɵadvance(); i0.ɵɵproperty("ngForOf", ctx_r1.quarters); } } function CalendarComponent_div_27_Template(rf, ctx) { if (rf & 1) { i0.ɵɵelementStart(0, "div", 34)(1, "span"); i0.ɵɵtext(2); i0.ɵɵelementEnd()(); } if (rf & 2) { const weekDay_r12 = ctx.$implicit; const ctx_r1 = i0.ɵɵnextContext(); i0.ɵɵadvance(2); i0.ɵɵtextInterpolate(ctx_r1.localeService.moment.weekdaysShort(weekDay_r12)); } } function CalendarComponent_div_29_Template(rf, ctx) { if (rf & 1) { i0.ɵɵelementStart(0, "div", 34); i0.ɵɵelement(1, "span"); i0.ɵɵelementEnd(); } } function CalendarComponent_div_30_Template(rf, ctx) { if (rf & 1) { const _r13 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "div", 35); i0.ɵɵlistener("mouseover", function CalendarComponent_div_30_Template_div_mouseover_0_listener() { const dayIndex_r14 = i0.ɵɵrestoreView(_r13).$implicit; const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.hoverDay(dayIndex_r14, true)); })("mouseout", function CalendarComponent_div_30_Template_div_mouseout_0_listener() { i0.ɵɵrestoreView(_r13); const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.outDay()); })("click", function CalendarComponent_div_30_Template_div_click_0_listener() { const dayIndex_r14 = i0.ɵɵrestoreView(_r13).$implicit; const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.selectWeek(dayIndex_r14)); }); i0.ɵɵelementStart(1, "span"); i0.ɵɵtext(2); i0.ɵɵelementEnd()(); } if (rf & 2) { const dayIndex_r14 = ctx.$implicit; const ctx_r1 = i0.ɵɵnextContext(); i0.ɵɵclassProp("today", ctx_r1.isDayToday(dayIndex_r14))("theme-bg", ctx_r1.isDaySelected(dayIndex_r14) || ctx_r1.isDayRanged(dayIndex_r14))("range-start", ctx_r1.isRanged() && ctx_r1.isDaySelected(dayIndex_r14))("range-end", ctx_r1.isRanged() && ctx_r1.isDayRanged(dayIndex_r14))("theme-hover-highlight", ctx_r1.hoverWeek(dayIndex_r14))("theme-highlight", ctx_r1.isDayInRange(dayIndex_r14))("disabled", ctx_r1.isItemDisabled(ctx_r1.displayedDate.getFullYear(), ctx_r1.displayedDate.getMonth(), dayIndex_r14)); i0.ɵɵadvance(2); i0.ɵɵtextInterpolate(dayIndex_r14); } } function CalendarComponent_div_33_Template(rf, ctx) { if (rf & 1) { i0.ɵɵelementStart(0, "div", 34)(1, "span"); i0.ɵɵtext(2); i0.ɵɵelementEnd()(); } if (rf & 2) { const weekDay_r15 = ctx.$implicit; const ctx_r1 = i0.ɵɵnextContext(); i0.ɵɵadvance(2); i0.ɵɵtextInterpolate(ctx_r1.localeService.moment.weekdaysShort(weekDay_r15)); } } function CalendarComponent_div_35_Template(rf, ctx) { if (rf & 1) { i0.ɵɵelementStart(0, "div", 34); i0.ɵɵelement(1, "span"); i0.ɵɵelementEnd(); } } function CalendarComponent_div_36_Template(rf, ctx) { if (rf & 1) { const _r16 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "div", 35); i0.ɵɵlistener("mouseover", function CalendarComponent_div_36_Template_div_mouseover_0_listener() { const dayIndex_r17 = i0.ɵɵrestoreView(_r16).$implicit; const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.hoverDay(dayIndex_r17)); })("mouseout", function CalendarComponent_div_36_Template_div_mouseout_0_listener() { i0.ɵɵrestoreView(_r16); const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.outDay()); })("click", function CalendarComponent_div_36_Template_div_click_0_listener() { const dayIndex_r17 = i0.ɵɵrestoreView(_r16).$implicit; const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.selectDay(dayIndex_r17)); }); i0.ɵɵelementStart(1, "span"); i0.ɵɵtext(2); i0.ɵɵelementEnd()(); } if (rf & 2) { const dayIndex_r17 = ctx.$implicit; const ctx_r1 = i0.ɵɵnextContext(); i0.ɵɵclassProp("today", ctx_r1.isDayToday(dayIndex_r17))("theme-bg", ctx_r1.isDaySelected(dayIndex_r17) || ctx_r1.isDayRanged(dayIndex_r17))("range-start", ctx_r1.isRanged() && ctx_r1.isDaySelected(dayIndex_r17))("range-end", ctx_r1.isRanged() && ctx_r1.isDayRanged(dayIndex_r17))("theme-highlight", ctx_r1.isDayInRange(dayIndex_r17))("disabled", ctx_r1.isItemDisabled(ctx_r1.displayedDate.getFullYear(), ctx_r1.displayedDate.getMonth(), dayIndex_r17)); i0.ɵɵadvance(2); i0.ɵɵtextInterpolate(dayIndex_r17); } } /* eslint-disable @typescript-eslint/naming-convention */ var DateFormat; (function (DateFormat) { DateFormat["YEAR"] = "year"; DateFormat["QUARTER"] = "quarter"; DateFormat["MONTH"] = "month"; DateFormat["WEEK"] = "week"; DateFormat["DAY"] = "day"; })(DateFormat || (DateFormat = {})); class CalendarComponent { constructor(localeService, _changeDetector) { this.localeService = localeService; this._changeDetector = _changeDetector; this.dateFormat = DateFormat.MONTH; this.endDate = new Date(9999, 11, 31, 0, 0, 0, 0); this.startDate = new Date(1970, 0, 1, 0, 0, 0, 0); this.currentDateChange = new EventEmitter(); this.rangedDateChange = new EventEmitter(); this.displayedDateChange = new EventEmitter(); this.formattedDateChange = new EventEmitter(); this.isRangedSelectionActive = false; this.editedSection = DateFormat.MONTH; this.years = []; this.weekDays = [1, 2, 3, 4, 5, 6, 7]; this.today = new Date(); this.displayedDate = new Date(); this.quarters = [1, 2, 3, 4]; this.prevYearPageDisabled = true; this.nextYearPageDisabled = true; this.prevYearDisabled = true; this.nextYearDisabled = true; this.prevMonthDisabled = true; this.nextMonthDisabled = true; this._emptyByDefault = false; this._defaultValues = { currentDate: new Date(), dateFormat: DateFormat.MONTH, endDate: new Date(9999, 11, 31, 0, 0, 0, 0), startDate: new Date(1970, 0, 1, 0, 0, 0, 0) }; const date = new Date(); this.firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1); } ngOnInit() { ChangesUtil.resetWrongInputs({}, this._defaultValues, this); } ngAfterViewInit() { if (!this.isEmptyByDefault && !this.currentDate) { this.currentDate = new Date(); } if (!this.isEmptyByDefault && !this.rangedDate) { this.rangedDate = new Date(); } if (this.currentDate) { const currYear = this.currentDate.getFullYear(); const beginIntervalle = currYear - (currYear % 10); this.constructInterval(beginIntervalle, true); } this._changeDetector.detectChanges(); } ngOnChanges(changes) { ChangesUtil.resetWrongInputs(changes, this._defaultValues, this); if (changes.dateFormat) { this.editedSection = this.dateFormat; } if (changes.currentDate) { this.displayedDate = new Date(this.currentDate.getTime()); this.editedSection = this.dateFormat; } if (changes.startDate && changes.startDate.currentValue) { this.startDateYear = this.startDate.getFullYear(); } if (changes.endDate && changes.endDate.currentValue) { this.endDateYear = this.endDate.getFullYear(); } if (changes.currentDate || changes.startDate || changes.endDate) { const currYear = this.currentDate.getFullYear(); const beginIntervalle = currYear - (currYear % 10); this.constructInterval(beginIntervalle, true); this._refreshDisplayedDate(); } if (changes.emitFormat && changes.emitFormat.isFirstChange) { this.emitFormattedDate(); } } isDisabled() { return typeof this.disabled !== 'undefined' && this.disabled !== false; } isEmptyByDefault() { return typeof this.emptyByDefault !== 'undefined' && this.emptyByDefault !== false; } isRanged() { if (this.dateFormat === DateFormat.WEEK) { return true; } return typeof this.ranged !== 'undefined' && this.ranged !== false; } isQuarterDisplayed() { return typeof this.displayQuarters !== 'undefined' && this.displayQuarters !== false; } isTodayHighlighted() { return typeof this.highlightToday !== 'undefined' && this.highlightToday !== false; } isOneLineHeader() { return typeof this.oneLineHeader !== 'undefined' && this.oneLineHeader !== false; } getDaysInMonth() { const nbDays = new Date(this.displayedDate.getFullYear(), this.displayedDate.getMonth() + 1, 0).getDate(); this.daysInMonth = Array.apply(null, { length: nbDays }).map((x, i) => i + 1); } getSelectedQuarter() { return Math.floor((this.displayedDate.getMonth() + 3) / 3); } getWeekIndexes(dayIndex) { const date = new Date(this.displayedDate.getFullYear(), this.displayedDate.getMonth(), dayIndex); const lastDayOfMonth = new Date(this.displayedDate.getFullYear(), this.displayedDate.getMonth() + 1, 0).getDate(); const firstDayIndex = this.getDayIndex(); const dateWeekIndex = this.getDayIndex(date); const startWeekIndex = dayIndex - dateWeekIndex; const endWeekIndex = dayIndex + 6 - dateWeekIndex; const weekIndexes = [ startWeekIndex === 0 ? -firstDayIndex + 1 : startWeekIndex, endWeekIndex > lastDayOfMonth ? endWeekIndex - lastDayOfMonth : endWeekIndex ]; return weekIndexes; } getDayIndex(day) { const date = day ? day : this.firstDayOfMonth; let weekDay = date.getDay() - 1; if (weekDay < 0) { weekDay = 6; } return weekDay; } getDummyDayArray() { this.dummyDays = []; for (let i = 0; i < this.getDayIndex(); i++) { this.dummyDays.push({}); } } isItemDisabled(yearIndex, monthIndex, dayIndex) { let precision = 'day'; if (typeof (monthIndex) === 'undefined') { precision = 'year'; monthIndex = this.currentDate.getMonth(); } if (typeof (dayIndex) === 'undefined') { precision = 'month'; dayIndex = 1; } const tempCurrentDate = new Date(yearIndex, monthIndex, dayIndex); const tempStartDate = new Date(this.startDate.getFullYear(), this.startDate.getMonth(), this.startDate.getDate()); const tempEndDate = new Date(this.endDate.getFullYear(), this.endDate.getMonth(), this.endDate.getDate()); switch (precision) { case 'year': return this.startDate.getFullYear() > yearIndex || yearIndex > this.endDate.getFullYear(); case 'month': tempCurrentDate.setDate(1); tempStartDate.setDate(1); tempEndDate.setDate(1); return tempCurrentDate > tempEndDate || tempCurrentDate < tempStartDate; case 'day': return tempCurrentDate > tempEndDate || tempCurrentDate < tempStartDate; } return false; } isYearToday(yearIndex) { if (this.isTodayHighlighted() && this.years[yearIndex] === this.today.getFullYear()) { return true; } return false; } isYearSelected(yearIndex) { return this.years[yearIndex] === this.displayedDate.getFullYear(); } isMonthToday(monthIndex) { if (this.isTodayHighlighted() && this.today.getFullYear() === this.displayedDate.getFullYear() && this.today.getMonth() === monthIndex) { return true; } return false; } isMonthSelected(monthIndex) { if (this.dateFormat === DateFormat.QUARTER) { return monthIndex >= (this.getSelectedQuarter() - 1) * 3 && monthIndex < (this.getSelectedQuarter() * 3); } if (!!this.currentDate && this.currentDate.getFullYear() === this.displayedDate.getFullYear() && this.currentDate.getMonth() === monthIndex) { return true; } return false; } isMonthRanged(monthIndex) { if (this.isRanged() && !!this.rangedDate && this.dateFormat === 'month' && this.rangedDate.getFullYear() === this.displayedDate.getFullYear() && this.rangedDate.getMonth() === monthIndex) { return true; } return false; } isMonthInRange(monthIndex) { if (this.isRanged() && this.dateFormat === 'month') { const tmpDate = new Date(this.displayedDate.getFullYear(), monthIndex, 1); const tmpCleanCurrentDate = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth(), 1, 0, 0, 0, 0); const tmpCleanRangedDate = new Date(this.rangedDate.getFullYear(), this.rangedDate.getMonth(), 1, 0, 0, 0, 0); if (tmpDate > tmpCleanCurrentDate && tmpDate < tmpCleanRangedDate) { return true; } if (tmpDate > tmpCleanCurrentDate && tmpDate < this.hoveredDate) {