-
Notifications
You must be signed in to change notification settings - Fork 319
/
lit-element.ts
178 lines (162 loc) · 5.63 KB
/
lit-element.ts
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* @license
* Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
import { PropertiesMixin } from '../../@polymer/polymer/lib/mixins/properties-mixin.js';
import { camelToDashCase } from '../../@polymer/polymer/lib/utils/case-map.js';
import { TemplateResult } from '../../lit-html/lit-html.js';
import { render } from '../../lit-html/lib/lit-extended.js';
export { html } from '../../lit-html/lib/lit-extended.js';
/**
* Renders attributes to the given element based on the `attrInfo` object where
* boolean values are added/removed as attributes.
* @param {*} element Element on which to set attributes.
* @param {*} attrInfo Object describing attributes.
*/
export function renderAttributes(element: HTMLElement, attrInfo: any) {
for (const a in attrInfo) {
const v = attrInfo[a] === true ? '' : attrInfo[a];
if (v || v === '' || v === 0) {
if (element.getAttribute(a) !== v) {
element.setAttribute(a, v);
}
} else if (element.hasAttribute(a)) {
element.removeAttribute(a);
}
}
}
/**
* Returns a string of css class names formed by taking the properties
* in the `classInfo` object and appending the property name to the string of
* class names if the property value is truthy.
* @param {*} classInfo
*/
export function classString(classInfo: any) {
const o = [];
for (const name in classInfo) {
const v = classInfo[name];
if (v) {
o.push(name);
}
}
return o.join(' ');
}
/**
* Returns a css style string formed by taking the properties in the `styleInfo`
* object and appending the property name (dash-cased) colon the
* property value. Properties are separated by a semi-colon.
* @param {*} styleInfo
*/
export function styleString(styleInfo: any) {
const o = [];
for (const name in styleInfo) {
const v = styleInfo[name];
if (v || v === 0) {
o.push(`${camelToDashCase(name)}: ${v}`);
}
}
return o.join('; ');
}
export class LitElement extends PropertiesMixin(HTMLElement) {
private __renderComplete: Promise<any>|null = null;
private __resolveRenderComplete: Function|null = null;
private __isInvalid: Boolean = false;
protected ready() {
this.attachShadow({mode: 'open'});
super.ready();
}
/**
* Override which always returns true so that `_propertiesChanged`
* is called whenver properties are invalidated. This ensures `render`
* is always called in response to `invalidate`.
* @param {*} _props Current element properties
* @param {*} _changedProps Changing element properties
* @param {*} _prevProps Previous element properties
* @returns {boolean} Default implementation always returns true.
*/
protected _shouldPropertiesChange(_props: any, _changedProps: any, _prevProps: any) {
return true;
}
/**
* Override which always calls `render` and `didRender` to perform
* element rendering.
* @param {*} props Current element properties
* @param {*} changedProps Changing element properties
* @param {*} prevProps Previous element properties
*/
protected _propertiesChanged(props: any, changedProps: any, prevProps: any) {
this.__isInvalid = false;
super._propertiesChanged(props, changedProps, prevProps);
const result = this.render(props);
if (result) {
render(result, this.shadowRoot!);
}
this.didRender(props, changedProps, prevProps);
if (this.__resolveRenderComplete) {
this.__resolveRenderComplete();
}
}
protected _flushProperties() {
super._flushProperties();
if (this.__dataPending) {
console.warn(`Setting properties in response to properties changing considered harmful. Offending properties: ${Object.keys(this.__dataPending)}.`);
}
}
/**
* Returns a lit-html TemplateResult which is rendered into the element's
* shadowRoot. This method must be implemented.
* @param {*} _props Current element properties
* @returns {TemplateResult} Must return a lit-html TemplateResult.
*/
protected render(_props: any): TemplateResult {
throw new Error('render() not implemented');
}
/**
* Called after element dom has been rendered. Implement to
* directly access element DOM.
* @param {*} _props Current element properties
* @param {*} _changedProps Changing element properties
* @param {*} _prevProps Previous element properties
*/
protected didRender(_props: any, _changedProps: any, _prevProps: any) {}
/**
* Provokes the element to asynchronously re-render.
*/
invalidate() {
this._invalidateProperties();
}
/**
* Override which provides tracking of invalidated state.
*/
_invalidateProperties() {
this.__isInvalid = true;
super._invalidateProperties();
}
/**
* Returns a promise which resolves after the element next renders.
*/
get renderComplete() {
if (!this.__renderComplete) {
// TODO(sorvell): handle rejected render.
this.__renderComplete = new Promise((resolve) => {
this.__resolveRenderComplete = () => {
this.__resolveRenderComplete = this.__renderComplete = null;
resolve();
}
});
if (!this.__isInvalid && this.__resolveRenderComplete) {
this.__resolveRenderComplete();
}
}
return this.__renderComplete;
}
}