1 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2 | return new (P || (P = Promise))(function (resolve, reject) {
|
3 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
4 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
5 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
6 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
7 | });
|
8 | };
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export function camelCaseToKebab(str) {
|
17 | return str.replace(/([A-Z])/g, '-$1').toLowerCase();
|
18 | }
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | export function createProperty(prop, attr, context, info) {
|
27 |
|
28 | const setVal = context[prop];
|
29 | Object.defineProperty(context, prop, {
|
30 | get() {
|
31 | return context.__data[prop];
|
32 | },
|
33 | set(val) {
|
34 | return __awaiter(this, void 0, void 0, function* () {
|
35 | const resolved = (val != null && val instanceof Promise
|
36 | ? yield val
|
37 | : val);
|
38 | context.setProperty(prop, resolved);
|
39 | });
|
40 | }
|
41 | });
|
42 | if (info.reflectToAttribute &&
|
43 | (info.type === Object || info.type === Array)) {
|
44 | console.warn('Rich Data shouldn\'t be set as attribte!');
|
45 | }
|
46 | if (info.observer) {
|
47 | if (context[info.observer]) {
|
48 |
|
49 | context.__methodsToCall[prop] = context[info.observer].bind(context);
|
50 | }
|
51 | else {
|
52 | console.warn(`Method ${info.observer} not defined!`);
|
53 | }
|
54 | }
|
55 |
|
56 | if (setVal) {
|
57 | context[prop] = setVal;
|
58 | return;
|
59 | }
|
60 | if (info.value !== undefined) {
|
61 |
|
62 | context[prop] = (typeof (info.value) === 'function'
|
63 | ? info.value.call(context)
|
64 | : info.value);
|
65 | }
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | export const LitLite = (superclass = HTMLElement, html, renderFunction) => class extends superclass {
|
72 | constructor() {
|
73 | super();
|
74 | this.__renderCallbacks = new Set();
|
75 | this.__pendingRender = false;
|
76 | this.__data = {};
|
77 | this.__methodsToCall = {};
|
78 | this.__firstRender = false;
|
79 | this.__propAttr = new Map();
|
80 | this.__attrProp = new Map();
|
81 | this.attachShadow({ mode: 'open' });
|
82 | for (let prop in this.constructor.properties) {
|
83 | const attr = camelCaseToKebab(prop);
|
84 | this.__propAttr.set(prop, attr);
|
85 | this.__attrProp.set(attr, prop);
|
86 | }
|
87 | }
|
88 | static get observedAttributes() {
|
89 | let attrs = [];
|
90 | for (const prop in this.properties) {
|
91 | if (this.properties[prop].reflectToAttribute) {
|
92 | attrs.push(camelCaseToKebab(prop));
|
93 | }
|
94 | }
|
95 | return attrs;
|
96 | }
|
97 | connectedCallback() {
|
98 | const props = this.constructor.properties;
|
99 | this.__wait = true;
|
100 | for (let prop in props) {
|
101 | if (typeof props[prop] === 'function')
|
102 | props[prop] = { type: props[prop] };
|
103 | this.__makeGetterSetter(prop, props[prop]);
|
104 | }
|
105 | delete this.__wait;
|
106 | this.__firstRender = true;
|
107 | if (this.connected)
|
108 | this.connected.call(this);
|
109 | |
110 |
|
111 |
|
112 | this.postponedRender();
|
113 | }
|
114 | disconnectedCallback() {
|
115 | if (this.disconnected)
|
116 | this.disconnected.call(this);
|
117 | }
|
118 | |
119 |
|
120 |
|
121 |
|
122 |
|
123 | __makeGetterSetter(prop, info) {
|
124 | const attr = this.__propAttr.get(prop);
|
125 | createProperty(prop, attr, this, info);
|
126 | }
|
127 | |
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | __propertiesChanged(prop, newVal) {
|
134 | if (this.__data[prop] !== newVal) {
|
135 | const oldVal = this.__data[prop];
|
136 | let doRefresh = true;
|
137 | this.__data[prop] = newVal;
|
138 | if (this.__methodsToCall[prop]) {
|
139 | if (this.__methodsToCall[prop](newVal, oldVal) === false) {
|
140 | doRefresh = false;
|
141 | }
|
142 | }
|
143 | if (doRefresh) {
|
144 | this.refresh();
|
145 | }
|
146 | }
|
147 | }
|
148 | setProperty(prop, newVal) {
|
149 | const info = this.constructor.properties[prop];
|
150 | const attr = this.__propAttr.get(prop);
|
151 | if (info.reflectToAttribute) {
|
152 | |
153 |
|
154 |
|
155 |
|
156 |
|
157 | this.setAttribute(attr, newVal);
|
158 | }
|
159 | else {
|
160 | |
161 |
|
162 |
|
163 | this.__propertiesChanged(prop, newVal);
|
164 | }
|
165 | if (info.notify) {
|
166 | this.dispatchEvent(new CustomEvent(`${attr}-changed`, {
|
167 | bubbles: true,
|
168 | composed: true,
|
169 | detail: newVal
|
170 | }));
|
171 | }
|
172 | }
|
173 | |
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | attributeChangedCallback(attr, old, val) {
|
181 | if (old === val)
|
182 | return;
|
183 | const prop = this.__attrProp.get(attr);
|
184 | if (this.__data[prop] !== val) {
|
185 | const { type } = this.constructor.properties[prop];
|
186 | let newVal = val;
|
187 | switch (type.name) {
|
188 | case 'Boolean':
|
189 | |
190 |
|
191 |
|
192 | if (val === 'false' || val === 'null' ||
|
193 | val === 'undefined' ||
|
194 | val === false || val === null) {
|
195 | this.removeAttribute(attr);
|
196 | newVal = false;
|
197 | }
|
198 | else {
|
199 | newVal = this.hasAttribute(attr);
|
200 | if (newVal)
|
201 | this.setAttribute(attr, '');
|
202 | }
|
203 | break;
|
204 | case 'String':
|
205 | |
206 |
|
207 |
|
208 |
|
209 | if (!val || val === 'null' || val === 'undefined') {
|
210 | this.removeAttribute(attr);
|
211 | newVal = '';
|
212 | }
|
213 | else {
|
214 | newVal = type(val);
|
215 | }
|
216 | break;
|
217 | default:
|
218 | newVal = type(val);
|
219 | break;
|
220 | }
|
221 | |
222 |
|
223 |
|
224 | this.__propertiesChanged(prop, newVal);
|
225 | }
|
226 | }
|
227 | |
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | postponedRender() {
|
234 | renderFunction(this.render(Object.assign({}, this.__data)), this.shadowRoot);
|
235 | for (let callback of this.__renderCallbacks) {
|
236 | callback();
|
237 | }
|
238 | this.__renderCallbacks.clear();
|
239 | if (this.afterRender) {
|
240 | this.afterRender(this._firstRender);
|
241 | this._firstRender = false;
|
242 | }
|
243 | }
|
244 | |
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 | refresh(callback) {
|
252 | return __awaiter(this, void 0, void 0, function* () {
|
253 | if (this._wait === true) {
|
254 | return;
|
255 | }
|
256 | if (callback != null) {
|
257 |
|
258 | this.__renderCallbacks.add(callback);
|
259 | }
|
260 | if (!this.__pendingRender) {
|
261 | this.__pendingRender = true;
|
262 | |
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | this.__pendingRender = yield false;
|
270 | this.postponedRender();
|
271 | }
|
272 | });
|
273 | }
|
274 | |
275 |
|
276 |
|
277 |
|
278 |
|
279 | render(data) {
|
280 | return html ``;
|
281 | }
|
282 | |
283 |
|
284 |
|
285 |
|
286 |
|
287 | get $() {
|
288 | const arr = this.shadowRoot.querySelectorAll('[id]');
|
289 | const obj = {};
|
290 | for (const el of arr) {
|
291 | obj[el.id] = el;
|
292 | }
|
293 | return obj;
|
294 | }
|
295 | };
|
296 |
|
\ | No newline at end of file |