angular-l10n
Version:
An Angular library to translate messages, dates and numbers
272 lines • 7.9 kB
JavaScript
/**
* @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
*/
var BaseDirective = /** @class */ (function () {
function BaseDirective(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 {?}
*/
BaseDirective.prototype.ngAfterViewInit = /**
* @return {?}
*/
function () {
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 {?}
*/
BaseDirective.prototype.ngOnChanges = /**
* @param {?} changes
* @return {?}
*/
function (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 {?}
*/
BaseDirective.prototype.ngOnDestroy = /**
* @return {?}
*/
function () {
this.destroy.next(true);
this.removeTextListener();
};
/**
* @return {?}
*/
BaseDirective.prototype.getAttributesData = /**
* @return {?}
*/
function () {
/** @type {?} */
var keys = this.getAttributesKeys();
/** @type {?} */
var data = {};
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
var key = keys_1[_i];
data[key] = this.getValue(key);
}
return data;
};
/**
* @return {?}
*/
BaseDirective.prototype.getAttributesKeys = /**
* @return {?}
*/
function () {
return this.attributes.map(function (attr) { return attr.key; });
};
/**
* @param {?} value
* @return {?}
*/
BaseDirective.prototype.setText = /**
* @param {?} value
* @return {?}
*/
function (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 {?}
*/
BaseDirective.prototype.setAttributes = /**
* @param {?} data
* @return {?}
*/
function (data) {
for (var _i = 0, _a = this.attributes; _i < _a.length; _i++) {
var attr = _a[_i];
this.renderer.setAttribute(this.element, attr.name, data[attr.key]);
}
};
/**
* @return {?}
*/
BaseDirective.prototype.addTextListener = /**
* @return {?}
*/
function () {
var _this = this;
if (typeof MutationObserver !== "undefined") {
this.textObserver = new MutationObserver(function (mutations) {
_this.renderNode = BFS.getTargetNode(_this.element);
_this.getKey();
_this.replaceText();
});
this.textObserver.observe(this.renderNode, this.TEXT_MUTATION_CONFIG);
}
};
/**
* @return {?}
*/
BaseDirective.prototype.removeTextListener = /**
* @return {?}
*/
function () {
if (typeof this.textObserver !== "undefined") {
this.textObserver.disconnect();
}
};
/**
* @return {?}
*/
BaseDirective.prototype.getText = /**
* @return {?}
*/
function () {
this.nodeValue = this.renderNode != null ? (/** @type {?} */ (this.renderNode.nodeValue)) : "";
return !!this.nodeValue ? this.nodeValue.trim() : "";
};
/**
* @return {?}
*/
BaseDirective.prototype.getKey = /**
* @return {?}
*/
function () {
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 {?}
*/
BaseDirective.prototype.getAttributes = /**
* @return {?}
*/
function () {
if (this.element.attributes) {
for (var _i = 0, _a = this.element.attributes; _i < _a.length; _i++) {
var attr = _a[_i];
if (attr && this.SELECTOR.test(attr.name)) {
/** @type {?} */
var name_1 = attr.name.substr(5);
for (var _b = 0, _c = this.element.attributes; _b < _c.length; _b++) {
var targetAttr = _c[_b];
if (new RegExp("^" + name_1 + "$").test(targetAttr.name)) {
this.attributes.push({ name: name_1, key: targetAttr.value });
}
}
}
}
}
};
BaseDirective.propDecorators = {
valueAttribute: [{ type: Input, args: ['value',] }],
innerHTMLProperty: [{ type: Input, args: ['innerHTML',] }]
};
return BaseDirective;
}());
export { BaseDirective };
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