##// END OF EJS Templates
Fixed session js ajax request....
Zachary Sailer -
Show More
@@ -1,445 +1,441 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // CodeCell
10 10 //============================================================================
11 11 /**
12 12 * An extendable module that provide base functionnality to create cell for notebook.
13 13 * @module IPython
14 14 * @namespace IPython
15 15 * @submodule CodeCell
16 16 */
17 17
18 18
19 19 /* local util for codemirror */
20 20 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;}
21 21
22 22 /**
23 23 *
24 24 * function to delete until previous non blanking space character
25 25 * or first multiple of 4 tabstop.
26 26 * @private
27 27 */
28 28 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
29 29 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
30 30 if (!posEq(from, to)) {cm.replaceRange("", from, to); return}
31 31 var cur = cm.getCursor(), line = cm.getLine(cur.line);
32 32 var tabsize = cm.getOption('tabSize');
33 33 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
34 34 var from = {ch:cur.ch-chToPrevTabStop,line:cur.line}
35 35 var select = cm.getRange(from,cur)
36 36 if( select.match(/^\ +$/) != null){
37 37 cm.replaceRange("",from,cur)
38 38 } else {
39 39 cm.deleteH(-1,"char")
40 40 }
41 41 };
42 42
43 43
44 44 var IPython = (function (IPython) {
45 45 "use strict";
46 46
47 47 var utils = IPython.utils;
48 48 var key = IPython.utils.keycodes;
49 49
50 50 /**
51 51 * A Cell conceived to write code.
52 52 *
53 53 * The kernel doesn't have to be set at creation time, in that case
54 54 * it will be null and set_kernel has to be called later.
55 55 * @class CodeCell
56 56 * @extends IPython.Cell
57 57 *
58 58 * @constructor
59 59 * @param {Object|null} kernel
60 60 * @param {object|undefined} [options]
61 61 * @param [options.cm_config] {object} config to pass to CodeMirror
62 62 */
63 63 var CodeCell = function (session, options) {
64 64 this.session = session || null;
65 65 this.code_mirror = null;
66 66 this.input_prompt_number = null;
67 67 this.collapsed = false;
68 68 this.cell_type = "code";
69 this.last_msg_id = null;
70 69
71 70
72 71 var cm_overwrite_options = {
73 72 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
74 73 };
75 74
76 75 options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options});
77 76
78 77 IPython.Cell.apply(this,[options]);
79 78
80 79 var that = this;
81 80 this.element.focusout(
82 81 function() { that.auto_highlight(); }
83 82 );
84 83 };
85 84
86 85 CodeCell.options_default = {
87 86 cm_config : {
88 87 extraKeys: {
89 88 "Tab" : "indentMore",
90 89 "Shift-Tab" : "indentLess",
91 90 "Backspace" : "delSpaceToPrevTabStop",
92 91 "Cmd-/" : "toggleComment",
93 92 "Ctrl-/" : "toggleComment"
94 93 },
95 94 mode: 'ipython',
96 95 theme: 'ipython',
97 96 matchBrackets: true
98 97 }
99 98 };
100 99
101 100
102 101 CodeCell.prototype = new IPython.Cell();
103 102
104 103 /**
105 104 * @method auto_highlight
106 105 */
107 106 CodeCell.prototype.auto_highlight = function () {
108 107 this._auto_highlight(IPython.config.cell_magic_highlight)
109 108 };
110 109
111 110 /** @method create_element */
112 111 CodeCell.prototype.create_element = function () {
113 112 IPython.Cell.prototype.create_element.apply(this, arguments);
114 113
115 114 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell');
116 115 cell.attr('tabindex','2');
117 116
118 117 this.celltoolbar = new IPython.CellToolbar(this);
119 118
120 119 var input = $('<div></div>').addClass('input');
121 120 var vbox = $('<div/>').addClass('vbox box-flex1')
122 121 input.append($('<div/>').addClass('prompt input_prompt'));
123 122 vbox.append(this.celltoolbar.element);
124 123 var input_area = $('<div/>').addClass('input_area');
125 124 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
126 125 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
127 126 vbox.append(input_area);
128 127 input.append(vbox);
129 128 var output = $('<div></div>');
130 129 cell.append(input).append(output);
131 130 this.element = cell;
132 131 this.output_area = new IPython.OutputArea(output, true);
133 132
134 133 // construct a completer only if class exist
135 134 // otherwise no print view
136 135 if (IPython.Completer !== undefined)
137 136 {
138 137 this.completer = new IPython.Completer(this);
139 138 }
140 139 };
141 140
142 141 /**
143 142 * This method gets called in CodeMirror's onKeyDown/onKeyPress
144 143 * handlers and is used to provide custom key handling. Its return
145 144 * value is used to determine if CodeMirror should ignore the event:
146 145 * true = ignore, false = don't ignore.
147 146 * @method handle_codemirror_keyevent
148 147 */
149 148 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
150 149
151 150 var that = this;
152 151 // whatever key is pressed, first, cancel the tooltip request before
153 152 // they are sent, and remove tooltip if any, except for tab again
154 153 if (event.type === 'keydown' && event.which != key.TAB ) {
155 154 IPython.tooltip.remove_and_cancel_tooltip();
156 155 };
157 156
158 157 var cur = editor.getCursor();
159 158 if (event.keyCode === key.ENTER){
160 159 this.auto_highlight();
161 160 }
162 161
163 162 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
164 163 // Always ignore shift-enter in CodeMirror as we handle it.
165 164 return true;
166 165 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
167 166 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
168 167 // browser and keyboard layout !
169 168 // Pressing '(' , request tooltip, don't forget to reappend it
170 169 // The second argument says to hide the tooltip if the docstring
171 170 // is actually empty
172 171 IPython.tooltip.pending(that, true);
173 172 } else if (event.which === key.UPARROW && event.type === 'keydown') {
174 173 // If we are not at the top, let CM handle the up arrow and
175 174 // prevent the global keydown handler from handling it.
176 175 if (!that.at_top()) {
177 176 event.stop();
178 177 return false;
179 178 } else {
180 179 return true;
181 180 };
182 181 } else if (event.which === key.ESC) {
183 182 return IPython.tooltip.remove_and_cancel_tooltip(true);
184 183 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
185 184 // If we are not at the bottom, let CM handle the down arrow and
186 185 // prevent the global keydown handler from handling it.
187 186 if (!that.at_bottom()) {
188 187 event.stop();
189 188 return false;
190 189 } else {
191 190 return true;
192 191 };
193 192 } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) {
194 193 if (editor.somethingSelected()){
195 194 var anchor = editor.getCursor("anchor");
196 195 var head = editor.getCursor("head");
197 196 if( anchor.line != head.line){
198 197 return false;
199 198 }
200 199 }
201 200 IPython.tooltip.request(that);
202 201 event.stop();
203 202 return true;
204 203 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
205 204 // Tab completion.
206 205 //Do not trim here because of tooltip
207 206 if (editor.somethingSelected()){return false}
208 207 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
209 208 if (pre_cursor.trim() === "") {
210 209 // Don't autocomplete if the part of the line before the cursor
211 210 // is empty. In this case, let CodeMirror handle indentation.
212 211 return false;
213 212 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) {
214 213 IPython.tooltip.request(that);
215 214 // Prevent the event from bubbling up.
216 215 event.stop();
217 216 // Prevent CodeMirror from handling the tab.
218 217 return true;
219 218 } else {
220 219 event.stop();
221 220 this.completer.startCompletion();
222 221 return true;
223 222 };
224 223 } else {
225 224 // keypress/keyup also trigger on TAB press, and we don't want to
226 225 // use those to disable tab completion.
227 226 return false;
228 227 };
229 228 return false;
230 229 };
231 230
232 231
233 232 // Kernel related calls.
234 233
235 234 CodeCell.prototype.set_session = function (session) {
236 235 this.session = session;
237 236 }
238 237
239 238 /**
240 239 * Execute current code cell to the kernel
241 240 * @method execute
242 241 */
243 242 CodeCell.prototype.execute = function () {
244 this.output_area.clear_output();
243 this.output_area.clear_output(true, true, true);
245 244 this.set_input_prompt('*');
246 245 this.element.addClass("running");
247 if (this.last_msg_id) {
248 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
249 }
250 246 var callbacks = {
251 247 'execute_reply': $.proxy(this._handle_execute_reply, this),
252 248 'output': $.proxy(this.output_area.handle_output, this.output_area),
253 249 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
254 250 'set_next_input': $.proxy(this._handle_set_next_input, this),
255 251 'input_request': $.proxy(this._handle_input_request, this)
256 252 };
257 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
253 var msg_id = this.session.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
258 254 };
259 255
260 256 /**
261 257 * @method _handle_execute_reply
262 258 * @private
263 259 */
264 260 CodeCell.prototype._handle_execute_reply = function (content) {
265 261 this.set_input_prompt(content.execution_count);
266 262 this.element.removeClass("running");
267 263 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
268 264 }
269 265
270 266 /**
271 267 * @method _handle_set_next_input
272 268 * @private
273 269 */
274 270 CodeCell.prototype._handle_set_next_input = function (text) {
275 271 var data = {'cell': this, 'text': text}
276 272 $([IPython.events]).trigger('set_next_input.Notebook', data);
277 273 }
278 274
279 275 /**
280 276 * @method _handle_input_request
281 277 * @private
282 278 */
283 279 CodeCell.prototype._handle_input_request = function (content) {
284 280 this.output_area.append_raw_input(content);
285 281 }
286 282
287 283
288 284 // Basic cell manipulation.
289 285
290 286 CodeCell.prototype.select = function () {
291 287 IPython.Cell.prototype.select.apply(this);
292 288 this.code_mirror.refresh();
293 289 this.code_mirror.focus();
294 290 this.auto_highlight();
295 291 // We used to need an additional refresh() after the focus, but
296 292 // it appears that this has been fixed in CM. This bug would show
297 293 // up on FF when a newly loaded markdown cell was edited.
298 294 };
299 295
300 296
301 297 CodeCell.prototype.select_all = function () {
302 298 var start = {line: 0, ch: 0};
303 299 var nlines = this.code_mirror.lineCount();
304 300 var last_line = this.code_mirror.getLine(nlines-1);
305 301 var end = {line: nlines-1, ch: last_line.length};
306 302 this.code_mirror.setSelection(start, end);
307 303 };
308 304
309 305
310 306 CodeCell.prototype.collapse = function () {
311 307 this.collapsed = true;
312 308 this.output_area.collapse();
313 309 };
314 310
315 311
316 312 CodeCell.prototype.expand = function () {
317 313 this.collapsed = false;
318 314 this.output_area.expand();
319 315 };
320 316
321 317
322 318 CodeCell.prototype.toggle_output = function () {
323 319 this.collapsed = Boolean(1 - this.collapsed);
324 320 this.output_area.toggle_output();
325 321 };
326 322
327 323
328 324 CodeCell.prototype.toggle_output_scroll = function () {
329 325 this.output_area.toggle_scroll();
330 326 };
331 327
332 328
333 329 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
334 330 var ns = prompt_value || "&nbsp;";
335 331 return 'In&nbsp;[' + ns + ']:'
336 332 };
337 333
338 334 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
339 335 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
340 336 for(var i=1; i < lines_number; i++){html.push(['...:'])};
341 337 return html.join('</br>')
342 338 };
343 339
344 340 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
345 341
346 342
347 343 CodeCell.prototype.set_input_prompt = function (number) {
348 344 var nline = 1
349 345 if( this.code_mirror != undefined) {
350 346 nline = this.code_mirror.lineCount();
351 347 }
352 348 this.input_prompt_number = number;
353 349 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
354 350 this.element.find('div.input_prompt').html(prompt_html);
355 351 };
356 352
357 353
358 354 CodeCell.prototype.clear_input = function () {
359 355 this.code_mirror.setValue('');
360 356 };
361 357
362 358
363 359 CodeCell.prototype.get_text = function () {
364 360 return this.code_mirror.getValue();
365 361 };
366 362
367 363
368 364 CodeCell.prototype.set_text = function (code) {
369 365 return this.code_mirror.setValue(code);
370 366 };
371 367
372 368
373 369 CodeCell.prototype.at_top = function () {
374 370 var cursor = this.code_mirror.getCursor();
375 371 if (cursor.line === 0 && cursor.ch === 0) {
376 372 return true;
377 373 } else {
378 374 return false;
379 375 }
380 376 };
381 377
382 378
383 379 CodeCell.prototype.at_bottom = function () {
384 380 var cursor = this.code_mirror.getCursor();
385 381 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
386 382 return true;
387 383 } else {
388 384 return false;
389 385 }
390 386 };
391 387
392 388
393 CodeCell.prototype.clear_output = function (wait) {
394 this.output_area.clear_output(wait);
389 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
390 this.output_area.clear_output(stdout, stderr, other);
395 391 };
396 392
397 393
398 394 // JSON serialization
399 395
400 396 CodeCell.prototype.fromJSON = function (data) {
401 397 IPython.Cell.prototype.fromJSON.apply(this, arguments);
402 398 if (data.cell_type === 'code') {
403 399 if (data.input !== undefined) {
404 400 this.set_text(data.input);
405 401 // make this value the starting point, so that we can only undo
406 402 // to this state, instead of a blank cell
407 403 this.code_mirror.clearHistory();
408 404 this.auto_highlight();
409 405 }
410 406 if (data.prompt_number !== undefined) {
411 407 this.set_input_prompt(data.prompt_number);
412 408 } else {
413 409 this.set_input_prompt();
414 410 };
415 411 this.output_area.fromJSON(data.outputs);
416 412 if (data.collapsed !== undefined) {
417 413 if (data.collapsed) {
418 414 this.collapse();
419 415 } else {
420 416 this.expand();
421 417 };
422 418 };
423 419 };
424 420 };
425 421
426 422
427 423 CodeCell.prototype.toJSON = function () {
428 424 var data = IPython.Cell.prototype.toJSON.apply(this);
429 425 data.input = this.get_text();
430 426 data.cell_type = 'code';
431 427 if (this.input_prompt_number) {
432 428 data.prompt_number = this.input_prompt_number;
433 429 };
434 430 var outputs = this.output_area.toJSON();
435 431 data.outputs = outputs;
436 432 data.language = 'python';
437 433 data.collapsed = this.collapsed;
438 434 return data;
439 435 };
440 436
441 437
442 438 IPython.CodeCell = CodeCell;
443 439
444 440 return IPython;
445 441 }(IPython)); No newline at end of file
@@ -1,101 +1,101 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // Notebook
10 10 //============================================================================
11 11
12 12 var IPython = (function (IPython) {
13 13
14 14 var Session = function(notebook_name, notebook_path, Notebook){
15 15 this.kernel = null;
16 this.kernel_id = null;
17 this.session_id = null;
18 this.notebook_name = notebook_name;
19 this.notebook_path = notebook_path;
16 this.id = null;
17 this.name = notebook_name;
18 this.path = notebook_path;
20 19 this.notebook = Notebook;
21 20 this._baseProjectUrl = Notebook.baseProjectUrl()
22 21 };
23 22
24 23 Session.prototype.start = function() {
25 24 var that = this
26 var notebook = {'notebook':{'name': this.notebook_name, 'path': this.notebook_path}}
25 var notebook = {'notebook':{'name': this.name, 'path': this.path}}
27 26 var settings = {
28 27 processData : false,
29 28 cache : false,
30 29 type : "POST",
31 30 data: JSON.stringify(notebook),
32 31 dataType : "json",
32 success : $.proxy(this.start_kernel, that),
33 33 };
34 34 var url = this._baseProjectUrl + 'api/sessions';
35 35 $.ajax(url, settings);
36 36 };
37 37
38 38 Session.prototype.notebook_rename = function (name, path) {
39 this.notebook_name = name;
40 this.notebook_path = path;
39 this.name = name;
40 this.path = path;
41 41 var notebook = {'notebook':{'name':name, 'path': path}};
42 42 var settings = {
43 43 processData : false,
44 44 cache : false,
45 45 type : "PATCH",
46 46 data: JSON.stringify(notebook),
47 47 dataType : "json",
48 48 };
49 49 var url = this._baseProjectUrl + 'api/sessions/' + this.session_id;
50 50 $.ajax(url, settings);
51 51 }
52 52
53 53 Session.prototype.delete_session = function() {
54 54 var settings = {
55 55 processData : false,
56 56 cache : false,
57 57 type : "DELETE",
58 58 dataType : "json",
59 59 };
60 60 var url = this._baseProjectUrl + 'api/sessions/' + this.session_id;
61 61 $.ajax(url, settings);
62 62 };
63 63
64 64 // Kernel related things
65 65 /**
66 66 * Start a new kernel and set it on each code cell.
67 67 *
68 68 * @method start_kernel
69 69 */
70 70 Session.prototype.start_kernel = function (json) {
71 this.session_id = json.id;
71 this.id = json.id;
72 72 this.kernel_content = json.kernel;
73 73 var base_url = $('body').data('baseKernelUrl') + "api/kernels";
74 74 this.kernel = new IPython.Kernel(base_url, this.session_id);
75 75 this.kernel._kernel_started(this.kernel_content);
76 76 };
77 77
78 78 /**
79 79 * Prompt the user to restart the IPython kernel.
80 80 *
81 81 * @method restart_kernel
82 82 */
83 83 Session.prototype.restart_kernel = function () {
84 84 this.kernel.restart();
85 85 };
86 86
87 87 Session.prototype.interrupt_kernel = function() {
88 88 this.kernel.interrupt();
89 89 };
90 90
91 91
92 92 Session.prototype.kill_kernel = function() {
93 93 this.kernel.kill();
94 94 };
95 95
96 96 IPython.Session = Session;
97 97
98 98
99 99 return IPython;
100 100
101 101 }(IPython));
@@ -1,355 +1,355 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // NotebookList
10 10 //============================================================================
11 11
12 12 var IPython = (function (IPython) {
13 13
14 14 var NotebookList = function (selector) {
15 15 this.selector = selector;
16 16 if (this.selector !== undefined) {
17 17 this.element = $(selector);
18 18 this.style();
19 19 this.bind_events();
20 20 }
21 21 this.notebooks_list = new Array();
22 22 this.sessions = new Object();
23 23 };
24 24
25 25 NotebookList.prototype.baseProjectUrl = function () {
26 26 return $('body').data('baseProjectUrl');
27 27 };
28 28
29 29 NotebookList.prototype.notebookPath = function() {
30 30 var path = $('body').data('notebookPath');
31 31 path = decodeURIComponent(path);
32 32 return path;
33 33 };
34 34
35 35 NotebookList.prototype.url_name = function(name){
36 36 return encodeURIComponent(name);
37 37 };
38 38
39 39 NotebookList.prototype.style = function () {
40 40 $('#notebook_toolbar').addClass('list_toolbar');
41 41 $('#drag_info').addClass('toolbar_info');
42 42 $('#notebook_buttons').addClass('toolbar_buttons');
43 43 $('#notebook_list_header').addClass('list_header');
44 44 this.element.addClass("list_container");
45 45 };
46 46
47 47
48 48 NotebookList.prototype.bind_events = function () {
49 49 var that = this;
50 50 $('#refresh_notebook_list').click(function () {
51 51 that.load_list();
52 52 });
53 53 this.element.bind('dragover', function () {
54 54 return false;
55 55 });
56 56 this.element.bind('drop', function(event){
57 57 that.handelFilesUpload(event,'drop');
58 58 return false;
59 59 });
60 60 };
61 61
62 62 NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) {
63 63 var that = this;
64 64 var files;
65 65 if(dropOrForm =='drop'){
66 66 files = event.originalEvent.dataTransfer.files;
67 67 } else
68 68 {
69 69 files = event.originalEvent.target.files
70 70 }
71 71 for (var i = 0, f; f = files[i]; i++) {
72 72 var reader = new FileReader();
73 73 reader.readAsText(f);
74 74 var fname = f.name.split('.');
75 75 var nbname = fname.slice(0,-1).join('.');
76 76 var nbformat = fname.slice(-1)[0];
77 77 if (nbformat === 'ipynb') {nbformat = 'json';};
78 78 if (nbformat === 'py' || nbformat === 'json') {
79 79 var item = that.new_notebook_item(0);
80 80 that.add_name_input(nbname, item);
81 81 item.data('nbformat', nbformat);
82 82 // Store the notebook item in the reader so we can use it later
83 83 // to know which item it belongs to.
84 84 $(reader).data('item', item);
85 85 reader.onload = function (event) {
86 86 var nbitem = $(event.target).data('item');
87 87 that.add_notebook_data(event.target.result, nbitem);
88 88 that.add_upload_button(nbitem);
89 89 };
90 90 };
91 91 }
92 92 return false;
93 93 };
94 94
95 95 NotebookList.prototype.clear_list = function () {
96 96 this.element.children('.list_item').remove();
97 97 };
98 98
99 99 NotebookList.prototype.load_sessions = function(){
100 100 var that = this;
101 101 var settings = {
102 102 processData : false,
103 103 cache : false,
104 104 type : "GET",
105 105 dataType : "json",
106 106 success : $.proxy(that.sessions_loaded, this)
107 107 };
108 108 var url = this.baseProjectUrl() + 'api/sessions';
109 109 $.ajax(url,settings);
110 110 };
111 111
112 112
113 113 NotebookList.prototype.sessions_loaded = function(data){
114 114 this.sessions = new Object();
115 console.log(data)
115 116 var len = data.length;
116 117 if (len != 0) {
117 118 for (var i=0; i<len; i++) {
118 if (data[i]['path']==null) {
119 nb_path = data[i]['name'];
119 if (data[i]['notebook']['path']==null) {
120 nb_path = data[i]['notebook']['name'];
120 121 }
121 122 else {
122 nb_path = data[i]['path'] + data[i]['name'];
123 nb_path = data[i]['notebook']['path'] + data[i]['notebook']['name'];
123 124 }
124 125 this.sessions[nb_path]= data[i]['id'];
125 126 }
126 127 };
127 128 this.load_list();
128 129 };
129 130
130 131 NotebookList.prototype.load_list = function () {
131 132 var that = this;
132 133 var settings = {
133 134 processData : false,
134 135 cache : false,
135 136 type : "GET",
136 137 dataType : "json",
137 138 success : $.proxy(this.list_loaded, this),
138 139 error : $.proxy( function(){
139 140 that.list_loaded([], null, null, {msg:"Error connecting to server."});
140 141 },this)
141 142 };
142 143
143 144 var url = this.baseProjectUrl() + 'api/notebooks' + this.notebookPath();
144 145 $.ajax(url, settings);
145 146 };
146 147
147 148
148 149 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
149 150 var message = 'Notebook list empty.';
150 151 if (param !== undefined && param.msg) {
151 152 var message = param.msg;
152 153 }
153 154 var len = data.length;
154 155 this.clear_list();
155 156 if(len == 0)
156 157 {
157 158 $(this.new_notebook_item(0))
158 159 .append(
159 160 $('<div style="margin:auto;text-align:center;color:grey"/>')
160 161 .text(message)
161 162 )
162 163 }
163 164 for (var i=0; i<len; i++) {
164 165 var name = data[i].name;
165 166 var path = this.notebookPath();
166 167 var nbname = name.split(".")[0];
167 168 var item = this.new_notebook_item(i);
168 169 this.add_link(path, nbname, item);
169 170 name = this.notebookPath() + name;
170 171 if(this.sessions[name] == undefined){
171 172 this.add_delete_button(item);
172 173 } else {
173 174 this.add_shutdown_button(item,this.sessions[name]);
174 175 }
175 176 };
176 177 };
177 178
178 179
179 180 NotebookList.prototype.new_notebook_item = function (index) {
180 181 var item = $('<div/>').addClass("list_item").addClass("row-fluid");
181 182 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
182 183 // item.css('border-top-style','none');
183 184 item.append($("<div/>").addClass("span12").append(
184 185 $("<a/>").addClass("item_link").append(
185 186 $("<span/>").addClass("item_name")
186 187 )
187 188 ).append(
188 189 $('<div/>').addClass("item_buttons btn-group pull-right")
189 190 ));
190 191
191 192 if (index === -1) {
192 193 this.element.append(item);
193 194 } else {
194 195 this.element.children().eq(index).after(item);
195 196 }
196 197 return item;
197 198 };
198 199
199 200
200 201 NotebookList.prototype.add_link = function (path, nbname, item) {
201 202 item.data('nbname', nbname);
202 203 item.data('path', path);
203 204 item.find(".item_name").text(nbname);
204 205 item.find("a.item_link")
205 206 .attr('href', this.baseProjectUrl() + "notebooks" + this.notebookPath() + nbname + ".ipynb")
206 207 .attr('target','_blank');
207 208 };
208 209
209 210
210 211 NotebookList.prototype.add_name_input = function (nbname, item) {
211 212 item.data('nbname', nbname);
212 213 item.find(".item_name").empty().append(
213 214 $('<input/>')
214 215 .addClass("nbname_input")
215 216 .attr('value', nbname)
216 217 .attr('size', '30')
217 218 .attr('type', 'text')
218 219 );
219 220 };
220 221
221 222
222 223 NotebookList.prototype.add_notebook_data = function (data, item) {
223 224 item.data('nbdata',data);
224 225 };
225 226
226 227
227 228 NotebookList.prototype.add_shutdown_button = function (item, session) {
228 229 var that = this;
229 230 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini").
230 231 click(function (e) {
231 232 var settings = {
232 233 processData : false,
233 234 cache : false,
234 235 type : "DELETE",
235 236 dataType : "json",
236 237 success : function () {
237 238 that.load_sessions();
238 239 }
239 240 };
240 241 var url = that.baseProjectUrl() + 'api/sessions/' + session;
241 242 $.ajax(url, settings);
242 243 return false;
243 244 });
244 245 // var new_buttons = item.find('a'); // shutdown_button;
245 246 item.find(".item_buttons").html("").append(shutdown_button);
246 247 };
247 248
248 249 NotebookList.prototype.add_delete_button = function (item) {
249 250 var new_buttons = $('<span/>').addClass("btn-group pull-right");
250 251 var notebooklist = this;
251 252 var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
252 253 click(function (e) {
253 254 // $(this) is the button that was clicked.
254 255 var that = $(this);
255 256 // We use the nbname and notebook_id from the parent notebook_item element's
256 257 // data because the outer scopes values change as we iterate through the loop.
257 258 var parent_item = that.parents('div.list_item');
258 259 var nbname = parent_item.data('nbname');
259 260 var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
260 261 IPython.dialog.modal({
261 262 title : "Delete notebook",
262 263 body : message,
263 264 buttons : {
264 265 Delete : {
265 266 class: "btn-danger",
266 267 click: function() {
267 268 var settings = {
268 269 processData : false,
269 270 cache : false,
270 271 type : "DELETE",
271 272 dataType : "json",
272 273 success : function (data, status, xhr) {
273 274 parent_item.remove();
274 275 }
275 276 };
276 277 var url = notebooklist.baseProjectUrl() + 'api/notebooks' + notebooklist.notebookPath() + nbname + '.ipynb';
277 278 $.ajax(url, settings);
278 279 }
279 280 },
280 281 Cancel : {}
281 282 }
282 283 });
283 284 return false;
284 285 });
285 286 item.find(".item_buttons").html("").append(delete_button);
286 287 };
287 288
288 289
289 290 NotebookList.prototype.add_upload_button = function (item) {
290 291 var that = this;
291 292 var upload_button = $('<button/>').text("Upload")
292 293 .addClass('btn btn-primary btn-mini upload_button')
293 294 .click(function (e) {
294 295 var nbname = item.find('.item_name > input').attr('value');
295 296 var nbformat = item.data('nbformat');
296 297 var nbdata = item.data('nbdata');
297 298 var content_type = 'text/plain';
298 299 if (nbformat === 'json') {
299 300 content_type = 'application/json';
300 301 } else if (nbformat === 'py') {
301 302 content_type = 'application/x-python';
302 303 };
303 304 var settings = {
304 305 processData : false,
305 306 cache : false,
306 307 type : 'POST',
307 308 dataType : 'json',
308 309 data : nbdata,
309 310 headers : {'Content-Type': content_type},
310 311 success : function (data, status, xhr) {
311 312 that.add_link(data, nbname, item);
312 313 that.add_delete_button(item);
313 314 }
314 315 };
315 316
316 317 var qs = $.param({name:nbname, format:nbformat});
317 318 var url = that.baseProjectUrl() + 'notebooks?' + qs;
318 319 $.ajax(url, settings);
319 320 return false;
320 321 });
321 322 var cancel_button = $('<button/>').text("Cancel")
322 323 .addClass("btn btn-mini")
323 324 .click(function (e) {
324 325 console.log('cancel click');
325 326 item.remove();
326 327 return false;
327 328 });
328 329 item.find(".item_buttons").empty()
329 330 .append(upload_button)
330 331 .append(cancel_button);
331 332 };
332 333
333 334
334 335 NotebookList.prototype.new_notebook = function(){
335 336 var path = this.notebookPath();
336 337 var settings = {
337 338 processData : false,
338 339 cache : false,
339 340 type : "POST",
340 341 dataType : "json",
341 342 success:$.proxy(function (data, status, xhr){
342 343 notebook_name = data.name;
343 344 window.open(this.baseProjectUrl() +'notebooks' + this.notebookPath()+ notebook_name, '_blank');
344 345 }, this)
345 346 };
346 347 var url = this.baseProjectUrl() + 'api/notebooks' + path;
347 348 $.ajax(url,settings);
348 349 };
349 350
350 351 IPython.NotebookList = NotebookList;
351 352
352 353 return IPython;
353 354
354 355 }(IPython));
355
General Comments 0
You need to be logged in to leave comments. Login now