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

Commit 8a68aeb

Browse files
committed
Merge branch 'hotfix-apacheless'
2 parents b002387 + 4985374 commit 8a68aeb

File tree

7 files changed

+91
-24
lines changed

7 files changed

+91
-24
lines changed

public/listeners/synclisteners.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ function connectContentSync() {
114114
// when receiving a message from WebSocket reload the current frame adding the received timestamp
115115
// as a request var to, hopefully, bust caches... cachi(?)
116116
wsc.onmessage = function (event) {
117-
document.getElementById('sg-viewport').contentWindow.location.reload();
117+
var targetOrigin = (window.location.protocol == "file:") ? "*" : window.location.protocol+"//"+window.location.host;
118+
document.getElementById('sg-viewport').contentWindow.postMessage( { "reload": true }, targetOrigin);
118119
}
119120

120121
// when there's an error update the pattern lab nav bar

public/styleguide/js/styleguide.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@
361361
if ((oGetVars.p != undefined) || (oGetVars.pattern != undefined)) {
362362
patternName = (oGetVars.p != undefined) ? oGetVars.p : oGetVars.pattern;
363363
patternPath = urlHandler.getFileName(patternName);
364-
iFramePath = (patternPath != "") ? window.location.protocol+"//"+window.location.host+window.location.pathname+patternPath : iFramePath;
364+
iFramePath = (patternPath != "") ? window.location.protocol+"//"+window.location.host+window.location.pathname.replace("index.html","")+patternPath : iFramePath;
365365
}
366366

367367
document.getElementById("sg-viewport").contentWindow.location.assign(iFramePath);
@@ -449,8 +449,8 @@
449449
// having it outside fixes an auto-close bug i ran into
450450
$('.sg-nav a').not('.sg-acc-handle').on("click", function(e){
451451

452-
// update the iframe
453-
document.getElementById("sg-viewport").contentWindow.location.replace(this.href);
452+
// update the iframe via the history api handler
453+
urlHandler.pushPattern($(this).attr("data-patternpartial"));
454454

455455
// close up the menu
456456
$(this).parents('.sg-acc-panel').toggleClass('active');
@@ -489,7 +489,7 @@ $('#sg-vp-wrap').click(function(e) {
489489
function receiveIframeMessage(event) {
490490

491491
// does the origin sending the message match the current host? if not dev/null the request
492-
if (event.origin !== window.location.protocol+"//"+window.location.host) {
492+
if ((window.location.protocol != "file:") && (event.origin !== window.location.protocol+"//"+window.location.host)) {
493493
return;
494494
}
495495

@@ -500,8 +500,9 @@ function receiveIframeMessage(event) {
500500
} else if (event.data.patternpartial != undefined) {
501501

502502
if (!urlHandler.skipBack) {
503+
503504
var iFramePath = urlHandler.getFileName(event.data.patternpartial);
504-
urlHandler.pushPattern(event.data.patternpartial);
505+
urlHandler.pushPattern(event.data.patternpartial, event.data.path);
505506
if (wsnConnected) {
506507
wsn.send( '{"url": "'+iFramePath+'", "patternpartial": "'+event.data.patternpartial+'" }' );
507508
}

public/styleguide/js/url-handler.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
var urlHandler = {
1414

15-
// if true it'll make sure iFrames and history aren't updated on back button click
15+
// set-up some default vars
1616
skipBack: false,
17+
targetOrigin: (window.location.protocol == "file:") ? "*" : window.location.protocol+"//"+window.location.host,
1718

1819
/**
1920
* get the real file name for a given pattern name
@@ -117,15 +118,17 @@ var urlHandler = {
117118
/**
118119
* push a pattern onto the current history based on a click
119120
* @param {String} the shorthand partials syntax for a given pattern
121+
* @param {String} the path given by the loaded iframe
120122
*/
121-
pushPattern: function (pattern) {
122-
var data = { "pattern": pattern };
123-
var path = window.location.protocol+"//"+window.location.host+window.location.pathname.replace("index.html","")+urlHandler.getFileName(pattern);
124-
if (document.getElementById("sg-viewport").contentWindow.location.toString() != path) {
125-
urlHandler.skipBack = true;
126-
document.getElementById("sg-viewport").contentWindow.location.replace(path);
123+
pushPattern: function (pattern, givenPath) {
124+
var data = { "pattern": pattern };
125+
var fileName = urlHandler.getFileName(pattern);
126+
var expectedPath = window.location.protocol+"//"+window.location.host+window.location.pathname.replace("public/index.html","public/")+fileName;
127+
if (givenPath != expectedPath) {
128+
document.getElementById("sg-viewport").contentWindow.postMessage( { "path": fileName }, urlHandler.targetOrigin);
127129
} else {
128-
history.pushState(data, "", window.location.protocol+"//"+window.location.host+window.location.pathname.replace("index.html","")+"?p="+pattern);
130+
var addressReplacement = (window.location.protocol == "file:") ? null : window.location.protocol+"//"+window.location.host+window.location.pathname.replace("index.html","")+"?p="+pattern;
131+
history.pushState(data, null, addressReplacement);
129132
}
130133
},
131134

@@ -152,10 +155,10 @@ var urlHandler = {
152155
var iFramePath = "";
153156
iFramePath = this.getFileName(patternName);
154157
if (iFramePath == "") {
155-
iFramePath = window.location.protocol+"//"+window.location.host+window.location.pathname.replace("index.html","")+"styleguide/html/styleguide.html";
158+
iFramePath = "styleguide/html/styleguide.html";
156159
}
157160

158-
document.getElementById("sg-viewport").contentWindow.location.replace(iFramePath);
161+
document.getElementById("sg-viewport").contentWindow.postMessage( { "path": iFramePath }, urlHandler.targetOrigin);
159162

160163
if (wsnConnected) {
161164
wsn.send( '{"url": "'+iFramePath+'", "patternpartial": "'+patternName+'" }' );

source/_patternlab-files/index.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<div id="sg-vp-wrap">
2424
<div id="sg-cover"></div>
2525
<div id="sg-gen-container">
26-
<iframe id="sg-viewport"></iframe>
26+
<iframe id="sg-viewport" sandbox="allow-same-origin allow-scripts"></iframe>
2727
<div id="sg-rightpull-container">
2828
<div id="sg-rightpull"></div>
2929
</div>

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)