Commit 067578b8 authored by Jonathon Duerig's avatar Jonathon Duerig

Convert download buttons in manage_profile and show-profile to use the same...

Convert download buttons in manage_profile and show-profile to use the same save-to-disk method as genilib-editor.

Also fixes a bug where downloading the source for a profile always downloaded the first version rather than the currently-viewed version.
parent 9762dedc
......@@ -45,7 +45,7 @@ function (_, sup, aptforms)
window.onbeforeunload = null;
$('#waitwait-modal').modal({ backdrop: 'static', keyboard: false, show: false });
$('#saveButton').on('click', save);
sup.DownloadOnClick($('#saveButton'), getSaveText, 'saved.py', saveComplete);
$('#loadButton').on('click', load);
$('#runButton').on('click', clickRun);
$('#settingsButton').on('click', toggleSettings);
......@@ -94,23 +94,23 @@ function (_, sup, aptforms)
window.onbeforeunload = beforeUnload;
}
function save()
function getSaveText()
{
if (! isWaiting)
{
var scriptString = editor.getValue();
var file = new Blob([scriptString],
{ type: 'application/octet-stream' });
var a = document.createElement('a');
a.href = window.URL.createObjectURL(file);
a.download = 'saved.py';
document.body.appendChild(a);
a.click();
$('a').last().remove();
window.onbeforeunload = null;
return editor.getValue();
}
else
{
return null;
}
}
function saveComplete()
{
window.onbeforeunload = null;
}
function load()
{
if (! isWaiting)
......
......@@ -255,13 +255,17 @@ function (_, sup, filesize, JacksEditor, ShowImagingModal, moment, aptforms,
type = "rspec";
}
if (profile_uuid) {
$('#rspec_modal_download_button')
sup.DownloadOnClick($('#rspec_modal_download_button'),
function () { return source; },
'profile.xml');
/* $('#rspec_modal_download_button')
.attr("href",
"show-profile.php?uuid=" + profile_uuid +
"&" + type + "=true");
"&" + type + "=true");*/
}
else {
$('#rspec_modal_download_button').addClass("hidden");
sup.ClearDownloadOnClick($('#rspec_modal_download_button'));
// $('#rspec_modal_download_button').addClass("hidden");
}
$('#rspec_modal_upload_span').removeClass("hidden");
$('#rspec_modal_editbuttons').removeClass("hidden");
......@@ -282,12 +286,16 @@ function (_, sup, filesize, JacksEditor, ShowImagingModal, moment, aptforms,
var source = $.trim($('#profile_rspec_textarea').val());
if (profile_uuid) {
$('#rspec_modal_download_button')
sup.DownloadOnClick($('#rspec_modal_download_button'),
function () { return source; },
'profile.xml');
/* $('#rspec_modal_download_button')
.attr("href",
"show-profile.php?uuid=" + profile_uuid +
"&rspec=true");
"&rspec=true");*/
}
else {
sup.ClearDownloadOnClick($('#rspec_modal_download_button'));
$('#rspec_modal_download_button').addClass("hidden");
}
$('#rspec_modal_upload_span').addClass("hidden");
......@@ -1153,6 +1161,7 @@ function (_, sup, filesize, JacksEditor, ShowImagingModal, moment, aptforms,
"</code></pre>");
return;
}
sup.ClearDownloadOnClick($('#rspec_modal_download_button'));
$('#rspec_modal_download_button').addClass("hidden");
$('#rspec_modal_editbuttons').removeClass("hidden");
$('#rspec_modal_viewbuttons').addClass("hidden");
......
......@@ -36,6 +36,40 @@ function CallServerMethod(url, route, method, args, callback)
return APT_OPTIONS.CallServerMethod(url, route, method, args, callback);
}
// button is a jQuery object containing the button(s) to add event to
// getText is a function to fetch the text to be saved. Invoked per click with no arguments and expects a string result. If getText returns null or undefined, no save will happen and no callback will be called.
// filename is the default filename used
// callback is invoked with button, text, and filename as arguments after download.
//
// This function will unset all other onclick events.
function DownloadOnClick(button, getText, filename, callback)
{
button.off('click');
button.on('click', function () {
var text = getText();
if (text !== undefined && text !== null)
{
var file = new Blob([text],
{ type: 'application/octet-stream' });
var a = document.createElement('a');
a.href = window.URL.createObjectURL(file);
a.download = filename;
document.body.appendChild(a);
a.click();
$('a').last().remove();
if (callback !== undefined && callback !== null)
{
callback(button, text, filename);
}
}
});
}
function ClearDownloadOnClick(button)
{
button.off('click');
}
var jacksInstance;
var jacksInput;
var jacksOutput;
......@@ -269,6 +303,8 @@ return {
ShowWaitWait: ShowWaitWait,
HideWaitWait: HideWaitWait,
CallServerMethod: CallServerMethod,
DownloadOnClick: DownloadOnClick,
ClearDownloadOnClick: ClearDownloadOnClick,
maketopmap: maketopmap,
SpitOops: SpitOops,
StartGeniLogin: StartGeniLogin,
......
......@@ -119,8 +119,11 @@ function (_, sup, moment,
var href = "show-profile.php?uuid=" + profile_uuid;
source = $.trim($('#profile_script_textarea').val());
$('#rspec_modal_download_button')
.attr("href", href + "&source=true");
sup.DownloadOnClick($('#rspec_modal_download_button'),
function () { return source; },
'profile.py');
/* $('#rspec_modal_download_button')
.attr("href", href + "&source=true");*/
if (! source || ! source.length) {
isScript = false;
}
......@@ -128,8 +131,11 @@ function (_, sup, moment,
$(this).attr("id") != "show_source_modal_button") {
source = $.trim($('#profile_rspec_textarea').val());
$('#rspec_modal_download_button')
.attr("href", href + "&rspec=true");
sup.DownloadOnClick($('#rspec_modal_download_button'),
function () { return source; },
'profile.xml');
// $('#rspec_modal_download_button')
// .attr("href", href + "&rspec=true");
}
if ($(this).attr("id") == "show_source_modal_button" && isScript) {
openEditor();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment