/
haxcms-backend-beaker.js
456 lines (455 loc) · 13.6 KB
/
haxcms-backend-beaker.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/**
* Copyright 2018 The Pennsylvania State University
* @license Apache-2.0, see License.md for full text.
*/
import { LitElement, html } from "lit-element/lit-element.js";
import { store } from "@lrnwebcomponents/haxcms-elements/lib/core/haxcms-site-store.js";
import "@lrnwebcomponents/beaker-broker/beaker-broker.js";
/**
* `haxcms-backend-beaker`
* `a simple element to check for and fetch JWTs`
*
* @microcopy - the mental model for this element
* - jwt - a json web token which is an encrypted security token to talk
*/
class HAXCMSBackendBeaker extends LitElement {
/**
* Store the tag name to make it easier to obtain directly.
*/
static get tag() {
return "haxcms-backend-beaker";
}
// render function
render() {
return html`
<beaker-broker id="beaker"></beaker-broker>
`;
}
static get properties() {
return {
/**
* JSON Web token, it'll come from a global call if it's available
*/
jwt: {
type: String
},
/**
* Store manifest that makes up the site.
*/
manifest: {
type: Object
},
/**
* Track activeItem
*/
activeItem: {
type: Object
}
};
}
/**
* Attached life cycle
*/
constructor() {
super();
document.body.addEventListener("jwt-token", this._jwtTokenFired.bind(this));
// HAX CMS events to intercept
document.body.addEventListener(
"haxcms-save-site-data",
this.saveManifest.bind(this)
);
document.body.addEventListener(
"haxcms-save-outline",
this.saveOutline.bind(this)
);
document.body.addEventListener(
"haxcms-save-node",
this.saveNode.bind(this)
);
document.body.addEventListener(
"haxcms-delete-node",
this.deleteNode.bind(this)
);
document.body.addEventListener(
"haxcms-create-node",
this.createNode.bind(this)
);
// listen for app being selected
document.body.addEventListener(
"hax-app-picker-selection",
this._appPicked.bind(this)
);
}
/**
* detached life cycle
*/
disconnectedCallback() {
document.body.removeEventListener(
"jwt-token",
this._jwtTokenFired.bind(this)
);
// HAX CMS events to intercept
document.body.removeEventListener(
"haxcms-save-site-data",
this.saveManifest.bind(this)
);
document.body.removeEventListener(
"haxcms-save-outline",
this.saveOutline.bind(this)
);
document.body.removeEventListener(
"haxcms-save-node",
this.saveNode.bind(this)
);
document.body.removeEventListener(
"haxcms-delete-node",
this.deleteNode.bind(this)
);
document.body.removeEventListener(
"haxcms-create-node",
this.createNode.bind(this)
);
// listen for app being selected
document.body.removeEventListener(
"hax-app-picker-selection",
this._appPicked.bind(this)
);
super.disconnectedCallback();
}
_appPicked(e) {
if (e.detail.connection.protocol === "dat") {
e.preventDefault();
e.stopPropagation();
let reader = new FileReader();
reader.onload = event => {
let fileLocation =
"files/" +
window.HaxStore.instance.haxManager.shadowRoot.querySelector(
"#fileupload"
).files[0].name;
this.shadowRoot
.querySelector("#beaker")
.write(fileLocation, event.target.result);
window.HaxStore.instance.haxManager.shadowRoot.querySelector(
"#url"
).value = fileLocation;
window.HaxStore.instance.haxManager.newAssetConfigure();
};
reader.readAsArrayBuffer(
window.HaxStore.instance.haxManager.shadowRoot.querySelector(
"#fileupload"
).files[0]
);
}
}
/**
* Save page data
*/
async saveNode(e) {
this.activeItem = e.detail;
// make sure this location exists
await this.shadowRoot
.querySelector("#beaker")
.write(
this.activeItem.location,
window.HaxStore.instance.activeHaxBody.haxToContent()
);
store.cmsSiteEditor.instance.shadowRoot
.querySelector("#toast")
.show("Page updated!");
const evt = new CustomEvent("haxcms-trigger-update-node", {
bubbles: true,
composed: true,
cancelable: false,
detail: true
});
store.cmsSiteEditor.instance.dispatchEvent(evt);
}
/**
* Outline save event.
*/
async saveOutline(e) {
// snag global to be sure we have it set first
this.manifest = store.cmsSiteEditor.instance.manifest;
// set items specifically since it's just an outline update
this.manifest.items = e.detail;
// loop through and match the data our backend generates
this.manifest.items.forEach((element, index) => {
// test for things that are not set and build the whole thing out
if (typeof element.location === typeof undefined) {
let id = this.generateResourceID("item-");
element.id = id;
element.location = "pages/" + id + "/index.html";
element.order = index;
element.description = "";
element.metadata = {
created: Math.floor(Date.now() / 1000),
updated: Math.floor(Date.now() / 1000)
};
// make a directory
this.shadowRoot.querySelector("#beaker").archive.mkdir("pages/" + id);
// make the page
this.shadowRoot
.querySelector("#beaker")
.write("pages/" + id + "/index.html", "<p>Ex uno Plures</p>");
this.manifest.items[index] = element;
}
});
this.shadowRoot
.querySelector("#beaker")
.write("site.json", JSON.stringify(this.manifest, null, 2));
// simulate save events since they wont fire
store.cmsSiteEditor.instance.shadowRoot
.querySelector("#toast")
.show("Outline saved!");
store.cmsSiteEditor.instance.dispatchEvent(
new CustomEvent("haxcms-trigger-update", {
bubbles: true,
composed: true,
cancelable: false,
detail: true
})
);
store.cmsSiteEditor.instance.dispatchEvent(
new CustomEvent("json-outline-schema-changed", {
bubbles: true,
composed: true,
cancelable: false,
detail: this.manifest
})
);
}
/**
* Outline save event.
*/
async deleteNode(e) {
let page = e.detail.item;
// snag global to be sure we have it set first
this.manifest = store.cmsSiteEditor.instance.manifest;
// set items specifically since it's just an outline update
this.manifest.items = e.detail;
// loop through and match the data our backend generates
this.manifest.items.forEach((element, index) => {
if (element.id === page.id) {
this.splice("manifest.items", index, 1);
}
});
this.shadowRoot
.querySelector("#beaker")
.write("site.json", JSON.stringify(this.manifest, null, 2));
// simulate save events since they wont fire
store.cmsSiteEditor.instance.shadowRoot
.querySelector("#toast")
.show(`${page.title} deleted`);
store.cmsSiteEditor.instance.dispatchEvent(
new CustomEvent("haxcms-trigger-update", {
bubbles: true,
composed: true,
cancelable: false,
detail: true
})
);
store.cmsSiteEditor.instance.dispatchEvent(
new CustomEvent("json-outline-schema-changed", {
bubbles: true,
composed: true,
cancelable: false,
detail: this.manifest
})
);
}
/**
* createNode
*/
async createNode(e) {
let page = e.detail.values;
// snag global to be sure we have it set first
this.manifest = store.cmsSiteEditor.instance.manifest;
// set items specifically since it's just an outline update
this.manifest.items = e.detail;
// loop through and match the data our backend generates
this.manifest.items.forEach((element, index) => {
// test for things that are not set and build the whole thing out
if (typeof element.location === typeof undefined) {
if (!page.id) {
page.id = this.generateResourceID("item-");
}
if (!page.location) {
page.location = "pages/" + page.id + "/index.html";
}
let directory = page.location.replace("/index.html", "");
element.id = page.id;
element.location = page.location;
element.order = page.order;
element.indent = page.indent;
element.parent = page.parent;
element.description = page.description;
element.metadata.created = Math.floor(Date.now() / 1000);
element.metadata.updated = Math.floor(Date.now() / 1000);
// make a directory
this.shadowRoot.querySelector("#beaker").archive.mkdir(directory);
// make the page
this.shadowRoot
.querySelector("#beaker")
.write(page.location, "<p>My great new content!</p>");
this.manifest.items[index] = element;
}
});
this.shadowRoot
.querySelector("#beaker")
.write("site.json", JSON.stringify(this.manifest, null, 2));
// simulate save events since they wont fire
store.cmsSiteEditor.instance.shadowRoot
.querySelector("#toast")
.show(`${page.title} created!`);
store.cmsSiteEditor.instance.dispatchEvent(
new CustomEvent("haxcms-trigger-update", {
bubbles: true,
composed: true,
cancelable: false,
detail: true
})
);
store.cmsSiteEditor.instance.dispatchEvent(
new CustomEvent("json-outline-schema-changed", {
bubbles: true,
composed: true,
cancelable: false,
detail: this.manifest
})
);
}
/**
* Manifest save event.
*/
async saveManifest(e) {
this.manifest = e.detail;
// limits options but makes it possible to switch core themes
if (typeof this.manifest.metadata.theme === "string") {
const themeData = {
"haxcms-dev-theme": {
element: "haxcms-dev-theme",
path: "@lrnwebcomponents/haxcms-elements/lib/haxcms-dev-theme.js",
name: "Developer theme"
},
"outline-player": {
element: "outline-player",
path: "@lrnwebcomponents/outline-player/outline-player.js",
name: "Outline player"
},
"simple-blog": {
element: "simple-blog",
path: "@lrnwebcomponents/simple-blog/simple-blog.js",
name: "Simple blog"
}
};
// if it's not a core theme we can't really do it
if (themeData[this.manifest.metadata.theme]) {
this.manifest.metadata.theme = themeData[this.manifest.metadata.theme];
}
}
await this.shadowRoot
.querySelector("#beaker")
.write("site.json", JSON.stringify(this.manifest, null, 2));
// simulate save events since they wont fire
store.cmsSiteEditor.instance.shadowRoot
.querySelector("#toast")
.show("Site details saved!");
store.cmsSiteEditor.instance.dispatchEvent(
new CustomEvent("haxcms-trigger-update", {
bubbles: true,
composed: true,
cancelable: false,
detail: true
})
);
store.cmsSiteEditor.instance.dispatchEvent(
new CustomEvent("json-outline-schema-changed", {
bubbles: true,
composed: true,
cancelable: false,
detail: this.manifest
})
);
}
/**
* JWT token fired, let's capture it
*/
_jwtTokenFired(e) {
this.jwt = e.detail;
store.jwt = this.jwt;
if (store.cmsSiteEditor && store.cmsSiteEditor.instance) {
store.cmsSiteEditor.instance.jwt = this.jwt;
}
}
/**
* Generate a uinque ID
*/
generateResourceID(base = "") {
function idPart() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return (
base +
idPart() +
idPart() +
"-" +
idPart() +
"-" +
idPart() +
"-" +
idPart() +
"-" +
idPart() +
idPart() +
idPart()
);
}
/**
* Attached life cycle
*/
async connectedCallback() {
super.connectedCallback();
let beaker = this.shadowRoot.querySelector("#beaker");
this.jwt = beaker.archive.url;
var info = await beaker.archive.getInfo();
// test that we have a url (we'll call it jwt for now) and that we own the site
if (this.jwt != null && typeof this.jwt == "string" && info.isOwner) {
var appstore = JSON.parse(await beaker.read("appstore.json"));
// attempt to dynamically import the hax cms site editor
// which will appear to be injecting into the page
// but because of this approach it should be non-blocking
try {
import("@lrnwebcomponents/haxcms-elements/lib/core/haxcms-site-editor.js").then(
e => {
store.cmsSiteEditorAvailability();
store.cmsSiteEditor.instance.jwt = this.jwt;
store.cmsSiteEditor.instance.appStore = appstore;
},
e => {
//import failed
}
);
} catch (err) {
// error in the event this is a double registration
}
} else {
// other things will have to sort out the fact that while we
// DO have a dynamic backend, we didn't get a hit on the JWT
// meaning that we are in a dynamic environment but logged out
// at the moment (or viewing a site we don't have authorization to)
window.dispatchEvent(
new CustomEvent("haxcms-not-logged-in", {
bubbles: true,
composed: true,
cancelable: false,
detail: this
})
);
}
}
}
window.customElements.define(HAXCMSBackendBeaker.tag, HAXCMSBackendBeaker);
export { HAXCMSBackendBeaker };