UNPKG

ember-legacy-class-transform

Version:
65 lines 2.26 kB
import { assign, EMPTY_ARRAY } from '@glimmer/util'; import { ElementStack } from './builder'; import Scanner from './scanner'; import { VM } from './vm'; export class TemplateIterator { constructor(vm) { this.vm = vm; } next() { return this.vm.next(); } } let clientId = 0; export default function templateFactory({ id: templateId, meta, block }) { let parsedBlock; let id = templateId || `client-${clientId++}`; let create = (env, envMeta) => { let newMeta = envMeta ? assign({}, envMeta, meta) : meta; if (!parsedBlock) { parsedBlock = JSON.parse(block); } return new ScannableTemplate(id, newMeta, env, parsedBlock); }; return { id, meta, create }; } class ScannableTemplate { constructor(id, meta, env, rawBlock) { this.id = id; this.meta = meta; this.env = env; this.entryPoint = null; this.layout = null; this.partial = null; this.block = null; this.scanner = new Scanner(rawBlock, env); this.symbols = rawBlock.symbols; this.hasEval = rawBlock.hasEval; } render(self, appendTo, dynamicScope) { let { env } = this; let elementStack = ElementStack.forInitialRender(env, appendTo, null); let compiled = this.asEntryPoint().compileDynamic(env); let vm = VM.initial(env, self, dynamicScope, elementStack, compiled); return new TemplateIterator(vm); } asEntryPoint() { if (!this.entryPoint) this.entryPoint = this.scanner.scanEntryPoint(this.compilationMeta()); return this.entryPoint; } asLayout(componentName, attrs) { if (!this.layout) this.layout = this.scanner.scanLayout(this.compilationMeta(), attrs || EMPTY_ARRAY, componentName); return this.layout; } asPartial() { if (!this.partial) this.partial = this.scanner.scanEntryPoint(this.compilationMeta(true)); return this.partial; } asBlock() { if (!this.block) this.block = this.scanner.scanBlock(this.compilationMeta()); return this.block; } compilationMeta(asPartial = false) { return { templateMeta: this.meta, symbols: this.symbols, asPartial }; } }