UNPKG

@lunaeme/circe-core

Version:
834 lines (810 loc) 27.4 kB
import { __decorate } from 'tslib'; import { Injectable, Pipe, NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { startCase, kebabCase, camelCase } from 'lodash'; import { v4 } from 'uuid'; import { DomSanitizer } from '@angular/platform-browser'; import { OrderPipe, OrderModule } from 'ngx-order-pipe'; import { Subject } from 'rxjs'; // ############################################# // ###### OPTION CONSTANTS DEFINITION API ###### // ############################################# const ccStringTransform = { start: 'start', camel: 'camel', kebab: 'kebab' }; let ToolService = class ToolService { constructor() { this.months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; } static getValueFromMultiLevelObject(object, key, separator) { const _separator = separator || '.'; if (object[key] !== undefined) { return object[key]; } try { return key.split(_separator).reduce((obj, index) => { return obj[index]; }, object); } catch (e) { if (e instanceof TypeError) { return undefined; } else { throw e; } } } static setValueInMultiLevelObject(object, key, value, separator) { const _separator = separator || '.'; return key.split(_separator).reduce((o, i) => { if (o && typeof o[i] === 'object') { return o[i]; } if (o && i in o) { o[i] = value; return o; } }, object); } static waitFor(milliseconds) { const _now = Date.now(); let _timeOut = false; do { _timeOut = (Date.now() - _now >= milliseconds); } while (!_timeOut); } /** * repeatedValuesInArray * @description * This method returns an array of uniques values if parameter "unique" is true; or returns * an array of ONLY repeated values (unique values are discarded) if unique is false. * Default value por parameter unique is true. */ static repeatedValuesInArray(values, unique) { const _unique = (unique === undefined) ? true : unique; return (_unique) ? Array.from(new Set(values)) : values.filter((e, i) => values.indexOf(e) !== i); } static checkLastChar(text, char) { if (text && text[text.length - 1] !== char) { return `${text}${char}`; } return text; } static hexToRgb(hex) { const _output = hex .replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, (m, r, g, b) => '#' + r + r + g + g + b + b) .substring(1) .match(/.{2}/g); return _output.map((e) => parseInt(e, 16)); } static rgbToHex(r, g, b) { const _output = [r, g, b].map((e) => { const _hex = e.toString(16); return (_hex.length === 1) ? `0${_hex}` : _hex; }); return _output.join(''); } /** * generateUuid * @description * Generates a new uuid using uuid dependency. */ static generateUuid() { return v4(); } /** * checkArray * @description * Returns true if parameter given "array" is an array, otherwise returns false; * If optional parameter "filled" is given, then this method checks the array is not empty. * Default value for "filled" is true. */ static checkArray(array, filled = true) { const _checkStructure = (!!array && Array.isArray(array)); if (filled) { return (_checkStructure && !!array.length); } return _checkStructure; } /** * @deprecated */ static getValueFromDotedKey(object, dotedKey, separator) { const _separator = separator || '.'; if (object[dotedKey] !== undefined) { return object[dotedKey]; } try { return dotedKey.split(_separator).reduce((obj, index) => { return obj[index]; }, object); } catch (e) { if (e instanceof TypeError) { return undefined; } else { throw e; } } } /** * @deprecated */ static formatString(text) { if (isNaN(Number(text))) { return startCase(text); } else { return text; } } identifier(index, item) { let _output = (typeof item === 'string') ? item : index; ['code', 'id', 'param', 'key'].forEach((e) => { if (item.hasOwnProperty(e)) { _output = item[e]; return; } }); return _output; } stringTransform(text, transformType) { const _transformType = transformType || ccStringTransform.start; let _output = text; if (isNaN(Number(text))) { switch (_transformType) { case ccStringTransform.start: _output = startCase(text); break; case ccStringTransform.camel: _output = camelCase(text); break; case ccStringTransform.kebab: _output = kebabCase(text); break; } } return _output; } }; ToolService = __decorate([ Injectable() ], ToolService); // ############################################### // ###### POSITION CONSTANTS DEFINITION API ###### // ############################################### const ccHashTypes = { class: 'class', tag: 'tag', id: 'id' }; const ccElementFields = { name: 'name', type: 'type', query: 'query', shadowElement: 'shadowElement' }; var BoxModelService_1; const boxModelTypeConstants = { HORIZONTAL: 'horizontal', VERTICAL: 'vertical' }; let BoxModelService = BoxModelService_1 = class BoxModelService { constructor() { this._defaultBoxModelType = boxModelTypeConstants.VERTICAL; this._defaultElementHashType = ccHashTypes.class; this._defaultComputedStylePropertyProcessed = false; this._allowCssUnits = ['px', '%']; this._additionHorizontalInsideClasses = ['padding-left', 'padding-right', 'border-left-width', 'border-right-width']; this._additionHorizontalOutsideClasses = ['margin-left', 'margin-right']; this._additionVerticalInsideClasses = ['padding-top', 'padding-bottom', 'border-top-width', 'border-bottom-width']; this._additionVerticalOutsideClasses = ['margin-top', 'margin-bottom']; this._fontSizeRule = { applyOnElements: ['i'], boxModelType: boxModelTypeConstants.HORIZONTAL }; } /** * _isElementHash * * @description * Checks if element given param is ElementHash type. */ static _isElementHash(element) { return !!(typeof element === 'object' && ccElementFields.type in element && ccElementFields.name in element && (Object.keys(element).length === 2 || Object.keys(element).length === 3)); } /** * _isElementQuery * * @description * Checks if element given param is ElementQuery type. */ static _isElementQuery(element) { return !!(typeof element === 'object' && ccElementFields.query in element && (Object.keys(element).length === 1 || Object.keys(element).length === 2)); } /** * _isNativeDomElement * * @description * Checks if element given param is a native DOM element. */ static _isNativeDomElement(param) { const _paramsToCheck = [ 'getBoundingClientRect', 'getElementsByClassName', 'getElementsByTagName', 'querySelector' ]; return (!!param) ? (_paramsToCheck.map((e) => e in param)).every((el) => !!el) : false; } /** * _convertToElementIdArray * * @description * Transform elementId type to array of Dom elements. */ _convertToElementIdArray(elementId) { const _auxArgument = []; const _elementId = (Array.isArray(elementId)) ? elementId : [elementId]; for (const element of _elementId) { _auxArgument.push(this.getElement(element)); } return _auxArgument; } _getElementAdditions(element, boxModelType) { const _type = boxModelType || this._defaultBoxModelType; const _elementStyle = window.getComputedStyle(element); const _output = { boxModelAdditionInside: 0, boxModelAdditionOutside: 0 }; const _auxOutputInside = []; const _auxOutputOutside = []; if (_type === boxModelTypeConstants.HORIZONTAL) { this._additionHorizontalInsideClasses.forEach((e) => { _auxOutputInside.push(_elementStyle.getPropertyValue(e)); }); this._additionHorizontalOutsideClasses.forEach((e) => { _auxOutputOutside.push(_elementStyle.getPropertyValue(e)); }); } else { this._additionVerticalInsideClasses.forEach((e) => { _auxOutputInside.push(_elementStyle.getPropertyValue(e)); }); this._additionVerticalOutsideClasses.forEach((e) => { _auxOutputOutside.push(_elementStyle.getPropertyValue(e)); }); } _auxOutputInside.forEach((el) => { if (el !== '0px') { _output.boxModelAdditionInside += this.readCssUnits(el).value; } }); _auxOutputOutside.forEach((el) => { if (el !== '0px') { _output.boxModelAdditionOutside += this.readCssUnits(el).value; } }); return _output; } getComputedStyleProperty(element, property, processed) { const _processed = processed || this._defaultComputedStylePropertyProcessed; const _elementComputedStyles = window.getComputedStyle(element); const _output = _elementComputedStyles.getPropertyValue(property); if (_output && _processed) { return this.readCssUnits(_output); } return _output; } processElementForSpecialRules(element) { if (this._fontSizeRule.applyOnElements.includes(element.tagName.toLowerCase())) { const _elementBoxModel = this.getBoxModel(element, boxModelTypeConstants.HORIZONTAL); const _elementFontSize = this.getComputedStyleProperty(element, 'font-size', true); if (_elementBoxModel.boxModelExtractedInside !== _elementFontSize.value) { element.style.width = `${_elementFontSize.value}px`; return element; } } return element; } readCssUnits(expression) { const _output = { value: 0, unit: '' }; this._allowCssUnits.forEach((e) => { if (expression.includes(e)) { _output.unit = e; const _aux = expression.split(e).filter((el) => !!el); if (_aux.length === 1) { _output.value = Number(_aux[0]); } return; } }); return (_output.value && _output.unit) ? _output : null; } processSizeString(sizeString) { const _output = { with: null, height: null }; if (sizeString) { const _auxSize = sizeString.split(' '); if (_auxSize.length === 1) { const _cssUnit = this.readCssUnits(_auxSize[0]); _output.with = _cssUnit; _output.height = _cssUnit; } else if (_auxSize.length === 2) { _output.height = this.readCssUnits(_auxSize[0]); _output.with = this.readCssUnits(_auxSize[1]); } } return (_output.with && _output.height) ? _output : null; } /** * getElement * * @description * Returns an element DOM native object from different types of given params. */ getElement(element) { let _output = element; let _element = element; let _shadowElement = document; if (typeof element === 'string') { _element = { type: this._defaultElementHashType, name: element }; } else { if (ccElementFields.shadowElement in _element) { _shadowElement = this.getElement(_element.shadowElement); } } if (!BoxModelService_1._isNativeDomElement(_element)) { if (BoxModelService_1._isElementHash(_element)) { switch (_element.type) { case ccHashTypes.class: _output = _shadowElement.getElementsByClassName(_element.name).item(0); break; case ccHashTypes.tag: _output = _shadowElement.getElementsByTagName(_element.name).item(0); break; case ccHashTypes.id: _output = document.getElementById(_element.name); break; } } else if (BoxModelService_1._isElementQuery(_element)) { _output = _shadowElement.querySelector(_element.query); } } if (!BoxModelService_1._isNativeDomElement(_output)) { throw new Error('BoxModel.getElement: Unrecognizable element.'); } return _output; } getBoxModel(elementId, boxModelType) { const _output = { type: boxModelType || this._defaultBoxModelType, boxModel: 0, boxModelAdditions: 0, boxModelAggregated: 0, boxModelExtracted: 0, boxModelAdditionsInside: 0, boxModelAdditionsOutside: 0, boxModelAggregatedInside: 0, boxModelAggregatedOutside: 0, boxModelExtractedInside: 0, boxModelExtractedOutside: 0 }; const _elementId = this._convertToElementIdArray(elementId); _elementId.forEach((e) => { const _elementRect = e.getBoundingClientRect(); const _additions = this._getElementAdditions(e, _output.type); _output.boxModel += (_output.type === boxModelTypeConstants.HORIZONTAL) ? _elementRect.width : _elementRect.height; _output.boxModelAdditions += _additions.boxModelAdditionInside + _additions.boxModelAdditionOutside; _output.boxModelAdditionsInside += _additions.boxModelAdditionInside; _output.boxModelAdditionsOutside += _additions.boxModelAdditionOutside; }); _output.boxModelAggregated = _output.boxModel + _output.boxModelAdditions; _output.boxModelExtracted = _output.boxModel - _output.boxModelAdditions; _output.boxModelAggregatedInside = _output.boxModel + _output.boxModelAdditionsInside; _output.boxModelAggregatedOutside = _output.boxModel + _output.boxModelAdditionsOutside; _output.boxModelExtractedInside = _output.boxModel - _output.boxModelAdditionsInside; _output.boxModelExtractedOutside = _output.boxModel - _output.boxModelAdditionsOutside; return _output; } }; BoxModelService = BoxModelService_1 = __decorate([ Injectable() ], BoxModelService); let EventsService = class EventsService { constructor(_bm) { this._bm = _bm; this._defaultEventImmediatePropagation = true; this._defaultSelectDomElement = { type: ccHashTypes.tag, name: 'main' }; } preventNeededEvent(event, immediatePropagation) { const _immediate = immediatePropagation || this._defaultEventImmediatePropagation; if (_immediate) { event.stopImmediatePropagation(); } else { event.stopPropagation(); } } preventNoNeededEvent(event, immediatePropagation) { const _immediate = immediatePropagation || this._defaultEventImmediatePropagation; this.preventNeededEvent(event, _immediate); if (event.cancelable) { event.preventDefault(); } } scrollTop(element) { const _element = element || this._defaultSelectDomElement; const _target = this._bm.getElement(_element); if (_target) { _target.scroll(0, 0); } } /** * @deprecated */ preventMouseEvent(event, immediatePropagation) { const _immediate = immediatePropagation || this._defaultEventImmediatePropagation; if (_immediate) { event.stopImmediatePropagation(); } else { event.stopPropagation(); } event.preventDefault(); } /** * @deprecated */ preventKeyEvent(event, immediatePropagation) { const _immediate = immediatePropagation || this._defaultEventImmediatePropagation; if (_immediate) { event.stopImmediatePropagation(); } else { event.stopPropagation(); } } }; EventsService.ctorParameters = () => [ { type: BoxModelService } ]; EventsService = __decorate([ Injectable() ], EventsService); let CustomValidationService = class CustomValidationService { constructor() { this._customMessages = { repeated: 'There can not be repeated values' }; } static formArrayRepeatedValues(control) { let _output = null; const _repeatedValues = ToolService.repeatedValuesInArray(control.value); if (_repeatedValues && Array.isArray(_repeatedValues) && _repeatedValues.length) { _output = { repeated: { repeatedValues: _repeatedValues.join(', ') } }; } return _output; } getCustomMessages(keys) { const _keys = (keys && typeof keys === 'string') ? [keys] : keys; const _output = {}; _keys.forEach((e) => { _output[e] = this._customMessages[e]; }); return _output; } }; CustomValidationService = __decorate([ Injectable() ], CustomValidationService); let SanitizeHtmlPipe = class SanitizeHtmlPipe { constructor(_sanitizer) { this._sanitizer = _sanitizer; } transform(value) { return this._sanitizer.bypassSecurityTrustHtml(value); } }; SanitizeHtmlPipe.ctorParameters = () => [ { type: DomSanitizer } ]; SanitizeHtmlPipe = __decorate([ Pipe({ name: 'sanitizeHtml' }) ], SanitizeHtmlPipe); let SanitizeHtmlModule = class SanitizeHtmlModule { }; SanitizeHtmlModule = __decorate([ NgModule({ exports: [SanitizeHtmlPipe], declarations: [SanitizeHtmlPipe], imports: [CommonModule] }) ], SanitizeHtmlModule); let OrderConditionPipe = class OrderConditionPipe { constructor(_o) { this._o = _o; } transform(value, orderBy, condition, reverse, caseSensitive) { const _condition = (condition === undefined) ? true : !!condition; return (_condition) ? this._o.transform(value, orderBy, !!reverse, !!caseSensitive) : value; } }; OrderConditionPipe.ctorParameters = () => [ { type: OrderPipe } ]; OrderConditionPipe = __decorate([ Pipe({ name: 'orderCondition' }) ], OrderConditionPipe); let OrderConditionModule = class OrderConditionModule { }; OrderConditionModule = __decorate([ NgModule({ exports: [OrderConditionPipe], declarations: [OrderConditionPipe], imports: [ CommonModule, OrderModule ], providers: [OrderConditionPipe] }) ], OrderConditionModule); var CoreModule_1; let CoreModule = CoreModule_1 = class CoreModule { static forChild() { return { ngModule: CoreModule_1, providers: [ BoxModelService, EventsService, CustomValidationService, ToolService ] }; } }; CoreModule = CoreModule_1 = __decorate([ NgModule({ exports: [ OrderConditionPipe, SanitizeHtmlPipe ], imports: [ CommonModule, OrderConditionModule, SanitizeHtmlModule ] }) ], CoreModule); var BoxModelModule_1; let BoxModelModule = BoxModelModule_1 = class BoxModelModule { static forChild() { return { ngModule: BoxModelModule_1, providers: [BoxModelService] }; } }; BoxModelModule = BoxModelModule_1 = __decorate([ NgModule({ imports: [CommonModule] }) ], BoxModelModule); var CustomValidationModule_1; let CustomValidationModule = CustomValidationModule_1 = class CustomValidationModule { static forChild() { return { ngModule: CustomValidationModule_1, providers: [CustomValidationService] }; } }; CustomValidationModule = CustomValidationModule_1 = __decorate([ NgModule({ imports: [CommonModule] }) ], CustomValidationModule); var EventsModule_1; let EventsModule = EventsModule_1 = class EventsModule { static forChild() { return { ngModule: EventsModule_1, providers: [EventsService] }; } }; EventsModule = EventsModule_1 = __decorate([ NgModule({ imports: [ CommonModule, BoxModelModule.forChild() ] }) ], EventsModule); var ToolModule_1; let ToolModule = ToolModule_1 = class ToolModule { static forChild() { return { ngModule: ToolModule_1, providers: [ToolService] }; } }; ToolModule = ToolModule_1 = __decorate([ NgModule({ imports: [CommonModule] }) ], ToolModule); // ##################################################### // ###### PRIVATE TYPES, INTERFACES AND CONSTANTS ###### // ##################################################### // ############################################# // ###### OPTION CONSTANTS DEFINITION API ###### // ############################################# const ccOptionFields = { value: 'value', label: 'label', color: 'color', icon: 'icon' }; // ############################################### // ###### POSITION CONSTANTS DEFINITION API ###### // ############################################### const ccPositions = { topLeft: 'top left', topCenter: 'top center', topRight: 'top right', centerLeft: 'center left', centerCenter: 'center center', centerRight: 'center right', bottomLeft: 'bottom left', bottomCenter: 'bottom center', bottomRight: 'bottom right' }; const ccPositionsVertical = { top: 'top', center: 'center', bottom: 'bottom' }; const ccPositionsHorizontal = { left: 'left', center: 'center', right: 'right' }; // ############################################# // ###### STATUS CONSTANTS DEFINITION API ###### // ############################################# const ccStatuses = { info: 'info', success: 'success', warning: 'warning', critical: 'critical' }; const ɵ0 = () => { }, ɵ1 = () => { }; const defaultConfig = { timeShowing: 400, timeToStop: 400, callbackShow: ɵ0, callbackStop: ɵ1 }; class AsynchronousPairVariables { constructor(config = defaultConfig) { this.trigger$ = new Subject(); this.show$ = new Subject(); this._startArguments = []; this._stopArguments = []; this._config = {}; if (!!config) { this._config.timeShowing = (!!config.timeShowing) ? config.timeShowing : defaultConfig.timeShowing; this._config.timeToStop = (!!config.timeToStop) ? config.timeToStop : defaultConfig.timeToStop; this._config.callbackShow = (!!config.callbackShow) ? config.callbackShow : defaultConfig.callbackShow; this._config.callbackStop = config.callbackStop; if (!!!this._config.callbackStop) { this._config.callbackStop = (!!config.callbackShow) ? config.callbackShow : defaultConfig.callbackStop; } } } set _trigger(state) { this.trigger$.next(state); if (state) { setTimeout(() => { this._config.callbackShow(...this._startArguments); this._show = true; this._timeoutShow = setTimeout(() => { this._show = false; }, this._config.timeShowing); }); } } set _show(state) { this.show$.next(state); if (!state) { setTimeout(() => { this._config.callbackStop(...this._stopArguments); this._trigger = false; }, this._config.timeToStop); } } _setStopArguments(...arg) { this._stopArguments = arg; } setConfig(config) { if (!!config.timeShowing) { this._config.timeShowing = config.timeShowing; } if (!!config.timeToStop) { this._config.timeToStop = config.timeToStop; } if (!!config.callbackShow) { this._config.callbackShow = config.callbackShow; } if (!!config.callbackStop) { this._config.callbackStop = config.callbackStop; } } start(...arg) { clearTimeout(this._timeoutShow); this._startArguments = arg; this._trigger = true; return this._setStopArguments.bind(this); } stop() { clearTimeout(this._timeoutShow); } remove() { this.stop(); this._show = false; } } const iconsClassBaseDefault = 'mda-icon'; class Icons { constructor(classBase = iconsClassBaseDefault) { this.classBase = classBase; this.defaultKeys = Object.assign(Object.assign({}, ccStatuses), { close: 'close' }); this._defaults = { info: 'icon-info', success: 'icon-circle-check', warning: 'icon-alert', critical: 'icon-shield', close: 'icon-cross' }; this.ccIconSetFields = { iconLeft: 'iconLeft', iconRight: 'iconRight' }; this.ccIconFields = { classes: 'classes', color: 'color' }; } _applyClassBase(icon) { return (!!this.classBase) ? `${this.classBase} ${icon}` : icon; } get(defaultKey) { const _icon = this._defaults[defaultKey]; if (!!_icon) { return this._applyClassBase(_icon); } return ''; } getCustom(icon) { if (!!icon) { return this._applyClassBase(icon); } return ''; } } /* * Public API Surface of core */ /** * Generated bundle index. Do not edit. */ export { AsynchronousPairVariables, BoxModelModule, BoxModelService, CoreModule, CustomValidationModule, CustomValidationService, EventsModule, EventsService, Icons, OrderConditionModule, OrderConditionPipe, SanitizeHtmlModule, SanitizeHtmlPipe, ToolModule, ToolService, boxModelTypeConstants, ccElementFields, ccHashTypes, ccOptionFields, ccPositions, ccPositionsHorizontal, ccPositionsVertical, ccStatuses, ccStringTransform, ɵ0, ɵ1 }; //# sourceMappingURL=lunaeme-circe-core.js.map