UNPKG

sprotty

Version:

A next-gen framework for graphical views

101 lines 4.13 kB
"use strict"; /******************************************************************************** * Copyright (c) 2017-2018 TypeFox and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * This Source Code may also be made available under the following Secondary * Licenses when the conditions for such availability set forth in the Eclipse * Public License v. 2.0 are satisfied: GNU General Public License, version 2 * with the GNU Classpath Exception which is available at * https://www.gnu.org/software/classpath/license.html. * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isThunk = exports.ThunkView = void 0; const snabbdom_1 = require("snabbdom"); const inversify_1 = require("inversify"); /** * An view that avoids calculation and patching of VNodes unless some model properties have changed. * Based on snabbdom's thunks. */ let ThunkView = class ThunkView { render(model, context) { return (0, snabbdom_1.h)(this.selector(model), { key: model.id, hook: { init: this.init.bind(this), prepatch: this.prepatch.bind(this) }, fn: () => this.renderAndDecorate(model, context), args: this.watchedArgs(model), thunk: true }); } renderAndDecorate(model, context) { const vnode = this.doRender(model, context); context.decorate(vnode, model); return vnode; } copyToThunk(vnode, thunk) { thunk.elm = vnode.elm; vnode.data.fn = thunk.data.fn; vnode.data.args = thunk.data.args; thunk.data = vnode.data; thunk.children = vnode.children; thunk.text = vnode.text; thunk.elm = vnode.elm; } init(thunk) { const cur = thunk.data; const vnode = cur.fn.apply(undefined, cur.args); this.copyToThunk(vnode, thunk); } prepatch(oldVnode, thunk) { const old = oldVnode.data, cur = thunk.data; if (!this.equals(old.args, cur.args)) this.copyToThunk(cur.fn.apply(undefined, cur.args), thunk); else this.copyToThunk(oldVnode, thunk); } equals(oldArg, newArg) { if (Array.isArray(oldArg) && Array.isArray(newArg)) { if (oldArg.length !== newArg.length) return false; for (let i = 0; i < newArg.length; ++i) { if (!this.equals(oldArg[i], newArg[i])) return false; } } else if (typeof oldArg === 'object' && typeof newArg === 'object') { if (Object.keys(oldArg).length !== Object.keys(newArg).length) return false; for (const key in oldArg) { if (key !== 'parent' && key !== 'root' && (!(key in newArg) || !this.equals(oldArg[key], newArg[key]))) return false; } } else if (oldArg !== newArg) { return false; } return true; } }; exports.ThunkView = ThunkView; exports.ThunkView = ThunkView = __decorate([ (0, inversify_1.injectable)() ], ThunkView); function isThunk(vnode) { return 'thunk' in vnode; } exports.isThunk = isThunk; //# sourceMappingURL=thunk-view.js.map