Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50659 views
1
//----------------------------------------------------------------------------
2
// Copyright (C) 2013 The IPython Development Team
3
//
4
// Distributed under the terms of the BSD License. The full license is in
5
// the file COPYING, distributed as part of this software.
6
//----------------------------------------------------------------------------
7
8
//============================================================================
9
// Utility for modal dialogs with bootstrap
10
//============================================================================
11
12
IPython.namespace('IPython.dialog');
13
14
IPython.dialog = (function (IPython) {
15
"use strict";
16
17
var modal = function (options) {
18
var dialog = $("<div/>").addClass("modal").attr("role", "dialog");
19
dialog.append(
20
$("<div/>")
21
.addClass("modal-header")
22
.append($("<button>")
23
.addClass("close")
24
.attr("data-dismiss", "modal")
25
.html("&times;")
26
).append(
27
$("<h3/>").text(options.title || "")
28
)
29
).append(
30
$("<div/>").addClass("modal-body").append(
31
options.body || $("<p/>")
32
)
33
);
34
35
var footer = $("<div/>").addClass("modal-footer");
36
37
for (var label in options.buttons) {
38
var btn_opts = options.buttons[label];
39
var button = $("<button/>")
40
.addClass("btn")
41
.attr("data-dismiss", "modal")
42
.text(label);
43
if (btn_opts.click) {
44
button.click($.proxy(btn_opts.click, dialog));
45
}
46
if (btn_opts.class) {
47
button.addClass(btn_opts.class);
48
}
49
footer.append(button);
50
}
51
dialog.append(footer);
52
// hook up on-open event
53
dialog.on("shown", function() {
54
setTimeout(function() {
55
footer.find("button").last().focus();
56
if (options.open) {
57
$.proxy(options.open, dialog)();
58
}
59
}, 0);
60
});
61
62
// destroy dialog on hide, unless explicitly asked not to
63
if (options.destroy === undefined || options.destroy) {
64
dialog.on("hidden", function () {
65
dialog.remove();
66
});
67
}
68
dialog.on("hidden", function () {
69
if (IPython.notebook) {
70
var cell = IPython.notebook.get_selected_cell();
71
if (cell) cell.select();
72
IPython.keyboard_manager.enable();
73
IPython.keyboard_manager.command_mode();
74
}
75
});
76
77
if (IPython.keyboard_manager) {
78
IPython.keyboard_manager.disable();
79
}
80
81
return dialog.modal(options);
82
};
83
84
var edit_metadata = function (md, callback, name) {
85
name = name || "Cell";
86
var error_div = $('<div/>').css('color', 'red');
87
var message =
88
"Manually edit the JSON below to manipulate the metadata for this " + name + "." +
89
" We recommend putting custom metadata attributes in an appropriately named sub-structure," +
90
" so they don't conflict with those of others.";
91
92
var textarea = $('<textarea/>')
93
.attr('rows', '13')
94
.attr('cols', '80')
95
.attr('name', 'metadata')
96
.text(JSON.stringify(md || {}, null, 2));
97
98
var dialogform = $('<div/>').attr('title', 'Edit the metadata')
99
.append(
100
$('<form/>').append(
101
$('<fieldset/>').append(
102
$('<label/>')
103
.attr('for','metadata')
104
.text(message)
105
)
106
.append(error_div)
107
.append($('<br/>'))
108
.append(textarea)
109
)
110
);
111
var editor = CodeMirror.fromTextArea(textarea[0], {
112
lineNumbers: true,
113
matchBrackets: true,
114
indentUnit: 2,
115
autoIndent: true,
116
mode: 'application/json',
117
});
118
IPython.dialog.modal({
119
title: "Edit " + name + " Metadata",
120
body: dialogform,
121
buttons: {
122
OK: { class : "btn-primary",
123
click: function() {
124
// validate json and set it
125
var new_md;
126
try {
127
new_md = JSON.parse(editor.getValue());
128
} catch(e) {
129
console.log(e);
130
error_div.text('WARNING: Could not save invalid JSON.');
131
return false;
132
}
133
callback(new_md);
134
}
135
},
136
Cancel: {}
137
}
138
});
139
editor.refresh();
140
};
141
142
return {
143
modal : modal,
144
edit_metadata : edit_metadata,
145
};
146
147
}(IPython));
148
149