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