This repository was 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
Expand file tree
/
Copy pathhaxcms-site-builder.html
More file actions
377 lines (372 loc) · 12.9 KB
/
Copy pathhaxcms-site-builder.html
File metadata and controls
377 lines (372 loc) · 12.9 KB
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-ajax/iron-ajax.html">
<link rel="import" href="../app-route/app-location.html">
<link rel="import" href="../app-route/app-route.html">
<link rel="import" href="../paper-progress/paper-progress.html">
<!--
`haxcms-site-builder`
A LRN element
@demo demo/index.html
@microcopy - the mental model for this element
- This is a factory element, it doesn't do much on its own visually
- it loads a site.json file and then utilizes this data in order to construct
what theme it should load (element) in order to get everything off and running
-->
<dom-module id="haxcms-site-builder">
<template>
<style>
:host {
display: block;
}
:host #slot {
background-color: white;
opacity: .2;
transition: all 1s linear;
visibility: hidden;
}
:host[loading] #slot {
opacity: .8;
}
:host[theme-loaded] #slot {
opacity: 1;
visibility: visible;
}
paper-progress {
display: block;
width: 100%;
--paper-progress-active-color: rgba(255, 255, 255, 0.5);
--paper-progress-container-color: transparent;
}
</style>
<haxcms-editor-builder></haxcms-editor-builder>
<paper-progress hidden$="[[!loading]]" value="100" indeterminate bottom-item></paper-progress>
<app-location route="{{route}}" query-params="{{queryParams}}"></app-location>
<app-route
route="{{route}}"
pattern=":page"
data="{{data}}"
tail="{{tail}}"
query-params="{{queryParams}}">
</app-route>
<iron-ajax
id="manifest"
url="[[outlineLocation]][[file]][[__timeStamp]]"
handle-as="json"
debounce-duration="250"
last-response="{{manifest}}"></iron-ajax>
<iron-ajax
id="activecontent"
url="[[outlineLocation]][[activeItem.location]][[__timeStamp]]"
handle-as="text"
loading="{{loading}}"
debounce-duration="250"
last-response="{{_activeItemContent}}"></iron-ajax>
<div id="slot">
<slot></slot>
</div>
</template>
<script>
Polymer({
is: 'haxcms-site-builder',
properties: {
/**
* queryParams
*/
queryParams: {
type: Object,
},
/**
* Loading status of the page to render.
*/
loading: {
type: Boolean,
value: false,
reflectToAttribute: true,
},
/**
* support for alternate locations.
*/
outlineLocation: {
type: String,
},
/**
* Manifest from file
*/
manifest: {
type: Object,
notify: true,
observer: '_manifestChanged',
},
/**
* Theme, used to boot a design element
*/
themeElementName: {
type: String,
reflectToAttribute: true,
observer: '_themeNameChanged',
},
/**
* Theme, used to boot a design element
*/
themeElement: {
type: Object,
},
/**
* registry to map theme names to locations
*/
themeData: {
type: Object,
value: {
"outline-player": "../outline-player/outline-player.html",
"simple-blog": "../simple-blog/simple-blog.html",
"infinite-scroll-site": "../infinite-scroll-site/infinite-scroll-site.html",
"haxcms-dev-theme": "../haxcms-elements/haxcms-dev-theme.html",
},
},
/**
* Imported items so we can allow theme flipping dynamically
*/
__imported: {
type: Object,
value: {},
},
/**
* theme loaded to indicate to the theme we have a theme ready to go
*/
themeLoaded: {
type: Boolean,
reflectToAttribute: true,
value: false,
},
/**
* Active item which is in JSON Outline Schema
*/
activeItem: {
type: Object,
notify: true,
observer: '_activeItemChanged',
},
/**
* Active item content
*/
_activeItemContent: {
type: String,
observer: '_activeItemContentChanged',
},
/**
* Location of the site.json file
*/
file: {
type: String,
observer: '_fileChanged',
},
},
/**
* ready life cycle
*/
created: function () {
document.body.addEventListener('haxcms-trigger-update', this._triggerUpdatedData.bind(this));
document.body.addEventListener('haxcms-trigger-update-page', this._triggerUpdatedPage.bind(this));
document.body.addEventListener('json-outline-schema-active-item-changed', this._setActiveItem.bind(this));
},
/**
* Detached life cycle
*/
detached: function () {
document.body.removeEventListener('haxcms-trigger-update', this._triggerUpdatedData.bind(this));
document.body.removeEventListener('haxcms-trigger-update-page', this._triggerUpdatedPage.bind(this));
document.body.removeEventListener('json-outline-schema-active-item-changed', this._setActiveItem.bind(this));
},
/**
* Query params changed
*/
_setupActiveFromQuery: function () {
if (typeof this.queryParams.page !== typeof undefined && typeof this.manifest.items !== typeof undefined) {
let find = this.manifest.items.filter(item => {
if (item.id !== this.queryParams.page) {
return false;
}
return true;
});
// if we found one, make it the active page
if (find.length > 0) {
let found = find.pop();
if (typeof Polymer.cmsSiteEditor !== typeof undefined) {
Polymer.cmsSiteEditor.initialActiveItem = found;
}
// @todo figure out why this is required in order for all timing to line up
setTimeout(() => {
this.fire('haxcms-active-item-changed', found);
}, 250);
}
}
},
/**
* set global active item
*/
_setActiveItem: function (e) {
this.set('activeItem', e.detail);
this.set('queryParams.page', e.detail.id);
},
/**
* React to content being loaded from a page.
*/
_activeItemContentChanged: function (newValue, oldValue) {
if (newValue) {
// only append if not empty
if (newValue !== null) {
this.wipeSlot(this.themeElement, '*');
newValue = this.encapScript(newValue);
this.async(() => {
let frag = document.createRange().createContextualFragment(newValue);
Polymer.dom(this.themeElement).appendChild(frag);
});
}
this.fire('json-outline-schema-active-body-changed', newValue);
}
},
/**
* Encapsulate script and style tags correctly
*/
encapScript: function (html) {
html = html.replace(/<script[\s\S]*?>/gi, "<script>");
html = html.replace(/<\/script>/gi, "</script>");
html = html.replace(/<style[\s\S]*?>/gi, "<style>");
html = html.replace(/<\/style>/gi, "</style>");
// special case, it's inside a template tag
html = html.replace(/<template[\s\S]*?>[\s\S]*?<script[\s\S]*?>[\s\S]*?<\/script>/gi, function (match, contents, offset, input_string) {
match = match.replace("<script>", "<script>");
match = match.replace("</script>", "<\/script>");
match = match.replace("<style>", "<style>");
match = match.replace("</style>", "<\/style>");
return match;
});
return html;
},
/**
* Active item updated, let's request the content from it
*/
_activeItemChanged: function (newValue, oldValue) {
if (typeof newValue.id !== typeof undefined) {
this.__timeStamp = '?' + (Math.floor(Date.now() / 1000));
this.$.activecontent.generateRequest();
}
// we had something, now we don't. wipe out the content area of the theme
else if (typeof newValue.id === typeof undefined && typeof oldValue.id !== typeof undefined) {
this.async(() => {
this.wipeSlot(this.themeElement, '*');
});
// fire event w/ nothing, this is because there is no content
this.fire('json-outline-schema-active-body-changed', null);
}
},
/**
* got a message that we need to update our json manifest data
*/
_triggerUpdatedData: function (e) {
// append a value so we know we get fresher data
this.__timeStamp = '?' + (Math.floor(Date.now() / 1000));
this.$.manifest.generateRequest();
},
/**
* got a message that we need to update our page content
*/
_triggerUpdatedPage: function (e) {
// append a value so we know we get fresher data
this.__timeStamp = '?' + (Math.floor(Date.now() / 1000));
this.$.activecontent.generateRequest();
},
/**
* File changed so let's pull from the location
*/
_fileChanged: function (newValue, oldValue) {
if (typeof newValue !== typeof undefined) {
this.$.manifest.generateRequest();
}
},
/**
* notice manifest changes and ensure slot is rebuilt.
*/
_manifestChanged: function (newValue, oldValue) {
if (typeof newValue !== typeof undefined && typeof newValue.id !== typeof undefined) {
this.themeElementName = newValue.metadata.theme;
// account for editor not being there
if (typeof Polymer.cmsSiteEditor !== typeof undefined) {
Polymer.cmsSiteEditor.jsonOutlineSchema = newValue;
}
this.fire('json-outline-schema-changed', newValue);
}
},
/**
* notice theme changes and ensure slot is rebuilt.
*/
_themeNameChanged: function (newValue, oldValue) {
if (newValue && oldValue) {
if (typeof Polymer.cmsSiteEditor.instance.haxCmsSiteEditorElement !== typeof undefined) {
Polymer.cmsSiteEditor.instance.appendChild(Polymer.cmsSiteEditor.instance.haxCmsSiteEditorElement);
}
}
if (newValue) {
this.themeLoaded = false;
var themeName = newValue;
// trap for blowing up the world ;)
if (typeof this.themeData[themeName] === typeof undefined) {
console.log('HAXCMS developer: ' + themeName + ' is not a valid theme name');
this.themeElementName = 'simple-blog';
return false;
}
// wipe out what we got
this.wipeSlot(this, '*');
// create the 'theme' as a new element
this.themeElement = document.createElement(themeName);
// give it our manifest
this.themeElement.manifest = this.manifest;
// weird but definition already here so we should be able
// to just use this without an import, it's possible..
if (typeof this.__imported[themeName] !== typeof undefined) {
Polymer.dom(this).appendChild(this.themeElement);
this.themeLoaded = true;
}
else {
// import the reference to the item dynamically, if we can
try {
this.importHref(this.resolveUrl(this.themeData[themeName]), (e) => {
// add it into ourselves so it unpacks and we kick this off!
Polymer.dom(this).appendChild(this.themeElement);
this.__imported[themeName] = themeName;
this.themeLoaded = true;
});
}
catch (err) {
// error in the event this is a double registration
// also strange to be able to reach this but technically possible
Polymer.dom(this).appendChild(this.themeElement);
this.themeLoaded = true;
}
}
// establish initial routing
this._setupActiveFromQuery();
}
},
/**
* Wipe slotted content
*/
wipeSlot: function (element, slot = '*') {
// 100% clean slate
if (slot === '*') {
while (Polymer.dom(element).firstChild !== null) {
Polymer.dom(element).removeChild(Polymer.dom(element).firstChild);
}
}
else {
for (var i in Polymer.dom(element).childNodes) {
// test for element nodes to be safe
if (typeof Polymer.dom(element).childNodes[i] !== typeof undefined && Polymer.dom(element).childNodes[i].slot === slot) {
Polymer.dom(element).removeChild(Polymer.dom(element).childNodes[i]);
}
}
}
},
});
</script>
</dom-module>