Skip to content

Commit

Permalink
try implement onInit hook for custom page espruino#1
Browse files Browse the repository at this point in the history
  • Loading branch information
flashback2k14 committed May 1, 2020
1 parent 11571bb commit 60ad48d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 22 deletions.
16 changes: 16 additions & 0 deletions apps/timer/custom/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@
<button id="upload" class="btn btn-primary" style="float: right;">Upload</button>
</div>
<script src="../../../lib/customize.js"></script>
<script src="../../../lib/interface.js"></script>
<script src="script.js"></script>
<script>
function onInit() {
Puck.write('\x03', (result) => {
if (result === null) {
console.error('result === null');
}
Puck.eval(`require('Storage').readJSON('timer.custom.json', 1)`, (content, err) => {
if (content === null) {
console.error('content === null');
}
console.log(content);
});
});
}
</script>
</body>

</html>
76 changes: 54 additions & 22 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function handleCustomApp(appTemplate) {
</div>
<div class="modal-body" style="height:100%">
<div class="content" style="height:100%">
<iframe src="apps/${appTemplate.id}/${appTemplate.custom}" style="width:100%;height:100%;border:0px;">
<iframe style="width:100%;height:100%;border:0px;">
</div>
</div>
</div>
Expand All @@ -69,27 +69,59 @@ function handleCustomApp(appTemplate) {
});

var iframe = modal.getElementsByTagName('iframe')[0];
iframe.contentWindow.addEventListener(
'message',
function (event) {
var appFiles = event.data;
var app = {};
Object.keys(appTemplate).forEach((k) => (app[k] = appTemplate[k]));
Object.keys(appFiles).forEach((k) => (app[k] = appFiles[k]));
console.log('Received custom app', app);
modal.remove();
Comms.uploadApp(app)
.then(() => {
Progress.hide({ sticky: true });
resolve();
})
.catch((e) => {
Progress.hide({ sticky: true });
reject(e);
});
},
false
);
iframe.onload = function () {
var iwin = iframe.contentWindow;
iwin.addEventListener(
'message',
function (event) {
var msg = event.data;
if (msg.type == 'eval') {
Puck.eval(msg.data, function (result) {
iwin.postMessage({
type: 'evalrsp',
data: result,
id: msg.id,
});
});
} else if (msg.type == 'write') {
Puck.write(msg.data, function (result) {
iwin.postMessage({
type: 'writersp',
data: result,
id: msg.id,
});
});
} else if (msg.type == 'readstoragefile') {
Comms.readStorageFile(msg.data /*filename*/).then(function (result) {
iwin.postMessage({
type: 'readstoragefilersp',
data: result,
id: msg.id,
});
});
} else {
var appFiles = event.data;
var app = {};
Object.keys(appTemplate).forEach((k) => (app[k] = appTemplate[k]));
Object.keys(appFiles).forEach((k) => (app[k] = appFiles[k]));
console.log('Received custom app', app);
modal.remove();
Comms.uploadApp(app)
.then(() => {
Progress.hide({ sticky: true });
resolve();
})
.catch((e) => {
Progress.hide({ sticky: true });
reject(e);
});
}
},
false
);
iwin.postMessage({ type: 'init' });
};
iframe.src = `apps/${appTemplate.id}/${appTemplate.custom}`;
});
}

Expand Down

0 comments on commit 60ad48d

Please sign in to comment.