Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit 4aa2a70

Browse files
committed
updating the main templates to handle postmessage calls to update their window.location
1 parent b002387 commit 4aa2a70

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

source/_patternlab-files/pattern-header-footer/footer.html

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,34 @@
99
<script>
1010

1111
// alert the iframe parent that the pattern has loaded. for page follow.
12-
parent.postMessage( { "url": window.location.host, "patternpartial": "{{ patternPartial }}"},window.location.protocol+"//"+window.location.host);
12+
var targetOrigin = (window.location.protocol == "file:") ? "*" : window.location.protocol+"//"+window.location.host;
13+
parent.postMessage( { "patternpartial": "{{ patternPartial }}", "path": window.location.toString() }, targetOrigin);
1314

1415
// if there are clicks on the iframe make sure the nav in the iframe parent closes
1516
var body = document.getElementsByTagName('body');
1617
body[0].onclick = function() {
17-
parent.postMessage({"bodyclick": "bodyclick"},window.location.protocol+"//"+window.location.host)
18+
var targetOrigin = (window.location.protocol == "file:") ? "*" : window.location.protocol+"//"+window.location.host;
19+
parent.postMessage( { "bodyclick": "bodyclick" }, targetOrigin)
1820
};
1921

22+
// watch the iframe source so that it can be sent back to everyone else.
23+
function receiveIframeMessage(event) {
24+
25+
// does the origin sending the message match the current host? if not dev/null the request
26+
if ((window.location.protocol != "file:") && (event.origin !== window.location.protocol+"//"+window.location.host)) {
27+
return;
28+
}
29+
30+
if (event.data.path != undefined) {
31+
var re = /patterns\/(.*)$/;
32+
var path = window.location.protocol+"//"+window.location.host+window.location.pathname.replace(re,'')+event.data.path;
33+
window.location.replace(path);
34+
} else if (event.data.reload != undefined) {
35+
window.location.reload();
36+
}
37+
38+
}
39+
window.addEventListener("message", receiveIframeMessage, false);
2040
</script>
2141
<!-- /DO NOT MODIFY -->
2242

source/_patternlab-files/styleguide.mustache

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,36 @@
2525
<script>
2626
var body = document.getElementsByTagName('body');
2727
body[0].onclick = function() {
28-
parent.postMessage({"bodyclick":"bodyclick"},window.location.protocol+"//"+window.location.host)
28+
var targetOrigin = (window.location.protocol == "file:") ? "*" : window.location.protocol+"//"+window.location.host;
29+
parent.postMessage( { "bodyclick": "bodyclick" }, targetOrigin)
2930
};
3031
3132
var els = document.getElementsByClassName("patternLink");
3233
for (i in els) {
3334
els[i].onclick = function() {
34-
parent.postMessage({"patternpartial":this.getAttribute("data-patternpartial")},window.location.protocol+"//"+window.location.host);
35+
var targetOrigin = (window.location.protocol == "file:") ? "*" : window.location.protocol+"//"+window.location.host;
36+
parent.postMessage( { "patternpartial": this.getAttribute("data-patternpartial"), "path": "foo" }, targetOrigin);
3537
return false;
3638
}
3739
}
40+
41+
// watch the iframe source so that it can be sent back to everyone else.
42+
function receiveIframeMessage(event) {
43+
44+
// does the origin sending the message match the current host? if not dev/null the request
45+
if ((window.location.protocol != "file:") && (event.origin !== window.location.protocol+"//"+window.location.host)) {
46+
return;
47+
}
48+
49+
if (event.data.path != undefined) {
50+
var path = window.location.protocol+"//"+window.location.host+window.location.pathname.replace("styleguide\/html\/styleguide.html","")+event.data.path;
51+
window.location.replace(path);
52+
} else if (event.data.reload != undefined) {
53+
window.location.reload();
54+
}
55+
56+
}
57+
window.addEventListener("message", receiveIframeMessage, false);
3858
</script>
3959
</body>
4060
</html>

source/_patternlab-files/viewall.mustache

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,41 @@
2727
2828
var body = document.getElementsByTagName('body');
2929
body[0].onclick = function() {
30-
parent.postMessage( { "bodyclick":"bodyclick" }, window.location.protocol+"//"+window.location.host)
30+
var targetOrigin = (window.location.protocol == "file:") ? "*" : window.location.protocol+"//"+window.location.host;
31+
parent.postMessage( { "bodyclick":"bodyclick" }, targetOrigin)
3132
};
3233
3334
var els = document.getElementsByClassName("patternLink");
3435
for (i in els) {
3536
els[i].onclick = function() {
36-
parent.postMessage( { "patternpartial": this.getAttribute("data-patternpartial") }, window.location.protocol+"//"+window.location.host);
37+
var targetOrigin = (window.location.protocol == "file:") ? "*" : window.location.protocol+"//"+window.location.host;
38+
parent.postMessage( { "patternpartial": this.getAttribute("data-patternpartial"), "path": "foo" }, targetOrigin);
3739
return false;
3840
}
3941
}
4042
4143
// alert the iframe parent that the pattern has loaded. for page follow.
42-
parent.postMessage( { "url": window.location.host, "patternpartial": "{{ patternPartial }}" },window.location.protocol+"//"+window.location.host);
44+
var targetOrigin = (window.location.protocol == "file:") ? "*" : window.location.protocol+"//"+window.location.host;
45+
parent.postMessage( { "patternpartial": "{{ patternPartial }}", "path": window.location.toString() }, targetOrigin);
46+
47+
// watch the iframe source so that it can be sent back to everyone else.
48+
function receiveIframeMessage(event) {
49+
50+
// does the origin sending the message match the current host? if not dev/null the request
51+
if ((window.location.protocol != "file:") && (event.origin !== window.location.protocol+"//"+window.location.host)) {
52+
return;
53+
}
54+
55+
if (event.data.path != undefined) {
56+
var re = /patterns\/(.*)$/;
57+
var path = window.location.protocol+"//"+window.location.host+window.location.pathname.replace(re,'')+event.data.path;
58+
window.location.replace(path);
59+
} else if (event.data.reload != undefined) {
60+
window.location.reload();
61+
}
62+
63+
}
64+
window.addEventListener("message", receiveIframeMessage, false);
4365
4466
</script>
4567
</body>

0 commit comments

Comments
 (0)