##// END OF EJS Templates
Make Contents.new more generic
Thomas Kluyver -
Show More
@@ -1,373 +1,373
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 'notebook/js/tour',
8 'notebook/js/tour',
9 'bootstrap',
9 'bootstrap',
10 'moment',
10 'moment',
11 ], function(IPython, $, utils, tour, bootstrap, moment) {
11 ], function(IPython, $, utils, tour, bootstrap, moment) {
12 "use strict";
12 "use strict";
13
13
14 var MenuBar = function (selector, options) {
14 var MenuBar = function (selector, options) {
15 // Constructor
15 // Constructor
16 //
16 //
17 // A MenuBar Class to generate the menubar of IPython notebook
17 // A MenuBar Class to generate the menubar of IPython notebook
18 //
18 //
19 // Parameters:
19 // Parameters:
20 // selector: string
20 // selector: string
21 // options: dictionary
21 // options: dictionary
22 // Dictionary of keyword arguments.
22 // Dictionary of keyword arguments.
23 // notebook: Notebook instance
23 // notebook: Notebook instance
24 // contents: ContentManager instance
24 // contents: ContentManager instance
25 // layout_manager: LayoutManager instance
25 // layout_manager: LayoutManager instance
26 // events: $(Events) instance
26 // events: $(Events) instance
27 // save_widget: SaveWidget instance
27 // save_widget: SaveWidget instance
28 // quick_help: QuickHelp instance
28 // quick_help: QuickHelp instance
29 // base_url : string
29 // base_url : string
30 // notebook_path : string
30 // notebook_path : string
31 // notebook_name : string
31 // notebook_name : string
32 options = options || {};
32 options = options || {};
33 this.base_url = options.base_url || utils.get_body_data("baseUrl");
33 this.base_url = options.base_url || utils.get_body_data("baseUrl");
34 this.selector = selector;
34 this.selector = selector;
35 this.notebook = options.notebook;
35 this.notebook = options.notebook;
36 this.contents = options.contents;
36 this.contents = options.contents;
37 this.layout_manager = options.layout_manager;
37 this.layout_manager = options.layout_manager;
38 this.events = options.events;
38 this.events = options.events;
39 this.save_widget = options.save_widget;
39 this.save_widget = options.save_widget;
40 this.quick_help = options.quick_help;
40 this.quick_help = options.quick_help;
41
41
42 try {
42 try {
43 this.tour = new tour.Tour(this.notebook, this.events);
43 this.tour = new tour.Tour(this.notebook, this.events);
44 } catch (e) {
44 } catch (e) {
45 this.tour = undefined;
45 this.tour = undefined;
46 console.log("Failed to instantiate Notebook Tour", e);
46 console.log("Failed to instantiate Notebook Tour", e);
47 }
47 }
48
48
49 if (this.selector !== undefined) {
49 if (this.selector !== undefined) {
50 this.element = $(selector);
50 this.element = $(selector);
51 this.style();
51 this.style();
52 this.bind_events();
52 this.bind_events();
53 }
53 }
54 };
54 };
55
55
56 // TODO: This has definitively nothing to do with style ...
56 // TODO: This has definitively nothing to do with style ...
57 MenuBar.prototype.style = function () {
57 MenuBar.prototype.style = function () {
58 var that = this;
58 var that = this;
59 this.element.find("li").click(function (event, ui) {
59 this.element.find("li").click(function (event, ui) {
60 // The selected cell loses focus when the menu is entered, so we
60 // The selected cell loses focus when the menu is entered, so we
61 // re-select it upon selection.
61 // re-select it upon selection.
62 var i = that.notebook.get_selected_index();
62 var i = that.notebook.get_selected_index();
63 that.notebook.select(i);
63 that.notebook.select(i);
64 }
64 }
65 );
65 );
66 };
66 };
67
67
68 MenuBar.prototype._nbconvert = function (format, download) {
68 MenuBar.prototype._nbconvert = function (format, download) {
69 download = download || false;
69 download = download || false;
70 var notebook_path = this.notebook.notebook_path;
70 var notebook_path = this.notebook.notebook_path;
71 var notebook_name = this.notebook.notebook_name;
71 var notebook_name = this.notebook.notebook_name;
72 if (this.notebook.dirty) {
72 if (this.notebook.dirty) {
73 this.notebook.save_notebook({async : false});
73 this.notebook.save_notebook({async : false});
74 }
74 }
75 var url = utils.url_join_encode(
75 var url = utils.url_join_encode(
76 this.base_url,
76 this.base_url,
77 'nbconvert',
77 'nbconvert',
78 format,
78 format,
79 notebook_path,
79 notebook_path,
80 notebook_name
80 notebook_name
81 ) + "?download=" + download.toString();
81 ) + "?download=" + download.toString();
82
82
83 window.open(url);
83 window.open(url);
84 };
84 };
85
85
86 MenuBar.prototype.bind_events = function () {
86 MenuBar.prototype.bind_events = function () {
87 // File
87 // File
88 var that = this;
88 var that = this;
89 this.element.find('#new_notebook').click(function () {
89 this.element.find('#new_notebook').click(function () {
90 // Create a new notebook in the same path as the current
90 // Create a new notebook in the same path as the current
91 // notebook's path.
91 // notebook's path.
92 that.contents.new_notebook(that.notebook.notebook_path,
92 that.contents.new(that.notebook.notebook_path, null, {
93 {
93 ext: ".ipynb",
94 success: function (data) {
94 success: function (data) {
95 window.open(
95 window.open(
96 utils.url_join_encode(
96 utils.url_join_encode(
97 that.base_url, 'notebooks',
97 that.base_url, 'notebooks',
98 data.path, data.name
98 data.path, data.name
99 ), '_blank');
99 ), '_blank');
100 },
100 },
101 error: function(error) {
101 error: function(error) {
102 dialog.modal({
102 dialog.modal({
103 title : 'Creating Notebook Failed',
103 title : 'Creating Notebook Failed',
104 body : "The error was: " + error.message,
104 body : "The error was: " + error.message,
105 buttons : {'OK' : {'class' : 'btn-primary'}}
105 buttons : {'OK' : {'class' : 'btn-primary'}}
106 });
106 });
107 }
107 }
108 });
108 });
109 });
109 });
110 this.element.find('#open_notebook').click(function () {
110 this.element.find('#open_notebook').click(function () {
111 window.open(utils.url_join_encode(
111 window.open(utils.url_join_encode(
112 that.notebook.base_url,
112 that.notebook.base_url,
113 'tree',
113 'tree',
114 that.notebook.notebook_path
114 that.notebook.notebook_path
115 ));
115 ));
116 });
116 });
117 this.element.find('#copy_notebook').click(function () {
117 this.element.find('#copy_notebook').click(function () {
118 that.notebook.copy_notebook();
118 that.notebook.copy_notebook();
119 return false;
119 return false;
120 });
120 });
121 this.element.find('#download_ipynb').click(function () {
121 this.element.find('#download_ipynb').click(function () {
122 var base_url = that.notebook.base_url;
122 var base_url = that.notebook.base_url;
123 var notebook_path = that.notebook.notebook_path;
123 var notebook_path = that.notebook.notebook_path;
124 var notebook_name = that.notebook.notebook_name;
124 var notebook_name = that.notebook.notebook_name;
125 if (that.notebook.dirty) {
125 if (that.notebook.dirty) {
126 that.notebook.save_notebook({async : false});
126 that.notebook.save_notebook({async : false});
127 }
127 }
128
128
129 var url = utils.url_join_encode(
129 var url = utils.url_join_encode(
130 base_url,
130 base_url,
131 'files',
131 'files',
132 notebook_path,
132 notebook_path,
133 notebook_name
133 notebook_name
134 );
134 );
135 window.open(url + '?download=1');
135 window.open(url + '?download=1');
136 });
136 });
137
137
138 this.element.find('#print_preview').click(function () {
138 this.element.find('#print_preview').click(function () {
139 that._nbconvert('html', false);
139 that._nbconvert('html', false);
140 });
140 });
141
141
142 this.element.find('#download_py').click(function () {
142 this.element.find('#download_py').click(function () {
143 that._nbconvert('python', true);
143 that._nbconvert('python', true);
144 });
144 });
145
145
146 this.element.find('#download_html').click(function () {
146 this.element.find('#download_html').click(function () {
147 that._nbconvert('html', true);
147 that._nbconvert('html', true);
148 });
148 });
149
149
150 this.element.find('#download_rst').click(function () {
150 this.element.find('#download_rst').click(function () {
151 that._nbconvert('rst', true);
151 that._nbconvert('rst', true);
152 });
152 });
153
153
154 this.element.find('#download_pdf').click(function () {
154 this.element.find('#download_pdf').click(function () {
155 that._nbconvert('pdf', true);
155 that._nbconvert('pdf', true);
156 });
156 });
157
157
158 this.element.find('#rename_notebook').click(function () {
158 this.element.find('#rename_notebook').click(function () {
159 that.save_widget.rename_notebook({notebook: that.notebook});
159 that.save_widget.rename_notebook({notebook: that.notebook});
160 });
160 });
161 this.element.find('#save_checkpoint').click(function () {
161 this.element.find('#save_checkpoint').click(function () {
162 that.notebook.save_checkpoint();
162 that.notebook.save_checkpoint();
163 });
163 });
164 this.element.find('#restore_checkpoint').click(function () {
164 this.element.find('#restore_checkpoint').click(function () {
165 });
165 });
166 this.element.find('#trust_notebook').click(function () {
166 this.element.find('#trust_notebook').click(function () {
167 that.notebook.trust_notebook();
167 that.notebook.trust_notebook();
168 });
168 });
169 this.events.on('trust_changed.Notebook', function (event, trusted) {
169 this.events.on('trust_changed.Notebook', function (event, trusted) {
170 if (trusted) {
170 if (trusted) {
171 that.element.find('#trust_notebook')
171 that.element.find('#trust_notebook')
172 .addClass("disabled")
172 .addClass("disabled")
173 .find("a").text("Trusted Notebook");
173 .find("a").text("Trusted Notebook");
174 } else {
174 } else {
175 that.element.find('#trust_notebook')
175 that.element.find('#trust_notebook')
176 .removeClass("disabled")
176 .removeClass("disabled")
177 .find("a").text("Trust Notebook");
177 .find("a").text("Trust Notebook");
178 }
178 }
179 });
179 });
180 this.element.find('#kill_and_exit').click(function () {
180 this.element.find('#kill_and_exit').click(function () {
181 var close_window = function () {
181 var close_window = function () {
182 // allow closing of new tabs in Chromium, impossible in FF
182 // allow closing of new tabs in Chromium, impossible in FF
183 window.open('', '_self', '');
183 window.open('', '_self', '');
184 window.close();
184 window.close();
185 };
185 };
186 // finish with close on success or failure
186 // finish with close on success or failure
187 that.notebook.session.delete(close_window, close_window);
187 that.notebook.session.delete(close_window, close_window);
188 });
188 });
189 // Edit
189 // Edit
190 this.element.find('#cut_cell').click(function () {
190 this.element.find('#cut_cell').click(function () {
191 that.notebook.cut_cell();
191 that.notebook.cut_cell();
192 });
192 });
193 this.element.find('#copy_cell').click(function () {
193 this.element.find('#copy_cell').click(function () {
194 that.notebook.copy_cell();
194 that.notebook.copy_cell();
195 });
195 });
196 this.element.find('#delete_cell').click(function () {
196 this.element.find('#delete_cell').click(function () {
197 that.notebook.delete_cell();
197 that.notebook.delete_cell();
198 });
198 });
199 this.element.find('#undelete_cell').click(function () {
199 this.element.find('#undelete_cell').click(function () {
200 that.notebook.undelete_cell();
200 that.notebook.undelete_cell();
201 });
201 });
202 this.element.find('#split_cell').click(function () {
202 this.element.find('#split_cell').click(function () {
203 that.notebook.split_cell();
203 that.notebook.split_cell();
204 });
204 });
205 this.element.find('#merge_cell_above').click(function () {
205 this.element.find('#merge_cell_above').click(function () {
206 that.notebook.merge_cell_above();
206 that.notebook.merge_cell_above();
207 });
207 });
208 this.element.find('#merge_cell_below').click(function () {
208 this.element.find('#merge_cell_below').click(function () {
209 that.notebook.merge_cell_below();
209 that.notebook.merge_cell_below();
210 });
210 });
211 this.element.find('#move_cell_up').click(function () {
211 this.element.find('#move_cell_up').click(function () {
212 that.notebook.move_cell_up();
212 that.notebook.move_cell_up();
213 });
213 });
214 this.element.find('#move_cell_down').click(function () {
214 this.element.find('#move_cell_down').click(function () {
215 that.notebook.move_cell_down();
215 that.notebook.move_cell_down();
216 });
216 });
217 this.element.find('#edit_nb_metadata').click(function () {
217 this.element.find('#edit_nb_metadata').click(function () {
218 that.notebook.edit_metadata({
218 that.notebook.edit_metadata({
219 notebook: that.notebook,
219 notebook: that.notebook,
220 keyboard_manager: that.notebook.keyboard_manager});
220 keyboard_manager: that.notebook.keyboard_manager});
221 });
221 });
222
222
223 // View
223 // View
224 this.element.find('#toggle_header').click(function () {
224 this.element.find('#toggle_header').click(function () {
225 $('div#header').toggle();
225 $('div#header').toggle();
226 that.layout_manager.do_resize();
226 that.layout_manager.do_resize();
227 });
227 });
228 this.element.find('#toggle_toolbar').click(function () {
228 this.element.find('#toggle_toolbar').click(function () {
229 $('div#maintoolbar').toggle();
229 $('div#maintoolbar').toggle();
230 that.layout_manager.do_resize();
230 that.layout_manager.do_resize();
231 });
231 });
232 // Insert
232 // Insert
233 this.element.find('#insert_cell_above').click(function () {
233 this.element.find('#insert_cell_above').click(function () {
234 that.notebook.insert_cell_above('code');
234 that.notebook.insert_cell_above('code');
235 that.notebook.select_prev();
235 that.notebook.select_prev();
236 });
236 });
237 this.element.find('#insert_cell_below').click(function () {
237 this.element.find('#insert_cell_below').click(function () {
238 that.notebook.insert_cell_below('code');
238 that.notebook.insert_cell_below('code');
239 that.notebook.select_next();
239 that.notebook.select_next();
240 });
240 });
241 // Cell
241 // Cell
242 this.element.find('#run_cell').click(function () {
242 this.element.find('#run_cell').click(function () {
243 that.notebook.execute_cell();
243 that.notebook.execute_cell();
244 });
244 });
245 this.element.find('#run_cell_select_below').click(function () {
245 this.element.find('#run_cell_select_below').click(function () {
246 that.notebook.execute_cell_and_select_below();
246 that.notebook.execute_cell_and_select_below();
247 });
247 });
248 this.element.find('#run_cell_insert_below').click(function () {
248 this.element.find('#run_cell_insert_below').click(function () {
249 that.notebook.execute_cell_and_insert_below();
249 that.notebook.execute_cell_and_insert_below();
250 });
250 });
251 this.element.find('#run_all_cells').click(function () {
251 this.element.find('#run_all_cells').click(function () {
252 that.notebook.execute_all_cells();
252 that.notebook.execute_all_cells();
253 });
253 });
254 this.element.find('#run_all_cells_above').click(function () {
254 this.element.find('#run_all_cells_above').click(function () {
255 that.notebook.execute_cells_above();
255 that.notebook.execute_cells_above();
256 });
256 });
257 this.element.find('#run_all_cells_below').click(function () {
257 this.element.find('#run_all_cells_below').click(function () {
258 that.notebook.execute_cells_below();
258 that.notebook.execute_cells_below();
259 });
259 });
260 this.element.find('#to_code').click(function () {
260 this.element.find('#to_code').click(function () {
261 that.notebook.to_code();
261 that.notebook.to_code();
262 });
262 });
263 this.element.find('#to_markdown').click(function () {
263 this.element.find('#to_markdown').click(function () {
264 that.notebook.to_markdown();
264 that.notebook.to_markdown();
265 });
265 });
266 this.element.find('#to_raw').click(function () {
266 this.element.find('#to_raw').click(function () {
267 that.notebook.to_raw();
267 that.notebook.to_raw();
268 });
268 });
269 this.element.find('#to_heading1').click(function () {
269 this.element.find('#to_heading1').click(function () {
270 that.notebook.to_heading(undefined, 1);
270 that.notebook.to_heading(undefined, 1);
271 });
271 });
272 this.element.find('#to_heading2').click(function () {
272 this.element.find('#to_heading2').click(function () {
273 that.notebook.to_heading(undefined, 2);
273 that.notebook.to_heading(undefined, 2);
274 });
274 });
275 this.element.find('#to_heading3').click(function () {
275 this.element.find('#to_heading3').click(function () {
276 that.notebook.to_heading(undefined, 3);
276 that.notebook.to_heading(undefined, 3);
277 });
277 });
278 this.element.find('#to_heading4').click(function () {
278 this.element.find('#to_heading4').click(function () {
279 that.notebook.to_heading(undefined, 4);
279 that.notebook.to_heading(undefined, 4);
280 });
280 });
281 this.element.find('#to_heading5').click(function () {
281 this.element.find('#to_heading5').click(function () {
282 that.notebook.to_heading(undefined, 5);
282 that.notebook.to_heading(undefined, 5);
283 });
283 });
284 this.element.find('#to_heading6').click(function () {
284 this.element.find('#to_heading6').click(function () {
285 that.notebook.to_heading(undefined, 6);
285 that.notebook.to_heading(undefined, 6);
286 });
286 });
287
287
288 this.element.find('#toggle_current_output').click(function () {
288 this.element.find('#toggle_current_output').click(function () {
289 that.notebook.toggle_output();
289 that.notebook.toggle_output();
290 });
290 });
291 this.element.find('#toggle_current_output_scroll').click(function () {
291 this.element.find('#toggle_current_output_scroll').click(function () {
292 that.notebook.toggle_output_scroll();
292 that.notebook.toggle_output_scroll();
293 });
293 });
294 this.element.find('#clear_current_output').click(function () {
294 this.element.find('#clear_current_output').click(function () {
295 that.notebook.clear_output();
295 that.notebook.clear_output();
296 });
296 });
297
297
298 this.element.find('#toggle_all_output').click(function () {
298 this.element.find('#toggle_all_output').click(function () {
299 that.notebook.toggle_all_output();
299 that.notebook.toggle_all_output();
300 });
300 });
301 this.element.find('#toggle_all_output_scroll').click(function () {
301 this.element.find('#toggle_all_output_scroll').click(function () {
302 that.notebook.toggle_all_output_scroll();
302 that.notebook.toggle_all_output_scroll();
303 });
303 });
304 this.element.find('#clear_all_output').click(function () {
304 this.element.find('#clear_all_output').click(function () {
305 that.notebook.clear_all_output();
305 that.notebook.clear_all_output();
306 });
306 });
307
307
308 // Kernel
308 // Kernel
309 this.element.find('#int_kernel').click(function () {
309 this.element.find('#int_kernel').click(function () {
310 that.notebook.kernel.interrupt();
310 that.notebook.kernel.interrupt();
311 });
311 });
312 this.element.find('#restart_kernel').click(function () {
312 this.element.find('#restart_kernel').click(function () {
313 that.notebook.restart_kernel();
313 that.notebook.restart_kernel();
314 });
314 });
315 // Help
315 // Help
316 if (this.tour) {
316 if (this.tour) {
317 this.element.find('#notebook_tour').click(function () {
317 this.element.find('#notebook_tour').click(function () {
318 that.tour.start();
318 that.tour.start();
319 });
319 });
320 } else {
320 } else {
321 this.element.find('#notebook_tour').addClass("disabled");
321 this.element.find('#notebook_tour').addClass("disabled");
322 }
322 }
323 this.element.find('#keyboard_shortcuts').click(function () {
323 this.element.find('#keyboard_shortcuts').click(function () {
324 that.quick_help.show_keyboard_shortcuts();
324 that.quick_help.show_keyboard_shortcuts();
325 });
325 });
326
326
327 this.update_restore_checkpoint(null);
327 this.update_restore_checkpoint(null);
328
328
329 this.events.on('checkpoints_listed.Notebook', function (event, data) {
329 this.events.on('checkpoints_listed.Notebook', function (event, data) {
330 that.update_restore_checkpoint(that.notebook.checkpoints);
330 that.update_restore_checkpoint(that.notebook.checkpoints);
331 });
331 });
332
332
333 this.events.on('checkpoint_created.Notebook', function (event, data) {
333 this.events.on('checkpoint_created.Notebook', function (event, data) {
334 that.update_restore_checkpoint(that.notebook.checkpoints);
334 that.update_restore_checkpoint(that.notebook.checkpoints);
335 });
335 });
336 };
336 };
337
337
338 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
338 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
339 var ul = this.element.find("#restore_checkpoint").find("ul");
339 var ul = this.element.find("#restore_checkpoint").find("ul");
340 ul.empty();
340 ul.empty();
341 if (!checkpoints || checkpoints.length === 0) {
341 if (!checkpoints || checkpoints.length === 0) {
342 ul.append(
342 ul.append(
343 $("<li/>")
343 $("<li/>")
344 .addClass("disabled")
344 .addClass("disabled")
345 .append(
345 .append(
346 $("<a/>")
346 $("<a/>")
347 .text("No checkpoints")
347 .text("No checkpoints")
348 )
348 )
349 );
349 );
350 return;
350 return;
351 }
351 }
352
352
353 var that = this;
353 var that = this;
354 checkpoints.map(function (checkpoint) {
354 checkpoints.map(function (checkpoint) {
355 var d = new Date(checkpoint.last_modified);
355 var d = new Date(checkpoint.last_modified);
356 ul.append(
356 ul.append(
357 $("<li/>").append(
357 $("<li/>").append(
358 $("<a/>")
358 $("<a/>")
359 .attr("href", "#")
359 .attr("href", "#")
360 .text(moment(d).format("LLLL"))
360 .text(moment(d).format("LLLL"))
361 .click(function () {
361 .click(function () {
362 that.notebook.restore_checkpoint_dialog(checkpoint);
362 that.notebook.restore_checkpoint_dialog(checkpoint);
363 })
363 })
364 )
364 )
365 );
365 );
366 });
366 });
367 };
367 };
368
368
369 // Backwards compatability.
369 // Backwards compatability.
370 IPython.MenuBar = MenuBar;
370 IPython.MenuBar = MenuBar;
371
371
372 return {'MenuBar': MenuBar};
372 return {'MenuBar': MenuBar};
373 });
373 });
@@ -1,271 +1,282
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 'base/js/dialog',
8 'base/js/dialog',
9 ], function(IPython, $, utils, dialog) {
9 ], function(IPython, $, utils, dialog) {
10 var Contents = function(options) {
10 var Contents = function(options) {
11 // Constructor
11 // Constructor
12 //
12 //
13 // A contents handles passing file operations
13 // A contents handles passing file operations
14 // to the back-end. This includes checkpointing
14 // to the back-end. This includes checkpointing
15 // with the normal file operations.
15 // with the normal file operations.
16 //
16 //
17 // Parameters:
17 // Parameters:
18 // options: dictionary
18 // options: dictionary
19 // Dictionary of keyword arguments.
19 // Dictionary of keyword arguments.
20 // base_url: string
20 // base_url: string
21 this.base_url = options.base_url;
21 this.base_url = options.base_url;
22 };
22 };
23
23
24 /** Error type */
24 /** Error type */
25 Contents.DIRECTORY_NOT_EMPTY_ERROR = 'DirectoryNotEmptyError';
25 Contents.DIRECTORY_NOT_EMPTY_ERROR = 'DirectoryNotEmptyError';
26
26
27 Contents.DirectoryNotEmptyError = function() {
27 Contents.DirectoryNotEmptyError = function() {
28 // Constructor
28 // Constructor
29 //
29 //
30 // An error representing the result of attempting to delete a non-empty
30 // An error representing the result of attempting to delete a non-empty
31 // directory.
31 // directory.
32 this.message = 'A directory must be empty before being deleted.';
32 this.message = 'A directory must be empty before being deleted.';
33 }
33 }
34 Contents.DirectoryNotEmptyError.prototype = new Error;
34 Contents.DirectoryNotEmptyError.prototype = new Error;
35 Contents.DirectoryNotEmptyError.prototype.name =
35 Contents.DirectoryNotEmptyError.prototype.name =
36 Contents.DIRECTORY_NOT_EMPTY_ERROR;
36 Contents.DIRECTORY_NOT_EMPTY_ERROR;
37
37
38
38
39 Contents.prototype.api_url = function() {
39 Contents.prototype.api_url = function() {
40 var url_parts = [this.base_url, 'api/contents'].concat(
40 var url_parts = [this.base_url, 'api/contents'].concat(
41 Array.prototype.slice.apply(arguments));
41 Array.prototype.slice.apply(arguments));
42 return utils.url_join_encode.apply(null, url_parts);
42 return utils.url_join_encode.apply(null, url_parts);
43 };
43 };
44
44
45 /**
45 /**
46 * Creates a basic error handler that wraps a jqXHR error as an Error.
46 * Creates a basic error handler that wraps a jqXHR error as an Error.
47 *
47 *
48 * Takes a callback that accepts an Error, and returns a callback that can
48 * Takes a callback that accepts an Error, and returns a callback that can
49 * be passed directly to $.ajax, which will wrap the error from jQuery
49 * be passed directly to $.ajax, which will wrap the error from jQuery
50 * as an Error, and pass that to the original callback.
50 * as an Error, and pass that to the original callback.
51 *
51 *
52 * @method create_basic_error_handler
52 * @method create_basic_error_handler
53 * @param{Function} callback
53 * @param{Function} callback
54 * @return{Function}
54 * @return{Function}
55 */
55 */
56 Contents.prototype.create_basic_error_handler = function(callback) {
56 Contents.prototype.create_basic_error_handler = function(callback) {
57 if (!callback) {
57 if (!callback) {
58 return function(xhr, status, error) { };
58 return function(xhr, status, error) { };
59 }
59 }
60 return function(xhr, status, error) {
60 return function(xhr, status, error) {
61 callback(utils.wrap_ajax_error(xhr, status, error));
61 callback(utils.wrap_ajax_error(xhr, status, error));
62 };
62 };
63 }
63 }
64
64
65 /**
65 /**
66 * File Functions (including notebook operations)
66 * File Functions (including notebook operations)
67 */
67 */
68
68
69 /**
69 /**
70 * Load a file.
70 * Load a file.
71 *
71 *
72 * Calls success with file JSON model, or error with error.
72 * Calls success with file JSON model, or error with error.
73 *
73 *
74 * @method load_notebook
74 * @method load_notebook
75 * @param {String} path
75 * @param {String} path
76 * @param {String} name
76 * @param {String} name
77 * @param {Function} success
77 * @param {Function} success
78 * @param {Function} error
78 * @param {Function} error
79 */
79 */
80 Contents.prototype.load_file = function (path, name, options) {
80 Contents.prototype.load_file = function (path, name, options) {
81 // We do the call with settings so we can set cache to false.
81 // We do the call with settings so we can set cache to false.
82 var settings = {
82 var settings = {
83 processData : false,
83 processData : false,
84 cache : false,
84 cache : false,
85 type : "GET",
85 type : "GET",
86 dataType : "json",
86 dataType : "json",
87 success : options.success,
87 success : options.success,
88 error : this.create_basic_error_handler(options.error)
88 error : this.create_basic_error_handler(options.error)
89 };
89 };
90 var url = this.api_url(path, name);
90 var url = this.api_url(path, name);
91 $.ajax(url, settings);
91 $.ajax(url, settings);
92 };
92 };
93
93
94
94
95 /**
95 /**
96 * Creates a new notebook file at the specified directory path.
96 * Creates a new notebook file at the specified directory path.
97 *
97 *
98 * @method scroll_to_cell
98 * @method scroll_to_cell
99 * @param {String} path The path to create the new notebook at
99 * @param {String} path The path to create the new notebook at
100 * @param {String} name Name for new file. Chosen by server if unspecified.
101 * @param {Object} options:
102 * ext: file extension to use if name unspecified
100 */
103 */
101 Contents.prototype.new_notebook = function(path, options) {
104 Contents.prototype.new = function(path, name, options) {
102 var error = options.error || function() {};
105 var method, data;
106 if (name) {
107 method = "PUT";
108 } else {
109 method = "POST";
110 data = JSON.stringify({ext: options.ext || ".ipynb"});
111 }
112
103 var settings = {
113 var settings = {
104 processData : false,
114 processData : false,
105 type : "POST",
115 type : method,
116 data: data,
106 dataType : "json",
117 dataType : "json",
107 success : options.success || function() {},
118 success : options.success || function() {},
108 error : this.create_basic_error_handler(options.error)
119 error : this.create_basic_error_handler(options.error)
109 };
120 };
110 $.ajax(this.api_url(path), settings);
121 $.ajax(this.api_url(path), settings);
111 };
122 };
112
123
113 Contents.prototype.delete_file = function(name, path, options) {
124 Contents.prototype.delete_file = function(name, path, options) {
114 var error_callback = options.error || function() {};
125 var error_callback = options.error || function() {};
115 var that = this;
126 var that = this;
116 var settings = {
127 var settings = {
117 processData : false,
128 processData : false,
118 type : "DELETE",
129 type : "DELETE",
119 dataType : "json",
130 dataType : "json",
120 success : options.success || function() {},
131 success : options.success || function() {},
121 error : function(xhr, status, error) {
132 error : function(xhr, status, error) {
122 // TODO: update IPEP27 to specify errors more precisely, so
133 // TODO: update IPEP27 to specify errors more precisely, so
123 // that error types can be detected here with certainty.
134 // that error types can be detected here with certainty.
124 if (xhr.status === 400) {
135 if (xhr.status === 400) {
125 error_callback(new Contents.DirectoryNotEmptyError());
136 error_callback(new Contents.DirectoryNotEmptyError());
126 }
137 }
127 error_callback(utils.wrap_ajax_error(xhr, status, error));
138 error_callback(utils.wrap_ajax_error(xhr, status, error));
128 }
139 }
129 };
140 };
130 var url = this.api_url(path, name);
141 var url = this.api_url(path, name);
131 $.ajax(url, settings);
142 $.ajax(url, settings);
132 };
143 };
133
144
134 Contents.prototype.rename_file = function(path, name, new_path, new_name, options) {
145 Contents.prototype.rename_file = function(path, name, new_path, new_name, options) {
135 var data = {name: new_name, path: new_path};
146 var data = {name: new_name, path: new_path};
136 var settings = {
147 var settings = {
137 processData : false,
148 processData : false,
138 type : "PATCH",
149 type : "PATCH",
139 data : JSON.stringify(data),
150 data : JSON.stringify(data),
140 dataType: "json",
151 dataType: "json",
141 contentType: 'application/json',
152 contentType: 'application/json',
142 success : options.success || function() {},
153 success : options.success || function() {},
143 error : this.create_basic_error_handler(options.error)
154 error : this.create_basic_error_handler(options.error)
144 };
155 };
145 var url = this.api_url(path, name);
156 var url = this.api_url(path, name);
146 $.ajax(url, settings);
157 $.ajax(url, settings);
147 };
158 };
148
159
149 Contents.prototype.save_file = function(path, name, model, options) {
160 Contents.prototype.save_file = function(path, name, model, options) {
150 // We do the call with settings so we can set cache to false.
161 // We do the call with settings so we can set cache to false.
151 var settings = {
162 var settings = {
152 processData : false,
163 processData : false,
153 type : "PUT",
164 type : "PUT",
154 data : JSON.stringify(model),
165 data : JSON.stringify(model),
155 contentType: 'application/json',
166 contentType: 'application/json',
156 success : options.success || function() {},
167 success : options.success || function() {},
157 error : this.create_basic_error_handler(options.error)
168 error : this.create_basic_error_handler(options.error)
158 };
169 };
159 if (options.extra_settings) {
170 if (options.extra_settings) {
160 $.extend(settings, options.extra_settings);
171 $.extend(settings, options.extra_settings);
161 }
172 }
162 var url = this.api_url(path, name);
173 var url = this.api_url(path, name);
163 $.ajax(url, settings);
174 $.ajax(url, settings);
164 };
175 };
165
176
166 Contents.prototype.copy_file = function(to_path, to_name, from, options) {
177 Contents.prototype.copy_file = function(to_path, to_name, from, options) {
167 var url, method;
178 var url, method;
168 if (to_name) {
179 if (to_name) {
169 url = this.api_url(to_path, to_name);
180 url = this.api_url(to_path, to_name);
170 method = "PUT";
181 method = "PUT";
171 } else {
182 } else {
172 url = this.api_url(to_path);
183 url = this.api_url(to_path);
173 method = "POST";
184 method = "POST";
174 }
185 }
175
186
176 var settings = {
187 var settings = {
177 processData : false,
188 processData : false,
178 type: method,
189 type: method,
179 data: JSON.stringify({copy_from: from}),
190 data: JSON.stringify({copy_from: from}),
180 dataType : "json",
191 dataType : "json",
181 success: options.success || function() {},
192 success: options.success || function() {},
182 error: this.create_basic_error_handler(options.error)
193 error: this.create_basic_error_handler(options.error)
183 };
194 };
184 if (options.extra_settings) {
195 if (options.extra_settings) {
185 $.extend(settings, options.extra_settings);
196 $.extend(settings, options.extra_settings);
186 }
197 }
187 $.ajax(url, settings);
198 $.ajax(url, settings);
188 };
199 };
189
200
190 /**
201 /**
191 * Checkpointing Functions
202 * Checkpointing Functions
192 */
203 */
193
204
194 Contents.prototype.create_checkpoint = function(path, name, options) {
205 Contents.prototype.create_checkpoint = function(path, name, options) {
195 var url = this.api_url(path, name, 'checkpoints');
206 var url = this.api_url(path, name, 'checkpoints');
196 var settings = {
207 var settings = {
197 type : "POST",
208 type : "POST",
198 success: options.success || function() {},
209 success: options.success || function() {},
199 error : this.create_basic_error_handler(options.error)
210 error : this.create_basic_error_handler(options.error)
200 };
211 };
201 $.ajax(url, settings);
212 $.ajax(url, settings);
202 };
213 };
203
214
204 Contents.prototype.list_checkpoints = function(path, name, options) {
215 Contents.prototype.list_checkpoints = function(path, name, options) {
205 var url = this.api_url(path, name, 'checkpoints');
216 var url = this.api_url(path, name, 'checkpoints');
206 var settings = {
217 var settings = {
207 type : "GET",
218 type : "GET",
208 success: options.success,
219 success: options.success,
209 error : this.create_basic_error_handler(options.error)
220 error : this.create_basic_error_handler(options.error)
210 };
221 };
211 $.ajax(url, settings);
222 $.ajax(url, settings);
212 };
223 };
213
224
214 Contents.prototype.restore_checkpoint = function(path, name, checkpoint_id, options) {
225 Contents.prototype.restore_checkpoint = function(path, name, checkpoint_id, options) {
215 var url = this.api_url(path, name, 'checkpoints', checkpoint_id);
226 var url = this.api_url(path, name, 'checkpoints', checkpoint_id);
216 var settings = {
227 var settings = {
217 type : "POST",
228 type : "POST",
218 success: options.success || function() {},
229 success: options.success || function() {},
219 error : this.create_basic_error_handler(options.error)
230 error : this.create_basic_error_handler(options.error)
220 };
231 };
221 $.ajax(url, settings);
232 $.ajax(url, settings);
222 };
233 };
223
234
224 Contents.prototype.delete_checkpoint = function(path, name, checkpoint_id, options) {
235 Contents.prototype.delete_checkpoint = function(path, name, checkpoint_id, options) {
225 var url = this.api_url(path, name, 'checkpoints', checkpoint_id);
236 var url = this.api_url(path, name, 'checkpoints', checkpoint_id);
226 var settings = {
237 var settings = {
227 type : "DELETE",
238 type : "DELETE",
228 success: options.success || function() {},
239 success: options.success || function() {},
229 error : this.create_basic_error_handler(options.error)
240 error : this.create_basic_error_handler(options.error)
230 };
241 };
231 $.ajax(url, settings);
242 $.ajax(url, settings);
232 };
243 };
233
244
234 /**
245 /**
235 * File management functions
246 * File management functions
236 */
247 */
237
248
238 /**
249 /**
239 * List notebooks and directories at a given path
250 * List notebooks and directories at a given path
240 *
251 *
241 * On success, load_callback is called with an array of dictionaries
252 * On success, load_callback is called with an array of dictionaries
242 * representing individual files or directories. Each dictionary has
253 * representing individual files or directories. Each dictionary has
243 * the keys:
254 * the keys:
244 * type: "notebook" or "directory"
255 * type: "notebook" or "directory"
245 * name: the name of the file or directory
256 * name: the name of the file or directory
246 * created: created date
257 * created: created date
247 * last_modified: last modified dat
258 * last_modified: last modified dat
248 * path: the path
259 * path: the path
249 * @method list_notebooks
260 * @method list_notebooks
250 * @param {String} path The path to list notebooks in
261 * @param {String} path The path to list notebooks in
251 * @param {Function} load_callback called with list of notebooks on success
262 * @param {Function} load_callback called with list of notebooks on success
252 * @param {Function} error called with ajax results on error
263 * @param {Function} error called with ajax results on error
253 */
264 */
254 Contents.prototype.list_contents = function(path, options) {
265 Contents.prototype.list_contents = function(path, options) {
255 var settings = {
266 var settings = {
256 processData : false,
267 processData : false,
257 cache : false,
268 cache : false,
258 type : "GET",
269 type : "GET",
259 dataType : "json",
270 dataType : "json",
260 success : options.success,
271 success : options.success,
261 error : this.create_basic_error_handler(options.error)
272 error : this.create_basic_error_handler(options.error)
262 };
273 };
263
274
264 $.ajax(this.api_url(path), settings);
275 $.ajax(this.api_url(path), settings);
265 };
276 };
266
277
267
278
268 IPython.Contents = Contents;
279 IPython.Contents = Contents;
269
280
270 return {'Contents': Contents};
281 return {'Contents': Contents};
271 });
282 });
@@ -1,143 +1,143
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 require([
4 require([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/events',
7 'base/js/events',
8 'base/js/page',
8 'base/js/page',
9 'base/js/utils',
9 'base/js/utils',
10 'contents',
10 'contents',
11 'tree/js/notebooklist',
11 'tree/js/notebooklist',
12 'tree/js/clusterlist',
12 'tree/js/clusterlist',
13 'tree/js/sessionlist',
13 'tree/js/sessionlist',
14 'tree/js/kernellist',
14 'tree/js/kernellist',
15 'tree/js/terminallist',
15 'tree/js/terminallist',
16 'auth/js/loginwidget',
16 'auth/js/loginwidget',
17 // only loaded, not used:
17 // only loaded, not used:
18 'jqueryui',
18 'jqueryui',
19 'bootstrap',
19 'bootstrap',
20 'custom/custom',
20 'custom/custom',
21 ], function(
21 ], function(
22 IPython,
22 IPython,
23 $,
23 $,
24 events,
24 events,
25 page,
25 page,
26 utils,
26 utils,
27 contents,
27 contents,
28 notebooklist,
28 notebooklist,
29 clusterlist,
29 clusterlist,
30 sesssionlist,
30 sesssionlist,
31 kernellist,
31 kernellist,
32 terminallist,
32 terminallist,
33 loginwidget){
33 loginwidget){
34
34
35 page = new page.Page();
35 page = new page.Page();
36
36
37 var common_options = {
37 var common_options = {
38 base_url: utils.get_body_data("baseUrl"),
38 base_url: utils.get_body_data("baseUrl"),
39 notebook_path: utils.get_body_data("notebookPath"),
39 notebook_path: utils.get_body_data("notebookPath"),
40 };
40 };
41 session_list = new sesssionlist.SesssionList($.extend({
41 session_list = new sesssionlist.SesssionList($.extend({
42 events: events},
42 events: events},
43 common_options));
43 common_options));
44 contents = new contents.Contents($.extend({
44 contents = new contents.Contents($.extend({
45 events: events},
45 events: events},
46 common_options));
46 common_options));
47 notebook_list = new notebooklist.NotebookList('#notebook_list', $.extend({
47 notebook_list = new notebooklist.NotebookList('#notebook_list', $.extend({
48 contents: contents,
48 contents: contents,
49 session_list: session_list},
49 session_list: session_list},
50 common_options));
50 common_options));
51 cluster_list = new clusterlist.ClusterList('#cluster_list', common_options);
51 cluster_list = new clusterlist.ClusterList('#cluster_list', common_options);
52 kernel_list = new kernellist.KernelList('#running_list', $.extend({
52 kernel_list = new kernellist.KernelList('#running_list', $.extend({
53 session_list: session_list},
53 session_list: session_list},
54 common_options));
54 common_options));
55
55
56 if (utils.get_body_data("terminalsAvailable") === "True") {
56 if (utils.get_body_data("terminalsAvailable") === "True") {
57 terminal_list = new terminallist.TerminalList('#terminal_list', common_options);
57 terminal_list = new terminallist.TerminalList('#terminal_list', common_options);
58 }
58 }
59
59
60 login_widget = new loginwidget.LoginWidget('#login_widget', common_options);
60 login_widget = new loginwidget.LoginWidget('#login_widget', common_options);
61
61
62 $('#new_notebook').click(function (e) {
62 $('#new_notebook').click(function (e) {
63 contents.new_notebook(common_options.notebook_path,
63 contents.new(common_options.notebook_path, null, {
64 {
64 ext: ".ipynb",
65 success: function (data) {
65 success: function (data) {
66 window.open(
66 window.open(
67 utils.url_join_encode(
67 utils.url_join_encode(
68 common_options.base_url, 'notebooks',
68 common_options.base_url, 'notebooks',
69 data.path, data.name
69 data.path, data.name
70 ), '_blank');
70 ), '_blank');
71 },
71 },
72 error: function(error) {
72 error: function(error) {
73 dialog.modal({
73 dialog.modal({
74 title : 'Creating Notebook Failed',
74 title : 'Creating Notebook Failed',
75 body : "The error was: " + error.message,
75 body : "The error was: " + error.message,
76 buttons : {'OK' : {'class' : 'btn-primary'}}
76 buttons : {'OK' : {'class' : 'btn-primary'}}
77 });
77 });
78 }
78 }
79 });
79 });
80 });
80 });
81
81
82 var interval_id=0;
82 var interval_id=0;
83 // auto refresh every xx secondes, no need to be fast,
83 // auto refresh every xx secondes, no need to be fast,
84 // update is done at least when page get focus
84 // update is done at least when page get focus
85 var time_refresh = 60; // in sec
85 var time_refresh = 60; // in sec
86
86
87 var enable_autorefresh = function(){
87 var enable_autorefresh = function(){
88 //refresh immediately , then start interval
88 //refresh immediately , then start interval
89 session_list.load_sessions();
89 session_list.load_sessions();
90 cluster_list.load_list();
90 cluster_list.load_list();
91 if (!interval_id){
91 if (!interval_id){
92 interval_id = setInterval(function(){
92 interval_id = setInterval(function(){
93 session_list.load_sessions();
93 session_list.load_sessions();
94 cluster_list.load_list();
94 cluster_list.load_list();
95 }, time_refresh*1000);
95 }, time_refresh*1000);
96 }
96 }
97 };
97 };
98
98
99 var disable_autorefresh = function(){
99 var disable_autorefresh = function(){
100 clearInterval(interval_id);
100 clearInterval(interval_id);
101 interval_id = 0;
101 interval_id = 0;
102 };
102 };
103
103
104 // stop autorefresh when page lose focus
104 // stop autorefresh when page lose focus
105 $(window).blur(function() {
105 $(window).blur(function() {
106 disable_autorefresh();
106 disable_autorefresh();
107 });
107 });
108
108
109 //re-enable when page get focus back
109 //re-enable when page get focus back
110 $(window).focus(function() {
110 $(window).focus(function() {
111 enable_autorefresh();
111 enable_autorefresh();
112 });
112 });
113
113
114 // finally start it, it will refresh immediately
114 // finally start it, it will refresh immediately
115 enable_autorefresh();
115 enable_autorefresh();
116
116
117 page.show();
117 page.show();
118
118
119 // For backwards compatability.
119 // For backwards compatability.
120 IPython.page = page;
120 IPython.page = page;
121 IPython.notebook_list = notebook_list;
121 IPython.notebook_list = notebook_list;
122 IPython.cluster_list = cluster_list;
122 IPython.cluster_list = cluster_list;
123 IPython.session_list = session_list;
123 IPython.session_list = session_list;
124 IPython.kernel_list = kernel_list;
124 IPython.kernel_list = kernel_list;
125 IPython.login_widget = login_widget;
125 IPython.login_widget = login_widget;
126
126
127 events.trigger('app_initialized.DashboardApp');
127 events.trigger('app_initialized.DashboardApp');
128
128
129 // bound the upload method to the on change of the file select list
129 // bound the upload method to the on change of the file select list
130 $("#alternate_upload").change(function (event){
130 $("#alternate_upload").change(function (event){
131 notebook_list.handleFilesUpload(event,'form');
131 notebook_list.handleFilesUpload(event,'form');
132 });
132 });
133
133
134 // set hash on tab click
134 // set hash on tab click
135 $("#tabs").find("a").click(function() {
135 $("#tabs").find("a").click(function() {
136 window.location.hash = $(this).attr("href");
136 window.location.hash = $(this).attr("href");
137 });
137 });
138
138
139 // load tab if url hash
139 // load tab if url hash
140 if (window.location.hash) {
140 if (window.location.hash) {
141 $("#tabs").find("a[href=" + window.location.hash + "]").click();
141 $("#tabs").find("a[href=" + window.location.hash + "]").click();
142 }
142 }
143 });
143 });
General Comments 0
You need to be logged in to leave comments. Login now