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