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