##// END OF EJS Templates
expose notebook checkpoints in html/js...
MinRK -
Show More
@@ -25,10 +25,10 b' var IPython = (function (IPython) {'
25 this.add_buttons_group([
25 this.add_buttons_group([
26 {
26 {
27 id : 'save_b',
27 id : 'save_b',
28 label : 'Save',
28 label : 'Save Checkpoint',
29 icon : 'ui-icon-disk',
29 icon : 'ui-icon-disk',
30 callback : function () {
30 callback : function () {
31 IPython.notebook.save_notebook();
31 IPython.notebook.save_checkpoint();
32 }
32 }
33 }
33 }
34 ]);
34 ]);
@@ -84,6 +84,9 b' var IPython = (function (IPython) {'
84 this.element.find('#save_notebook').click(function () {
84 this.element.find('#save_notebook').click(function () {
85 IPython.notebook.save_notebook();
85 IPython.notebook.save_notebook();
86 });
86 });
87 this.element.find('#save_checkpoint').click(function () {
88 IPython.notebook.save_checkpoint();
89 });
87 this.element.find('#download_ipynb').click(function () {
90 this.element.find('#download_ipynb').click(function () {
88 var notebook_id = IPython.notebook.get_notebook_id();
91 var notebook_id = IPython.notebook.get_notebook_id();
89 var url = that.baseProjectUrl() + 'notebooks/' +
92 var url = that.baseProjectUrl() + 'notebooks/' +
@@ -39,6 +39,8 b' var IPython = (function (IPython) {'
39 this.paste_enabled = false;
39 this.paste_enabled = false;
40 this.dirty = false;
40 this.dirty = false;
41 this.metadata = {};
41 this.metadata = {};
42 this._checkpoint_after_save = false;
43 this.last_checkpoint = null;
42 // single worksheet for now
44 // single worksheet for now
43 this.worksheet_metadata = {};
45 this.worksheet_metadata = {};
44 this.control_key_active = false;
46 this.control_key_active = false;
@@ -250,7 +252,7 b' var IPython = (function (IPython) {'
250 return false;
252 return false;
251 } else if (event.which === 83 && that.control_key_active) {
253 } else if (event.which === 83 && that.control_key_active) {
252 // Save notebook = s
254 // Save notebook = s
253 that.save_notebook();
255 that.save_checkpoint();
254 that.control_key_active = false;
256 that.control_key_active = false;
255 return false;
257 return false;
256 } else if (event.which === 74 && that.control_key_active) {
258 } else if (event.which === 74 && that.control_key_active) {
@@ -1574,7 +1576,7 b' var IPython = (function (IPython) {'
1574 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id;
1576 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id;
1575 $.ajax(url, settings);
1577 $.ajax(url, settings);
1576 };
1578 };
1577
1579
1578 /**
1580 /**
1579 * Success callback for saving a notebook.
1581 * Success callback for saving a notebook.
1580 *
1582 *
@@ -1586,8 +1588,12 b' var IPython = (function (IPython) {'
1586 Notebook.prototype.save_notebook_success = function (data, status, xhr) {
1588 Notebook.prototype.save_notebook_success = function (data, status, xhr) {
1587 this.dirty = false;
1589 this.dirty = false;
1588 $([IPython.events]).trigger('notebook_saved.Notebook');
1590 $([IPython.events]).trigger('notebook_saved.Notebook');
1591 if (this._checkpoint_after_save) {
1592 this.create_checkpoint();
1593 this._checkpoint_after_save = false;
1594 };
1589 };
1595 };
1590
1596
1591 /**
1597 /**
1592 * Failure callback for saving a notebook.
1598 * Failure callback for saving a notebook.
1593 *
1599 *
@@ -1599,7 +1605,7 b' var IPython = (function (IPython) {'
1599 Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) {
1605 Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) {
1600 $([IPython.events]).trigger('notebook_save_failed.Notebook');
1606 $([IPython.events]).trigger('notebook_save_failed.Notebook');
1601 };
1607 };
1602
1608
1603 /**
1609 /**
1604 * Request a notebook's data from the server.
1610 * Request a notebook's data from the server.
1605 *
1611 *
@@ -1731,6 +1737,226 b' var IPython = (function (IPython) {'
1731 }
1737 }
1732 }
1738 }
1733
1739
1740 /********************* checkpoint-related *********************/
1741
1742 /**
1743 * Save the notebook then immediately create a checkpoint.
1744 *
1745 * @method save_checkpoint
1746 */
1747 Notebook.prototype.save_checkpoint = function () {
1748 this._checkpoint_after_save = true;
1749 this.save_notebook();
1750 };
1751
1752 /**
1753 * List checkpoints for this notebook.
1754 *
1755 * @method list_checkpoint
1756 */
1757 Notebook.prototype.list_checkpoints = function () {
1758 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints';
1759 $.get(url).done(
1760 $.proxy(this.list_checkpoints_success, this)
1761 ).fail(
1762 $.proxy(this.list_checkpoints_error, this)
1763 );
1764 };
1765
1766 /**
1767 * Success callback for listing checkpoints.
1768 *
1769 * @method list_checkpoint_success
1770 * @param {Object} data JSON representation of a checkpoint
1771 * @param {String} status Description of response status
1772 * @param {jqXHR} xhr jQuery Ajax object
1773 */
1774 Notebook.prototype.list_checkpoints_success = function (data, status, xhr) {
1775 var data = $.parseJSON(data);
1776 if (data.length) {
1777 this.last_checkpoint = data[0];
1778 } else {
1779 this.last_checkpoint = null;
1780 }
1781 $([IPython.events]).trigger('checkpoints_listed.Notebook', data);
1782 };
1783
1784 /**
1785 * Failure callback for listing a checkpoint.
1786 *
1787 * @method list_checkpoint_error
1788 * @param {jqXHR} xhr jQuery Ajax object
1789 * @param {String} status Description of response status
1790 * @param {String} error_msg HTTP error message
1791 */
1792 Notebook.prototype.list_checkpoints_error = function (xhr, status, error_msg) {
1793 $([IPython.events]).trigger('list_checkpoints_failed.Notebook');
1794 };
1795
1796 /**
1797 * Create a checkpoint of this notebook on the server from the most recent save.
1798 *
1799 * @method create_checkpoint
1800 */
1801 Notebook.prototype.create_checkpoint = function () {
1802 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints';
1803 $.post(url).done(
1804 $.proxy(this.create_checkpoint_success, this)
1805 ).fail(
1806 $.proxy(this.create_checkpoint_error, this)
1807 );
1808 };
1809
1810 /**
1811 * Success callback for creating a checkpoint.
1812 *
1813 * @method create_checkpoint_success
1814 * @param {Object} data JSON representation of a checkpoint
1815 * @param {String} status Description of response status
1816 * @param {jqXHR} xhr jQuery Ajax object
1817 */
1818 Notebook.prototype.create_checkpoint_success = function (data, status, xhr) {
1819 var data = $.parseJSON(data);
1820 this.last_checkpoint = data;
1821 $([IPython.events]).trigger('checkpoint_created.Notebook', data);
1822 };
1823
1824 /**
1825 * Failure callback for creating a checkpoint.
1826 *
1827 * @method create_checkpoint_error
1828 * @param {jqXHR} xhr jQuery Ajax object
1829 * @param {String} status Description of response status
1830 * @param {String} error_msg HTTP error message
1831 */
1832 Notebook.prototype.create_checkpoint_error = function (xhr, status, error_msg) {
1833 $([IPython.events]).trigger('checkpoint_failed.Notebook');
1834 };
1835
1836 Notebook.prototype.restore_checkpoint_dialog = function () {
1837 var that = this;
1838 var checkpoint = this.last_checkpoint;
1839 if ( ! checkpoint ) {
1840 console.log("restore dialog, but no checkpoint to restore to!");
1841 return;
1842 }
1843 var dialog = $('<div/>').append(
1844 $('<p/>').text("Are you sure you want to revert the notebook to " +
1845 "the latest checkpoint?"
1846 ).append(
1847 $("<strong/>").text(
1848 " This cannot be undone."
1849 )
1850 )
1851 ).append(
1852 $('<p/>').text("The checkpoint was last updated at")
1853 ).append(
1854 $('<p/>').text(Date(checkpoint.last_modified))
1855 );
1856
1857 $(document).append(dialog);
1858
1859 dialog.dialog({
1860 resizable: false,
1861 modal: true,
1862 title: "Revert notebook to checkpoint",
1863 closeText: '',
1864 buttons : {
1865 "Revert": function () {
1866 that.restore_checkpoint(checkpoint.checkpoint_id);
1867 $(this).dialog('close');
1868 },
1869 "Cancel": function () {
1870 $(this).dialog('close');
1871 }
1872 },
1873 width: 400
1874 });
1875 }
1876
1877 /**
1878 * Restore the notebook to a checkpoint state.
1879 *
1880 * @method restore_checkpoint
1881 * @param {String} checkpoint ID
1882 */
1883 Notebook.prototype.restore_checkpoint = function (checkpoint) {
1884 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
1885 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints/' + checkpoint;
1886 $.post(url).done(
1887 $.proxy(this.restore_checkpoint_success, this)
1888 ).fail(
1889 $.proxy(this.restore_checkpoint_error, this)
1890 );
1891 };
1892
1893 /**
1894 * Success callback for restoring a notebook to a checkpoint.
1895 *
1896 * @method restore_checkpoint_success
1897 * @param {Object} data (ignored, should be empty)
1898 * @param {String} status Description of response status
1899 * @param {jqXHR} xhr jQuery Ajax object
1900 */
1901 Notebook.prototype.restore_checkpoint_success = function (data, status, xhr) {
1902 $([IPython.events]).trigger('checkpoint_restored.Notebook');
1903 this.load_notebook(this.notebook_id);
1904 };
1905
1906 /**
1907 * Failure callback for restoring a notebook to a checkpoint.
1908 *
1909 * @method restore_checkpoint_error
1910 * @param {jqXHR} xhr jQuery Ajax object
1911 * @param {String} status Description of response status
1912 * @param {String} error_msg HTTP error message
1913 */
1914 Notebook.prototype.restore_checkpoint_error = function (xhr, status, error_msg) {
1915 $([IPython.events]).trigger('checkpoint_restore_failed.Notebook');
1916 };
1917
1918 /**
1919 * Delete a notebook checkpoint.
1920 *
1921 * @method delete_checkpoint
1922 * @param {String} checkpoint ID
1923 */
1924 Notebook.prototype.delete_checkpoint = function (checkpoint) {
1925 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
1926 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints/' + checkpoint;
1927 $.ajax(url, {
1928 type: 'DELETE',
1929 success: $.proxy(this.delete_checkpoint_success, this),
1930 error: $.proxy(this.delete_notebook_error,this)
1931 });
1932 };
1933
1934 /**
1935 * Success callback for deleting a notebook checkpoint
1936 *
1937 * @method delete_checkpoint_success
1938 * @param {Object} data (ignored, should be empty)
1939 * @param {String} status Description of response status
1940 * @param {jqXHR} xhr jQuery Ajax object
1941 */
1942 Notebook.prototype.delete_checkpoint_success = function (data, status, xhr) {
1943 $([IPython.events]).trigger('checkpoint_deleted.Notebook', data);
1944 this.load_notebook(this.notebook_id);
1945 };
1946
1947 /**
1948 * Failure callback for deleting a notebook checkpoint.
1949 *
1950 * @method delete_checkpoint_error
1951 * @param {jqXHR} xhr jQuery Ajax object
1952 * @param {String} status Description of response status
1953 * @param {String} error_msg HTTP error message
1954 */
1955 Notebook.prototype.delete_checkpoint_error = function (xhr, status, error_msg) {
1956 $([IPython.events]).trigger('checkpoint_delete_failed.Notebook');
1957 };
1958
1959
1734 IPython.Notebook = Notebook;
1960 IPython.Notebook = Notebook;
1735
1961
1736
1962
@@ -87,7 +87,7 b' var IPython = (function (IPython) {'
87 );
87 );
88 } else {
88 } else {
89 IPython.notebook.set_notebook_name(new_name);
89 IPython.notebook.set_notebook_name(new_name);
90 IPython.notebook.save_notebook();
90 IPython.notebook.save_checkpoint();
91 $(this).dialog('close');
91 $(this).dialog('close');
92 }
92 }
93 },
93 },
@@ -57,6 +57,7 b' class="notebook_app"'
57 <li id="copy_notebook"><a href="#">Make a Copy...</a></li>
57 <li id="copy_notebook"><a href="#">Make a Copy...</a></li>
58 <li id="rename_notebook"><a href="#">Rename...</a></li>
58 <li id="rename_notebook"><a href="#">Rename...</a></li>
59 <li id="save_notebook"><a href="#">Save</a></li>
59 <li id="save_notebook"><a href="#">Save</a></li>
60 <li id="save_checkpoint"><a href="#">Save Checkpoint</a></li>
60 <hr/>
61 <hr/>
61 <li><a href="#">Download as</a>
62 <li><a href="#">Download as</a>
62 <ul>
63 <ul>
General Comments 0
You need to be logged in to leave comments. Login now