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-theme-behavior.html
128 lines (128 loc) · 4.51 KB
/
haxcms-theme-behavior.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
<link rel="import" href="../simple-colors/simple-colors.html">
<script>
// ensure HAXCMSBehaviors exists
window.HAXCMSBehaviors = window.HAXCMSBehaviors || {};
/**
* `HAXCMSBehaviors.Theme` streamline hooking themes up to HAXCMS
*
* @polymerBehavior HAXCMSBehaviors.Schema
**/
window.HAXCMSBehaviors.Theme = {
properties: {
/**
* editting state for the page
*/
editMode: {
type: Boolean,
reflectToAttribute: true,
value: false,
notify: true,
},
/**
* Active item which is in JSON Outline Schema
*/
activeItem: {
type: Object,
notify: true,
observer: '_activeItemChanged',
},
/**
* a manifest json file decoded, in JSON Outline Schema format
*/
manifest: {
type: Object,
notify: true,
observer: '_manifestChanged',
},
},
/**
* Toggle edit mode
*/
_globalEditChanged: function (e) {
this.editMode = e.detail;
},
/**
* Notice that active item has changed.
*/
_activeItemChanged: function (newValue, oldValue) {
if (newValue && typeof newValue.id !== typeof undefined) {
this.fire('json-outline-schema-active-item-changed', newValue);
if (typeof newValue.title !== typeof undefined) {
document.title = this.manifest.title + ' - ' + newValue.title;
}
else {
document.title = this.manifest.title + ' - ' + this.manifest.description;
}
}
else {
document.title = this.manifest.title + ' - ' + this.manifest.description;
}
},
/**
* See if we need to update to match state if something external
* to the theme makes a change in the active item globally
*/
_activeItemUpdate: function (e) {
this.set('activeItem', e.detail);
},
/**
* Setup HAX theme common design needs at a data layer
*/
setupHAXTheme: function (load = true, injector = this.$.contentcontainer) {
if (load) {
document.body.addEventListener('haxcms-edit-mode-changed', this._globalEditChanged.bind(this));
document.body.addEventListener('json-outline-schema-changed', this._manifestUpdate.bind(this));
document.body.addEventListener('haxcms-active-item-changed', this._activeItemUpdate.bind(this));
document.body.addEventListener('haxcms-trigger-update', this._triggerUpdate.bind(this));
// this implies there's the possibility of an authoring experience
if (typeof Polymer.cmsSiteEditor !== typeof undefined) {
Polymer.cmsSiteEditor.requestAvailability(this, injector);
}
}
else {
document.body.removeEventListener('haxcms-active-item-changed', this._activeItemUpdate.bind(this));
document.body.removeEventListener('json-outline-schema-changed', this._manifestUpdate.bind(this));
document.body.removeEventListener('haxcms-edit-mode-changed', this._globalEditChanged.bind(this));
document.body.removeEventListener('haxcms-trigger-update', this._triggerUpdate.bind(this));
}
},
/**
* refreshed data call
*/
_triggerUpdate: function (e) {
this.fire('json-outline-schema-active-item-changed', {});
},
/**
* react to manifest being changed
*/
_manifestUpdate: function (e) {
this.set('manifest', e.detail);
},
/**
* Manifest has changed
*/
_manifestChanged: function (newValue, oldValue) {
if (typeof newValue.title !== typeof undefined) {
document.title = this.manifest.title + ' - ' + this.manifest.description;
}
if (typeof newValue.metadata !== typeof undefined && typeof newValue.metadata.cssVariable !== typeof undefined) {
// json outline schema changed, allow other things to react
// fake way of forcing an update of these items
let color = this.manifest.metadata.cssVariable.replace('--simple-colors-', '');
let ary = color.split('-');
if (ary.length == 2) {
color = ary[0];
this.accentColor = color;
}
else if (ary.length == 3) {
color = ary[0] + ' ' + ary[1];
this.accentColor = color;
}
// set this directly instead of messing w/ accentColor
this.customStyle['--haxcms-color'] = this.customStyle[this.manifest.metadata.cssVariable];
document.body.style.setProperty('--haxcms-color', this.customStyle[this.manifest.metadata.cssVariable]);
this.updateStyles();
}
},
};
</script>