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