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