-
Notifications
You must be signed in to change notification settings - Fork 2
/
px-demo-theme-switcher.html
175 lines (150 loc) · 5.02 KB
/
px-demo-theme-switcher.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
<!--
Copyright (c) 2018, General Electric
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../polymer/polymer.html"/>
<link rel="import" href="px-demo-theme-util.html"/>
<script>
(function(){
/*
* Each element `origin` should be the protocol + hostname + port (if other
* than 80) where both the iFrame and parent will be hosted. This cannot
* be dynamic to prevent cross-site scripting attacks. When developing,
* comment out the 'https://www.predix-ui.com' origin and set the variable
* to the origin you develop from (e.g. 'http://localhost:8000').
*
* Make sure the origin is changed back before pushing changes or this will
* not work in production.
*/
function inFrame() {
try { return window.self !== window.top; }
catch (e) { return true; }
};
Polymer({
is: 'px-demo-theme-switcher',
properties: {
isDarkTheme: {
type: Boolean,
value: false,
observer: '_themeChanged'
}
},
_themeChanged: function(isDarkTheme, wasDarkTheme) {
if (isDarkTheme === false && typeof wasDarkTheme === 'undefined') {
return;
}
if (isDarkTheme && !wasDarkTheme) {
var darkTheme = PxDemo.ThemeUtil.getDarkThemeProperties();
if (darkTheme === null) {
console.log('Dark theme not loaded. Try again later.');
return;
}
Polymer.updateStyles(darkTheme);
}
else if (!isDarkTheme && wasDarkTheme) {
var defaultTheme = PxDemo.ThemeUtil.getDefaultThemeProperties();
if (defaultTheme === null) {
console.log('Theme not loaded. Try again later.');
return;
}
Polymer.updateStyles(defaultTheme);
}
}
});
Polymer({
is: 'px-demo-theme-switcher-frame',
properties: {
origin: {
type: String
},
isDarkTheme: {
type: Boolean,
notify: true
}
},
created: function() {
this._boundMessageHandler = this._handleParentMessage.bind(this);
this._inFrame = inFrame();
},
attached: function() {
if (this._inFrame) {
window.addEventListener('message', this._boundMessageHandler, false);
this._requestParentTheme();
}
},
detached: function() {
if (this._inFrame) {
window.removeEventListener('message', this._boundMessageHandler, false);
}
},
_handleParentMessage: function(evt) {
if (evt.origin !== this.origin || evt.data.type !== 'THEME_STATUS' || typeof evt.data.IS_DARK_THEME !== 'boolean') {
return;
}
this.isDarkTheme = evt.data.IS_DARK_THEME;
},
_requestParentTheme: function() {
if (!this.origin) return;
parent.postMessage({ type: 'REQUEST_THEME_STATUS' }, this.origin);
}
});
Polymer({
is: 'px-demo-theme-switcher-parent',
properties: {
origin: {
type: String
},
isDarkTheme: {
type: Boolean,
value: null,
observer: '_themeChanged'
},
childFrame: {
type: HTMLElement,
value: null,
observer: '_frameChanged'
}
},
created: function() {
this._boundMessageHandler = this._handleChildMessage.bind(this);
},
detached: function() {
if (this.childFrame) {
this.childFrame = null;
}
},
_themeChanged: function() {
if (this.childFrame && this.childFrame.contentWindow && typeof this.isDarkTheme === 'boolean') {
this._sendThemeToChild(this.childFrame.contentWindow, this.isDarkTheme);
}
},
_handleChildMessage: function(evt) {
if (evt.origin !== this.origin || evt.data.type !== 'REQUEST_THEME_STATUS') {
return;
}
this._themeChanged();
},
_sendThemeToChild: function(child, isDarkTheme) {
if (!this.origin) return;
child.postMessage({ type: 'THEME_STATUS', IS_DARK_THEME: isDarkTheme }, this.origin);
},
_frameChanged: function(newFrame, oldFrame) {
if (oldFrame && oldFrame.contentWindow) {
oldFrame.contentWindow.removeEventListener('message', this._boundMessageHandler, false);
}
if (newFrame && newFrame.contentWindow) {
newFrame.contentWindow.addEventListener('message', this._boundMessageHandler, false);
this._themeChanged();
}
}
})
})();
</script>