notebooklist.js
300 lines
| 10.8 KiB
| application/javascript
|
JavascriptLexer
Brian E. Granger
|
r4609 | //---------------------------------------------------------------------------- | ||
// 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. | ||||
//---------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r4488 | |||
//============================================================================ | ||||
Brian E. Granger
|
r4609 | // NotebookList | ||
Brian E. Granger
|
r4488 | //============================================================================ | ||
var IPython = (function (IPython) { | ||||
var NotebookList = function (selector) { | ||||
this.selector = selector; | ||||
if (this.selector !== undefined) { | ||||
this.element = $(selector); | ||||
this.style(); | ||||
this.bind_events(); | ||||
} | ||||
}; | ||||
Bussonnier Matthias
|
r9502 | NotebookList.prototype.baseProjectUrl = function () { | ||
return $('body').data('baseProjectUrl') | ||||
}; | ||||
Brian E. Granger
|
r4488 | NotebookList.prototype.style = function () { | ||
Brian Granger
|
r6195 | $('#notebook_toolbar').addClass('list_toolbar'); | ||
$('#drag_info').addClass('toolbar_info'); | ||||
$('#notebook_buttons').addClass('toolbar_buttons'); | ||||
MinRK
|
r10919 | $('#notebook_list_header').addClass('list_header'); | ||
MinRK
|
r10920 | this.element.addClass("list_container"); | ||
Brian E. Granger
|
r4488 | }; | ||
NotebookList.prototype.bind_events = function () { | ||||
Brian E. Granger
|
r4491 | var that = this; | ||
Brian Granger
|
r6195 | $('#refresh_notebook_list').click(function () { | ||
that.load_list(); | ||||
}); | ||||
Brian E. Granger
|
r4491 | this.element.bind('dragover', function () { | ||
return false; | ||||
}); | ||||
Matthias BUSSONNIER
|
r6838 | this.element.bind('drop', function(event){ | ||
Matthias BUSSONNIER
|
r6851 | that.handelFilesUpload(event,'drop'); | ||
Brian E. Granger
|
r4491 | return false; | ||
}); | ||||
Brian E. Granger
|
r4488 | }; | ||
Matthias BUSSONNIER
|
r6838 | NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) { | ||
var that = this; | ||||
var files; | ||||
if(dropOrForm =='drop'){ | ||||
files = event.originalEvent.dataTransfer.files; | ||||
} else | ||||
{ | ||||
files = event.originalEvent.target.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; | ||||
}; | ||||
Brian E. Granger
|
r4488 | |||
Brian Granger
|
r6195 | NotebookList.prototype.clear_list = function () { | ||
this.element.children('.list_item').remove(); | ||||
Matthias BUSSONNIER
|
r7941 | }; | ||
Brian Granger
|
r6195 | |||
Brian E. Granger
|
r4488 | NotebookList.prototype.load_list = function () { | ||
Matthias BUSSONNIER
|
r7881 | var that = this; | ||
Brian E. Granger
|
r4488 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "GET", | ||||
dataType : "json", | ||||
Matthias BUSSONNIER
|
r7881 | success : $.proxy(this.list_loaded, this), | ||
Matthias BUSSONNIER
|
r7941 | error : $.proxy( function(){ | ||
that.list_loaded([], null, null, {msg:"Error connecting to server."}); | ||||
},this) | ||||
Brian E. Granger
|
r4488 | }; | ||
Matthias BUSSONNIER
|
r7881 | |||
Bussonnier Matthias
|
r9502 | var url = this.baseProjectUrl() + 'notebooks'; | ||
Brian E. Granger
|
r5106 | $.ajax(url, settings); | ||
Brian E. Granger
|
r4488 | }; | ||
Matthias BUSSONNIER
|
r7881 | NotebookList.prototype.list_loaded = function (data, status, xhr, param) { | ||
MinRK
|
r7976 | var message = 'Notebook list empty.'; | ||
if (param !== undefined && param.msg) { | ||||
var message = param.msg; | ||||
} | ||||
Brian E. Granger
|
r4488 | var len = data.length; | ||
Matthias BUSSONNIER
|
r6842 | this.clear_list(); | ||
Matthias BUSSONNIER
|
r6857 | |||
if(len == 0) | ||||
{ | ||||
$(this.new_notebook_item(0)) | ||||
.append( | ||||
$('<div style="margin:auto;text-align:center;color:grey"/>') | ||||
Matthias BUSSONNIER
|
r7881 | .text(message) | ||
Matthias BUSSONNIER
|
r6857 | ) | ||
} | ||||
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; | ||||
Matthias BUSSONNIER
|
r6846 | var kernel = data[i].kernel_id; | ||
Brian E. Granger
|
r4491 | var item = this.new_notebook_item(i); | ||
this.add_link(notebook_id, nbname, item); | ||||
MinRK
|
r11644 | // hide delete buttons when readonly | ||
if(kernel == null){ | ||||
this.add_delete_button(item); | ||||
} else { | ||||
this.add_shutdown_button(item,kernel); | ||||
MinRK
|
r5200 | } | ||
Brian E. Granger
|
r4491 | }; | ||
}; | ||||
Brian E. Granger
|
r4490 | |||
Brian E. Granger
|
r4491 | |||
NotebookList.prototype.new_notebook_item = function (index) { | ||||
MinRK
|
r10919 | var item = $('<div/>').addClass("list_item").addClass("row-fluid"); | ||
MinRK
|
r10891 | // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix'); | ||
// item.css('border-top-style','none'); | ||||
MinRK
|
r10919 | item.append($("<div/>").addClass("span12").append( | ||
$("<a/>").addClass("item_link").append( | ||||
MinRK
|
r10891 | $("<span/>").addClass("item_name") | ||
) | ||||
MinRK
|
r10919 | ).append( | ||
$('<div/>').addClass("item_buttons btn-group pull-right") | ||||
)); | ||||
MinRK
|
r10891 | |||
Brian E. Granger
|
r4491 | 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); | ||||
MinRK
|
r10919 | item.find(".item_name").text(nbname); | ||
item.find("a.item_link") | ||||
MinRK
|
r10891 | .attr('href', this.baseProjectUrl()+notebook_id) | ||
MinRK
|
r10919 | .attr('target','_blank'); | ||
Brian E. Granger
|
r4491 | }; | ||
NotebookList.prototype.add_name_input = function (nbname, item) { | ||||
item.data('nbname', nbname); | ||||
MinRK
|
r10919 | item.find(".item_name").empty().append( | ||
MinRK
|
r10896 | $('<input/>') | ||
.addClass("nbname_input") | ||||
.attr('value', nbname) | ||||
.attr('size', '30') | ||||
.attr('type', 'text') | ||||
Brian E. Granger
|
r4491 | ); | ||
}; | ||||
NotebookList.prototype.add_notebook_data = function (data, item) { | ||||
item.data('nbdata',data); | ||||
}; | ||||
MinRK
|
r10891 | NotebookList.prototype.add_shutdown_button = function (item, kernel) { | ||
Matthias BUSSONNIER
|
r6842 | var that = this; | ||
MinRK
|
r10891 | var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini"). | ||
Matthias BUSSONNIER
|
r6842 | click(function (e) { | ||
var settings = { | ||||
processData : false, | ||||
cache : false, | ||||
type : "DELETE", | ||||
dataType : "json", | ||||
success : function (data, status, xhr) { | ||||
that.load_list(); | ||||
} | ||||
}; | ||||
Matthias BUSSONNIER
|
r9699 | var url = that.baseProjectUrl() + 'kernels/'+kernel; | ||
Matthias BUSSONNIER
|
r6842 | $.ajax(url, settings); | ||
MinRK
|
r10891 | return false; | ||
Matthias BUSSONNIER
|
r6842 | }); | ||
MinRK
|
r10891 | // var new_buttons = item.find('a'); // shutdown_button; | ||
item.find(".item_buttons").html("").append(shutdown_button); | ||||
Matthias BUSSONNIER
|
r6842 | }; | ||
Brian E. Granger
|
r4491 | NotebookList.prototype.add_delete_button = function (item) { | ||
MinRK
|
r10891 | var new_buttons = $('<span/>').addClass("btn-group pull-right"); | ||
Matthias BUSSONNIER
|
r9787 | var notebooklist = this; | ||
MinRK
|
r10936 | var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini"). | ||
Brian E. Granger
|
r4491 | 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. | ||||
Brian Granger
|
r6195 | var parent_item = that.parents('div.list_item'); | ||
Brian E. Granger
|
r4491 | var nbname = parent_item.data('nbname'); | ||
var notebook_id = parent_item.data('notebook_id'); | ||||
MinRK
|
r10921 | var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?'; | ||
IPython.dialog.modal({ | ||||
title : "Delete notebook", | ||||
body : message, | ||||
Brian E. Granger
|
r4491 | buttons : { | ||
MinRK
|
r10921 | Delete : { | ||
class: "btn-danger", | ||||
click: function() { | ||||
var settings = { | ||||
processData : false, | ||||
cache : false, | ||||
type : "DELETE", | ||||
dataType : "json", | ||||
success : function (data, status, xhr) { | ||||
parent_item.remove(); | ||||
} | ||||
}; | ||||
var url = notebooklist.baseProjectUrl() + 'notebooks/' + notebook_id; | ||||
$.ajax(url, settings); | ||||
} | ||||
Brian E. Granger
|
r4491 | }, | ||
MinRK
|
r10921 | Cancel : {} | ||
Brian E. Granger
|
r4491 | } | ||
}); | ||||
MinRK
|
r10891 | return false; | ||
Brian E. Granger
|
r4491 | }); | ||
MinRK
|
r10891 | item.find(".item_buttons").html("").append(delete_button); | ||
Brian E. Granger
|
r4491 | }; | ||
NotebookList.prototype.add_upload_button = function (item) { | ||||
var that = this; | ||||
MinRK
|
r10896 | var upload_button = $('<button/>').text("Upload") | ||
.addClass('btn btn-primary btn-mini upload_button') | ||||
.click(function (e) { | ||||
Brian E. Granger
|
r4491 | 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'; | ||
Brian E. Granger
|
r4633 | if (nbformat === 'json') { | ||
Brian E. Granger
|
r4493 | 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}); | ||||
Matthias BUSSONNIER
|
r9699 | var url = that.baseProjectUrl() + 'notebooks?' + qs; | ||
Brian E. Granger
|
r5106 | $.ajax(url, settings); | ||
MinRK
|
r10891 | return false; | ||
Brian E. Granger
|
r4491 | }); | ||
MinRK
|
r10896 | var cancel_button = $('<button/>').text("Cancel") | ||
.addClass("btn btn-mini") | ||||
.click(function (e) { | ||||
console.log('cancel click'); | ||||
Brian E. Granger
|
r4491 | item.remove(); | ||
MinRK
|
r10891 | return false; | ||
Brian E. Granger
|
r4491 | }); | ||
MinRK
|
r10896 | item.find(".item_buttons").empty() | ||
MinRK
|
r10891 | .append(upload_button) | ||
.append(cancel_button); | ||||
Brian E. Granger
|
r4488 | }; | ||
IPython.NotebookList = NotebookList; | ||||
return IPython; | ||||
}(IPython)); | ||||