dialog.js
158 lines
| 5.2 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17189 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
MinRK
|
r10903 | |||
Jonathan Frederic
|
r17189 | define([ | ||
'base/js/namespace', | ||||
'components/jquery/jquery.min', | ||||
], function(IPython, $) { | ||||
Matthias BUSSONNIER
|
r12103 | "use strict"; | ||
MinRK
|
r10903 | |||
Jonathan Frederic
|
r17189 | var modal = function (options, keyboard_manager, notebook) { | ||
jon
|
r16933 | var modal = $("<div/>") | ||
.addClass("modal") | ||||
.addClass("fade") | ||||
.attr("role", "dialog"); | ||||
var dialog = $("<div/>") | ||||
.addClass("modal-dialog") | ||||
.appendTo(modal); | ||||
var dialog_content = $("<div/>") | ||||
.addClass("modal-content") | ||||
.appendTo(dialog); | ||||
dialog_content.append( | ||||
MinRK
|
r10903 | $("<div/>") | ||
.addClass("modal-header") | ||||
.append($("<button>") | ||||
jon
|
r16933 | .attr("type", "button") | ||
MinRK
|
r10903 | .addClass("close") | ||
.attr("data-dismiss", "modal") | ||||
jon
|
r16933 | .attr("aria-hidden", "true") | ||
MinRK
|
r10903 | .html("×") | ||
).append( | ||||
jon
|
r16933 | $("<h4/>") | ||
.addClass('modal-title') | ||||
.text(options.title || "") | ||||
MinRK
|
r10903 | ) | ||
).append( | ||||
$("<div/>").addClass("modal-body").append( | ||||
options.body || $("<p/>") | ||||
) | ||||
); | ||||
var footer = $("<div/>").addClass("modal-footer"); | ||||
for (var label in options.buttons) { | ||||
var btn_opts = options.buttons[label]; | ||||
var button = $("<button/>") | ||||
Jonathan Frederic
|
r16970 | .addClass("btn btn-default btn-sm") | ||
MinRK
|
r10903 | .attr("data-dismiss", "modal") | ||
.text(label); | ||||
if (btn_opts.click) { | ||||
jon
|
r16933 | button.click($.proxy(btn_opts.click, dialog_content)); | ||
MinRK
|
r10903 | } | ||
if (btn_opts.class) { | ||||
button.addClass(btn_opts.class); | ||||
} | ||||
footer.append(button); | ||||
} | ||||
jon
|
r16933 | dialog_content.append(footer); | ||
MinRK
|
r10903 | // hook up on-open event | ||
Jonathan Frederic
|
r16955 | modal.on("shown.bs.modal", function() { | ||
MinRK
|
r10903 | setTimeout(function() { | ||
footer.find("button").last().focus(); | ||||
if (options.open) { | ||||
jon
|
r16933 | $.proxy(options.open, modal)(); | ||
MinRK
|
r10903 | } | ||
}, 0); | ||||
}); | ||||
jon
|
r16933 | // destroy modal on hide, unless explicitly asked not to | ||
MinRK
|
r12872 | if (options.destroy === undefined || options.destroy) { | ||
Jonathan Frederic
|
r16955 | modal.on("hidden.bs.modal", function () { | ||
jon
|
r16933 | modal.remove(); | ||
MinRK
|
r10903 | }); | ||
} | ||||
Jonathan Frederic
|
r16955 | modal.on("hidden.bs.modal", function () { | ||
Jonathan Frederic
|
r17189 | if (notebook) { | ||
var cell = notebook.get_selected_cell(); | ||||
Brian E. Granger
|
r14020 | if (cell) cell.select(); | ||
Jonathan Frederic
|
r17189 | keyboard_manager.enable(); | ||
keyboard_manager.command_mode(); | ||||
Brian E. Granger
|
r14020 | } | ||
}); | ||||
Jonathan Frederic
|
r17189 | if (keyboard_manager) { | ||
keyboard_manager.disable(); | ||||
MinRK
|
r11439 | } | ||
MinRK
|
r10903 | |||
jon
|
r16933 | return modal.modal(options); | ||
MinRK
|
r12872 | }; | ||
Jonathan Frederic
|
r17190 | var edit_metadata = function (md, callback, name, keyboard_manager, notebook) { | ||
MinRK
|
r12872 | name = name || "Cell"; | ||
var error_div = $('<div/>').css('color', 'red'); | ||||
var message = | ||||
"Manually edit the JSON below to manipulate the metadata for this " + name + "." + | ||||
" We recommend putting custom metadata attributes in an appropriately named sub-structure," + | ||||
" so they don't conflict with those of others."; | ||||
var textarea = $('<textarea/>') | ||||
.attr('rows', '13') | ||||
.attr('cols', '80') | ||||
.attr('name', 'metadata') | ||||
.text(JSON.stringify(md || {}, null, 2)); | ||||
var dialogform = $('<div/>').attr('title', 'Edit the metadata') | ||||
.append( | ||||
$('<form/>').append( | ||||
$('<fieldset/>').append( | ||||
$('<label/>') | ||||
.attr('for','metadata') | ||||
.text(message) | ||||
) | ||||
.append(error_div) | ||||
.append($('<br/>')) | ||||
.append(textarea) | ||||
) | ||||
); | ||||
var editor = CodeMirror.fromTextArea(textarea[0], { | ||||
lineNumbers: true, | ||||
matchBrackets: true, | ||||
indentUnit: 2, | ||||
autoIndent: true, | ||||
mode: 'application/json', | ||||
}); | ||||
Jonathan Frederic
|
r17189 | var modal = modal({ | ||
MinRK
|
r12872 | title: "Edit " + name + " Metadata", | ||
body: dialogform, | ||||
buttons: { | ||||
OK: { class : "btn-primary", | ||||
click: function() { | ||||
// validate json and set it | ||||
var new_md; | ||||
try { | ||||
new_md = JSON.parse(editor.getValue()); | ||||
} catch(e) { | ||||
console.log(e); | ||||
error_div.text('WARNING: Could not save invalid JSON.'); | ||||
return false; | ||||
} | ||||
callback(new_md); | ||||
} | ||||
}, | ||||
Cancel: {} | ||||
} | ||||
Jonathan Frederic
|
r17190 | }, keyboard_manager, notebook); | ||
Jonathan Frederic
|
r16957 | |||
modal.on('shown.bs.modal', function(){ editor.refresh(); }); | ||||
MinRK
|
r12872 | }; | ||
MinRK
|
r10903 | |||
Jonathan Frederic
|
r17189 | Dialog = { | ||
MinRK
|
r10903 | modal : modal, | ||
MinRK
|
r12872 | edit_metadata : edit_metadata, | ||
MinRK
|
r10903 | }; | ||
Jonathan Frederic
|
r17189 | // Backwards compatability. | ||
IPython.Dialog = Dialog; | ||||
return Dialog; | ||||
}); | ||||