UNPKG

@syncfusion/ej2-ng-base

Version:

A common package of Essential JS 2 base Angular libraries, methods and class definitions

207 lines (206 loc) 8.25 kB
/** * Angular Component Base Module */ import { getValue, isUndefined, setValue, isNullOrUndefined, attributes, createElement } from '@syncfusion/ej2-base'; import { EventEmitter } from '@angular/core'; import { clearTemplate } from './util'; var SVG_REG = /^svg|^path|^g/; var ComponentBase = /** @class */ (function () { function ComponentBase() { this.isProtectedOnChange = true; } ComponentBase.prototype.saveChanges = function (key, newValue, oldValue) { if (this.isProtectedOnChange) { return; } this.oldProperties[key] = oldValue; this.changedProperties[key] = newValue; this.finalUpdate(); // tslint:disable-next-line:no-any var changeTime = setTimeout(this.dataBind.bind(this)); var clearUpdate = function () { clearTimeout(changeTime); }; this.finalUpdate = clearUpdate; }; ; ComponentBase.prototype.ngOnInit = function () { var _this = this; this.registeredTemplate = {}; this.ngBoundedEvents = {}; this.isAngular = true; this.tags = this.tags || []; this.complexTemplate = this.complexTemplate || []; this.tagObjects = []; this.ngAttr = this.getAngularAttr(this.element); /* istanbul ignore next */ this.createElement = function (tagName, prop) { //tslint:disable-next-line var ele = _this.srenderer ? _this.srenderer.createElement(tagName) : createElement(tagName); if (typeof (prop) === 'undefined') { return ele; } ele.innerHTML = (prop.innerHTML ? prop.innerHTML : ''); if (prop.className !== undefined) { ele.className = prop.className; } if (prop.id !== undefined) { ele.id = prop.id; } if (prop.styles !== undefined) { ele.setAttribute('style', prop.styles); } if (_this.ngAttr !== undefined) { ele.setAttribute(_this.ngAttr, ''); } if (prop.attrs !== undefined) { attributes(ele, prop.attrs); } return ele; }; for (var _i = 0, _a = this.tags; _i < _a.length; _i++) { var tag = _a[_i]; var tagObject = { instance: getValue('child' + tag.substring(0, 1).toUpperCase() + tag.substring(1), this), name: tag }; this.tagObjects.push(tagObject); } var complexTemplates = Object.keys(this); complexTemplates = complexTemplates.filter(function (val) { return /Ref$/i.test(val) && /\_/i.test(val); }); for (var _b = 0, complexTemplates_1 = complexTemplates; _b < complexTemplates_1.length; _b++) { var tempName = complexTemplates_1[_b]; var propName = tempName.replace('Ref', ''); var val = {}; setValue(propName.replace('_', '.'), getValue(propName, this), val); this.setProperties(val, true); } }; ComponentBase.prototype.getAngularAttr = function (ele) { var attributes = ele.attributes; var length = attributes.length; var ngAr; for (var i = 0; i < length; i++) { if (/_ngcontent/g.test(attributes[i].name)) { return ngAr = attributes[i].name; } } return ngAr; }; ; ComponentBase.prototype.ngAfterViewInit = function () { var _this = this; // Used setTimeout for template binding // Refer Link: https://github.com/angular/angular/issues/6005 setTimeout(function () { /* istanbul ignore else */ if (typeof window !== 'undefined') { _this.appendTo(_this.element); } }); }; ComponentBase.prototype.ngOnDestroy = function () { /* istanbul ignore else */ if (typeof window !== 'undefined' && this.element.classList.contains('e-control')) { this.destroy(); this.clearTemplate(null); } }; ComponentBase.prototype.clearTemplate = function (templateNames) { clearTemplate(this, templateNames); }; ; ComponentBase.prototype.ngAfterContentChecked = function () { for (var _i = 0, _a = this.tagObjects; _i < _a.length; _i++) { var tagObject = _a[_i]; if (!isUndefined(tagObject.instance) && (tagObject.instance.isInitChanges || tagObject.instance.hasChanges)) { if (tagObject.instance.isInitChanges) { var propObj = {}; propObj[tagObject.name] = tagObject.instance.getProperties(); this.setProperties(propObj, tagObject.instance.isInitChanges); } else { for (var _b = 0, _c = tagObject.instance.list; _b < _c.length; _b++) { var list = _c[_b]; if (list.hasChanges) { var curIndex = tagObject.instance.list.indexOf(list); getValue(tagObject.name, this)[curIndex].setProperties(list.getProperties()); } } } } } }; ComponentBase.prototype.registerEvents = function (eventList) { var ngEventsEmitter = {}; if (eventList && eventList.length) { for (var _i = 0, eventList_1 = eventList; _i < eventList_1.length; _i++) { var event_1 = eventList_1[_i]; ngEventsEmitter[event_1] = new EventEmitter(false); } this.setProperties(ngEventsEmitter, true); } }; ComponentBase.prototype.twoWaySetter = function (newVal, prop) { var oldVal = getValue(prop, this.properties); if (oldVal === newVal) { return; } this.saveChanges(prop, newVal, oldVal); setValue(prop, (isNullOrUndefined(newVal) ? null : newVal), this.properties); getValue(prop + 'Change', this).emit(newVal); }; ComponentBase.prototype.addTwoWay = function (propList) { var _this = this; var _loop_1 = function (prop) { getValue(prop, this_1); Object.defineProperty(this_1, prop, { get: function () { return getValue(prop, _this.properties); }, set: function (newVal) { return _this.twoWaySetter(newVal, prop); } }); setValue(prop + 'Change', new EventEmitter(), this_1); }; var this_1 = this; for (var _i = 0, propList_1 = propList; _i < propList_1.length; _i++) { var prop = propList_1[_i]; _loop_1(prop); } }; ComponentBase.prototype.addEventListener = function (eventName, handler) { var eventObj = getValue(eventName, this); if (!isUndefined(eventObj)) { if (!this.ngBoundedEvents[eventName]) { this.ngBoundedEvents[eventName] = new Map(); } this.ngBoundedEvents[eventName].set(handler, eventObj.subscribe(handler)); } }; ComponentBase.prototype.removeEventListener = function (eventName, handler) { var eventObj = getValue(eventName, this); if (!isUndefined(eventObj)) { this.ngBoundedEvents[eventName].get(handler).unsubscribe(); } }; ComponentBase.prototype.trigger = function (eventName, eventArgs) { var eventObj = getValue(eventName, this); var prevDetection = this.isProtectedOnChange; this.isProtectedOnChange = false; if (eventArgs) { eventArgs.name = eventName; } if (!isUndefined(eventObj)) { eventObj.next(eventArgs); } var localEventObj = getValue('local' + eventName.charAt(0).toUpperCase() + eventName.slice(1), this); if (!isUndefined(localEventObj)) { localEventObj.call(this, eventArgs); } this.isProtectedOnChange = prevDetection; }; return ComponentBase; }()); export { ComponentBase };