notebooklist.js
412 lines
| 14.6 KiB
| application/javascript
|
JavascriptLexer
Brian E. Granger
|
r4609 | //---------------------------------------------------------------------------- | ||
MinRK
|
r13063 | // Copyright (C) 2011 The IPython Development Team | ||
Brian E. Granger
|
r4609 | // | ||
// 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) { | ||||
MinRK
|
r13063 | "use strict"; | ||
var utils = IPython.utils; | ||||
Brian E. Granger
|
r4488 | |||
Paul Ivanov
|
r15454 | var NotebookList = function (selector, options, element_name) { | ||
Paul Ivanov
|
r15479 | var that = this | ||
Paul Ivanov
|
r15454 | // allow code re-use by just changing element_name in kernellist.js | ||
this.element_name = element_name || 'notebook'; | ||||
Brian E. Granger
|
r4488 | this.selector = selector; | ||
if (this.selector !== undefined) { | ||||
this.element = $(selector); | ||||
this.style(); | ||||
this.bind_events(); | ||||
} | ||||
MinRK
|
r13063 | this.notebooks_list = []; | ||
this.sessions = {}; | ||||
MinRK
|
r15240 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); | ||
this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath"); | ||||
Paul Ivanov
|
r15479 | $([IPython.events]).on('sessions_loaded.Dashboard', | ||
function(e, d) { that.sessions_loaded(d); }); | ||||
Brian E. Granger
|
r4488 | }; | ||
NotebookList.prototype.style = function () { | ||||
Paul Ivanov
|
r15518 | var prefix = '#' + this.element_name | ||
$(prefix + '_toolbar').addClass('list_toolbar'); | ||||
$(prefix + '_list_info').addClass('toolbar_info'); | ||||
$(prefix + '_buttons').addClass('toolbar_buttons'); | ||||
$(prefix + '_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; | ||
Paul Ivanov
|
r15456 | $('#refresh_' + this.element_name + '_list').click(function () { | ||
that.load_sessions(); | ||||
Brian Granger
|
r6195 | }); | ||
Brian E. Granger
|
r4491 | this.element.bind('dragover', function () { | ||
return false; | ||||
}); | ||||
Matthias BUSSONNIER
|
r6838 | this.element.bind('drop', function(event){ | ||
Paul Ivanov
|
r15455 | that.handleFilesUpload(event,'drop'); | ||
Brian E. Granger
|
r4491 | return false; | ||
}); | ||||
Brian E. Granger
|
r4488 | }; | ||
Paul Ivanov
|
r15455 | NotebookList.prototype.handleFilesUpload = function(event, dropOrForm) { | ||
Matthias BUSSONNIER
|
r6838 | var that = this; | ||
var files; | ||||
if(dropOrForm =='drop'){ | ||||
files = event.originalEvent.dataTransfer.files; | ||||
} else | ||||
{ | ||||
MinRK
|
r13063 | files = event.originalEvent.target.files; | ||
Matthias BUSSONNIER
|
r6838 | } | ||
MinRK
|
r13063 | for (var i = 0; i < files.length; i++) { | ||
var f = files[i]; | ||||
Matthias BUSSONNIER
|
r6838 | var reader = new FileReader(); | ||
reader.readAsText(f); | ||||
MinRK
|
r13121 | var name_and_ext = utils.splitext(f.name); | ||
Matthias BUSSONNIER
|
r13322 | var file_ext = name_and_ext[1]; | ||
MinRK
|
r13121 | if (file_ext === '.ipynb') { | ||
Matthias BUSSONNIER
|
r6838 | var item = that.new_notebook_item(0); | ||
MinRK
|
r15094 | that.add_name_input(f.name, item); | ||
Matthias BUSSONNIER
|
r6838 | // 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); | ||||
}; | ||||
Brian E. Granger
|
r13115 | } else { | ||
var dialog = 'Uploaded notebooks must be .ipynb files'; | ||||
IPython.dialog.modal({ | ||||
title : 'Invalid file type', | ||||
body : dialog, | ||||
buttons : {'OK' : {'class' : 'btn-primary'}} | ||||
}); | ||||
MinRK
|
r13063 | } | ||
Matthias BUSSONNIER
|
r6838 | } | ||
Brian E. Granger
|
r14926 | // Replace the file input form wth a clone of itself. This is required to | ||
// reset the form. Otherwise, if you upload a file, delete it and try to | ||||
// upload it again, the changed event won't fire. | ||||
var form = $('input.fileinput'); | ||||
form.replaceWith(form.clone(true)); | ||||
Matthias BUSSONNIER
|
r6838 | return false; | ||
MinRK
|
r13063 | }; | ||
Brian E. Granger
|
r4488 | |||
Brian Granger
|
r6195 | NotebookList.prototype.clear_list = function () { | ||
this.element.children('.list_item').remove(); | ||||
Matthias BUSSONNIER
|
r7941 | }; | ||
Brian Granger
|
r6195 | |||
Zachary Sailer
|
r12988 | NotebookList.prototype.load_sessions = function(){ | ||
Paul Ivanov
|
r15479 | IPython.session_list.load_sessions(); | ||
Zachary Sailer
|
r12988 | }; | ||
NotebookList.prototype.sessions_loaded = function(data){ | ||||
Paul Ivanov
|
r15479 | this.sessions = data; | ||
Zachary Sailer
|
r12988 | this.load_list(); | ||
}; | ||||
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 | |||
MinRK
|
r13693 | var url = utils.url_join_encode( | ||
MinRK
|
r15238 | this.base_url, | ||
MinRK
|
r13063 | 'api', | ||
'notebooks', | ||||
MinRK
|
r15234 | this.notebook_path | ||
MinRK
|
r13063 | ); | ||
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) { | ||||
MinRK
|
r13063 | message = param.msg; | ||
MinRK
|
r7976 | } | ||
Brian E. Granger
|
r15078 | var item = null; | ||
Brian E. Granger
|
r4488 | var len = data.length; | ||
Matthias BUSSONNIER
|
r6842 | this.clear_list(); | ||
MinRK
|
r13063 | if (len === 0) { | ||
MinRK
|
r15094 | item = this.new_notebook_item(0); | ||
Brian E. Granger
|
r15078 | var span12 = item.children().first(); | ||
span12.empty(); | ||||
MinRK
|
r15094 | span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message)); | ||
Matthias BUSSONNIER
|
r6857 | } | ||
MinRK
|
r15234 | var path = this.notebook_path; | ||
Brian E. Granger
|
r15071 | var offset = 0; | ||
if (path !== '') { | ||||
Brian E. Granger
|
r15078 | item = this.new_notebook_item(0); | ||
Brian E. Granger
|
r15071 | this.add_dir(path, '..', item); | ||
offset = 1; | ||||
} | ||||
Brian E. Granger
|
r4488 | for (var i=0; i<len; i++) { | ||
Brian E. Granger
|
r15071 | if (data[i].type === 'directory') { | ||
var name = data[i].name; | ||||
Brian E. Granger
|
r15078 | item = this.new_notebook_item(i+offset); | ||
Brian E. Granger
|
r15071 | this.add_dir(path, name, item); | ||
MinRK
|
r11644 | } else { | ||
Brian E. Granger
|
r15071 | var name = data[i].name; | ||
Brian E. Granger
|
r15078 | item = this.new_notebook_item(i+offset); | ||
MinRK
|
r15094 | this.add_link(path, name, item); | ||
Brian E. Granger
|
r15071 | name = utils.url_path_join(path, name); | ||
if(this.sessions[name] === undefined){ | ||||
this.add_delete_button(item); | ||||
} else { | ||||
this.add_shutdown_button(item,this.sessions[name]); | ||||
} | ||||
MinRK
|
r5200 | } | ||
MinRK
|
r13063 | } | ||
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( | ||
Brian E. Granger
|
r15071 | $('<i/>').addClass('item_icon') | ||
).append( | ||||
MinRK
|
r10919 | $("<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; | ||
}; | ||||
Brian E. Granger
|
r15071 | NotebookList.prototype.add_dir = function (path, name, item) { | ||
item.data('name', name); | ||||
item.data('path', path); | ||||
item.find(".item_name").text(name); | ||||
item.find(".item_icon").addClass('icon-folder-open'); | ||||
item.find("a.item_link") | ||||
.attr('href', | ||||
utils.url_join_encode( | ||||
MinRK
|
r15238 | this.base_url, | ||
Brian E. Granger
|
r15071 | "tree", | ||
path, | ||||
name | ||||
) | ||||
); | ||||
}; | ||||
Zachary Sailer
|
r12988 | NotebookList.prototype.add_link = function (path, nbname, item) { | ||
Brian E. Granger
|
r4491 | item.data('nbname', nbname); | ||
Zachary Sailer
|
r12988 | item.data('path', path); | ||
MinRK
|
r15094 | item.find(".item_name").text(nbname); | ||
Brian E. Granger
|
r15071 | item.find(".item_icon").addClass('icon-book'); | ||
MinRK
|
r10919 | item.find("a.item_link") | ||
MinRK
|
r13063 | .attr('href', | ||
MinRK
|
r13693 | utils.url_join_encode( | ||
MinRK
|
r15238 | this.base_url, | ||
MinRK
|
r13063 | "notebooks", | ||
MinRK
|
r13693 | path, | ||
MinRK
|
r15094 | nbname | ||
MinRK
|
r13063 | ) | ||
).attr('target','_blank'); | ||||
Brian E. Granger
|
r4491 | }; | ||
NotebookList.prototype.add_name_input = function (nbname, item) { | ||||
item.data('nbname', nbname); | ||||
Brian E. Granger
|
r15071 | item.find(".item_icon").addClass('icon-book'); | ||
MinRK
|
r10919 | item.find(".item_name").empty().append( | ||
MinRK
|
r10896 | $('<input/>') | ||
.addClass("nbname_input") | ||||
MinRK
|
r15094 | .attr('value', utils.splitext(nbname)[0]) | ||
MinRK
|
r10896 | .attr('size', '30') | ||
.attr('type', 'text') | ||||
Brian E. Granger
|
r4491 | ); | ||
}; | ||||
NotebookList.prototype.add_notebook_data = function (data, item) { | ||||
MinRK
|
r13072 | item.data('nbdata', data); | ||
Brian E. Granger
|
r4491 | }; | ||
Zachary Sailer
|
r12988 | NotebookList.prototype.add_shutdown_button = function (item, session) { | ||
Matthias BUSSONNIER
|
r6842 | var that = this; | ||
Brian E. Granger
|
r15075 | var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini btn-danger"). | ||
Matthias BUSSONNIER
|
r6842 | click(function (e) { | ||
var settings = { | ||||
processData : false, | ||||
cache : false, | ||||
type : "DELETE", | ||||
dataType : "json", | ||||
Zachary Sailer
|
r12996 | success : function () { | ||
Zachary Sailer
|
r12988 | that.load_sessions(); | ||
Matthias BUSSONNIER
|
r6842 | } | ||
}; | ||||
MinRK
|
r13693 | var url = utils.url_join_encode( | ||
MinRK
|
r15238 | that.base_url, | ||
MinRK
|
r13063 | 'api/sessions', | ||
session | ||||
); | ||||
Matthias BUSSONNIER
|
r6842 | $.ajax(url, settings); | ||
MinRK
|
r10891 | return false; | ||
Matthias BUSSONNIER
|
r6842 | }); | ||
MinRK
|
r10891 | // var new_buttons = item.find('a'); // shutdown_button; | ||
Matthias BUSSONNIER
|
r14634 | item.find(".item_buttons").text("").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'); | ||
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(); | ||||
} | ||||
}; | ||||
MinRK
|
r13693 | var url = utils.url_join_encode( | ||
MinRK
|
r15238 | notebooklist.base_url, | ||
MinRK
|
r13063 | 'api/notebooks', | ||
MinRK
|
r15234 | notebooklist.notebook_path, | ||
MinRK
|
r15094 | nbname | ||
MinRK
|
r13063 | ); | ||
MinRK
|
r10921 | $.ajax(url, settings); | ||
} | ||||
Brian E. Granger
|
r4491 | }, | ||
MinRK
|
r10921 | Cancel : {} | ||
Brian E. Granger
|
r4491 | } | ||
}); | ||||
MinRK
|
r10891 | return false; | ||
Brian E. Granger
|
r4491 | }); | ||
Matthias BUSSONNIER
|
r14634 | item.find(".item_buttons").text("").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) { | ||||
MinRK
|
r13074 | var nbname = item.find('.item_name > input').val(); | ||
MinRK
|
r15094 | if (nbname.slice(nbname.length-6, nbname.length) != ".ipynb") { | ||
nbname = nbname + ".ipynb"; | ||||
} | ||||
MinRK
|
r15234 | var path = that.notebook_path; | ||
Brian E. Granger
|
r4491 | var nbdata = item.data('nbdata'); | ||
MinRK
|
r13072 | var content_type = 'application/json'; | ||
var model = { | ||||
content : JSON.parse(nbdata), | ||||
}; | ||||
Brian E. Granger
|
r4491 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
Matthias BUSSONNIER
|
r13322 | type : 'PUT', | ||
Brian E. Granger
|
r4493 | dataType : 'json', | ||
MinRK
|
r13072 | data : JSON.stringify(model), | ||
Brian E. Granger
|
r4493 | headers : {'Content-Type': content_type}, | ||
Brian E. Granger
|
r4491 | success : function (data, status, xhr) { | ||
Brian E. Granger
|
r14921 | that.add_link(path, nbname, item); | ||
Brian E. Granger
|
r4491 | that.add_delete_button(item); | ||
MinRK
|
r13072 | }, | ||
error : function (data, status, xhr) { | ||||
console.log(data, status); | ||||
Brian E. Granger
|
r4491 | } | ||
}; | ||||
MinRK
|
r13693 | var url = utils.url_join_encode( | ||
MinRK
|
r15238 | that.base_url, | ||
MinRK
|
r13072 | 'api/notebooks', | ||
MinRK
|
r15234 | that.notebook_path, | ||
MinRK
|
r15094 | nbname | ||
MinRK
|
r13063 | ); | ||
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 | }; | ||
Zachary Sailer
|
r13017 | NotebookList.prototype.new_notebook = function(){ | ||
MinRK
|
r15234 | var path = this.notebook_path; | ||
MinRK
|
r15238 | var base_url = this.base_url; | ||
Zachary Sailer
|
r13017 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "POST", | ||||
dataType : "json", | ||||
MinRK
|
r13075 | async : false, | ||
success : function (data, status, xhr) { | ||||
MinRK
|
r13063 | var notebook_name = data.name; | ||
window.open( | ||||
MinRK
|
r13693 | utils.url_join_encode( | ||
MinRK
|
r15238 | base_url, | ||
MinRK
|
r13063 | 'notebooks', | ||
MinRK
|
r13069 | path, | ||
MinRK
|
r13063 | notebook_name), | ||
'_blank' | ||||
); | ||||
MinRK
|
r13075 | } | ||
Zachary Sailer
|
r13017 | }; | ||
MinRK
|
r13693 | var url = utils.url_join_encode( | ||
MinRK
|
r15238 | base_url, | ||
MinRK
|
r13063 | 'api/notebooks', | ||
path | ||||
); | ||||
$.ajax(url, settings); | ||||
Zachary Sailer
|
r13017 | }; | ||
Zachary Sailer
|
r13016 | |||
Brian E. Granger
|
r4488 | IPython.NotebookList = NotebookList; | ||
return IPython; | ||||
}(IPython)); | ||||