Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
define(function(require) {
5
"use strict";
6
7
var CodeMirror = require('codemirror/lib/codemirror');
8
var IPython = require('base/js/namespace');
9
var $ = require('jquery');
10
11
/**
12
* A wrapper around bootstrap modal for easier use
13
* Pass it an option dictionary with the following properties:
14
*
15
* - body : <string> or <DOM node>, main content of the dialog
16
* if pass a <string> it will be wrapped in a p tag and
17
* html element escaped, unless you specify sanitize=false
18
* option.
19
* - title : Dialog title, default to empty string.
20
* - buttons : dict of btn_options who keys are button label.
21
* see btn_options below for description
22
* - open : callback to trigger on dialog open.
23
* - destroy:
24
* - notebook : notebook instance
25
* - keyboard_manager: keyboard manager instance.
26
*
27
* Unlike bootstrap modals, the backdrop options is set by default
28
* to 'static'.
29
*
30
* The rest of the options are passed as is to bootstrap modals.
31
*
32
* btn_options: dict with the following property:
33
*
34
* - click : callback to trigger on click
35
* - class : css classes to add to button.
36
*
37
*
38
*
39
**/
40
var modal = function (options) {
41
42
var modal = $("<div/>")
43
.addClass("modal")
44
.addClass("fade")
45
.attr("role", "dialog");
46
var dialog = $("<div/>")
47
.addClass("modal-dialog")
48
.appendTo(modal);
49
var dialog_content = $("<div/>")
50
.addClass("modal-content")
51
.appendTo(dialog);
52
if(typeof(options.body) === 'string' && options.sanitize !== false){
53
options.body = $("<p/>").text(options.body)
54
}
55
dialog_content.append(
56
$("<div/>")
57
.addClass("modal-header")
58
.append($("<button>")
59
.attr("type", "button")
60
.addClass("close")
61
.attr("data-dismiss", "modal")
62
.attr("aria-hidden", "true")
63
.html("&times;")
64
).append(
65
$("<h4/>")
66
.addClass('modal-title')
67
.text(options.title || "")
68
)
69
).append(
70
$("<div/>").addClass("modal-body").append(
71
options.body || $("<p/>")
72
)
73
);
74
75
var footer = $("<div/>").addClass("modal-footer");
76
77
for (var label in options.buttons) {
78
var btn_opts = options.buttons[label];
79
var button = $("<button/>")
80
.addClass("btn btn-default btn-sm")
81
.attr("data-dismiss", "modal")
82
.text(label);
83
if (btn_opts.click) {
84
button.click($.proxy(btn_opts.click, dialog_content));
85
}
86
if (btn_opts.class) {
87
button.addClass(btn_opts.class);
88
}
89
footer.append(button);
90
}
91
dialog_content.append(footer);
92
// hook up on-open event
93
modal.on("shown.bs.modal", function() {
94
setTimeout(function() {
95
footer.find("button").last().focus();
96
if (options.open) {
97
$.proxy(options.open, modal)();
98
}
99
}, 0);
100
});
101
102
// destroy modal on hide, unless explicitly asked not to
103
if (options.destroy === undefined || options.destroy) {
104
modal.on("hidden.bs.modal", function () {
105
modal.remove();
106
});
107
}
108
modal.on("hidden.bs.modal", function () {
109
if (options.notebook) {
110
var cell = options.notebook.get_selected_cell();
111
if (cell) cell.select();
112
}
113
if (options.keyboard_manager) {
114
options.keyboard_manager.enable();
115
options.keyboard_manager.command_mode();
116
}
117
});
118
119
if (options.keyboard_manager) {
120
options.keyboard_manager.disable();
121
}
122
123
options.backdrop = options.backdrop || 'static';
124
125
return modal.modal(options);
126
};
127
128
var kernel_modal = function (options) {
129
/**
130
* only one kernel dialog should be open at a time -- but
131
* other modal dialogs can still be open
132
*/
133
$('.kernel-modal').modal('hide');
134
var dialog = modal(options);
135
dialog.addClass('kernel-modal');
136
return dialog;
137
};
138
139
var edit_metadata = function (options) {
140
options.name = options.name || "Cell";
141
var error_div = $('<div/>').css('color', 'red');
142
var message =
143
"Manually edit the JSON below to manipulate the metadata for this " + options.name + "." +
144
" We recommend putting custom metadata attributes in an appropriately named sub-structure," +
145
" so they don't conflict with those of others.";
146
147
var textarea = $('<textarea/>')
148
.attr('rows', '13')
149
.attr('cols', '80')
150
.attr('name', 'metadata')
151
.text(JSON.stringify(options.md || {}, null, 2));
152
153
var dialogform = $('<div/>').attr('title', 'Edit the metadata')
154
.append(
155
$('<form/>').append(
156
$('<fieldset/>').append(
157
$('<label/>')
158
.attr('for','metadata')
159
.text(message)
160
)
161
.append(error_div)
162
.append($('<br/>'))
163
.append(textarea)
164
)
165
);
166
var editor = CodeMirror.fromTextArea(textarea[0], {
167
lineNumbers: true,
168
matchBrackets: true,
169
indentUnit: 2,
170
autoIndent: true,
171
mode: 'application/json',
172
});
173
var modal_obj = modal({
174
title: "Edit " + options.name + " Metadata",
175
body: dialogform,
176
buttons: {
177
OK: { class : "btn-primary",
178
click: function() {
179
/**
180
* validate json and set it
181
*/
182
var new_md;
183
try {
184
new_md = JSON.parse(editor.getValue());
185
} catch(e) {
186
console.log(e);
187
error_div.text('WARNING: Could not save invalid JSON.');
188
return false;
189
}
190
options.callback(new_md);
191
}
192
},
193
Cancel: {}
194
},
195
notebook: options.notebook,
196
keyboard_manager: options.keyboard_manager,
197
});
198
199
modal_obj.on('shown.bs.modal', function(){ editor.refresh(); });
200
};
201
202
var dialog = {
203
modal : modal,
204
kernel_modal : kernel_modal,
205
edit_metadata : edit_metadata,
206
};
207
208
// Backwards compatability.
209
IPython.dialog = dialog;
210
211
return dialog;
212
});
213
214