This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
haxcms-beaker.html
213 lines (209 loc) · 8.94 KB
/
haxcms-beaker.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../beaker-broker/beaker-broker.html">
<!--
`haxcms-beaker`
a simple element to check for and fetch JWTs
@demo demo/index.html
@microcopy - the mental model for this element
- jwt - a json web token which is an encrypted security token to talk
-->
<dom-module id="haxcms-beaker">
<template>
<beaker-broker id="beaker"></beaker-broker>
</template>
<script>
Polymer({
is: 'haxcms-beaker',
properties: {
/**
* 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
*/
created: function () {
document.body.addEventListener('jwt-token', this._jwtTokenFired.bind(this));
document.body.addEventListener('json-outline-schema-active-item-changed', this.initialItem.bind(this));
document.body.addEventListener('json-outline-schema-active-body-changed', this.initialBody.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-page', this.savePage.bind(this));
// listen for app being selected
document.body.addEventListener('hax-app-picker-selection', this._appPicked.bind(this));
},
/**
* detached life cycle
*/
detached: function () {
document.body.removeEventListener('jwt-token', this._jwtTokenFired.bind(this));
document.body.removeEventListener('json-outline-schema-active-item-changed', this.initialItem.bind(this));
document.body.removeEventListener('json-outline-schema-active-body-changed', this.initialBody.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-page', this.savePage.bind(this));
// listen for app being selected
document.body.addEventListener('hax-app-picker-selection', this._appPicked.bind(this));
},
_appPicked: function (e) {
if (e.detail.connection.protocol === 'dat') {
e.preventDefault();
e.stopPropagation();
let reader = new FileReader();
reader.onload = (event) => {
let fileLocation = 'files/' + Polymer.HaxStore.instance.haxManager.$.fileupload.files[0].name;
this.$.beaker.write(fileLocation, event.target.result);
Polymer.HaxStore.instance.haxManager.$.url.value = fileLocation;
Polymer.HaxStore.instance.haxManager.newAssetConfigure();
};
reader.readAsArrayBuffer(Polymer.HaxStore.instance.haxManager.$.fileupload.files[0])
}
},
initialItem: function (e) {
this.activeItem = e.detail;
this.__item = e.detail;
},
initialManifest: function (e) {
this.manifest = e.detail;
this.__manifest = e.detail;
},
initialBody: function (e) {
this.__body = e.detail;
},
/**
* Save page data
*/
savePage: async function (e) {
this.activeItem = e.detail;
// make sure this location exists
await this.$.beaker.write(this.activeItem.location, Polymer.HaxStore.instance.activeHaxBody.haxToContent());
Polymer.cmsSiteEditor.instance.haxCmsSiteEditorElement.$.toast.show('Page updated!');
Polymer.cmsSiteEditor.instance.haxCmsSiteEditorElement.fire('haxcms-trigger-update-page', true);
},
/**
* Outline save event.
*/
saveOutline: async function (e) {
// snag global to be sure we have it set first
this.manifest = Polymer.cmsSiteEditor.instance.haxCmsSiteEditorElement.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.$.beaker.archive.mkdir('pages/' + id);
// make the page
this.$.beaker.write('pages/' + id + '/index.html', '<p>My great new content!</p>');
this.manifest.items[index] = element;
}
});
this.$.beaker.write('site.json', JSON.stringify(this.manifest, null, 2));
// simulate save events since they wont fire
Polymer.cmsSiteEditor.instance.haxCmsSiteEditorElement.$.toast.show('Outline saved!');
Polymer.cmsSiteEditor.instance.haxCmsSiteEditorElement.fire('haxcms-trigger-update', true);
this.fire('json-outline-schema-changed', this.manifest);
},
/**
* Manifest save event.
*/
saveManifest: async function (e) {
this.manifest = e.detail;
await this.$.beaker.write('site.json', JSON.stringify(this.manifest, null, 2));
// simulate save events since they wont fire
Polymer.cmsSiteEditor.instance.haxCmsSiteEditorElement.$.toast.show('Site details saved!');
Polymer.cmsSiteEditor.instance.haxCmsSiteEditorElement.fire('haxcms-trigger-update', true);
this.fire('json-outline-schema-changed', this.manifest);
},
/**
* JWT token fired, let's capture it
*/
_jwtTokenFired: function (e) {
this.jwt = e.detail;
},
/**
* Generate a uinque ID
*/
generateResourceID: function (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
*/
attached: async function () {
let beaker = this.$.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 && 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 {
this.importHref(this.resolveUrl('haxcms-site-editor.html'), (e) => {
let haxCmsSiteEditorElement = document.createElement('haxcms-site-editor');
haxCmsSiteEditorElement.jwt = this.jwt;
//haxCmsSiteEditorElement.savePagePath = "<?php print $HAXCMS->basePath . 'system/savePage.php';?>";
//haxCmsSiteEditorElement.saveManifestPath = "<?php print $HAXCMS->basePath . 'system/saveManifest.php';?>";
//haxCmsSiteEditorElement.saveOutlinePath = "<?php print $HAXCMS->basePath . 'system/saveOutline.php';?>";
//haxCmsSiteEditorElement.publishPath = null;
haxCmsSiteEditorElement.appStore = appstore;
// pass along the initial state management stuff that may be missed
// based on timing on the initial setup
if (typeof this.__item !== typeof undefined) {
haxCmsSiteEditorElement.activeItem = this.__item;
}
if (typeof this.__manifest !== typeof undefined) {
haxCmsSiteEditorElement.manifest = this.__manifest;
}
if (typeof this.__body !== typeof undefined) {
haxCmsSiteEditorElement.__body = this.__body;
}
Polymer.cmsSiteEditor.instance.haxCmsSiteEditorElement = haxCmsSiteEditorElement;
Polymer.cmsSiteEditor.instance.appendTarget.appendChild(haxCmsSiteEditorElement);
}, (e) => {
//import failed
});
}
catch (err) {
// error in the event this is a double registration
}
}
},
});
</script>
</dom-module>