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