notebooklist.js
548 lines
| 19.8 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17189 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
define([ | ||||
'base/js/namespace', | ||||
Jonathan Frederic
|
r17200 | 'jquery', | ||
Jonathan Frederic
|
r17189 | 'base/js/utils', | ||
'base/js/dialog', | ||||
Scott Sanderson
|
r19031 | 'base/js/events', | ||
Jonathan Frederic
|
r19694 | 'base/js/keyboard', | ||
], function(IPython, $, utils, dialog, events, keyboard) { | ||||
MinRK
|
r13063 | "use strict"; | ||
jon
|
r17210 | var NotebookList = function (selector, options) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Constructor | ||||
* | ||||
* Parameters: | ||||
* selector: string | ||||
* options: dictionary | ||||
* Dictionary of keyword arguments. | ||||
* session_list: SessionList instance | ||||
* element_name: string | ||||
* base_url: string | ||||
* notebook_path: string | ||||
* contents: Contents instance | ||||
*/ | ||||
Jonathan Frederic
|
r17189 | var that = this; | ||
jon
|
r17210 | this.session_list = options.session_list; | ||
Paul Ivanov
|
r15454 | // allow code re-use by just changing element_name in kernellist.js | ||
jon
|
r17210 | this.element_name = options.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 = {}; | ||||
Jonathan Frederic
|
r17198 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); | ||
this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath"); | ||||
Jeff Hemmelgarn
|
r18643 | this.contents = options.contents; | ||
Jonathan Frederic
|
r17195 | if (this.session_list && this.session_list.events) { | ||
this.session_list.events.on('sessions_loaded.Dashboard', | ||||
function(e, d) { that.sessions_loaded(d); }); | ||||
} | ||||
Brian E. Granger
|
r4488 | }; | ||
NotebookList.prototype.style = function () { | ||||
Jonathan Frederic
|
r17189 | var prefix = '#' + this.element_name; | ||
Paul Ivanov
|
r15518 | $(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; | ||
}); | ||||
Jonathan Frederic
|
r19694 | |||
// Bind events for singleton controls. | ||||
if (!NotebookList._bound_singletons) { | ||||
NotebookList._bound_singletons = true; | ||||
$('#new-file').click(function(e) { | ||||
Jonathan Frederic
|
r19699 | var w = window.open(); | ||
that.contents.new_untitled(that.notebook_path || '', {type: 'file', ext: '.txt'}).then(function(data) { | ||||
var url = utils.url_join_encode( | ||||
that.base_url, 'edit', data.path | ||||
); | ||||
w.location = url; | ||||
}); | ||||
Jonathan Frederic
|
r19696 | that.load_sessions(); | ||
Jonathan Frederic
|
r19694 | }); | ||
$('#new-folder').click(function(e) { | ||||
Matthias Bussonnier
|
r19983 | that.contents.new_untitled(that.notebook_path || '', {type: 'directory'}) | ||
.then(function(){ | ||||
that.load_list(); | ||||
}); | ||||
Jonathan Frederic
|
r19694 | }); | ||
} | ||||
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]; | ||||
Jonathan Frederic
|
r17198 | var name_and_ext = utils.splitext(f.name); | ||
Matthias BUSSONNIER
|
r13322 | var file_ext = name_and_ext[1]; | ||
MinRK
|
r17536 | |||
var reader = new FileReader(); | ||||
MinRK
|
r13121 | if (file_ext === '.ipynb') { | ||
MinRK
|
r17536 | reader.readAsText(f); | ||
Brian E. Granger
|
r13115 | } else { | ||
MinRK
|
r17536 | // read non-notebook files as binary | ||
reader.readAsArrayBuffer(f); | ||||
MinRK
|
r13063 | } | ||
MinRK
|
r17536 | var item = that.new_item(0); | ||
item.addClass('new-file'); | ||||
Jeffrey Bush
|
r17643 | that.add_name_input(f.name, item, file_ext == '.ipynb' ? 'notebook' : 'file'); | ||
MinRK
|
r17536 | // Store the list 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 item = $(event.target).data('item'); | ||||
that.add_file_data(event.target.result, item); | ||||
that.add_upload_button(item); | ||||
}; | ||||
Jeffrey Bush
|
r17650 | reader.onerror = function (event) { | ||
var item = $(event.target).data('item'); | ||||
Min RK
|
r18752 | var name = item.data('name'); | ||
Jeffrey Bush
|
r17650 | item.remove(); | ||
dialog.modal({ | ||||
title : 'Failed to read file', | ||||
body : "Failed to read file '" + name + "'", | ||||
buttons : {'OK' : { 'class' : 'btn-primary' }} | ||||
}); | ||||
}; | ||||
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 | |||
Jonathan Frederic
|
r16213 | NotebookList.prototype.clear_list = function (remove_uploads) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Clears the navigation tree. | ||||
* | ||||
* Parameters | ||||
* remove_uploads: bool=False | ||||
* Should upload prompts also be removed from the tree. | ||||
*/ | ||||
Jonathan Frederic
|
r16213 | if (remove_uploads) { | ||
this.element.children('.list_item').remove(); | ||||
} else { | ||||
this.element.children('.list_item:not(.new-file)').remove(); | ||||
} | ||||
Matthias BUSSONNIER
|
r7941 | }; | ||
Brian Granger
|
r6195 | |||
Zachary Sailer
|
r12988 | NotebookList.prototype.load_sessions = function(){ | ||
Jonathan Frederic
|
r17189 | this.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 () { | ||
Min RK
|
r18752 | var that = this; | ||
Thomas Kluyver
|
r18827 | this.contents.list_contents(that.notebook_path).then( | ||
$.proxy(this.draw_notebook_list, this), | ||||
function(error) { | ||||
Thomas Kluyver
|
r18678 | that.draw_notebook_list({content: []}, "Server error: " + error.message); | ||
Thomas Kluyver
|
r18653 | } | ||
Thomas Kluyver
|
r18827 | ); | ||
Brian E. Granger
|
r4488 | }; | ||
KesterTong
|
r18636 | /** | ||
* Draw the list of notebooks | ||||
* @method draw_notebook_list | ||||
* @param {Array} list An array of dictionaries representing files or | ||||
Thomas Kluyver
|
r18660 | * directories. | ||
KesterTong
|
r18636 | * @param {String} error_msg An error message | ||
*/ | ||||
Bussonnier Matthias
|
r19479 | |||
var type_order = {'directory':0,'notebook':1,'file':2}; | ||||
KesterTong
|
r18636 | NotebookList.prototype.draw_notebook_list = function (list, error_msg) { | ||
Bussonnier Matthias
|
r19449 | list.content.sort(function(a, b) { | ||
Bussonnier Matthias
|
r19479 | if (type_order[a['type']] < type_order[b['type']]) { | ||
Bussonnier Matthias
|
r19449 | return -1; | ||
} | ||||
Bussonnier Matthias
|
r19479 | if (type_order[a['type']] > type_order[b['type']]) { | ||
Bussonnier Matthias
|
r19449 | return 1; | ||
} | ||||
if (a['name'] < b['name']) { | ||||
return -1; | ||||
} | ||||
if (a['name'] > b['name']) { | ||||
return 1; | ||||
} | ||||
return 0; | ||||
}); | ||||
KesterTong
|
r18636 | var message = error_msg || 'Notebook list empty.'; | ||
Brian E. Granger
|
r15078 | var item = null; | ||
Jeff Hemmelgarn
|
r18640 | var model = null; | ||
var len = list.content.length; | ||||
Matthias BUSSONNIER
|
r6842 | this.clear_list(); | ||
Jeffrey Bush
|
r17651 | var n_uploads = this.element.children('.list_item').length; | ||
MinRK
|
r13063 | if (len === 0) { | ||
MinRK
|
r17526 | item = this.new_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; | ||
Jeffrey Bush
|
r17651 | var offset = n_uploads; | ||
Brian E. Granger
|
r15071 | if (path !== '') { | ||
Jeffrey Bush
|
r17651 | item = this.new_item(offset); | ||
MinRK
|
r17526 | model = { | ||
type: 'directory', | ||||
name: '..', | ||||
Min RK
|
r18752 | path: utils.url_path_split(path)[0], | ||
MinRK
|
r17526 | }; | ||
this.add_link(model, item); | ||||
Jeffrey Bush
|
r17651 | offset += 1; | ||
Brian E. Granger
|
r15071 | } | ||
Brian E. Granger
|
r4488 | for (var i=0; i<len; i++) { | ||
Jeff Hemmelgarn
|
r18640 | model = list.content[i]; | ||
item = this.new_item(i+offset); | ||||
this.add_link(model, item); | ||||
MinRK
|
r13063 | } | ||
Scott Sanderson
|
r19031 | // Trigger an event when we've finished drawing the notebook list. | ||
events.trigger('draw_notebook_list.NotebookList'); | ||||
Brian E. Granger
|
r4491 | }; | ||
Brian E. Granger
|
r4490 | |||
Brian E. Granger
|
r4491 | |||
MinRK
|
r17526 | NotebookList.prototype.new_item = function (index) { | ||
Jonathan Frederic
|
r16913 | var item = $('<div/>').addClass("list_item").addClass("row"); | ||
MinRK
|
r10891 | // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix'); | ||
// item.css('border-top-style','none'); | ||||
Jonathan Frederic
|
r16923 | item.append($("<div/>").addClass("col-md-12").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( | ||
Matthias Bussonnier
|
r19687 | $('<div/>').addClass("item_buttons pull-right") | ||
MinRK
|
r10919 | )); | ||
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; | ||
}; | ||||
MinRK
|
r17526 | NotebookList.icons = { | ||
directory: 'folder_icon', | ||||
notebook: 'notebook_icon', | ||||
file: 'file_icon', | ||||
}; | ||||
NotebookList.uri_prefixes = { | ||||
directory: 'tree', | ||||
notebook: 'notebooks', | ||||
Min RK
|
r19307 | file: 'edit', | ||
MinRK
|
r17526 | }; | ||
NotebookList.prototype.add_link = function (model, item) { | ||||
var path = model.path, | ||||
name = model.name; | ||||
Brian E. Granger
|
r15071 | item.data('name', name); | ||
item.data('path', path); | ||||
item.find(".item_name").text(name); | ||||
MinRK
|
r17526 | var icon = NotebookList.icons[model.type]; | ||
var uri_prefix = NotebookList.uri_prefixes[model.type]; | ||||
item.find(".item_icon").addClass(icon).addClass('icon-fixed-width'); | ||||
MinRK
|
r17535 | var link = item.find("a.item_link") | ||
Brian E. Granger
|
r15071 | .attr('href', | ||
Jonathan Frederic
|
r17198 | utils.url_join_encode( | ||
MinRK
|
r15238 | this.base_url, | ||
MinRK
|
r17526 | uri_prefix, | ||
Min RK
|
r18752 | path | ||
Brian E. Granger
|
r15071 | ) | ||
); | ||||
MinRK
|
r17535 | // directory nav doesn't open new tabs | ||
// files, notebooks do | ||||
if (model.type !== "directory") { | ||||
link.attr('target','_blank'); | ||||
} | ||||
Min RK
|
r18928 | if (model.type !== 'directory') { | ||
this.add_duplicate_button(item); | ||||
} | ||||
MinRK
|
r17526 | if (model.type == 'file') { | ||
this.add_delete_button(item); | ||||
} else if (model.type == 'notebook') { | ||||
Min RK
|
r18928 | if (this.sessions[path] === undefined){ | ||
MinRK
|
r17526 | this.add_delete_button(item); | ||
} else { | ||||
Min RK
|
r18928 | this.add_shutdown_button(item, this.sessions[path]); | ||
MinRK
|
r17526 | } | ||
} | ||||
Brian E. Granger
|
r15071 | }; | ||
Jeffrey Bush
|
r17643 | NotebookList.prototype.add_name_input = function (name, item, icon_type) { | ||
MinRK
|
r17526 | item.data('name', name); | ||
Jeffrey Bush
|
r17643 | item.find(".item_icon").addClass(NotebookList.icons[icon_type]).addClass('icon-fixed-width'); | ||
MinRK
|
r10919 | item.find(".item_name").empty().append( | ||
MinRK
|
r10896 | $('<input/>') | ||
MinRK
|
r17536 | .addClass("filename_input") | ||
.attr('value', name) | ||||
MinRK
|
r10896 | .attr('size', '30') | ||
.attr('type', 'text') | ||||
Jeffrey Bush
|
r17646 | .keyup(function(event){ | ||
if(event.keyCode == 13){item.find('.upload_button').click();} | ||||
else if(event.keyCode == 27){item.remove();} | ||||
}) | ||||
Brian E. Granger
|
r4491 | ); | ||
}; | ||||
MinRK
|
r17536 | NotebookList.prototype.add_file_data = function (data, item) { | ||
item.data('filedata', data); | ||||
Brian E. Granger
|
r4491 | }; | ||
Zachary Sailer
|
r12988 | NotebookList.prototype.add_shutdown_button = function (item, session) { | ||
Matthias BUSSONNIER
|
r6842 | var that = this; | ||
Bussonnier Matthias
|
r19643 | var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-xs btn-warning"). | ||
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(); | ||
MinRK
|
r16445 | }, | ||
Jonathan Frederic
|
r17198 | error : utils.log_ajax_error, | ||
Matthias BUSSONNIER
|
r6842 | }; | ||
Jonathan Frederic
|
r17198 | 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 | }); | ||
David Neto
|
r18927 | item.find(".item_buttons").append(shutdown_button); | ||
}; | ||||
NotebookList.prototype.add_duplicate_button = function (item) { | ||||
var notebooklist = this; | ||||
Min RK
|
r18928 | var duplicate_button = $("<button/>").text("Duplicate").addClass("btn btn-default btn-xs"). | ||
David Neto
|
r18927 | click(function (e) { | ||
// $(this) is the button that was clicked. | ||||
var that = $(this); | ||||
Min RK
|
r18930 | var name = item.data('name'); | ||
var path = item.data('path'); | ||||
Min RK
|
r18928 | var message = 'Are you sure you want to duplicate ' + name + '?'; | ||
var copy_from = {copy_from : path}; | ||||
David Neto
|
r18927 | IPython.dialog.modal({ | ||
Min RK
|
r18928 | title : "Duplicate " + name, | ||
David Neto
|
r18927 | body : message, | ||
buttons : { | ||||
Duplicate : { | ||||
class: "btn-primary", | ||||
click: function() { | ||||
Min RK
|
r18930 | notebooklist.contents.copy(path, notebooklist.notebook_path).then(function () { | ||
notebooklist.load_list(); | ||||
}); | ||||
David Neto
|
r18927 | } | ||
}, | ||||
Cancel : {} | ||||
} | ||||
}); | ||||
return false; | ||||
}); | ||||
item.find(".item_buttons").append(duplicate_button); | ||||
Matthias BUSSONNIER
|
r6842 | }; | ||
Brian E. Granger
|
r4491 | NotebookList.prototype.add_delete_button = function (item) { | ||
Matthias BUSSONNIER
|
r9787 | var notebooklist = this; | ||
Jonathan Frederic
|
r16913 | var delete_button = $("<button/>").text("Delete").addClass("btn btn-default btn-xs"). | ||
Brian E. Granger
|
r4491 | click(function (e) { | ||
// $(this) is the button that was clicked. | ||||
var that = $(this); | ||||
MinRK
|
r17536 | // We use the filename from the parent list_item element's | ||
// data because the outer scope's values change as we iterate through the loop. | ||||
Brian Granger
|
r6195 | var parent_item = that.parents('div.list_item'); | ||
Thomas Kluyver
|
r18772 | var name = parent_item.data('name'); | ||
Jeff Hemmelgarn
|
r18625 | var path = parent_item.data('path'); | ||
Min RK
|
r18752 | var message = 'Are you sure you want to permanently delete the file: ' + name + '?'; | ||
Jonathan Frederic
|
r17202 | dialog.modal({ | ||
MinRK
|
r17526 | title : "Delete file", | ||
MinRK
|
r10921 | body : message, | ||
Brian E. Granger
|
r4491 | buttons : { | ||
MinRK
|
r10921 | Delete : { | ||
class: "btn-danger", | ||||
click: function() { | ||||
Thomas Kluyver
|
r18829 | notebooklist.contents.delete(path).then( | ||
function() { | ||||
Min RK
|
r18752 | notebooklist.notebook_deleted(path); | ||
Thomas Kluyver
|
r18656 | } | ||
Thomas Kluyver
|
r18829 | ); | ||
MinRK
|
r10921 | } | ||
Brian E. Granger
|
r4491 | }, | ||
MinRK
|
r10921 | Cancel : {} | ||
Brian E. Granger
|
r4491 | } | ||
}); | ||||
MinRK
|
r10891 | return false; | ||
Brian E. Granger
|
r4491 | }); | ||
David Neto
|
r18927 | item.find(".item_buttons").append(delete_button); | ||
Brian E. Granger
|
r4491 | }; | ||
Min RK
|
r18752 | NotebookList.prototype.notebook_deleted = function(path) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Remove the deleted notebook. | ||||
*/ | ||||
Thomas Kluyver
|
r18772 | $( ":data(path)" ).each(function() { | ||
Min RK
|
r18752 | var element = $(this); | ||
if (element.data("path") == path) { | ||||
Thomas Kluyver
|
r18655 | element.remove(); | ||
Scott Sanderson
|
r19031 | events.trigger('notebook_deleted.NotebookList'); | ||
Thomas Kluyver
|
r18655 | } | ||
}); | ||||
Min RK
|
r18752 | }; | ||
Thomas Kluyver
|
r18655 | |||
Brian E. Granger
|
r4491 | |||
Min RK
|
r18752 | NotebookList.prototype.add_upload_button = function (item) { | ||
Brian E. Granger
|
r4491 | var that = this; | ||
MinRK
|
r10896 | var upload_button = $('<button/>').text("Upload") | ||
Jonathan Frederic
|
r16914 | .addClass('btn btn-primary btn-xs upload_button') | ||
MinRK
|
r10896 | .click(function (e) { | ||
MinRK
|
r17536 | var filename = item.find('.item_name > input').val(); | ||
Min RK
|
r18752 | var path = utils.url_path_join(that.notebook_path, filename); | ||
MinRK
|
r17536 | var filedata = item.data('filedata'); | ||
var format = 'text'; | ||||
Jeffrey Bush
|
r17650 | if (filename.length === 0 || filename[0] === '.') { | ||
dialog.modal({ | ||||
title : 'Invalid file name', | ||||
body : "File names must be at least one character and not start with a dot", | ||||
buttons : {'OK' : { 'class' : 'btn-primary' }} | ||||
}); | ||||
return false; | ||||
} | ||||
MinRK
|
r17536 | if (filedata instanceof ArrayBuffer) { | ||
// base64-encode binary file data | ||||
var bytes = ''; | ||||
var buf = new Uint8Array(filedata); | ||||
var nbytes = buf.byteLength; | ||||
for (var i=0; i<nbytes; i++) { | ||||
bytes += String.fromCharCode(buf[i]); | ||||
} | ||||
filedata = btoa(bytes); | ||||
format = 'base64'; | ||||
} | ||||
Min RK
|
r18752 | var model = {}; | ||
MinRK
|
r17536 | |||
var name_and_ext = utils.splitext(filename); | ||||
var file_ext = name_and_ext[1]; | ||||
var content_type; | ||||
if (file_ext === '.ipynb') { | ||||
model.type = 'notebook'; | ||||
model.format = 'json'; | ||||
MinRK
|
r17537 | try { | ||
model.content = JSON.parse(filedata); | ||||
} catch (e) { | ||||
dialog.modal({ | ||||
title : 'Cannot upload invalid Notebook', | ||||
body : "The error was: " + e, | ||||
buttons : {'OK' : { | ||||
'class' : 'btn-primary', | ||||
click: function () { | ||||
item.remove(); | ||||
} | ||||
}} | ||||
}); | ||||
Jeffrey Bush
|
r17650 | return false; | ||
MinRK
|
r17537 | } | ||
MinRK
|
r17536 | content_type = 'application/json'; | ||
} else { | ||||
model.type = 'file'; | ||||
model.format = format; | ||||
model.content = filedata; | ||||
content_type = 'application/octet-stream'; | ||||
} | ||||
Min RK
|
r18752 | filedata = item.data('filedata'); | ||
MinRK
|
r17536 | |||
Thomas Kluyver
|
r18829 | var on_success = function () { | ||
item.removeClass('new-file'); | ||||
that.add_link(model, item); | ||||
that.add_delete_button(item); | ||||
that.session_list.load_sessions(); | ||||
Brian E. Granger
|
r4491 | }; | ||
Jeffrey Bush
|
r17648 | |||
var exists = false; | ||||
$.each(that.element.find('.list_item:not(.new-file)'), function(k,v){ | ||||
if ($(v).data('name') === filename) { exists = true; return false; } | ||||
}); | ||||
Min RK
|
r18752 | |||
Jeffrey Bush
|
r17648 | if (exists) { | ||
dialog.modal({ | ||||
title : "Replace file", | ||||
body : 'There is already a file named ' + filename + ', do you want to replace it?', | ||||
buttons : { | ||||
Overwrite : { | ||||
class: "btn-danger", | ||||
Min RK
|
r18752 | click: function () { | ||
Thomas Kluyver
|
r18829 | that.contents.save(path, model).then(on_success); | ||
} | ||||
Jeffrey Bush
|
r17648 | }, | ||
Cancel : { | ||||
click: function() { item.remove(); } | ||||
} | ||||
} | ||||
}); | ||||
} else { | ||||
Thomas Kluyver
|
r18829 | that.contents.save(path, model).then(on_success); | ||
Jeffrey Bush
|
r17648 | } | ||
MinRK
|
r10891 | return false; | ||
Brian E. Granger
|
r4491 | }); | ||
MinRK
|
r10896 | var cancel_button = $('<button/>').text("Cancel") | ||
Jonathan Frederic
|
r16913 | .addClass("btn btn-default btn-xs") | ||
MinRK
|
r10896 | .click(function (e) { | ||
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 | }; | ||
Min RK
|
r18752 | // Backwards compatability. | ||
Brian E. Granger
|
r4488 | IPython.NotebookList = NotebookList; | ||
Jonathan Frederic
|
r17201 | return {'NotebookList': NotebookList}; | ||
Jonathan Frederic
|
r17189 | }); | ||