savewidget.js
157 lines
| 4.8 KiB
| application/javascript
|
JavascriptLexer
|
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. | ||||
//---------------------------------------------------------------------------- | ||||
|
r4369 | |||
//============================================================================ | ||||
|
r4609 | // SaveWidget | ||
|
r4369 | //============================================================================ | ||
var IPython = (function (IPython) { | ||||
var utils = IPython.utils; | ||||
var SaveWidget = function (selector) { | ||||
|
r4372 | this.selector = selector; | ||
|
r5008 | this.notebook_name_blacklist_re = /[\/\\]/ | ||
|
r4632 | this.last_saved_name = ''; | ||
|
r4372 | if (this.selector !== undefined) { | ||
this.element = $(selector); | ||||
this.style(); | ||||
|
r4369 | this.bind_events(); | ||
} | ||||
}; | ||||
|
r4372 | SaveWidget.prototype.style = function () { | ||
this.element.find('input#notebook_name').addClass('ui-widget ui-widget-content'); | ||||
|
r4630 | this.element.find('input#notebook_name').attr('tabindex','1'); | ||
|
r4372 | this.element.find('button#save_notebook').button(); | ||
|
r5097 | this.element.find('button#save_notebook').attr('title', 'Save the Notebook'); | ||
|
r4372 | var left_panel_width = $('div#left_panel').outerWidth(); | ||
var left_panel_splitter_width = $('div#left_panel_splitter').outerWidth(); | ||||
$('span#save_widget').css({marginLeft:left_panel_width+left_panel_splitter_width}); | ||||
|
r4630 | |||
|
r4372 | }; | ||
|
r4369 | SaveWidget.prototype.bind_events = function () { | ||
var that = this; | ||||
|
r4372 | this.element.find('button#save_notebook').click(function () { | ||
|
r4645 | that.save_notebook(); | ||
|
r4372 | }); | ||
|
r4632 | this.element.find('input#notebook_name').keyup(function () { | ||
that.is_renaming(); | ||||
|
r4630 | }); | ||
|
r4369 | }; | ||
|
r4645 | SaveWidget.prototype.save_notebook = function () { | ||
IPython.notebook.save_notebook(); | ||||
|
r5006 | }; | ||
SaveWidget.prototype.notebook_saved = function () { | ||||
|
r4645 | this.set_document_title(); | ||
this.last_saved_name = this.get_notebook_name(); | ||||
}; | ||||
|
r4632 | SaveWidget.prototype.is_renaming = function () { | ||
if (this.get_notebook_name() !== this.last_saved_name) { | ||||
this.status_rename(); | ||||
} else { | ||||
this.status_save(); | ||||
}; | ||||
}; | ||||
|
r4372 | SaveWidget.prototype.get_notebook_name = function () { | ||
return this.element.find('input#notebook_name').attr('value'); | ||||
} | ||||
|
r4484 | SaveWidget.prototype.set_notebook_name = function (nbname) { | ||
this.element.find('input#notebook_name').attr('value',nbname); | ||||
|
r4549 | this.set_document_title(); | ||
|
r4632 | this.last_saved_name = nbname; | ||
|
r4372 | } | ||
|
r4369 | |||
|
r4549 | SaveWidget.prototype.set_document_title = function () { | ||
nbname = this.get_notebook_name(); | ||||
|
r5104 | document.title = nbname; | ||
|
r4549 | }; | ||
|
r4484 | SaveWidget.prototype.get_notebook_id = function () { | ||
|
r5100 | return $('body').data('notebookId'); | ||
|
r4484 | }; | ||
SaveWidget.prototype.update_url = function () { | ||||
var notebook_id = this.get_notebook_id(); | ||||
if (notebook_id !== '') { | ||||
window.history.replaceState({}, '', notebook_id); | ||||
}; | ||||
}; | ||||
SaveWidget.prototype.test_notebook_name = function () { | ||||
var nbname = this.get_notebook_name(); | ||||
|
r5008 | if (this.notebook_name_blacklist_re.test(nbname) == false) { | ||
|
r4484 | return true; | ||
} else { | ||||
var bad_name = $('<div/>'); | ||||
bad_name.html( | ||||
"The notebook name you entered (" + | ||||
nbname + | ||||
|
r5008 | ") is not valid. Notebook names can contain any characters except / and \\." | ||
|
r4484 | ); | ||
bad_name.dialog({title: 'Invalid name', modal: true}); | ||||
return false; | ||||
}; | ||||
}; | ||||
|
r5007 | SaveWidget.prototype.reset_status = function () { | ||
this.is_renaming(); | ||||
}; | ||||
|
r4484 | SaveWidget.prototype.status_save = function () { | ||
|
r4660 | this.element.find('button#save_notebook').button('option', 'label', '<u>S</u>ave'); | ||
|
r4484 | this.element.find('button#save_notebook').button('enable'); | ||
|
r4615 | IPython.print_widget.enable(); | ||
|
r4592 | }; | ||
|
r4484 | |||
SaveWidget.prototype.status_saving = function () { | ||||
|
r4615 | this.element.find('button#save_notebook').button('option', 'label', 'Saving'); | ||
|
r4484 | this.element.find('button#save_notebook').button('disable'); | ||
|
r4615 | IPython.print_widget.disable(); | ||
|
r4592 | }; | ||
|
r4484 | |||
SaveWidget.prototype.status_loading = function () { | ||||
|
r4615 | this.element.find('button#save_notebook').button('option', 'label', 'Loading'); | ||
|
r4484 | this.element.find('button#save_notebook').button('disable'); | ||
|
r4615 | IPython.print_widget.disable(); | ||
|
r4484 | }; | ||
|
r4630 | SaveWidget.prototype.status_rename = function () { | ||
this.element.find('button#save_notebook').button('option', 'label', 'Rename'); | ||||
this.element.find('button#save_notebook').button('enable'); | ||||
IPython.print_widget.enable(); | ||||
}; | ||||
|
r4369 | IPython.SaveWidget = SaveWidget; | ||
return IPython; | ||||
}(IPython)); | ||||