UNPKG

elmer-ui-core

Version:

web app framework

158 lines (157 loc) 7.16 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Component = void 0; var elmer_common_1 = require("elmer-common"); var inject_1 = require("../inject"); var IComponent_1 = require("../interface/IComponent"); var withRoter_1 = require("../widget/router/withRoter"); var ElmerServiceRequest_1 = require("./ElmerServiceRequest"); var Component = (function (_super) { __extends(Component, _super); function Component(props, context) { var _this = _super.call(this) || this; _this.domList = {}; _this.dom = {}; _this.props = {}; _this.context = {}; _this.state = {}; _this.id = ""; var getRandomID = function () { var now = new Date(); var year = now.getFullYear().toString(), month = now.getMonth() + 1 < 10 ? "0" + (now.getMonth() + 1).toString() : (now.getMonth() + 1).toString(), date = now.getDate() < 10 ? "0" + now.getDate().toString() : now.getDate().toString(), hour = now.getHours() < 10 ? ["0", now.getHours()].join("") : now.getHours().toString(), minute = now.getMinutes() < 10 ? ["0", now.getMinutes()].join("") : now.getMinutes().toString(), second = now.getSeconds() < 10 ? ["0", now.getSeconds()].join("") : now.getSeconds().toString(), reSecond = now.getMilliseconds(); var randValue = parseInt((Math.random() * 9999 + 1000).toString(), 10); return [year, month, date, hour, minute, second, reSecond, randValue].join(""); }; Object.defineProperty(_this, "props", { configurable: true, enumerable: false, value: props }); if (context) { _this.defineReadOnlyProperty(_this, "context", context); } if (props === undefined || props === null) { throw new Error("Please set the props to super method"); } else if (typeof props !== "object") { throw new Error("Please set the props by component parameter"); } _this.id = getRandomID(); return _this; } Component.prototype.addEvent = function (handle, dom, eventName, callBack, options) { throw new Error("The current function is defined when the component is initialized."); }; Component.prototype.insertAdjacentElement = function (refElement, newElement, InsertMethod) { switch (InsertMethod) { case IComponent_1.EnumHTMLElementInsertMethod.beforeBegin: { refElement.parentNode !== null && refElement.parentNode.insertBefore(newElement, refElement); break; } case IComponent_1.EnumHTMLElementInsertMethod.beforeEnd: { refElement.appendChild(newElement); break; } case IComponent_1.EnumHTMLElementInsertMethod.afterBegin: { refElement.insertBefore(newElement, refElement.firstChild); break; } case IComponent_1.EnumHTMLElementInsertMethod.afterEnd: { if (refElement.nextSibling) { refElement.parentNode !== null && refElement.parentNode.insertBefore(newElement, refElement.nextSibling); } else { refElement.parentNode !== null && refElement.parentNode.appendChild(newElement); } break; } default: { refElement.parentNode !== null && refElement.parentNode.appendChild(newElement); } } }; Component.prototype.setData = function (data, refresh) { throw new Error("Component类未通过ElmerRender类做初始化!"); }; Component.prototype.setState = function (data, refresh) { throw new Error("setState方法未通过ElmerRender类做初始化!"); }; Component.prototype.render = function () { throw new Error("【render】方法未通过ElmerRender类做初始化!"); }; Component.prototype.getChildContext = function () { return null; }; Component.prototype.setTheme = function (theme, themeConfig) { var innerTheme = { default: "elmerThemeDefault", themePink: "elmerThemePink" }; var classListData = document.body.classList; var themeValue = ""; if (themeConfig) { Object.keys(themeConfig).map(function (tmpThemeKey) { classListData.remove(themeConfig[tmpThemeKey]); if (tmpThemeKey === theme) { themeValue = themeConfig[tmpThemeKey]; } }); } Object.keys(innerTheme).map(function (tmpThemeKey) { classListData.remove(innerTheme[tmpThemeKey]); if (tmpThemeKey === theme) { themeValue = innerTheme[tmpThemeKey]; } }); if (!this.isEmpty(themeValue)) { classListData.add(themeValue); } }; Component.prototype.loadTemplate = function (url, isEndPoint, ajaxType) { var _this = this; if (this.isEmpty(this.templateCode)) { var request_1 = { obj: {} }; var type_1 = /(GET|POST)/i.test(ajaxType) ? ajaxType : "GET"; inject_1.autowired(ElmerServiceRequest_1.ElmerServiceRequest)(request_1, "obj"); return new Promise(function (resolve) { request_1.obj.sendRequest({ endPoint: isEndPoint ? url : null, type: type_1, url: !isEndPoint ? url : null, header: { "Content-Type": "text/plain;charset=utf8;" } }).then(function (resp) { _this.templateCode = resp; resolve(resp); }).catch(function (err) { var msg = err.statusText || "加载模版文件失败"; resolve("<label>" + msg + "</label>"); }); }); } else { return new Promise(function (resolve) { resolve(_this.templateCode); }); } }; Component.propType = {}; Component.contextType = {}; return Component; }(elmer_common_1.Common)); exports.Component = Component; withRoter_1.withRouter(Component, inject_1.autoInit);