Show More
@@ -1,178 +1,180 b'' | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | 'base/js/namespace', |
|
6 | 6 | 'jquery', |
|
7 | 7 | 'codemirror/lib/codemirror', |
|
8 | 8 | ], function(IPython, $, CodeMirror) { |
|
9 | 9 | "use strict"; |
|
10 | 10 | |
|
11 | 11 | var modal = function (options) { |
|
12 | 12 | |
|
13 | 13 | var modal = $("<div/>") |
|
14 | 14 | .addClass("modal") |
|
15 | 15 | .addClass("fade") |
|
16 | 16 | .attr("role", "dialog"); |
|
17 | 17 | var dialog = $("<div/>") |
|
18 | 18 | .addClass("modal-dialog") |
|
19 | 19 | .appendTo(modal); |
|
20 | 20 | var dialog_content = $("<div/>") |
|
21 | 21 | .addClass("modal-content") |
|
22 | 22 | .appendTo(dialog); |
|
23 | 23 | dialog_content.append( |
|
24 | 24 | $("<div/>") |
|
25 | 25 | .addClass("modal-header") |
|
26 | 26 | .append($("<button>") |
|
27 | 27 | .attr("type", "button") |
|
28 | 28 | .addClass("close") |
|
29 | 29 | .attr("data-dismiss", "modal") |
|
30 | 30 | .attr("aria-hidden", "true") |
|
31 | 31 | .html("×") |
|
32 | 32 | ).append( |
|
33 | 33 | $("<h4/>") |
|
34 | 34 | .addClass('modal-title') |
|
35 | 35 | .text(options.title || "") |
|
36 | 36 | ) |
|
37 | 37 | ).append( |
|
38 | 38 | $("<div/>").addClass("modal-body").append( |
|
39 | 39 | options.body || $("<p/>") |
|
40 | 40 | ) |
|
41 | 41 | ); |
|
42 | 42 | |
|
43 | 43 | var footer = $("<div/>").addClass("modal-footer"); |
|
44 | 44 | |
|
45 | 45 | for (var label in options.buttons) { |
|
46 | 46 | var btn_opts = options.buttons[label]; |
|
47 | 47 | var button = $("<button/>") |
|
48 | 48 | .addClass("btn btn-default btn-sm") |
|
49 | 49 | .attr("data-dismiss", "modal") |
|
50 | 50 | .text(label); |
|
51 | 51 | if (btn_opts.click) { |
|
52 | 52 | button.click($.proxy(btn_opts.click, dialog_content)); |
|
53 | 53 | } |
|
54 | 54 | if (btn_opts.class) { |
|
55 | 55 | button.addClass(btn_opts.class); |
|
56 | 56 | } |
|
57 | 57 | footer.append(button); |
|
58 | 58 | } |
|
59 | 59 | dialog_content.append(footer); |
|
60 | 60 | // hook up on-open event |
|
61 | 61 | modal.on("shown.bs.modal", function() { |
|
62 | 62 | setTimeout(function() { |
|
63 | 63 | footer.find("button").last().focus(); |
|
64 | 64 | if (options.open) { |
|
65 | 65 | $.proxy(options.open, modal)(); |
|
66 | 66 | } |
|
67 | 67 | }, 0); |
|
68 | 68 | }); |
|
69 | 69 | |
|
70 | 70 | // destroy modal on hide, unless explicitly asked not to |
|
71 | 71 | if (options.destroy === undefined || options.destroy) { |
|
72 | 72 | modal.on("hidden.bs.modal", function () { |
|
73 | 73 | modal.remove(); |
|
74 | 74 | }); |
|
75 | 75 | } |
|
76 | 76 | modal.on("hidden.bs.modal", function () { |
|
77 | 77 | if (options.notebook) { |
|
78 | 78 | var cell = options.notebook.get_selected_cell(); |
|
79 | 79 | if (cell) cell.select(); |
|
80 | 80 | } |
|
81 | 81 | if (options.keyboard_manager) { |
|
82 | 82 | options.keyboard_manager.enable(); |
|
83 | 83 | options.keyboard_manager.command_mode(); |
|
84 | 84 | } |
|
85 | 85 | }); |
|
86 | 86 | |
|
87 | 87 | if (options.keyboard_manager) { |
|
88 | 88 | options.keyboard_manager.disable(); |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | options.backdrop = options.backdrop || 'static'; | |
|
92 | ||
|
91 | 93 | return modal.modal(options); |
|
92 | 94 | }; |
|
93 | 95 | |
|
94 | 96 | var kernel_modal = function (options) { |
|
95 | 97 | /** |
|
96 | 98 | * only one kernel dialog should be open at a time -- but |
|
97 | 99 | * other modal dialogs can still be open |
|
98 | 100 | */ |
|
99 | 101 | $('.kernel-modal').modal('hide'); |
|
100 | 102 | var dialog = modal(options); |
|
101 | 103 | dialog.addClass('kernel-modal'); |
|
102 | 104 | return dialog; |
|
103 | 105 | }; |
|
104 | 106 | |
|
105 | 107 | var edit_metadata = function (options) { |
|
106 | 108 | options.name = options.name || "Cell"; |
|
107 | 109 | var error_div = $('<div/>').css('color', 'red'); |
|
108 | 110 | var message = |
|
109 | 111 | "Manually edit the JSON below to manipulate the metadata for this " + options.name + "." + |
|
110 | 112 | " We recommend putting custom metadata attributes in an appropriately named sub-structure," + |
|
111 | 113 | " so they don't conflict with those of others."; |
|
112 | 114 | |
|
113 | 115 | var textarea = $('<textarea/>') |
|
114 | 116 | .attr('rows', '13') |
|
115 | 117 | .attr('cols', '80') |
|
116 | 118 | .attr('name', 'metadata') |
|
117 | 119 | .text(JSON.stringify(options.md || {}, null, 2)); |
|
118 | 120 | |
|
119 | 121 | var dialogform = $('<div/>').attr('title', 'Edit the metadata') |
|
120 | 122 | .append( |
|
121 | 123 | $('<form/>').append( |
|
122 | 124 | $('<fieldset/>').append( |
|
123 | 125 | $('<label/>') |
|
124 | 126 | .attr('for','metadata') |
|
125 | 127 | .text(message) |
|
126 | 128 | ) |
|
127 | 129 | .append(error_div) |
|
128 | 130 | .append($('<br/>')) |
|
129 | 131 | .append(textarea) |
|
130 | 132 | ) |
|
131 | 133 | ); |
|
132 | 134 | var editor = CodeMirror.fromTextArea(textarea[0], { |
|
133 | 135 | lineNumbers: true, |
|
134 | 136 | matchBrackets: true, |
|
135 | 137 | indentUnit: 2, |
|
136 | 138 | autoIndent: true, |
|
137 | 139 | mode: 'application/json', |
|
138 | 140 | }); |
|
139 | 141 | var modal_obj = modal({ |
|
140 | 142 | title: "Edit " + options.name + " Metadata", |
|
141 | 143 | body: dialogform, |
|
142 | 144 | buttons: { |
|
143 | 145 | OK: { class : "btn-primary", |
|
144 | 146 | click: function() { |
|
145 | 147 | /** |
|
146 | 148 | * validate json and set it |
|
147 | 149 | */ |
|
148 | 150 | var new_md; |
|
149 | 151 | try { |
|
150 | 152 | new_md = JSON.parse(editor.getValue()); |
|
151 | 153 | } catch(e) { |
|
152 | 154 | console.log(e); |
|
153 | 155 | error_div.text('WARNING: Could not save invalid JSON.'); |
|
154 | 156 | return false; |
|
155 | 157 | } |
|
156 | 158 | options.callback(new_md); |
|
157 | 159 | } |
|
158 | 160 | }, |
|
159 | 161 | Cancel: {} |
|
160 | 162 | }, |
|
161 | 163 | notebook: options.notebook, |
|
162 | 164 | keyboard_manager: options.keyboard_manager, |
|
163 | 165 | }); |
|
164 | 166 | |
|
165 | 167 | modal_obj.on('shown.bs.modal', function(){ editor.refresh(); }); |
|
166 | 168 | }; |
|
167 | 169 | |
|
168 | 170 | var dialog = { |
|
169 | 171 | modal : modal, |
|
170 | 172 | kernel_modal : kernel_modal, |
|
171 | 173 | edit_metadata : edit_metadata, |
|
172 | 174 | }; |
|
173 | 175 | |
|
174 | 176 | // Backwards compatability. |
|
175 | 177 | IPython.dialog = dialog; |
|
176 | 178 | |
|
177 | 179 | return dialog; |
|
178 | 180 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now