Skip to content

Commit 6784419

Browse files
committed
Merge remote-tracking branch 'remotes/nate/gh-pages-module-loader'
2 parents 09b47fe + 6d8e9c0 commit 6784419

File tree

11 files changed

+308
-300
lines changed

11 files changed

+308
-300
lines changed

client/frame.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>Try PureScript!</title>
5+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
6+
<meta content="utf-8" http-equiv="encoding">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<script src="js/frame.js"></script>
9+
</head>
10+
<body>
11+
<main id="main"></main>
12+
</body>
13+
</html>

client/index.html

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,6 @@
4747
</a>
4848
</li>
4949
</ul>
50-
</li><li class="menu-item menu-dropdown no-mobile">
51-
<label title="Select a Backend">Backend</label>
52-
<ul id="backend">
53-
<li>
54-
<input type="radio" name="backend_inputs" value="core" id="backend_core" checked="true">
55-
<label for="backend_core" title="Use the core libraries backend">Core</label>
56-
</li>
57-
<!--
58-
<li>
59-
<input type="radio" name="backend_inputs" value="thermite" id="backend_thermite">
60-
<label for="backend_thermite" title="Use the try-thermite backend">Thermite</label>
61-
</li>
62-
<li>
63-
<input type="radio" name="backend_inputs" value="slides" id="backend_slides">
64-
<label for="backend_slides" title="Use the try-slides backend">Slides</label>
65-
</li>
66-
<li>
67-
<input type="radio" name="backend_inputs" value="flare" id="backend_flare">
68-
<label for="backend_flare" title="Use the try-flare backend">Flare</label>
69-
</li>
70-
<li>
71-
<input type="radio" name="backend_inputs" value="mathbox" id="backend_mathbox">
72-
<label for="backend_mathbox" title="Use the try-mathbox backend">Mathbox</label>
73-
</li>
74-
<li>
75-
<input type="radio" name="backend_inputs" value="behaviors" id="backend_behaviors">
76-
<label for="backend_behaviors" title="Use the try-behaviors backend">Behaviors</label>
77-
</li>
78-
-->
79-
</ul>
8050
</li><li class="menu-item view_gist_li mobile-only">
8151
<a class="view_gist" target="trypurs_gist">
8252
<label title="Open the original gist in a new window">View Gist</label>
@@ -188,30 +158,31 @@
188158
})(marker));
189159
}
190160

191-
function setupIFrame($ctr, html, js) {
192-
var $iframe = $('<iframe id="output-iframe">');
161+
function setupIFrame($ctr, data) {
162+
var $iframe = $('<iframe sandbox="allow-scripts" id="output-iframe" src="frame.html">');
193163

194164
$ctr
195165
.empty()
196166
.append($iframe);
197167

198-
var iframe = $iframe.get(0).contentWindow.document;
199-
iframe.open();
200-
iframe.write(html);
201-
iframe.close();
202-
203-
var script = iframe.createElement('script');
204-
script.appendChild(iframe.createTextNode(js));
205-
206-
$iframe.ready(function() {
207-
var checkExists = setInterval(function() {
208-
var body = iframe.getElementsByTagName('body')[0];
209-
if (body) {
210-
body.appendChild(script);
211-
clearInterval(checkExists);
212-
}
213-
}, 100);
214-
});
168+
var tries = 0;
169+
var sendSources = setInterval(function() {
170+
// Stop after 10 seconds
171+
if (tries >= 100) {
172+
return clearInterval(sendSources);
173+
}
174+
tries++;
175+
var iframe = $iframe.get(0).contentWindow;
176+
if (iframe) {
177+
iframe.postMessage(data, "*");
178+
} else {
179+
console.warn("Frame is not available");
180+
}
181+
}, 100);
182+
183+
window.addEventListener("message", function() {
184+
clearInterval(sendSources);
185+
}, { once: true });
215186

216187
return $iframe;
217188
}

client/js/frame.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
(function() {
2+
function evalSources(sources) {
3+
var modules = {};
4+
function dirname(str) {
5+
var ix = str.lastIndexOf("/");
6+
return ix < 0 ? "" : str.slice(0, ix);
7+
}
8+
function resolvePath(a, b) {
9+
if (b[0] === "." && b[1] === "/") {
10+
return dirname(a) + b.slice(1);
11+
}
12+
if (b[0] === "." && b[1] === "." && b[2] === "/") {
13+
return dirname(dirname(a)) + b.slice(2);
14+
}
15+
return b;
16+
}
17+
return function load(name) {
18+
if (modules[name]) {
19+
return modules[name].exports;
20+
}
21+
function require(path) {
22+
return load(resolvePath(name, path));
23+
}
24+
var module = modules[name] = { exports: {} };
25+
new Function("module", "exports", "require", sources[name])(module, module.exports, require);
26+
return module.exports;
27+
};
28+
}
29+
30+
document.addEventListener("DOMContentLoaded", function() {
31+
window.addEventListener("message", function(event) {
32+
event.source.postMessage("trypurescript", "*");
33+
var file = evalSources(event.data)("<file>");
34+
if (file.main && typeof file.main === "function") {
35+
file.main();
36+
}
37+
}, { once: true });
38+
}, { once: true });
39+
})();

client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"scripts": {
44
"clean": "rimraf output && rimraf .pulp-cache",
55
"build": "pulp build -- --censor-lib --strict",
6-
"bundle": "pulp build -O --to js/index.js && open index.html"
6+
"bundle": "pulp build -O --to js/index.js"
77
},
88
"devDependencies": {
99
"pulp": "^13.0.0",

0 commit comments

Comments
 (0)