diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2022-03-17 19:27:17 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2022-03-17 19:27:17 +0300 |
commit | b8f659f3a48d4c6e523a5884ba00643df51346b5 (patch) | |
tree | 0f56672b7a347f1fdbf790ef1f6b59a2897d0133 /assets/js | |
parent | Makefile: fix accidental failures (diff) | |
download | wireguard-config-b8f659f3a48d4c6e523a5884ba00643df51346b5.tar.gz wireguard-config-b8f659f3a48d4c6e523a5884ba00643df51346b5.zip |
add "Edit" & "Download" buttons
Diffstat (limited to '')
-rw-r--r-- | assets/js/main.js | 67 |
1 files changed, 52 insertions, 15 deletions
diff --git a/assets/js/main.js b/assets/js/main.js index 89551da..0d203dd 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -341,33 +341,70 @@ Data.prototype.hide_advanced = function() { }); } -function format_pre_text(text) { - return $('<pre/>').text(text); +function edit_btn_on_click(btn, pre) { + var editable = pre.prop('isContentEditable'); + pre.prop('contentEditable', !editable); + if (editable) { + btn.text('Edit'); + btn.blur(); // a.k.a. unfocus + } else { + btn.text('Save'); + pre.focus(); + } } -var ConfigFile = function(name, text) { - this.name = name; - this.text = text; +function basename(path) { + return path.substring(path.lastIndexOf('/') + 1); } -ConfigFile.prototype.toString = function() { - return this.text; +function dload_btn_on_click(btn, path, pre) { + var blob = new Blob([pre.text()], {type: 'text/plain'}); + var url = URL.createObjectURL(blob); + + var link = $('<a/>'); + link.prop('href', url); + link.prop('download', basename(path)); + + // Whoever thought of this [0] is fucking crazy: + // https://stackoverflow.com/a/36483380/514684 + // It literally silently does nothing if this is omitted. + link[0].click(); } -ConfigFile.prototype.format = function() { - return format_pre_text(this.toString()); +function make_pre_buttons(path, pre) { + var edit_btn = $('<button class="btn btn-default" type="button"/>').text('Edit'); + edit_btn.click(function() { + edit_btn_on_click(edit_btn, pre); + }); + + var dload_btn = $('<button class="btn btn-default" type="button"/>').text('Download'); + dload_btn.click(function() { + dload_btn_on_click(dload_btn, path, pre); + }); + + return $('<div class="pre_buttons btn-group btn-group-xs" role="group"/>') + .append(dload_btn) + .append(edit_btn); +} + +function format_pre_text(text, name) { + var pre = $('<pre/>').text(text); + return $('<div class="pre_container"/>') + .append(pre) + .append(make_pre_buttons(name, pre)); } -var Script = function(text) { +var ConfigFile = function(name, text) { + this.name = name; this.text = text; } -Script.prototype.toString = function() { +ConfigFile.prototype.toString = function() { return this.text; } -Script.prototype.format = function() { - return format_pre_text(this.toString()); +ConfigFile.prototype.format = function() { + return format_pre_text(this.toString(), this.name); } var QRCode = function(text) { @@ -566,7 +603,7 @@ var shell_info = # Better yet, put into a file and run (possibly, using sudo).`; function manual_client_script(data) { - return new Script( + return new ConfigFile('client_setup.sh', `# On the client, run this to set up a connection. ${shell_info} @@ -585,7 +622,7 @@ ip link set ${iface} up } function manual_server_script(data) { - return new Script( + return new ConfigFile('server_setup.sh', `# On the server, run this to tweak an existing connection. ${shell_info} |