UNPKG

@dooboostore/dom-render

Version:
147 lines (134 loc) 9.97 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 DrAppender_exports = {}; __export(DrAppender_exports, { DrAppender: () => DrAppender }); module.exports = __toCommonJS(DrAppender_exports); var import_OperatorExecuterAttrRequire = require("./OperatorExecuterAttrRequire"); var import_RawSet = require("../rawsets/RawSet"); var import_OperatorExecuter = require("./OperatorExecuter"); var import_core = require("@dooboostore/core"); class DrAppender 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, "DrAppender"); 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; const itemIndexVariableName = this.elementSource.attrs.drItemIndexVariableNameOption; const itemOffsetIndexVariableName = this.elementSource.attrs.drItemOffsetIndexVariableNameOption; const hasVariableName = !!variableName; const hasItemVariableName = !!itemVariableName; const hasItemIndexVariableName = !!itemIndexVariableName; const hasItemOffsetIndexVariableName = !!itemOffsetIndexVariableName; const indexOffsetOption = this.elementSource.attrs.drItemIndexOffsetOption; const hasIndexOffsetOption = !!indexOffsetOption; import_core.ObjectUtils.Script.evaluate( ` try{ ${this.render.bindScript} ${this.elementSource.attrs.drBeforeOption ?? ""} const offset = ${hasIndexOffsetOption ? Number(indexOffsetOption) : 0}; const data = ${import_core.ObjectUtils.Path.toOptionalChainPath(attr)}; const attrStr = \`${attr}\`.trim(); const isChunkPath = /\\[\\d+\\]$/.test(attrStr); if (data !== undefined && data !== null) { // SSR \uD638\uD658\uC131\uC744 \uC704\uD574 Array.from \uC0AC\uC6A9 const items = isChunkPath ? (Array.isArray(data) ? data : [data]) : (typeof data.getAll === 'function' ? data.getAll().slice(offset) : Array.from(data).slice(offset)); var i = -1; for(const it of items) { i++; const userIdx = i + offset; // \uC544\uC774\uD15C \uC811\uADFC \uACBD\uB85C \uC0DD\uC131 let destItStr = attrStr + '[' + (isChunkPath ? i : userIdx) + ']'; if (!isChunkPath && typeof data.getAll === 'function') { destItStr = '(' + attrStr + '.getAll()[' + userIdx + '])'; } else { destItStr = '(' + destItStr + ')'; } const n = this.__render.element.cloneNode(true); // \uC635\uC158 \uBCF5\uC6D0 \uBC0F \uC815\uC81C Object.entries(this.__render.drAttr).filter(([k,v]) => k !== 'drAppender' && k !== 'drNextOption' && v).forEach(([k, v]) => n.setAttribute(this.__render.drAttrsOriginName[k], v)); // \uC815\uAD50\uD55C \uCE58\uD658 \uB85C\uC9C1 (\uC18D\uC131) n.getAttributeNames().forEach(itName => { let attrVal = n.getAttribute(itName); attrVal = attrVal.replace(/\\#it\\#/g, destItStr) .replace(/\\#nearForOfIt\\#/g, destItStr) .replace(/\\#index\\#/g, String(i)) .replace(/\\#offsetIndex\\#/g, String(userIdx)) .replace(/\\#nearForOfIndex\\#/g, String(i)); if (${hasVariableName} && '${variableName}') attrVal = attrVal.replaceAll('#${variableName}#', attrStr); if (${hasItemVariableName} && '${itemVariableName}') attrVal = attrVal.replaceAll('#${itemVariableName}#', destItStr); if (${hasItemIndexVariableName} && '${itemIndexVariableName}') attrVal = attrVal.replaceAll('#${itemIndexVariableName}#', String(i)); if (${hasItemOffsetIndexVariableName} && '${itemOffsetIndexVariableName}') attrVal = attrVal.replaceAll('#${itemOffsetIndexVariableName}#', String(userIdx)); n.setAttribute(itName, attrVal); }); // \uC815\uAD50\uD55C \uCE58\uD658 \uB85C\uC9C1 (InnerHTML) n.innerHTML = n.innerHTML.replace(/\\#it\\#/g, destItStr) .replace(/\\#index\\#/g, String(i)) .replace(/\\#offsetIndex\\#/g, String(userIdx)); if (${hasVariableName} && '${variableName}') n.innerHTML = n.innerHTML.replaceAll('#${variableName}#', attrStr); if (${hasItemVariableName} && '${itemVariableName}') n.innerHTML = n.innerHTML.replaceAll('#${itemVariableName}#', destItStr); if (${hasItemIndexVariableName} && '${itemIndexVariableName}') n.innerHTML = n.innerHTML.replaceAll('#${itemIndexVariableName}#', String(i)); if (${hasItemOffsetIndexVariableName} && '${itemOffsetIndexVariableName}') n.innerHTML = n.innerHTML.replaceAll('#${itemOffsetIndexVariableName}#', String(userIdx)); if (this.__render.drStripOption === 'true') { Array.from(n.childNodes).forEach(it => this.__render.fag.append(it)); } else { this.__render.fag.append(n); } } // \uB2E4\uC74C \uC138\uB300 \uC13C\uD2F0\uB12C \uC0DD\uC131 (\uC815\uC11D \uCCB4\uC774\uB2DD) const nextN = this.__render.element.cloneNode(true); Object.entries(this.__render.drAttr).filter(([k,v]) => v).forEach(([k, v]) => nextN.setAttribute(this.__render.drAttrsOriginName[k], v)); const basePath = attrStr.replace(/\\[\\d+\\]$/, ''); const appenderObj = ${import_core.ObjectUtils.Path.toOptionalChainPath(attr.replace(/\\[\\d+\\]$/, ""))}; const nextChunkIdx = isChunkPath ? Number(attrStr.match(/\\[(\\d+)\\]$/)[1]) + 1 : (appenderObj?.length ?? 0); nextN.setAttribute('${import_RawSet.RawSet.DR_APPENDER_NAME}', basePath + '[' + nextChunkIdx + ']'); nextN.setAttribute('${import_RawSet.RawSet.DR_ITEM_INDEX_OFFSET_OPTIONNAME}', (offset + items.length)); this.__render.fag.append(nextN); } ${this.elementSource.attrs.drAfterOption ?? ""} }catch(e){console.error('DrAppender 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=DrAppender.js.map