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

Commit 4985374

Browse files
committed
use post message to update patterns
1 parent a8b3f40 commit 4985374

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

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+'" }' );

0 commit comments

Comments
 (0)