notebook/IPython/html/static/base/js/dialog.js

162 lines
5.6 KiB
JavaScript
Raw Normal View History

2013-06-03 06:44:23 +08:00
//----------------------------------------------------------------------------
// Copyright (C) 2013 The IPython Development Team
//
// Distributed under the terms of the BSD License. The full license is in
// the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------
//============================================================================
// Utility for modal dialogs with bootstrap
//============================================================================
IPython.namespace('IPython.dialog');
IPython.dialog = (function (IPython) {
"use strict";
2013-06-03 06:44:23 +08:00
var modal = function (options) {
2014-04-18 07:05:35 +08:00
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(
2013-06-03 06:44:23 +08:00
$("<div/>")
.addClass("modal-header")
.append($("<button>")
2014-04-18 07:05:35 +08:00
.attr("type", "button")
2013-06-03 06:44:23 +08:00
.addClass("close")
.attr("data-dismiss", "modal")
2014-04-18 07:05:35 +08:00
.attr("aria-hidden", "true")
2013-06-03 06:44:23 +08:00
.html("&times;")
).append(
2014-04-18 07:05:35 +08:00
$("<h4/>")
.addClass('modal-title')
.text(options.title || "")
2013-06-03 06:44:23 +08:00
)
).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/>")
2014-06-10 07:13:59 +08:00
.addClass("btn btn-default btn-sm")
2013-06-03 06:44:23 +08:00
.attr("data-dismiss", "modal")
.text(label);
if (btn_opts.click) {
2014-04-18 07:05:35 +08:00
button.click($.proxy(btn_opts.click, dialog_content));
2013-06-03 06:44:23 +08:00
}
if (btn_opts.class) {
button.addClass(btn_opts.class);
}
footer.append(button);
}
2014-04-18 07:05:35 +08:00
dialog_content.append(footer);
2013-06-03 06:44:23 +08:00
// hook up on-open event
2014-05-23 05:29:50 +08:00
modal.on("shown.bs.modal", function() {
2013-06-03 06:44:23 +08:00
setTimeout(function() {
footer.find("button").last().focus();
if (options.open) {
2014-04-18 07:05:35 +08:00
$.proxy(options.open, modal)();
2013-06-03 06:44:23 +08:00
}
}, 0);
});
2014-04-18 07:05:35 +08:00
// destroy modal on hide, unless explicitly asked not to
2013-10-03 06:26:19 +08:00
if (options.destroy === undefined || options.destroy) {
2014-05-23 05:29:50 +08:00
modal.on("hidden.bs.modal", function () {
2014-04-18 07:05:35 +08:00
modal.remove();
2013-06-03 06:44:23 +08:00
});
}
2014-05-23 05:29:50 +08:00
modal.on("hidden.bs.modal", function () {
if (IPython.notebook) {
var cell = IPython.notebook.get_selected_cell();
if (cell) cell.select();
IPython.keyboard_manager.enable();
IPython.keyboard_manager.command_mode();
}
});
if (IPython.keyboard_manager) {
IPython.keyboard_manager.disable();
}
2013-06-03 06:44:23 +08:00
2014-04-18 07:05:35 +08:00
return modal.modal(options);
2013-10-03 06:26:19 +08:00
};
var edit_metadata = function (md, callback, name) {
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',
});
2014-06-07 07:32:10 +08:00
var modal = IPython.dialog.modal({
2013-10-03 06:26:19 +08:00
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: {}
}
});
2014-06-07 07:32:10 +08:00
modal.on('shown.bs.modal', function(){ editor.refresh(); });
2013-10-03 06:26:19 +08:00
};
2013-06-03 06:44:23 +08:00
return {
modal : modal,
2013-10-03 06:26:19 +08:00
edit_metadata : edit_metadata,
2013-06-03 06:44:23 +08:00
};
}(IPython));