|
| 1 | +/*! |
| 2 | + * Basic postMessage Support - v0.1 |
| 3 | + * |
| 4 | + * Copyright (c) 2013 Dave Olsen, http://dmolsen.com |
| 5 | + * Licensed under the MIT license |
| 6 | + * |
| 7 | + * Handles the postMessage stuff in the pattern, view-all, and style guide templates. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +// alert the iframe parent that the pattern has loaded assuming this view was loaded in an iframe |
| 12 | +if (self != top) { |
| 13 | + |
| 14 | + // handle the options that could be sent to the parent window |
| 15 | + // - all get path |
| 16 | + // - pattern & view all get a pattern partial, styleguide gets all |
| 17 | + // - pattern shares lineage |
| 18 | + var options = { "path": window.location.toString() }; |
| 19 | + options.patternpartial = (patternPartial != "") ? patternPartial : "all"; |
| 20 | + if (lineage != "") { |
| 21 | + options.lineage = lineage; |
| 22 | + } |
| 23 | + |
| 24 | + var targetOrigin = (window.location.protocol == "file:") ? "*" : window.location.protocol+"//"+window.location.host; |
| 25 | + parent.postMessage(options, targetOrigin); |
| 26 | + |
| 27 | + // find all links and add an onclick handler for replacing the iframe address so the history works |
| 28 | + var aTags = document.getElementsByTagName('a'); |
| 29 | + for (a in aTags) { |
| 30 | + aTags[a].onclick = function(e) { |
| 31 | + e.preventDefault(); |
| 32 | + window.location.replace(this.getAttribute("href")); |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | +} |
| 37 | + |
| 38 | +// if there are clicks on the iframe make sure the nav in the iframe parent closes |
| 39 | +var body = document.getElementsByTagName('body'); |
| 40 | +body[0].onclick = function() { |
| 41 | + var targetOrigin = (window.location.protocol == "file:") ? "*" : window.location.protocol+"//"+window.location.host; |
| 42 | + parent.postMessage( { "bodyclick": "bodyclick" }, targetOrigin) |
| 43 | +}; |
| 44 | + |
| 45 | +// watch the iframe source so that it can be sent back to everyone else. |
| 46 | +function receiveIframeMessage(event) { |
| 47 | + |
| 48 | + // does the origin sending the message match the current host? if not dev/null the request |
| 49 | + if ((window.location.protocol != "file:") && (event.origin !== window.location.protocol+"//"+window.location.host)) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + // see if it got a path to replace |
| 54 | + if (event.data.path != undefined) { |
| 55 | + |
| 56 | + if (patternPartial != "") { |
| 57 | + |
| 58 | + // handle patterns and the view all page |
| 59 | + var re = /patterns\/(.*)$/; |
| 60 | + var path = window.location.protocol+"//"+window.location.host+window.location.pathname.replace(re,'')+event.data.path; |
| 61 | + window.location.replace(path); |
| 62 | + |
| 63 | + } else { |
| 64 | + |
| 65 | + // handle the style guide |
| 66 | + var path = window.location.protocol+"//"+window.location.host+window.location.pathname.replace("styleguide\/html\/styleguide.html","")+event.data.path; |
| 67 | + window.location.replace(path); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + } else if (event.data.reload != undefined) { |
| 72 | + |
| 73 | + // reload the location if there was a message to do so |
| 74 | + window.location.reload(); |
| 75 | + } |
| 76 | + |
| 77 | +} |
| 78 | +window.addEventListener("message", receiveIframeMessage, false); |
0 commit comments