|
1 |
| -define(['base/js/namespace', |
2 |
| - 'jquery', |
3 |
| - 'base/js/dialog', |
4 |
| - './list'], function(Jupyter, $, dialog, list) { |
5 |
| - function storageAvailable(type) { |
6 |
| - try { |
7 |
| - var storage = window[type]; |
8 |
| - var x = '__storage_test__'; |
9 |
| - storage.setItem(x, x); |
10 |
| - storage.removeItem(x); |
11 |
| - return true; |
12 |
| - } catch (e) { |
13 |
| - return false; |
14 |
| - } |
| 1 | +define(['base/js/namespace', |
| 2 | + 'jquery', |
| 3 | + 'base/js/dialog', |
| 4 | + './list' |
| 5 | +], function(Jupyter, $, dialog, list) { |
| 6 | + function storageAvailable(type) { |
| 7 | + try { |
| 8 | + var storage = window[type]; |
| 9 | + var x = '__storage_test__'; |
| 10 | + storage.setItem(x, x); |
| 11 | + storage.removeItem(x); |
| 12 | + return true; |
| 13 | + } catch (e) { |
| 14 | + return false; |
15 | 15 | }
|
16 |
| - |
17 |
| - function add_snippet_to_storage(snippet_name, snippet_content) { |
18 |
| - var storage = window['localStorage']; |
19 |
| - var stored_snippets = storage.getItem('JupyterNotebookSnippets'); |
20 |
| - if (stored_snippets == null) { |
21 |
| - stored_snippets = {}; |
22 |
| - } else { |
23 |
| - stored_snippets = JSON.parse(stored_snippets); |
24 |
| - } |
25 |
| - stored_snippets[snippet_name] = snippet_content; |
26 |
| - var updated_snippets = JSON.stringify(stored_snippets); |
27 |
| - storage.setItem('JupyterNotebookSnippets', updated_snippets); |
| 16 | + } |
| 17 | + |
| 18 | + function add_snippet_to_storage(snippet_name, snippet_content) { |
| 19 | + var storage = window['localStorage']; |
| 20 | + var stored_snippets = storage.getItem('JupyterNotebookSnippets'); |
| 21 | + if (stored_snippets == null) { |
| 22 | + stored_snippets = {}; |
| 23 | + } else { |
| 24 | + stored_snippets = JSON.parse(stored_snippets); |
28 | 25 | }
|
29 |
| - |
30 |
| - function add_cell_to_snippet_manager() { |
31 |
| - var selected_cell = Jupyter.notebook.get_selected_cell(); |
32 |
| - var selected_content = selected_cell.get_text(); |
33 |
| - |
34 |
| - var modal_content = $('<p/>').html("Please provide a name for this snippet."); |
35 |
| - modal_content.append($('<br><br>')); |
36 |
| - modal_content.append($('<input type="text" name="snippet-name"/>')); |
37 |
| - |
38 |
| - Jupyter.keyboard_manager.register_events(modal_content); |
39 |
| - |
40 |
| - dialog.modal({ |
41 |
| - title: 'Add Code Cell to Snippet Manager', |
42 |
| - body: modal_content, |
43 |
| - buttons: { |
44 |
| - Cancel: { |
45 |
| - 'class': 'btn-danger' |
46 |
| - }, |
47 |
| - OK: { |
48 |
| - 'class': 'btn-primary', |
49 |
| - 'click': function() { |
50 |
| - var snippet_name = $('input[name=snippet-name]').val(); |
51 |
| - add_snippet_to_storage(snippet_name, selected_content); |
52 |
| - } |
53 |
| - } |
54 |
| - } |
55 |
| - }); |
| 26 | + stored_snippets[snippet_name] = snippet_content; |
| 27 | + var updated_snippets = JSON.stringify(stored_snippets); |
| 28 | + storage.setItem('JupyterNotebookSnippets', updated_snippets); |
| 29 | + } |
| 30 | + |
| 31 | + function remove_snippet_from_storage(snippet_name) { |
| 32 | + var storage = window['localStorage']; |
| 33 | + var stored_snippets = storage.getItem('JupyterNotebookSnippets'); |
| 34 | + if (sotred_snippets == null) { |
| 35 | + return; |
| 36 | + } else { |
| 37 | + stored_snippets = JSON.parse(stored_snippets); |
56 | 38 | }
|
57 |
| - |
58 |
| - function get_snippets() { |
59 |
| - var storage = window['localStorage']; |
60 |
| - var stored_snippets = storage.getItem('JupyterNotebookSnippets'); |
61 |
| - if (stored_snippets == null) { |
62 |
| - return {}; |
63 |
| - } else { |
64 |
| - return JSON.parse(stored_snippets); |
| 39 | + delete stored_snippets[snippet_name]; |
| 40 | + var updated_snippets = JSON.stringify(stored_snippets); |
| 41 | + storage.setItem('JupyterNotebookSnippets', updated_snippets); |
| 42 | + } |
| 43 | + |
| 44 | + function add_cell_to_snippet_manager() { |
| 45 | + var selected_cell = Jupyter.notebook.get_selected_cell(); |
| 46 | + var selected_content = selected_cell.get_text(); |
| 47 | + |
| 48 | + var modal_content = $('<p/>').html("Please provide a name for this snippet."); |
| 49 | + modal_content.append($('<br><br>')); |
| 50 | + modal_content.append($('<input type="text" name="snippet-name"/>')); |
| 51 | + |
| 52 | + Jupyter.keyboard_manager.register_events(modal_content); |
| 53 | + |
| 54 | + dialog.modal({ |
| 55 | + title: 'Add Code Cell to Snippet Manager', |
| 56 | + body: modal_content, |
| 57 | + buttons: { |
| 58 | + Cancel: { |
| 59 | + 'class': 'btn-danger' |
| 60 | + }, |
| 61 | + OK: { |
| 62 | + 'class': 'btn-primary', |
| 63 | + 'click': function() { |
| 64 | + var snippet_name = $('input[name=snippet-name]').val(); |
| 65 | + add_snippet_to_storage(snippet_name, selected_content); |
| 66 | + } |
65 | 67 | }
|
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + function get_snippets() { |
| 73 | + var storage = window['localStorage']; |
| 74 | + var stored_snippets = storage.getItem('JupyterNotebookSnippets'); |
| 75 | + if (stored_snippets == null) { |
| 76 | + return {}; |
| 77 | + } else { |
| 78 | + return JSON.parse(stored_snippets); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + function add_cell_from_snippet_manager() { |
| 83 | + var selected_index = Jupyter.notebook.get_selected_index(); |
| 84 | + |
| 85 | + var modal_content = $('<p/>').html('Select a snippet to insert.'); |
| 86 | + modal_content.append('<br><br>'); |
| 87 | + var list_content = $('<div id="snippets" style="white-space: pre-wrap;">'); |
| 88 | + var table = $('<table class="table"><tbody class="list"></tbody></table>'); |
| 89 | + var table_children = table.children(); |
| 90 | + table_children.append('<tr><th>Selected</th>' + |
| 91 | + '<th>Snippet Name</th>' + |
| 92 | + '<th>Snippet Content</th></tr>'); |
| 93 | + |
| 94 | + var snippets = get_snippets(); |
| 95 | + for (var index in snippets) { |
| 96 | + var snippet = snippets[index]; |
| 97 | + table_children.append('<tr><td class="selected"><input type="checkbox"/></td>' + |
| 98 | + '<td class="name">' + index + '</td>' + |
| 99 | + '<td class="content">' + snippet + '</td></tr>'); |
66 | 100 | }
|
67 | 101 |
|
68 |
| - function add_cell_from_snippet_manager() { |
69 |
| - var selected_index = Jupyter.notebook.get_selected_index(); |
70 |
| - |
71 |
| - var modal_content = $('<p/>').html('Select a snippet to insert.'); |
72 |
| - modal_content.append('<br><br>'); |
73 |
| - var list_content = $('<div id="snippets" style="white-space: pre-wrap;">'); |
74 |
| - var table = $('<table class="table"><tbody class="list"></tbody></table>'); |
75 |
| - var table_children = table.children(); |
76 |
| - table_children.append('<tr><th>Insert?</th>' + |
77 |
| - '<th>Snippet Name</th>' + |
78 |
| - '<th>Snippet Content</th></tr>'); |
79 |
| - |
80 |
| - var snippets = get_snippets(); |
81 |
| - for (var index in snippets) { |
82 |
| - var snippet = snippets[index]; |
83 |
| - table_children.append('<tr><td class="selected"><input type="checkbox"/></td>' + |
84 |
| - '<td class="name">' + index + '</td>' + |
85 |
| - '<td class="content">' + snippet + '</td></tr>'); |
| 102 | + list_content.append(table); |
| 103 | + modal_content.append(list_content); |
| 104 | + |
| 105 | + Jupyter.keyboard_manager.register_events(modal_content); |
| 106 | + |
| 107 | + dialog.modal({ |
| 108 | + title: 'Select A Snippet to Include', |
| 109 | + body: modal_content, |
| 110 | + buttons: { |
| 111 | + 'Insert Snippet': { |
| 112 | + 'class': 'btn-primary', |
| 113 | + 'click': function() { |
| 114 | + var selected_snippets = $('.selected input:checked'); |
| 115 | + var selected_content = selected_snippets.map(function() { |
| 116 | + var content = $(this).parent('td').parent('tr').children('.content'); |
| 117 | + return $(content.get(0)).text(); |
| 118 | + }); |
| 119 | + for (var index in selected_content) { |
| 120 | + var code_cell = Jupyter.notebook.insert_cell_at_index('code', selected_index + index); |
| 121 | + code_cell.set_text(selected_content[index]); |
| 122 | + code_cell.execute(); |
| 123 | + } |
| 124 | + } |
| 125 | + }, |
| 126 | + 'Delete Snippet': { |
| 127 | + 'class': 'btn-danger', |
| 128 | + 'click': function() { |
| 129 | + var selected_snippets = $('.selected input:checked'); |
| 130 | + var selected_names = selected_snippets.map(function() { |
| 131 | + var name = $(this).parent('td').parent('tr').children('.name'); |
| 132 | + return $(name.get(0)).text(); |
| 133 | + }); |
| 134 | + for (var index in selected_names) { |
| 135 | + remove_snippet_from_manager(selected_names[index]); |
| 136 | + } |
| 137 | + } |
86 | 138 | }
|
| 139 | + } |
| 140 | + }); |
87 | 141 |
|
88 |
| - list_content.append(table); |
89 |
| - modal_content.append(list_content); |
90 |
| - |
91 |
| - Jupyter.keyboard_manager.register_events(modal_content); |
92 |
| - |
93 |
| - dialog.modal({ |
94 |
| - title: 'Select A Snippet to Include', |
95 |
| - body: modal_content, |
96 |
| - buttons: { |
97 |
| - 'Insert Snippet': { |
98 |
| - 'class': 'btn-primary', |
99 |
| - 'click': function() { |
100 |
| - var selected_snippets = $('.selected input:checked'); |
101 |
| - var selected_content = selected_snippets.map(function() { |
102 |
| - var content = $(this).parent('td').parent('tr').children('.content'); |
103 |
| - return $(content.get(0)).text(); |
104 |
| - }); |
105 |
| - for (var index in selected_content) { |
106 |
| - var code_cell = Jupyter.notebook.insert_cell_at_index('code', selected_index + index); |
107 |
| - code_cell.set_text(selected_content[index]); |
108 |
| - code_cell.execute(); |
109 |
| - } |
110 |
| - } |
111 |
| - } |
112 |
| - } |
113 |
| - }); |
| 142 | + } |
114 | 143 |
|
| 144 | + function place_snippet_manager_buttons() { |
| 145 | + if (!Jupyter.toolbar) { |
| 146 | + $([Jupyter.events]).on("app_initialized.NotebookApp", place_snippet_manager_buttons); |
| 147 | + return; |
115 | 148 | }
|
116 | 149 |
|
117 |
| - function place_snippet_manager_buttons() { |
118 |
| - if (!Jupyter.toolbar) { |
119 |
| - $([Jupyter.events]).on("app_initialized.NotebookApp", place_snippet_manager_buttons); |
120 |
| - return; |
121 |
| - } |
122 |
| - |
123 |
| - if ($(".snippet-manager-buttons").length === 0) { |
124 |
| - Jupyter.toolbar.add_buttons_group([ |
125 |
| - { |
126 |
| - 'label': 'Add Cell to Snippet Manager', |
127 |
| - 'icon': 'fa-arrow-circle-up', |
128 |
| - 'callback': add_cell_to_snippet_manager, |
129 |
| - 'id': 'add-cell-to-snippet-manager', |
130 |
| - 'class': 'snippet-manager-buttons' |
131 |
| - }, |
132 |
| - { |
133 |
| - 'label': 'Add Cell from Snippet Manager', |
134 |
| - 'icon': 'fa-arrow-circle-down', |
135 |
| - 'callback': add_cell_from_snippet_manager, |
136 |
| - 'id': 'add-cell-from-snippet-manager', |
137 |
| - 'class': 'snippet-manager-buttons' |
138 |
| - } |
139 |
| - ]); |
| 150 | + if ($(".snippet-manager-buttons").length === 0) { |
| 151 | + Jupyter.toolbar.add_buttons_group([{ |
| 152 | + 'label': 'Add Cell to Snippet Manager', |
| 153 | + 'icon': 'fa-arrow-circle-up', |
| 154 | + 'callback': add_cell_to_snippet_manager, |
| 155 | + 'id': 'add-cell-to-snippet-manager', |
| 156 | + 'class': 'snippet-manager-buttons' |
| 157 | + }, |
| 158 | + { |
| 159 | + 'label': 'Add Cell from Snippet Manager', |
| 160 | + 'icon': 'fa-arrow-circle-down', |
| 161 | + 'callback': add_cell_from_snippet_manager, |
| 162 | + 'id': 'add-cell-from-snippet-manager', |
| 163 | + 'class': 'snippet-manager-buttons' |
140 | 164 | }
|
| 165 | + ]); |
141 | 166 | }
|
142 |
| - |
143 |
| - function load_ipython_extension() { |
144 |
| - console.log("Loading notebook-snippet-manager extension..."); |
145 |
| - if (storageAvailable('localStorage')) { |
146 |
| - place_snippet_manager_buttons(); |
147 |
| - } else { |
148 |
| - var modal_text = "It looks like you have the snippet-manager"; |
149 |
| - modal_text += " enabled but your browser doesn't support WebStorage."; |
150 |
| - modal_text += " Please switch to a browser with WebStorage to use snippet-manager."; |
151 |
| - var modal_content = $('<p/>').html(modal_text); |
152 |
| - dialog.modal({ |
153 |
| - 'title': 'WebStorage Unavaialble', |
154 |
| - 'body': modal_content, |
155 |
| - 'buttons': {OK: {}} |
156 |
| - }); |
| 167 | + } |
| 168 | + |
| 169 | + function load_ipython_extension() { |
| 170 | + console.log("Loading notebook-snippet-manager extension..."); |
| 171 | + if (storageAvailable('localStorage')) { |
| 172 | + place_snippet_manager_buttons(); |
| 173 | + } else { |
| 174 | + var modal_text = "It looks like you have the snippet-manager"; |
| 175 | + modal_text += " enabled but your browser doesn't support WebStorage."; |
| 176 | + modal_text += " Please switch to a browser with WebStorage to use snippet-manager."; |
| 177 | + var modal_content = $('<p/>').html(modal_text); |
| 178 | + dialog.modal({ |
| 179 | + 'title': 'WebStorage Unavaialble', |
| 180 | + 'body': modal_content, |
| 181 | + 'buttons': { |
| 182 | + OK: {} |
157 | 183 | }
|
| 184 | + }); |
158 | 185 | }
|
| 186 | + } |
159 | 187 |
|
160 |
| - return { |
161 |
| - load_ipython_extension: load_ipython_extension |
162 |
| - }; |
| 188 | + return { |
| 189 | + load_ipython_extension: load_ipython_extension |
| 190 | + }; |
163 | 191 | });
|
0 commit comments