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