1212
1313var 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