2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-09-16 10:45:06 +02:00
|
|
|
import { setAttribute, text, isChildren, classNames, TAG_NAMES, HTML_NS } from "./html";
|
2021-09-06 17:12:14 +02:00
|
|
|
import {mountView} from "./utils";
|
2021-09-16 14:01:33 +02:00
|
|
|
import {BaseUpdateView} from "./BaseUpdateView";
|
2019-06-14 23:08:41 +02:00
|
|
|
|
2019-06-16 15:12:54 +02:00
|
|
|
function objHasFns(obj) {
|
|
|
|
for(const value of Object.values(obj)) {
|
|
|
|
if (typeof value === "function") {
|
|
|
|
return true;
|
2019-06-14 23:08:41 +02:00
|
|
|
}
|
2019-06-16 15:12:54 +02:00
|
|
|
}
|
|
|
|
return false;
|
2019-06-14 23:08:41 +02:00
|
|
|
}
|
2019-06-14 23:46:31 +02:00
|
|
|
/**
|
|
|
|
Bindable template. Renders once, and allows bindings for given nodes. If you need
|
|
|
|
to change the structure on a condition, use a subtemplate (if)
|
|
|
|
|
2019-06-13 00:41:45 +02:00
|
|
|
supports
|
|
|
|
- event handlers (attribute fn value with name that starts with on)
|
|
|
|
- one way binding of attributes (other attribute fn value)
|
|
|
|
- one way binding of text values (child fn value)
|
|
|
|
- refs to get dom nodes
|
|
|
|
- className binding returning object with className => enabled map
|
2020-04-29 10:10:33 +02:00
|
|
|
- add subviews inside the template
|
2019-06-13 00:41:45 +02:00
|
|
|
*/
|
2020-10-09 17:04:17 +02:00
|
|
|
// TODO: should we rename this to BoundView or something? As opposed to StaticView ...
|
2021-04-15 15:09:45 +02:00
|
|
|
export class TemplateView extends BaseUpdateView {
|
2020-04-29 10:00:51 +02:00
|
|
|
constructor(value, render = undefined) {
|
2021-04-15 15:09:45 +02:00
|
|
|
super(value);
|
2021-04-07 14:38:48 +02:00
|
|
|
// TODO: can avoid this if we have a separate class for inline templates vs class template views
|
2020-04-29 10:00:51 +02:00
|
|
|
this._render = render;
|
2019-06-14 22:43:31 +02:00
|
|
|
this._eventListeners = null;
|
|
|
|
this._bindings = null;
|
2020-04-29 10:00:51 +02:00
|
|
|
this._subViews = null;
|
|
|
|
this._root = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_attach() {
|
|
|
|
if (this._eventListeners) {
|
2020-10-30 22:48:56 +01:00
|
|
|
for (let {node, name, fn, useCapture} of this._eventListeners) {
|
|
|
|
node.addEventListener(name, fn, useCapture);
|
2019-06-14 22:43:31 +02:00
|
|
|
}
|
2019-06-13 00:41:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 10:00:51 +02:00
|
|
|
_detach() {
|
2019-06-14 22:43:31 +02:00
|
|
|
if (this._eventListeners) {
|
2020-10-30 22:48:56 +01:00
|
|
|
for (let {node, name, fn, useCapture} of this._eventListeners) {
|
|
|
|
node.removeEventListener(name, fn, useCapture);
|
2019-06-14 22:43:31 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-29 10:00:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mount(options) {
|
2020-04-29 19:12:12 +02:00
|
|
|
const builder = new TemplateBuilder(this);
|
2021-04-15 15:14:02 +02:00
|
|
|
try {
|
|
|
|
if (this._render) {
|
|
|
|
this._root = this._render(builder, this._value);
|
|
|
|
} else if (this.render) { // overriden in subclass
|
|
|
|
this._root = this.render(builder, this._value);
|
|
|
|
} else {
|
|
|
|
throw new Error("no render function passed in, or overriden in subclass");
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
builder.close();
|
2019-06-13 00:41:45 +02:00
|
|
|
}
|
2021-04-15 15:09:45 +02:00
|
|
|
// takes care of update being called when needed
|
2021-09-16 14:01:33 +02:00
|
|
|
this.subscribeOnMount(options);
|
2020-04-29 10:00:51 +02:00
|
|
|
this._attach();
|
|
|
|
return this._root;
|
2019-06-13 00:41:45 +02:00
|
|
|
}
|
|
|
|
|
2020-04-29 10:00:51 +02:00
|
|
|
unmount() {
|
|
|
|
this._detach();
|
2021-04-15 15:09:45 +02:00
|
|
|
super.unmount();
|
2020-04-30 18:27:21 +02:00
|
|
|
if (this._subViews) {
|
|
|
|
for (const v of this._subViews) {
|
|
|
|
v.unmount();
|
|
|
|
}
|
2020-04-29 10:00:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
root() {
|
|
|
|
return this._root;
|
|
|
|
}
|
|
|
|
|
|
|
|
update(value) {
|
|
|
|
this._value = value;
|
|
|
|
if (this._bindings) {
|
|
|
|
for (const binding of this._bindings) {
|
|
|
|
binding();
|
2019-06-14 22:43:31 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-13 00:41:45 +02:00
|
|
|
}
|
|
|
|
|
2020-10-30 22:48:56 +01:00
|
|
|
_addEventListener(node, name, fn, useCapture = false) {
|
2019-06-14 22:43:31 +02:00
|
|
|
if (!this._eventListeners) {
|
|
|
|
this._eventListeners = [];
|
|
|
|
}
|
2020-10-30 22:48:56 +01:00
|
|
|
this._eventListeners.push({node, name, fn, useCapture});
|
2019-06-13 00:41:45 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 22:43:31 +02:00
|
|
|
_addBinding(bindingFn) {
|
|
|
|
if (!this._bindings) {
|
|
|
|
this._bindings = [];
|
|
|
|
}
|
|
|
|
this._bindings.push(bindingFn);
|
|
|
|
}
|
|
|
|
|
2020-11-13 15:57:14 +01:00
|
|
|
addSubView(view) {
|
2020-04-29 10:00:51 +02:00
|
|
|
if (!this._subViews) {
|
|
|
|
this._subViews = [];
|
2019-06-14 22:43:31 +02:00
|
|
|
}
|
2020-04-29 10:00:51 +02:00
|
|
|
this._subViews.push(view);
|
2019-06-14 22:43:31 +02:00
|
|
|
}
|
2020-11-13 15:57:14 +01:00
|
|
|
|
|
|
|
removeSubView(view) {
|
|
|
|
const idx = this._subViews.indexOf(view);
|
|
|
|
if (idx !== -1) {
|
|
|
|
this._subViews.splice(idx, 1);
|
|
|
|
}
|
|
|
|
}
|
2021-04-15 15:11:05 +02:00
|
|
|
|
|
|
|
updateSubViews(value, props) {
|
|
|
|
if (this._subViews) {
|
|
|
|
for (const v of this._subViews) {
|
|
|
|
v.update(value, props);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 19:12:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// what is passed to render
|
|
|
|
class TemplateBuilder {
|
|
|
|
constructor(templateView) {
|
|
|
|
this._templateView = templateView;
|
2021-04-15 15:14:02 +02:00
|
|
|
this._closed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this._closed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_addBinding(fn) {
|
|
|
|
if (this._closed) {
|
|
|
|
console.trace("Adding a binding after render will likely cause memory leaks");
|
|
|
|
}
|
|
|
|
this._templateView._addBinding(fn);
|
2020-04-29 19:12:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get _value() {
|
|
|
|
return this._templateView._value;
|
|
|
|
}
|
2019-06-14 22:43:31 +02:00
|
|
|
|
2020-10-30 22:48:56 +01:00
|
|
|
addEventListener(node, name, fn, useCapture = false) {
|
|
|
|
this._templateView._addEventListener(node, name, fn, useCapture);
|
|
|
|
}
|
|
|
|
|
2019-06-14 22:43:31 +02:00
|
|
|
_addAttributeBinding(node, name, fn) {
|
2019-06-13 00:41:45 +02:00
|
|
|
let prevValue = undefined;
|
|
|
|
const binding = () => {
|
|
|
|
const newValue = fn(this._value);
|
|
|
|
if (prevValue !== newValue) {
|
|
|
|
prevValue = newValue;
|
|
|
|
setAttribute(node, name, newValue);
|
|
|
|
}
|
|
|
|
};
|
2021-04-15 15:14:02 +02:00
|
|
|
this._addBinding(binding);
|
2019-06-13 00:41:45 +02:00
|
|
|
binding();
|
|
|
|
}
|
|
|
|
|
2019-06-14 23:08:41 +02:00
|
|
|
_addClassNamesBinding(node, obj) {
|
|
|
|
this._addAttributeBinding(node, "className", value => classNames(obj, value));
|
|
|
|
}
|
|
|
|
|
2019-06-13 00:41:45 +02:00
|
|
|
_addTextBinding(fn) {
|
|
|
|
const initialValue = fn(this._value);
|
|
|
|
const node = text(initialValue);
|
|
|
|
let prevValue = initialValue;
|
|
|
|
const binding = () => {
|
|
|
|
const newValue = fn(this._value);
|
|
|
|
if (prevValue !== newValue) {
|
|
|
|
prevValue = newValue;
|
|
|
|
node.textContent = newValue+"";
|
|
|
|
}
|
|
|
|
};
|
2019-06-14 22:43:31 +02:00
|
|
|
|
2021-04-15 15:14:02 +02:00
|
|
|
this._addBinding(binding);
|
2019-06-13 00:41:45 +02:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2019-06-14 23:46:31 +02:00
|
|
|
_setNodeAttributes(node, attributes) {
|
|
|
|
for(let [key, value] of Object.entries(attributes)) {
|
|
|
|
const isFn = typeof value === "function";
|
|
|
|
// binding for className as object of className => enabled
|
|
|
|
if (key === "className" && typeof value === "object" && value !== null) {
|
2019-06-16 15:12:54 +02:00
|
|
|
if (objHasFns(value)) {
|
|
|
|
this._addClassNamesBinding(node, value);
|
|
|
|
} else {
|
|
|
|
setAttribute(node, key, classNames(value));
|
|
|
|
}
|
2019-06-14 23:46:31 +02:00
|
|
|
} else if (key.startsWith("on") && key.length > 2 && isFn) {
|
|
|
|
const eventName = key.substr(2, 1).toLowerCase() + key.substr(3);
|
|
|
|
const handler = value;
|
2020-04-29 19:12:12 +02:00
|
|
|
this._templateView._addEventListener(node, eventName, handler);
|
2019-06-14 23:46:31 +02:00
|
|
|
} else if (isFn) {
|
|
|
|
this._addAttributeBinding(node, key, value);
|
|
|
|
} else {
|
|
|
|
setAttribute(node, key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_setNodeChildren(node, children) {
|
|
|
|
if (!Array.isArray(children)) {
|
|
|
|
children = [children];
|
|
|
|
}
|
|
|
|
for (let child of children) {
|
|
|
|
if (typeof child === "function") {
|
|
|
|
child = this._addTextBinding(child);
|
|
|
|
} else if (!child.nodeType) {
|
|
|
|
// not a DOM node, turn into text
|
|
|
|
child = text(child);
|
|
|
|
}
|
|
|
|
node.appendChild(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 22:43:31 +02:00
|
|
|
_addReplaceNodeBinding(fn, renderNode) {
|
|
|
|
let prevValue = fn(this._value);
|
|
|
|
let node = renderNode(null);
|
|
|
|
|
|
|
|
const binding = () => {
|
|
|
|
const newValue = fn(this._value);
|
|
|
|
if (prevValue !== newValue) {
|
|
|
|
prevValue = newValue;
|
|
|
|
const newNode = renderNode(node);
|
2020-08-07 16:07:44 +02:00
|
|
|
if (node.parentNode) {
|
|
|
|
node.parentNode.replaceChild(newNode, node);
|
2019-06-14 22:43:31 +02:00
|
|
|
}
|
|
|
|
node = newNode;
|
|
|
|
}
|
|
|
|
};
|
2021-04-15 15:14:02 +02:00
|
|
|
this._addBinding(binding);
|
2019-06-14 22:43:31 +02:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-04-29 19:12:48 +02:00
|
|
|
el(name, attributes, children) {
|
2020-05-03 01:08:53 +02:00
|
|
|
return this.elNS(HTML_NS, name, attributes, children);
|
|
|
|
}
|
|
|
|
|
|
|
|
elNS(ns, name, attributes, children) {
|
2020-04-29 19:12:48 +02:00
|
|
|
if (attributes && isChildren(attributes)) {
|
|
|
|
children = attributes;
|
|
|
|
attributes = null;
|
|
|
|
}
|
|
|
|
|
2020-05-03 01:08:53 +02:00
|
|
|
const node = document.createElementNS(ns, name);
|
2020-04-29 19:12:48 +02:00
|
|
|
|
|
|
|
if (attributes) {
|
|
|
|
this._setNodeAttributes(node, attributes);
|
|
|
|
}
|
|
|
|
if (children) {
|
|
|
|
this._setNodeChildren(node, children);
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-06-04 10:07:12 +02:00
|
|
|
// this inserts a view, and is not a view factory for `if`, so returns the root element to insert in the template
|
2020-04-29 10:00:51 +02:00
|
|
|
// you should not call t.view() and not use the result (e.g. attach the result to the template DOM tree).
|
2021-04-15 15:11:05 +02:00
|
|
|
view(view, mountOptions = undefined) {
|
2020-11-13 15:57:14 +01:00
|
|
|
this._templateView.addSubView(view);
|
2021-06-04 10:07:12 +02:00
|
|
|
return mountView(view, mountOptions);
|
2020-04-29 10:00:51 +02:00
|
|
|
}
|
|
|
|
|
2020-05-06 23:44:52 +02:00
|
|
|
// map a value to a view, every time the value changes
|
|
|
|
mapView(mapFn, viewCreator) {
|
|
|
|
return this._addReplaceNodeBinding(mapFn, (prevNode) => {
|
2019-06-14 22:43:31 +02:00
|
|
|
if (prevNode && prevNode.nodeType !== Node.COMMENT_NODE) {
|
2020-05-05 23:17:48 +02:00
|
|
|
const subViews = this._templateView._subViews;
|
|
|
|
const viewIdx = subViews.findIndex(v => v.root() === prevNode);
|
2020-04-29 10:00:51 +02:00
|
|
|
if (viewIdx !== -1) {
|
2020-05-05 23:17:48 +02:00
|
|
|
const [view] = subViews.splice(viewIdx, 1);
|
2020-04-29 10:00:51 +02:00
|
|
|
view.unmount();
|
|
|
|
}
|
2019-06-14 22:43:31 +02:00
|
|
|
}
|
2020-05-06 23:44:52 +02:00
|
|
|
const view = viewCreator(mapFn(this._value));
|
|
|
|
if (view) {
|
2020-04-29 10:00:51 +02:00
|
|
|
return this.view(view);
|
2019-06-14 22:43:31 +02:00
|
|
|
} else {
|
2020-05-06 23:44:52 +02:00
|
|
|
return document.createComment("node binding placeholder");
|
2019-06-14 22:43:31 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-05-06 23:44:52 +02:00
|
|
|
|
2021-03-18 12:43:00 +01:00
|
|
|
// Special case of mapView for a TemplateView.
|
|
|
|
// Always creates a TemplateView, if this is optional depending
|
|
|
|
// on mappedValue, use `if` or `mapView`
|
|
|
|
map(mapFn, renderFn) {
|
|
|
|
return this.mapView(mapFn, mappedValue => {
|
|
|
|
return new TemplateView(this._value, (t, vm) => {
|
2021-04-01 14:59:18 +02:00
|
|
|
const rootNode = renderFn(mappedValue, t, vm);
|
|
|
|
if (!rootNode) {
|
2021-04-07 14:38:48 +02:00
|
|
|
// TODO: this will confuse mapView which assumes that
|
|
|
|
// a comment node means there is no view to clean up
|
2021-04-01 14:59:18 +02:00
|
|
|
return document.createComment("map placeholder");
|
|
|
|
}
|
|
|
|
return rootNode;
|
2021-03-18 12:43:00 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ifView(predicate, viewCreator) {
|
2020-05-06 23:44:52 +02:00
|
|
|
return this.mapView(
|
2021-03-18 12:43:00 +01:00
|
|
|
value => !!predicate(value),
|
2020-05-06 23:44:52 +02:00
|
|
|
enabled => enabled ? viewCreator(this._value) : null
|
|
|
|
);
|
|
|
|
}
|
2021-03-18 12:43:00 +01:00
|
|
|
|
|
|
|
// creates a conditional subtemplate
|
|
|
|
// use mapView if you need to map to a different view class
|
|
|
|
if(predicate, renderFn) {
|
|
|
|
return this.ifView(predicate, vm => new TemplateView(vm, renderFn));
|
|
|
|
}
|
2021-05-31 11:57:17 +02:00
|
|
|
|
|
|
|
/** You probably are looking for something else, like map or mapView.
|
|
|
|
This is an escape hatch that allows you to do manual DOM manipulations
|
|
|
|
as a reaction to a binding change.
|
|
|
|
This should only be used if the side-effect won't add any bindings,
|
|
|
|
event handlers, ...
|
|
|
|
You should not call the TemplateBuilder (e.g. `t.xxx()`) at all from the side effect,
|
|
|
|
instead use tags from html.js to help you construct any DOM you need. */
|
|
|
|
mapSideEffect(mapFn, sideEffect) {
|
|
|
|
let prevValue = mapFn(this._value);
|
|
|
|
const binding = () => {
|
|
|
|
const newValue = mapFn(this._value);
|
|
|
|
if (prevValue !== newValue) {
|
|
|
|
sideEffect(newValue, prevValue);
|
|
|
|
prevValue = newValue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this._addBinding(binding);
|
|
|
|
sideEffect(prevValue, undefined);
|
|
|
|
}
|
2019-06-13 00:41:45 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 01:08:53 +02:00
|
|
|
|
|
|
|
for (const [ns, tags] of Object.entries(TAG_NAMES)) {
|
|
|
|
for (const tag of tags) {
|
|
|
|
TemplateBuilder.prototype[tag] = function(attributes, children) {
|
|
|
|
return this.elNS(ns, tag, attributes, children);
|
|
|
|
};
|
|
|
|
}
|
2019-06-13 00:41:45 +02:00
|
|
|
}
|