##// END OF EJS Templates
Fix typo in notebook list javascript...
Thomas Kluyver -
Show More
@@ -1,251 +1,251
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 };
21 };
22
22
23 NotebookList.prototype.style = function () {
23 NotebookList.prototype.style = function () {
24 this.element.addClass('ui-widget ui-widget-content');
24 this.element.addClass('ui-widget ui-widget-content');
25 $('div#project_name').addClass('ui-widget ui-widget-header');
25 $('div#project_name').addClass('ui-widget ui-widget-header');
26 };
26 };
27
27
28
28
29 NotebookList.prototype.bind_events = function () {
29 NotebookList.prototype.bind_events = function () {
30 if (IPython.read_only){
30 if (IPython.read_only){
31 return;
31 return;
32 }
32 }
33 var that = this;
33 var that = this;
34 this.element.bind('dragover', function () {
34 this.element.bind('dragover', function () {
35 return false;
35 return false;
36 });
36 });
37 this.element.bind('drop', function (event) {
37 this.element.bind('drop', function (event) {
38 var files = event.originalEvent.dataTransfer.files;
38 var files = event.originalEvent.dataTransfer.files;
39 for (var i = 0, f; f = files[i]; i++) {
39 for (var i = 0, f; f = files[i]; i++) {
40 var reader = new FileReader();
40 var reader = new FileReader();
41 reader.readAsText(f);
41 reader.readAsText(f);
42 var fname = f.name.split('.');
42 var fname = f.name.split('.');
43 var nbname = fname.slice(0,-1).join('.');
43 var nbname = fname.slice(0,-1).join('.');
44 var nbformat = fname.slice(-1)[0];
44 var nbformat = fname.slice(-1)[0];
45 if (nbformat === 'ipynb') {nbformat = 'json';};
45 if (nbformat === 'ipynb') {nbformat = 'json';};
46 if (nbformat === 'py' || nbformat === 'json') {
46 if (nbformat === 'py' || nbformat === 'json') {
47 var item = that.new_notebook_item(0);
47 var item = that.new_notebook_item(0);
48 that.add_name_input(nbname, item);
48 that.add_name_input(nbname, item);
49 item.data('nbformat', nbformat);
49 item.data('nbformat', nbformat);
50 // Store the notebook item in the reader so we can use it later
50 // Store the notebook item in the reader so we can use it later
51 // to know which item it belongs to.
51 // to know which item it belongs to.
52 $(reader).data('item', item);
52 $(reader).data('item', item);
53 reader.onload = function (event) {
53 reader.onload = function (event) {
54 var nbitem = $(event.target).data('item');
54 var nbitem = $(event.target).data('item');
55 that.add_notebook_data(event.target.result, nbitem);
55 that.add_notebook_data(event.target.result, nbitem);
56 that.add_upload_button(nbitem);
56 that.add_upload_button(nbitem);
57 };
57 };
58 };
58 };
59 }
59 }
60 return false;
60 return false;
61 });
61 });
62 };
62 };
63
63
64
64
65 NotebookList.prototype.load_list = function () {
65 NotebookList.prototype.load_list = function () {
66 var settings = {
66 var settings = {
67 processData : false,
67 processData : false,
68 cache : false,
68 cache : false,
69 type : "GET",
69 type : "GET",
70 dataType : "json",
70 dataType : "json",
71 success : $.proxy(this.list_loaded, this)
71 success : $.proxy(this.list_loaded, this)
72 };
72 };
73 var url = $('body').data('baseProjectUrl') + 'notebooks';
73 var url = $('body').data('baseProjectUrl') + 'notebooks';
74 $.ajax(url, settings);
74 $.ajax(url, settings);
75 };
75 };
76
76
77
77
78 NotebookList.prototype.list_loaded = function (data, status, xhr) {
78 NotebookList.prototype.list_loaded = function (data, status, xhr) {
79 var len = data.length;
79 var len = data.length;
80 // Todo: remove old children
80 // Todo: remove old children
81 for (var i=0; i<len; i++) {
81 for (var i=0; i<len; i++) {
82 var notebook_id = data[i].notebook_id;
82 var notebook_id = data[i].notebook_id;
83 var nbname = data[i].name;
83 var nbname = data[i].name;
84 var item = this.new_notebook_item(i);
84 var item = this.new_notebook_item(i);
85 this.add_link(notebook_id, nbname, item);
85 this.add_link(notebook_id, nbname, item);
86 if (!IPython.read_only){
86 if (!IPython.read_only){
87 // hide delete buttons when readonly
87 // hide delete buttons when readonly
88 this.add_delete_button(item);
88 this.add_delete_button(item);
89 }
89 }
90 };
90 };
91 };
91 };
92
92
93
93
94 NotebookList.prototype.new_notebook_item = function (index) {
94 NotebookList.prototype.new_notebook_item = function (index) {
95 var item = $('<div/>');
95 var item = $('<div/>');
96 item.addClass('notebook_item ui-widget ui-widget-content ui-helper-clearfix');
96 item.addClass('notebook_item ui-widget ui-widget-content ui-helper-clearfix');
97 var item_name = $('<span/>').addClass('item_name');
97 var item_name = $('<span/>').addClass('item_name');
98
98
99 item.append(item_name);
99 item.append(item_name);
100 if (index === -1) {
100 if (index === -1) {
101 this.element.append(item);
101 this.element.append(item);
102 } else {
102 } else {
103 this.element.children().eq(index).after(item);
103 this.element.children().eq(index).after(item);
104 }
104 }
105 return item;
105 return item;
106 };
106 };
107
107
108
108
109 NotebookList.prototype.add_link = function (notebook_id, nbname, item) {
109 NotebookList.prototype.add_link = function (notebook_id, nbname, item) {
110 item.data('nbname', nbname);
110 item.data('nbname', nbname);
111 item.data('notebook_id', notebook_id);
111 item.data('notebook_id', notebook_id);
112 var new_item_name = $('<span/>').addClass('item_name');
112 var new_item_name = $('<span/>').addClass('item_name');
113 new_item_name.append(
113 new_item_name.append(
114 $('<a/>').
114 $('<a/>').
115 attr('href', $('body').data('baseProjectURL')+notebook_id).
115 attr('href', $('body').data('baseProjectUrl')+notebook_id).
116 attr('target','_blank').
116 attr('target','_blank').
117 text(nbname)
117 text(nbname)
118 );
118 );
119 var e = item.find('.item_name');
119 var e = item.find('.item_name');
120 if (e.length === 0) {
120 if (e.length === 0) {
121 item.append(new_item_name);
121 item.append(new_item_name);
122 } else {
122 } else {
123 e.replaceWith(new_item_name);
123 e.replaceWith(new_item_name);
124 };
124 };
125 };
125 };
126
126
127
127
128 NotebookList.prototype.add_name_input = function (nbname, item) {
128 NotebookList.prototype.add_name_input = function (nbname, item) {
129 item.data('nbname', nbname);
129 item.data('nbname', nbname);
130 var new_item_name = $('<span/>').addClass('item_name');
130 var new_item_name = $('<span/>').addClass('item_name');
131 new_item_name.append(
131 new_item_name.append(
132 $('<input/>').addClass('ui-widget ui-widget-content').
132 $('<input/>').addClass('ui-widget ui-widget-content').
133 attr('value', nbname).
133 attr('value', nbname).
134 attr('size', '30').
134 attr('size', '30').
135 attr('type', 'text')
135 attr('type', 'text')
136 );
136 );
137 var e = item.find('.item_name');
137 var e = item.find('.item_name');
138 if (e.length === 0) {
138 if (e.length === 0) {
139 item.append(new_item_name);
139 item.append(new_item_name);
140 } else {
140 } else {
141 e.replaceWith(new_item_name);
141 e.replaceWith(new_item_name);
142 };
142 };
143 };
143 };
144
144
145
145
146 NotebookList.prototype.add_notebook_data = function (data, item) {
146 NotebookList.prototype.add_notebook_data = function (data, item) {
147 item.data('nbdata',data);
147 item.data('nbdata',data);
148 };
148 };
149
149
150
150
151 NotebookList.prototype.add_delete_button = function (item) {
151 NotebookList.prototype.add_delete_button = function (item) {
152 var new_buttons = $('<span/>').addClass('item_buttons');
152 var new_buttons = $('<span/>').addClass('item_buttons');
153 var delete_button = $('<button>Delete</button>').button().
153 var delete_button = $('<button>Delete</button>').button().
154 click(function (e) {
154 click(function (e) {
155 // $(this) is the button that was clicked.
155 // $(this) is the button that was clicked.
156 var that = $(this);
156 var that = $(this);
157 // We use the nbname and notebook_id from the parent notebook_item element's
157 // We use the nbname and notebook_id from the parent notebook_item element's
158 // data because the outer scopes values change as we iterate through the loop.
158 // data because the outer scopes values change as we iterate through the loop.
159 var parent_item = that.parents('div.notebook_item');
159 var parent_item = that.parents('div.notebook_item');
160 var nbname = parent_item.data('nbname');
160 var nbname = parent_item.data('nbname');
161 var notebook_id = parent_item.data('notebook_id');
161 var notebook_id = parent_item.data('notebook_id');
162 var dialog = $('<div/>');
162 var dialog = $('<div/>');
163 dialog.html('Are you sure you want to permanently delete the notebook: ' + nbname + '?');
163 dialog.html('Are you sure you want to permanently delete the notebook: ' + nbname + '?');
164 parent_item.append(dialog);
164 parent_item.append(dialog);
165 dialog.dialog({
165 dialog.dialog({
166 resizable: false,
166 resizable: false,
167 modal: true,
167 modal: true,
168 title: "Delete notebook",
168 title: "Delete notebook",
169 buttons : {
169 buttons : {
170 "Delete": function () {
170 "Delete": function () {
171 var settings = {
171 var settings = {
172 processData : false,
172 processData : false,
173 cache : false,
173 cache : false,
174 type : "DELETE",
174 type : "DELETE",
175 dataType : "json",
175 dataType : "json",
176 success : function (data, status, xhr) {
176 success : function (data, status, xhr) {
177 parent_item.remove();
177 parent_item.remove();
178 }
178 }
179 };
179 };
180 var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id;
180 var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id;
181 $.ajax(url, settings);
181 $.ajax(url, settings);
182 $(this).dialog('close');
182 $(this).dialog('close');
183 },
183 },
184 "Cancel": function () {
184 "Cancel": function () {
185 $(this).dialog('close');
185 $(this).dialog('close');
186 }
186 }
187 }
187 }
188 });
188 });
189 });
189 });
190 new_buttons.append(delete_button);
190 new_buttons.append(delete_button);
191 var e = item.find('.item_buttons');
191 var e = item.find('.item_buttons');
192 if (e.length === 0) {
192 if (e.length === 0) {
193 item.append(new_buttons);
193 item.append(new_buttons);
194 } else {
194 } else {
195 e.replaceWith(new_buttons);
195 e.replaceWith(new_buttons);
196 };
196 };
197 };
197 };
198
198
199
199
200 NotebookList.prototype.add_upload_button = function (item) {
200 NotebookList.prototype.add_upload_button = function (item) {
201 var that = this;
201 var that = this;
202 var new_buttons = $('<span/>').addClass('item_buttons');
202 var new_buttons = $('<span/>').addClass('item_buttons');
203 var upload_button = $('<button>Upload</button>').button().
203 var upload_button = $('<button>Upload</button>').button().
204 click(function (e) {
204 click(function (e) {
205 var nbname = item.find('.item_name > input').attr('value');
205 var nbname = item.find('.item_name > input').attr('value');
206 var nbformat = item.data('nbformat');
206 var nbformat = item.data('nbformat');
207 var nbdata = item.data('nbdata');
207 var nbdata = item.data('nbdata');
208 var content_type = 'text/plain';
208 var content_type = 'text/plain';
209 if (nbformat === 'json') {
209 if (nbformat === 'json') {
210 content_type = 'application/json';
210 content_type = 'application/json';
211 } else if (nbformat === 'py') {
211 } else if (nbformat === 'py') {
212 content_type = 'application/x-python';
212 content_type = 'application/x-python';
213 };
213 };
214 var settings = {
214 var settings = {
215 processData : false,
215 processData : false,
216 cache : false,
216 cache : false,
217 type : 'POST',
217 type : 'POST',
218 dataType : 'json',
218 dataType : 'json',
219 data : nbdata,
219 data : nbdata,
220 headers : {'Content-Type': content_type},
220 headers : {'Content-Type': content_type},
221 success : function (data, status, xhr) {
221 success : function (data, status, xhr) {
222 that.add_link(data, nbname, item);
222 that.add_link(data, nbname, item);
223 that.add_delete_button(item);
223 that.add_delete_button(item);
224 }
224 }
225 };
225 };
226
226
227 var qs = $.param({name:nbname, format:nbformat});
227 var qs = $.param({name:nbname, format:nbformat});
228 var url = $('body').data('baseProjectUrl') + 'notebooks?' + qs;
228 var url = $('body').data('baseProjectUrl') + 'notebooks?' + qs;
229 $.ajax(url, settings);
229 $.ajax(url, settings);
230 });
230 });
231 var cancel_button = $('<button>Cancel</button>').button().
231 var cancel_button = $('<button>Cancel</button>').button().
232 click(function (e) {
232 click(function (e) {
233 item.remove();
233 item.remove();
234 });
234 });
235 upload_button.addClass('upload_button');
235 upload_button.addClass('upload_button');
236 new_buttons.append(upload_button).append(cancel_button);
236 new_buttons.append(upload_button).append(cancel_button);
237 var e = item.find('.item_buttons');
237 var e = item.find('.item_buttons');
238 if (e.length === 0) {
238 if (e.length === 0) {
239 item.append(new_buttons);
239 item.append(new_buttons);
240 } else {
240 } else {
241 e.replaceWith(new_buttons);
241 e.replaceWith(new_buttons);
242 };
242 };
243 };
243 };
244
244
245
245
246 IPython.NotebookList = NotebookList;
246 IPython.NotebookList = NotebookList;
247
247
248 return IPython;
248 return IPython;
249
249
250 }(IPython));
250 }(IPython));
251
251
General Comments 0
You need to be logged in to leave comments. Login now