UNPKG

angular-l10n

Version:

An Angular library to translate messages, dates and numbers

230 lines 6.58 kB
/** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ import { Input } from '@angular/core'; import { Subject } from 'rxjs'; import { BFS } from './bfs'; /** * @abstract */ export class BaseDirective { /** * @param {?} el * @param {?} renderer */ constructor(el, renderer) { this.el = el; this.renderer = renderer; this.attributes = []; this.destroy = new Subject(); this.TEXT_MUTATION_CONFIG = { subtree: true, characterData: true }; this.SELECTOR = /^l10n-/; } /** * @return {?} */ ngAfterViewInit() { if (this.el && this.el.nativeElement) { this.element = this.el.nativeElement; this.renderNode = BFS.getTargetNode(this.element); this.getKey(); this.getAttributes(); this.addTextListener(); this.setup(); } } /** * @param {?} changes * @return {?} */ ngOnChanges(changes) { if (!!this.key) { if (this.nodeValue == null || this.nodeValue == "") { if (!!this.valueAttribute) { this.key = this.valueAttribute; } else if (!!this.innerHTMLProperty) { this.key = this.innerHTMLProperty; } } this.replaceText(); } if (this.attributes.length > 0) { this.replaceAttributes(); } } /** * @return {?} */ ngOnDestroy() { this.destroy.next(true); this.removeTextListener(); } /** * @return {?} */ getAttributesData() { /** @type {?} */ const keys = this.getAttributesKeys(); /** @type {?} */ const data = {}; for (const key of keys) { data[key] = this.getValue(key); } return data; } /** * @return {?} */ getAttributesKeys() { return this.attributes.map((attr) => attr.key); } /** * @param {?} value * @return {?} */ setText(value) { if (!!value) { if (!!this.nodeValue && !!this.key) { this.removeTextListener(); this.renderer.setValue(this.renderNode, this.nodeValue.replace(this.key, value)); this.addTextListener(); } else if (!!this.valueAttribute) { this.renderer.setAttribute(this.element, "value", value); } else if (!!this.innerHTMLProperty) { this.renderer.setProperty(this.element, "innerHTML", value); } } } /** * @param {?} data * @return {?} */ setAttributes(data) { for (const attr of this.attributes) { this.renderer.setAttribute(this.element, attr.name, data[attr.key]); } } /** * @return {?} */ addTextListener() { if (typeof MutationObserver !== "undefined") { this.textObserver = new MutationObserver((mutations) => { this.renderNode = BFS.getTargetNode(this.element); this.getKey(); this.replaceText(); }); this.textObserver.observe(this.renderNode, this.TEXT_MUTATION_CONFIG); } } /** * @return {?} */ removeTextListener() { if (typeof this.textObserver !== "undefined") { this.textObserver.disconnect(); } } /** * @return {?} */ getText() { this.nodeValue = this.renderNode != null ? (/** @type {?} */ (this.renderNode.nodeValue)) : ""; return !!this.nodeValue ? this.nodeValue.trim() : ""; } /** * @return {?} */ getKey() { if (this.element.childNodes.length > 0) { this.key = this.getText(); } else if (!!this.valueAttribute) { this.key = this.valueAttribute; } else if (!!this.innerHTMLProperty) { this.key = this.innerHTMLProperty; } } /** * @return {?} */ getAttributes() { if (this.element.attributes) { for (const attr of this.element.attributes) { if (attr && this.SELECTOR.test(attr.name)) { /** @type {?} */ const name = attr.name.substr(5); for (const targetAttr of this.element.attributes) { if (new RegExp("^" + name + "$").test(targetAttr.name)) { this.attributes.push({ name: name, key: targetAttr.value }); } } } } } } } BaseDirective.propDecorators = { valueAttribute: [{ type: Input, args: ['value',] }], innerHTMLProperty: [{ type: Input, args: ['innerHTML',] }] }; if (false) { /** @type {?} */ BaseDirective.prototype.valueAttribute; /** @type {?} */ BaseDirective.prototype.innerHTMLProperty; /** @type {?} */ BaseDirective.prototype.key; /** @type {?} */ BaseDirective.prototype.attributes; /** @type {?} */ BaseDirective.prototype.destroy; /** @type {?} */ BaseDirective.prototype.element; /** @type {?} */ BaseDirective.prototype.renderNode; /** @type {?} */ BaseDirective.prototype.nodeValue; /** @type {?} */ BaseDirective.prototype.textObserver; /** @type {?} */ BaseDirective.prototype.TEXT_MUTATION_CONFIG; /** @type {?} */ BaseDirective.prototype.SELECTOR; /** @type {?} */ BaseDirective.prototype.el; /** @type {?} */ BaseDirective.prototype.renderer; /** * @abstract * @return {?} */ BaseDirective.prototype.setup = function () { }; /** * @abstract * @return {?} */ BaseDirective.prototype.replace = function () { }; /** * @abstract * @return {?} */ BaseDirective.prototype.replaceText = function () { }; /** * @abstract * @return {?} */ BaseDirective.prototype.replaceAttributes = function () { }; /** * @abstract * @param {?} key * @return {?} */ BaseDirective.prototype.getValue = function (key) { }; } //# sourceMappingURL=base-directive.js.map