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