-
Notifications
You must be signed in to change notification settings - Fork 28
/
LitWidget.js
89 lines (79 loc) · 2.47 KB
/
LitWidget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Only needed on IE and legacy Edge, but it's so small I'm just including it for everyone.
// Difficult to load directly (like other polyfills) because the code is ES6.
import * as templatePolyfill from "lit-html/polyfills/template_polyfill.js";
templatePolyfill.initTemplatePolyfill();
import { html, render } from "lit-html";
import dcl from "dcl/dcl";
import Widget from "./Widget";
import { render as shadyRender } from "lit-html/lib/shady-render";
/**
* Base class for widgets that render using lit-html.
*/
export default dcl([Widget], {
/**
* Render using shadow dom. Cannot be changed dynamically: should be set in a subclass prototype (or here).
*/
shadow: false,
/**
* If this is a Set with property names, the widget only (re)renders
* when one of the properties in this list changes.
*/
propsAffectingRender: null,
/**
* If this is a Set with property names, the widget doesn't (re)render
* unless a property *not* listed here changes.
*/
propsNotAffectingRender: null,
/**
* Return true if the property changes listed in `oldValues` require the widget to rerender.
* @param oldValues
* @returns {boolean}
*/
needsRender: function (oldValues) {
// Reduce rerenders on widget creation by waiting until we are attached to the document for first render.
// Then force render the first time we are attached.
if (!this.attached) {
return false;
} else if ("attached" in oldValues) {
return true;
}
if (this.propsAffectingRender) {
return Object.keys(oldValues).some(function (prop) {
return this.propsAffectingRender.has(prop);
}.bind(this));
} else if (this.propsNotAffectingRender) {
return !Object.keys(oldValues).every(function (prop) {
return this.propsNotAffectingRender.has(prop);
}.bind(this));
} else {
return true;
}
},
/**
* Return widget template filled in with all the widget's current property values.
* Will be passed to lit-html's render() method.
* @returns {TemplateResult}.
*/
render: function () {
return html``;
},
initializeRendering: function () {
if (this.shadow) {
this.attachShadow({mode: "open"});
}
},
refreshRendering: function (oldValues) {
if (this.needsRender(oldValues)) {
if (this.shadow) {
shadyRender(this.render(), this.shadowRoot, { scopeName: this.tagName.toLowerCase() });
} else {
render(this.render(), this);
}
}
},
connectedCallback: function () {
if (window.ShadyCSS !== undefined) {
window.ShadyCSS.styleElement(this);
}
}
});