##// END OF EJS Templates
add autosave timer...
MinRK -
Show More
@@ -41,6 +41,9 b' var IPython = (function (IPython) {'
41 this.metadata = {};
41 this.metadata = {};
42 this._checkpoint_after_save = false;
42 this._checkpoint_after_save = false;
43 this.last_checkpoint = null;
43 this.last_checkpoint = null;
44 this.autosave_interval = 0;
45 this.autosave_timer = null;
46 this.minimum_autosave_interval = 30000;
44 // single worksheet for now
47 // single worksheet for now
45 this.worksheet_metadata = {};
48 this.worksheet_metadata = {};
46 this.control_key_active = false;
49 this.control_key_active = false;
@@ -1552,6 +1555,31 b' var IPython = (function (IPython) {'
1552 };
1555 };
1553
1556
1554 /**
1557 /**
1558 * Start an autosave timer, for periodically saving the notebook.
1559 *
1560 * @method autosave_notebook
1561 * @param {Integer} interval the autosave interval in milliseconds
1562 */
1563 Notebook.prototype.autosave_notebook = function (interval) {
1564 var that = this;
1565 // clear previous interval, so we don't get simultaneous timers
1566 if (this.autosave_timer) {
1567 clearInterval(this.autosave_timer);
1568 }
1569
1570 this.autosave_interval = interval;
1571 if (interval) {
1572 this.autosave_timer = setInterval(function() {
1573 that.save_notebook();
1574 }, interval);
1575 $([IPython.events]).trigger("autosave_enabled.Notebook", interval);
1576 } else {
1577 this.autosave_timer = null;
1578 $([IPython.events]).trigger("autosave_disabled.Notebook");
1579 };
1580 };
1581
1582 /**
1555 * Save this notebook on the server.
1583 * Save this notebook on the server.
1556 *
1584 *
1557 * @method save_notebook
1585 * @method save_notebook
@@ -1562,6 +1590,10 b' var IPython = (function (IPython) {'
1562 data.metadata.name = this.notebook_name;
1590 data.metadata.name = this.notebook_name;
1563 data.nbformat = this.nbformat;
1591 data.nbformat = this.nbformat;
1564 data.nbformat_minor = this.nbformat_minor;
1592 data.nbformat_minor = this.nbformat_minor;
1593
1594 // time the ajax call for autosave tuning purposes.
1595 var start = new Date().getTime();
1596
1565 // We do the call with settings so we can set cache to false.
1597 // We do the call with settings so we can set cache to false.
1566 var settings = {
1598 var settings = {
1567 processData : false,
1599 processData : false,
@@ -1569,8 +1601,8 b' var IPython = (function (IPython) {'
1569 type : "PUT",
1601 type : "PUT",
1570 data : JSON.stringify(data),
1602 data : JSON.stringify(data),
1571 headers : {'Content-Type': 'application/json'},
1603 headers : {'Content-Type': 'application/json'},
1572 success : $.proxy(this.save_notebook_success,this),
1604 success : $.proxy(this.save_notebook_success, this, start),
1573 error : $.proxy(this.save_notebook_error,this)
1605 error : $.proxy(this.save_notebook_error, this)
1574 };
1606 };
1575 $([IPython.events]).trigger('notebook_saving.Notebook');
1607 $([IPython.events]).trigger('notebook_saving.Notebook');
1576 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id;
1608 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id;
@@ -1581,13 +1613,15 b' var IPython = (function (IPython) {'
1581 * Success callback for saving a notebook.
1613 * Success callback for saving a notebook.
1582 *
1614 *
1583 * @method save_notebook_success
1615 * @method save_notebook_success
1616 * @param {Integer} start the time when the save request started
1584 * @param {Object} data JSON representation of a notebook
1617 * @param {Object} data JSON representation of a notebook
1585 * @param {String} status Description of response status
1618 * @param {String} status Description of response status
1586 * @param {jqXHR} xhr jQuery Ajax object
1619 * @param {jqXHR} xhr jQuery Ajax object
1587 */
1620 */
1588 Notebook.prototype.save_notebook_success = function (data, status, xhr) {
1621 Notebook.prototype.save_notebook_success = function (start, data, status, xhr) {
1589 this.dirty = false;
1622 this.dirty = false;
1590 $([IPython.events]).trigger('notebook_saved.Notebook');
1623 $([IPython.events]).trigger('notebook_saved.Notebook');
1624 this._update_autosave_interval(start);
1591 if (this._checkpoint_after_save) {
1625 if (this._checkpoint_after_save) {
1592 this.create_checkpoint();
1626 this.create_checkpoint();
1593 this._checkpoint_after_save = false;
1627 this._checkpoint_after_save = false;
@@ -1595,6 +1629,26 b' var IPython = (function (IPython) {'
1595 };
1629 };
1596
1630
1597 /**
1631 /**
1632 * update the autosave interval based on how long the last save took
1633 *
1634 * @method _update_autosave_interval
1635 * @param {Integer} timestamp when the save request started
1636 */
1637 Notebook.prototype._update_autosave_interval = function (start) {
1638 var duration = (new Date().getTime() - start);
1639 if (this.autosave_interval) {
1640 // new save interval: higher of 10x save duration or parameter (default 30 seconds)
1641 var interval = Math.max(10 * duration, this.minimum_autosave_interval);
1642 // round to 10 seconds, otherwise we will be setting a new interval too often
1643 interval = 10000 * Math.round(interval / 10000);
1644 // set new interval, if it's changed
1645 if (interval != this.autosave_interval) {
1646 this.autosave_notebook(interval);
1647 }
1648 }
1649 };
1650
1651 /**
1598 * Failure callback for saving a notebook.
1652 * Failure callback for saving a notebook.
1599 *
1653 *
1600 * @method save_notebook_error
1654 * @method save_notebook_error
@@ -80,14 +80,19 b' $(document).ready(function () {'
80 IPython.page.show();
80 IPython.page.show();
81
81
82 IPython.layout_manager.do_resize();
82 IPython.layout_manager.do_resize();
83 $([IPython.events]).on('notebook_loaded.Notebook', function () {
83 var first_load = function () {
84 IPython.layout_manager.do_resize();
84 IPython.layout_manager.do_resize();
85 var hash = document.location.hash;
85 var hash = document.location.hash;
86 if (hash) {
86 if (hash) {
87 document.location.hash = '';
87 document.location.hash = '';
88 document.location.hash = hash;
88 document.location.hash = hash;
89 }
89 }
90 });
90 IPython.notebook.autosave_notebook(30000);
91 // only do this once
92 $([IPython.events]).off('notebook_loaded.Notebook', first_load);
93 };
94
95 $([IPython.events]).on('notebook_loaded.Notebook', first_load);
91 IPython.notebook.load_notebook($('body').data('notebookId'));
96 IPython.notebook.load_notebook($('body').data('notebookId'));
92
97
93 });
98 });
@@ -203,6 +203,14 b' var IPython = (function (IPython) {'
203 nnw.set_message("Checkpoint restore failed");
203 nnw.set_message("Checkpoint restore failed");
204 });
204 });
205
205
206 // Autosave events
207 $([IPython.events]).on('autosave_disabled.Notebook', function () {
208 nnw.set_message("Autosave disabled", 2000);
209 });
210 $([IPython.events]).on('autosave_enabled.Notebook', function (evt, interval) {
211 nnw.set_message("Saving every " + interval / 1000 + "s", 1000);
212 });
213
206 };
214 };
207
215
208 IPython.NotificationArea = NotificationArea;
216 IPython.NotificationArea = NotificationArea;
General Comments 0
You need to be logged in to leave comments. Login now