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