UNPKG

elmer-ui-core

Version:

web app framework

241 lines (240 loc) 11.6 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 __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RenderMiddleware = void 0; var elmer_common_1 = require("elmer-common"); var ElmerDom_1 = require("../core/ElmerDom"); var inject_1 = require("../inject"); var injectable_1 = require("../inject/injectable"); var RenderMiddleware = (function (_super) { __extends(RenderMiddleware, _super); function RenderMiddleware() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.svgAttrNameSpace = null; return _this; } RenderMiddleware.prototype.injectRenderDomAttribute = function (dom, options) { if (dom) { var attrKey = options.attrKey, attrValue = options.attrValue, domData = options.domData; var isSVG = this.isSVGDOM(dom); if (domData.tagName === "input" && (/checkbox/.test(domData.props.type) || /radio/.test(domData.props.type))) { if (attrKey === "checked") { if ((/\s*(true|1)\s*/i.test(attrValue) || /\s*(false|0)\s*/i.test(attrValue))) { if (/\s*(true|1)\s*/i.test(attrValue)) { dom.setAttribute(attrKey, "checked"); dom.checked = true; } else { dom.removeAttribute(attrKey); dom.checked = false; } } else if (/checked/i.test(attrValue) || this.isEmpty(attrValue)) { if (this.isEmpty(attrValue)) { dom.removeAttribute(attrKey); dom.checked = false; } else { dom.setAttribute(attrKey, "checked"); dom.checked = true; } } return true; } } if (/^class\.[a-z0-9\-_]*$/i.test(attrKey)) { var myClassName = attrKey.replace(/^class\./, ""); if (attrValue) { this.$.addClass(dom, myClassName); } else { this.$.removeClass(dom, myClassName); } return true; } else if (/^disabled$/.test(attrKey)) { if (this.isString(attrValue) && (/\s*(true|1)\s*/i.test(attrValue) || /\s*(false|0)\s*/i.test(attrValue))) { if (/\s*(true|1)\s*/i.test(attrValue)) { !isSVG && dom.setAttribute(attrKey, "disabled"); isSVG && dom.setAttributeNS(this.svgAttrNameSpace, attrKey, "disabled"); } else { !isSVG && dom.removeAttribute(attrKey); isSVG && dom.removeAttributeNS(this.svgAttrNameSpace, attrKey); } } else if (typeof attrValue === "boolean") { if (attrValue) { !isSVG && dom.setAttribute(attrKey, "disabled"); isSVG && dom.setAttributeNS(this.svgAttrNameSpace, attrKey, "disabled"); } else { !isSVG && dom.removeAttribute(attrKey); isSVG && dom.removeAttributeNS(this.svgAttrNameSpace, attrKey); } } return true; } else if (/^tabindex$/i.test(attrKey)) { dom.tabIndex = attrValue; return true; } else if (/^(id|name|style|class)$/i.test(attrKey)) { return this.isEmpty(attrValue); } } return false; }; RenderMiddleware.prototype.virtualRenderLogic = function (domComponent, options, saveResult) { var resultAttrKey = options.attrKey.replace(/^[a-z]*:/i, ""); var exMessage = ""; try { var checkValue = options.attrValue || ""; checkValue = checkValue.replace(/\s*\{\{/, "").replace(/\}\}\s*/, ""); if (/\s(eq|neq|gt|lt|gteq|lteq|\&\&|\|\|)\s/.test(checkValue)) { var attrValue = checkValue || ""; var varArr = attrValue.split(/\s+/); var logicData = {}; var checkLogic = []; var logicCode = ""; for (var i = 0; i < varArr.length; i++) { var tmpVar = varArr[i]; var lenLogic = /\.length$/.test(tmpVar); var noneLogic = /^\!/.test(tmpVar); var dataKey = tmpVar.replace(/^\!/, "").replace(/^this\./, "").replace(/\.length$/, ""); var domData = this.getValue(domComponent, dataKey); var tmpData = !this.isEmpty(domData) ? domData : this.getValue(options.optionData, dataKey); var saveKey = dataKey.replace(/\./g, ""); saveKey = lenLogic ? saveKey + "length" : saveKey; if (!this.isEmpty(tmpData)) { logicData[saveKey] = lenLogic ? tmpData["length"] : tmpData; if (!noneLogic) { checkLogic.push("logicData." + saveKey); } else { checkLogic.push("!logicData." + saveKey); } } else { if (!/\./.test(tmpVar)) { checkLogic.push(tmpVar); } else { if (!noneLogic) { checkLogic.push("logicData." + saveKey); } else { checkLogic.push("!logicData." + saveKey); } } } } logicCode = checkLogic.join(" "); logicCode = logicCode.replace(/\s(eq)\s/g, " === "); logicCode = logicCode.replace(/\s(neq)\s/g, " !== "); logicCode = logicCode.replace(/\s(gt)\s/g, " > "); logicCode = logicCode.replace(/\s(lt)\s/g, " < "); logicCode = logicCode.replace(/\s(gteq)\s/g, " >= "); logicCode = logicCode.replace(/\s(lteq)\s/g, " <= "); exMessage = logicCode; var fn = new Function("logicData", "return " + logicCode); saveResult[resultAttrKey] = fn(logicData); fn = null; return true; } exMessage = null; return false; } catch (e) { console.error(e, exMessage); return true; } }; RenderMiddleware.prototype.virtualRenderBindText = function (domComponent, text, optionData) { var _this = this; var staticReg = /\s*\{\{\s*([\!a-z0-9\-_.]*)\(([a-z0-9\-_.,"']*)\s*\)\}\}\s*/i; if (!this.isEmpty(text)) { var execText = text; var staticMatch = execText.match(staticReg); var _loop_1 = function () { var execKey = staticMatch[1]; var execParam = staticMatch[2]; var exec = this_1.getValue(domComponent, execKey); exec = typeof exec === "function" ? exec : this_1.getValue(optionData, execKey); if (typeof exec === "function") { if (!this_1.isEmpty(execParam)) { var execParamArr = execParam.split(","); var execArr_1 = []; execParamArr.map(function (tmpVar) { if (_this.isString(tmpVar) && isNaN(tmpVar)) { if ((/^\"/.test(tmpVar) && /\"$/.test(tmpVar)) || (/^\'/.test(tmpVar) && /\'$/.test(tmpVar))) { var myVar = tmpVar.replace(/^\"/, "").replace(/\"$/, "").replace(/^\'/, "").replace(/\'$/, ""); execArr_1.push(myVar); } else { var tmpParam = _this.getValue(domComponent, tmpVar); tmpParam = tmpParam !== undefined ? tmpParam : _this.getValue(optionData, tmpVar); execArr_1.push(_this.val(tmpParam)); } } else { execArr_1.push(_this.val(tmpVar)); } }); execText = execText.replace(staticMatch[0], exec.apply(domComponent, execArr_1)); execParamArr = null; execArr_1 = null; } else { execText = execText.replace(staticMatch[0], exec()); } } else { execText = execText.replace(staticMatch[0], staticMatch[0].replace(/^\s*\{\{/, "").replace(/\}\}\s*$/, "")); } execKey = null; execParam = null; staticMatch = execText.match(staticReg); }; var this_1 = this; while (staticMatch) { _loop_1(); } staticMatch = null; return execText; } staticReg = null; }; __decorate([ injectable_1.autowired(ElmerDom_1.ElmerDOM), __metadata("design:type", ElmerDom_1.ElmerDOM) ], RenderMiddleware.prototype, "$", void 0); RenderMiddleware = __decorate([ inject_1.Injectable("RenderMiddleware") ], RenderMiddleware); return RenderMiddleware; }(elmer_common_1.Common)); exports.RenderMiddleware = RenderMiddleware;