notebooklist.js
238 lines
| 8.4 KiB
| application/javascript
|
JavascriptLexer
Brian E. Granger
|
r4488 | |||
//============================================================================ | ||||
// Cell | ||||
//============================================================================ | ||||
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 () { | ||||
Brian E. Granger
|
r4491 | 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[0]; | ||||
var nbformat = fname[1]; | ||||
if (nbformat === 'ipynb') {nbformat = 'xml';}; | ||||
if (nbformat === 'xml' || 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; | ||||
}); | ||||
Brian E. Granger
|
r4488 | }; | ||
NotebookList.prototype.load_list = function () { | ||||
var settings = { | ||||
processData : false, | ||||
cache : false, | ||||
type : "GET", | ||||
dataType : "json", | ||||
Brian E. Granger
|
r4491 | success : $.proxy(this.list_loaded, this) | ||
Brian E. Granger
|
r4488 | }; | ||
$.ajax("/notebooks", settings); | ||||
}; | ||||
NotebookList.prototype.list_loaded = function (data, status, xhr) { | ||||
var len = data.length; | ||||
Brian E. Granger
|
r4491 | // Todo: remove old children | ||
Brian E. Granger
|
r4488 | for (var i=0; i<len; i++) { | ||
Brian E. Granger
|
r4490 | var notebook_id = data[i].notebook_id; | ||
var nbname = data[i].name; | ||||
Brian E. Granger
|
r4491 | var item = this.new_notebook_item(i); | ||
this.add_link(notebook_id, nbname, item); | ||||
this.add_delete_button(item); | ||||
}; | ||||
}; | ||||
Brian E. Granger
|
r4490 | |||
Brian E. Granger
|
r4491 | |||
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) { | ||||
Brian E. Granger
|
r4490 | this.element.append(item); | ||
Brian E. Granger
|
r4491 | } else { | ||
this.element.children().eq(index).after(item); | ||||
Brian E. Granger
|
r4488 | } | ||
Brian E. Granger
|
r4491 | 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','/'+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(); | ||||
} | ||||
}; | ||||
$.ajax("/notebooks/" + notebook_id, 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'); | ||||
Brian E. Granger
|
r4493 | var content_type = 'text/plain'; | ||
if (nbformat === 'xml') { | ||||
content_type = 'application/xml'; | ||||
} else if (nbformat === 'json') { | ||||
content_type = 'application/json'; | ||||
} else if (nbformat === 'py') { | ||||
content_type = 'application/x-python'; | ||||
}; | ||||
Brian E. Granger
|
r4491 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
Brian E. Granger
|
r4493 | type : 'POST', | ||
dataType : 'json', | ||||
Brian E. Granger
|
r4491 | data : nbdata, | ||
Brian E. Granger
|
r4493 | headers : {'Content-Type': content_type}, | ||
Brian E. Granger
|
r4491 | success : function (data, status, xhr) { | ||
that.add_link(data, nbname, item); | ||||
that.add_delete_button(item); | ||||
} | ||||
}; | ||||
var qs = $.param({name:nbname, format:nbformat}); | ||||
Brian E. Granger
|
r4493 | $.ajax('/notebooks?' + qs, settings); | ||
Brian E. Granger
|
r4491 | }); | ||
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); | ||||
}; | ||||
Brian E. Granger
|
r4488 | }; | ||
IPython.NotebookList = NotebookList; | ||||
return IPython; | ||||
}(IPython)); | ||||