1
1
/*!
2
2
* Basic postMessage Support - v0.1
3
3
*
4
- * Copyright (c) 2013 Dave Olsen, http://dmolsen.com
4
+ * Copyright (c) 2013-2014 Dave Olsen, http://dmolsen.com
5
5
* Licensed under the MIT license
6
6
*
7
7
* Handles the postMessage stuff in the pattern, view-all, and style guide templates.
@@ -15,9 +15,11 @@ if (self != top) {
15
15
// - all get path
16
16
// - pattern & view all get a pattern partial, styleguide gets all
17
17
// - pattern shares lineage
18
- var options = { "path" : window . location . toString ( ) } ;
19
- options . patternpartial = ( patternPartial != "" ) ? patternPartial : "all" ;
20
- if ( lineage != "" ) {
18
+ var path = window . location . toString ( ) ;
19
+ var parts = path . split ( "?" ) ;
20
+ var options = { "path" : parts [ 0 ] } ;
21
+ options . patternpartial = ( patternPartial !== "" ) ? patternPartial : "all" ;
22
+ if ( lineage !== "" ) {
21
23
options . lineage = lineage ;
22
24
}
23
25
@@ -26,20 +28,22 @@ if (self != top) {
26
28
27
29
// find all links and add an onclick handler for replacing the iframe address so the history works
28
30
var aTags = document . getElementsByTagName ( 'a' ) ;
29
- for ( a in aTags ) {
30
- aTags [ a ] . onclick = function ( e ) {
31
+ for ( var i = 0 ; i < aTags . length ; i ++ ) {
32
+ aTags [ i ] . onclick = function ( e ) {
31
33
e . preventDefault ( ) ;
32
- window . location . replace ( this . getAttribute ( "href" ) ) ;
34
+ var href = this . getAttribute ( "href" ) ;
35
+ if ( href != "#" ) {
36
+ window . location . replace ( href ) ;
37
+ }
33
38
} ;
34
39
}
35
-
40
+
36
41
// bind the keyboard shortcuts for various viewport resizings + pattern search
37
42
var keys = [ "s" , "m" , "l" , "d" , "h" , "f" ] ;
38
43
for ( var i = 0 ; i < keys . length ; i ++ ) {
39
44
jwerty . key ( 'ctrl+shift+' + keys [ i ] , function ( k , t ) {
40
45
return function ( e ) {
41
- var obj = JSON . stringify ( { "keyPress" : "ctrl+shift+" + k } ) ;
42
- parent . postMessage ( obj , t ) ;
46
+ parent . postMessage ( { "keyPress" : "ctrl+shift+" + k } , t ) ;
43
47
return false ;
44
48
}
45
49
} ( keys [ i ] , targetOrigin ) ) ;
@@ -51,8 +55,7 @@ if (self != top) {
51
55
jwerty . key ( 'ctrl+shift+' + i , function ( k , t ) {
52
56
return function ( e ) {
53
57
var targetOrigin = ( window . location . protocol == "file:" ) ? "*" : window . location . protocol + "//" + window . location . host ;
54
- var obj = JSON . stringify ( { "keyPress" : "ctrl+shift+" + k } ) ;
55
- parent . postMessage ( obj , t ) ;
58
+ parent . postMessage ( { "keyPress" : "ctrl+shift+" + k } , t ) ;
56
59
return false ;
57
60
}
58
61
} ( i , targetOrigin ) ) ;
@@ -65,40 +68,44 @@ if (self != top) {
65
68
var body = document . getElementsByTagName ( 'body' ) ;
66
69
body [ 0 ] . onclick = function ( ) {
67
70
var targetOrigin = ( window . location . protocol == "file:" ) ? "*" : window . location . protocol + "//" + window . location . host ;
68
- parent . postMessage ( { "bodyclick" : "bodyclick" } , targetOrigin )
71
+ parent . postMessage ( { "bodyclick" : "bodyclick" } , targetOrigin ) ;
69
72
} ;
70
73
71
74
// watch the iframe source so that it can be sent back to everyone else.
72
75
function receiveIframeMessage ( event ) {
73
-
76
+
77
+ var path ;
78
+ var data = ( typeof event . data !== "string" ) ? event . data : JSON . parse ( event . data ) ;
79
+
74
80
// does the origin sending the message match the current host? if not dev/null the request
75
81
if ( ( window . location . protocol != "file:" ) && ( event . origin !== window . location . protocol + "//" + window . location . host ) ) {
76
82
return ;
77
83
}
78
84
79
85
// see if it got a path to replace
80
- if ( event . data . path != undefined ) {
86
+ if ( data . path != = undefined ) {
81
87
82
- if ( patternPartial != "" ) {
88
+ if ( patternPartial !== "" ) {
83
89
84
90
// handle patterns and the view all page
85
- var re = / p a t t e r n s \/ ( .* ) $ / ;
86
- var path = window . location . protocol + "//" + window . location . host + window . location . pathname . replace ( re , '' ) + event . data . path ;
91
+ var re = / p a t t e r n s \/ ( .* ) $ / ;
92
+ path = window . location . protocol + "//" + window . location . host + window . location . pathname . replace ( re , '' ) + data . path + '?' + Date . now ( ) ;
87
93
window . location . replace ( path ) ;
88
94
89
95
} else {
90
96
91
97
// handle the style guide
92
- var path = window . location . protocol + "//" + window . location . host + window . location . pathname . replace ( "styleguide\/html\/styleguide.html" , "" ) + event . data . path ;
98
+ path = window . location . protocol + "//" + window . location . host + window . location . pathname . replace ( "styleguide\/html\/styleguide.html" , "" ) + data . path + '?' + Date . now ( ) ;
93
99
window . location . replace ( path ) ;
94
100
95
101
}
96
102
97
- } else if ( event . data . reload != undefined ) {
103
+ } else if ( data . reload != = undefined ) {
98
104
99
105
// reload the location if there was a message to do so
100
106
window . location . reload ( ) ;
107
+
101
108
}
102
109
103
110
}
104
- window . addEventListener ( "message" , receiveIframeMessage , false ) ;
111
+ window . addEventListener ( "message" , receiveIframeMessage , false ) ;
0 commit comments