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