From 8f4ee0a1123d01d45ec2c22a5878a5bab97a9ec3 Mon Sep 17 00:00:00 2001 From: Damiano Azzolini Date: Sun, 19 Jan 2020 22:36:39 +0100 Subject: [PATCH] added the option to download the code stored in a notebook --- web/js/notebook.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/web/js/notebook.js b/web/js/notebook.js index 17081f950..01eaa5f07 100644 --- a/web/js/notebook.js +++ b/web/js/notebook.js @@ -128,7 +128,9 @@ CodeMirror.modes.eval = CodeMirror.modes.prolog; sep(), glyphButton("erase", "clear_all", "Clear all query output", "warning"), glyphButton("play", "run_all", "Run all queries", "primary"), - glyphButton("wrench", "settings", "Settings", "default"), + glyphButton("wrench", "settings", "Settings", "default"), + sep(), + glyphButton("download", "download_data", "Download program", "default"), glyphButton("fullscreen", "fullscreen", "Full screen", "default") )); elem.append(notebookMenu()); @@ -907,6 +909,33 @@ CodeMirror.modes.eval = CodeMirror.modes.prolog; } }, + download_data: function() { + var data = []; + this.find(".nb-cell.program").each(function () { + console.log($(this)[0].innerText); + + if ($(this)[0].hidden == false) { + var str = $(this)[0].innerText.split("\n").filter(i => !i.match(/^\d/)); + data.push(str); + } + }); + + data = data.flat().join('\n'); + + var pom = document.createElement('a'); + pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(data)); + pom.setAttribute('download', "program.pl"); + + if (document.createEvent) { + var event = document.createEvent('MouseEvents'); + event.initEvent('click', true, true); + pom.dispatchEvent(event); + } + else { + pom.click(); + } + }, + /** * Erase all query output, killing possibly running queries */