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