UNPKG

@dooboostore/dom-render

Version:
153 lines (142 loc) 9.41 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var DrForOf_exports = {}; __export(DrForOf_exports, { DrForOf: () => DrForOf }); module.exports = __toCommonJS(DrForOf_exports); var import_OperatorExecuterAttrRequire = require("./OperatorExecuterAttrRequire"); var import_RawSet = require("../rawsets/RawSet"); var import_OperatorExecuter = require("./OperatorExecuter"); var import_core = require("@dooboostore/core"); class DrForOf extends import_OperatorExecuterAttrRequire.OperatorExecuterAttrRequire { constructor(rawSet, render, returnContainer, elementSource, source, afterCallBack) { source.operatorAround = void 0; super(rawSet, render, returnContainer, elementSource, source, afterCallBack, false); } async executeAttrRequire(attr) { const itRandom = import_RawSet.RawSet.drItOtherEncoding(this.elementSource.element, "DrForOf"); const vars = import_RawSet.RawSet.drVarEncoding(this.elementSource.element, this.elementSource.attrs.drVarOption ?? ""); const newTemp = this.source.config.window.document.createElement("temp"); const variableName = this.elementSource.attrs.drVariableNameOption; const itemVariableName = this.elementSource.attrs.drItemVariableNameOption || import_RawSet.RawSet.DR_ITEM_VARIABLE_NAME_OPTIONNAME_DEFAULT; const itemIndexVariableName = this.elementSource.attrs.drItemIndexVariableNameOption || import_RawSet.RawSet.DR_ITEM_INDEX_VARIABLE_NAME_OPTIONNAME_DEFAULT; const hasVariableName = !!variableName; const hasItemVariableName = !!itemVariableName; const hasItemIndexVariableName = !!itemIndexVariableName; import_core.ObjectUtils.Script.evaluate( ` try { ${this.render.bindScript} ${this.elementSource.attrs.drBeforeOption ?? ""} var i = -1; const forOf = ${import_core.ObjectUtils.Path.toOptionalChainPath(attr)}; const forOfStr = '('+\`${attr}\`.trim() + ')'; if (forOf) { for(const it of forOf) { i++; var destIt = it; if (/\\[(.*,?)\\],/g.test(forOfStr)) { if (typeof it === 'string') { destIt = it; } else { destIt = forOfStr.substring(1, forOfStr.length-1).split(',')[i]; } } else if (forOf.isRange) { destIt = it; } else { destIt = forOfStr + '[' + i +']' } destIt = '(' + destIt + ')'; const n = this.__render.element.cloneNode(true); // dr-for-of \uBC0F \uAD00\uB828 \uC635\uC158 \uC81C\uAC70 \uD6C4 \uB098\uBA38\uC9C0 \uC18D\uC131 \uC720\uC9C0 Object.entries(this.__render.drAttr).filter(([k,v]) => k !== 'drForOf' && k !== 'drNextOption' && v).forEach(([k, v]) => n.setAttribute(this.__render.drAttrsOriginName[k], v)); // \uC815\uAD50\uD55C \uCE58\uD658 \uD568\uC218 (Regex \uAE30\uBC18\uC73C\uB85C \uD638\uD658\uC131 \uD655\uBCF4) const replaceAllVars = (text) => { if (!text) return text; let result = text.replace(/\\#it\\#/g, destIt) .replace(/\\#nearForOfIt\\#/g, destIt) .replace(/\\#index\\#/g, String(i)) .replace(/\\#nearForOfIndex\\#/g, String(i)); if (${hasVariableName} && '${variableName}') result = result.replace(new RegExp('#${variableName}#', 'g'), forOfStr); if (${hasItemVariableName} && '${itemVariableName}') result = result.replace(new RegExp('#${itemVariableName}#', 'g'), destIt); if (${hasItemIndexVariableName} && '${itemIndexVariableName}') result = result.replace(new RegExp('#${itemIndexVariableName}#', 'g'), String(i)); return result; }; // \uC18D\uC131 \uCE58\uD658 n.getAttributeNames().forEach(itName => { n.setAttribute(itName, replaceAllVars(n.getAttribute(itName))); }); const hasDrOptionAttr = n.hasAttribute('${import_RawSet.RawSet.DR_ATTR_OPTIONNAME}'); if (hasDrOptionAttr) { const drOptionAttr = n.getAttribute('${import_RawSet.RawSet.DR_ATTR_OPTIONNAME}'); const drOptionAttrResult = $scriptUtils.evaluateReturn(drOptionAttr, this); Array.from(Object.entries(drOptionAttrResult??{})).forEach(([k,v])=>{ if (v === null) n.removeAttribute(k); else n.setAttribute(k,v); }); } const hasDrOptionIf = n.hasAttribute('${import_RawSet.RawSet.DR_IF_OPTIONNAME}'); if (hasDrOptionIf) { const drOptionIf = n.getAttribute('${import_RawSet.RawSet.DR_IF_OPTIONNAME}'); if(!$scriptUtils.evaluateReturn(drOptionIf, this)) continue; } const drOptionAttrDetect = n.getAttribute('${import_RawSet.RawSet.DR_DETECT_ATTR_OPTIONNAME}'); if (drOptionAttrDetect) { const drOptionAttrResult = $scriptUtils.evaluateReturn(drOptionAttrDetect, this); Array.from(Object.entries(drOptionAttrResult??{})).forEach(([k,v])=>{ if (v === null) n.removeAttribute(k); else n.setAttribute(k,v); }); } const drOptionFilter = n.getAttribute('${import_RawSet.RawSet.DR_DETECT_FILTER_OPTIONNAME}'); if (drOptionFilter) { if (!$scriptUtils.evaluateReturn(drOptionFilter, this)) continue; } // InnerHTML \uCE58\uD658 n.innerHTML = replaceAllVars(n.innerHTML); if (this.__render.drStripOption === 'true') { Array.from(n.childNodes).forEach(it => this.__render.fag.append(it)); } else { this.__render.fag.append(n); } } } ${this.elementSource.attrs.drAfterOption ?? ""} } catch(e) { console.error('DrForOf Error:', e); } `, Object.assign(this.source.obj, { __render: Object.freeze({ drStripOption: this.elementSource.attrs.drStripOption, drAttr: this.elementSource.attrs, drAttrsOriginName: import_RawSet.RawSet.drAttrsOriginName, fag: newTemp, ...this.render }) }) ); import_RawSet.RawSet.drVarDecoding(newTemp, vars); import_RawSet.RawSet.drItOtherDecoding(newTemp, itRandom); const template = this.source.config.window.document.createElement("template"); template.innerHTML = newTemp.innerHTML; this.returnContainer.fag.append(template.content); const rr = import_RawSet.RawSet.checkPointCreates(this.returnContainer.fag, this.source.obj, this.source.config); this.elementSource.element.parentNode?.replaceChild(this.returnContainer.fag, this.elementSource.element); this.returnContainer.raws.push(...rr); return import_OperatorExecuter.ExecuteState.EXECUTE; } } //# sourceMappingURL=DrForOf.js.map