##// END OF EJS Templates
Make ContentManager stateless...
KesterTong -
Show More
@@ -1,177 +1,183
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3
4 4 define([
5 5 'base/js/namespace',
6 6 'jquery',
7 7 'base/js/utils',
8 8 ], function(IPython, $, utils) {
9 9 var ContentManager = function(options) {
10 10 // Constructor
11 11 //
12 12 // A contentmanager handles passing file operations
13 13 // to the back-end. This includes checkpointing
14 14 // with the normal file operations.
15 15 //
16 16 // Parameters:
17 17 // options: dictionary
18 18 // Dictionary of keyword arguments.
19 // notebook_path: string
19 // events: $(Events) instance
20 20 // base_url: string
21 21 this.version = 0.1;
22 this.notebook_path = options.notebook_path;
22 this.events = options.events;
23 23 this.base_url = options.base_url;
24 24 };
25 25
26 ContentManager.prototype.new_notebook = function() {
27 var path = this.notebook_path;
26 /**
27 * Creates a new notebook file at the specified path, and
28 * opens that notebook in a new window.
29 *
30 * @method scroll_to_cell
31 * @param {String} path The path to create the new notebook at
32 */
33 ContentManager.prototype.new_notebook = function(path) {
28 34 var base_url = this.base_url;
29 35 var settings = {
30 36 processData : false,
31 37 cache : false,
32 38 type : "POST",
33 39 dataType : "json",
34 40 async : false,
35 41 success : function (data, status, xhr){
36 42 var notebook_name = data.name;
37 43 window.open(
38 44 utils.url_join_encode(
39 45 base_url,
40 46 'notebooks',
41 47 path,
42 48 notebook_name
43 49 ),
44 50 '_blank'
45 51 );
46 52 },
47 53 error : utils.log_ajax_error,
48 54 };
49 55 var url = utils.url_join_encode(
50 56 base_url,
51 57 'api/notebooks',
52 58 path
53 59 );
54 60 $.ajax(url,settings);
55 61 };
56 62
57 63 ContentManager.prototype.delete_notebook = function(notebook) {
58 64 var that = notebook;
59 65 var settings = {
60 66 processData : false,
61 67 cache : false,
62 68 type : "DELETE",
63 69 dataType: "json",
64 70 error : utils.log_ajax_error,
65 71 };
66 72 var url = utils.url_join_encode(
67 73 that.base_url,
68 74 'api/notebooks',
69 75 that.notebook_path,
70 76 that.notebook_name
71 77 );
72 78 $.ajax(url, settings);
73 79 };
74 80
75 81 ContentManager.prototype.rename_notebook = function(notebook, nbname) {
76 82 var that = notebook;
77 83 if (!nbname.match(/\.ipynb$/)) {
78 84 nbname = nbname + ".ipynb";
79 85 }
80 86 var data = {name: nbname};
81 87 var settings = {
82 88 processData : false,
83 89 cache : false,
84 90 type : "PATCH",
85 91 data : JSON.stringify(data),
86 92 dataType: "json",
87 93 headers : {'Content-Type': 'application/json'},
88 94 success : $.proxy(that.rename_success, this),
89 95 error : $.proxy(that.rename_error, this)
90 96 };
91 97 this.events.trigger('rename_notebook.Notebook', data);
92 98 var url = utils.url_join_encode(
93 99 this.base_url,
94 100 'api/notebooks',
95 101 this.notebook_path,
96 102 this.notebook_name
97 103 );
98 104 $.ajax(url, settings);
99 105 };
100 106
101 107 ContentManager.prototype.save_notebook = function(notebook, extra_settings) {
102 108 // Create a JSON model to be sent to the server.
103 109 var model = {};
104 110 model.name = notebook.notebook_name;
105 111 model.path = notebook.notebook_path;
106 112 model.content = notebook.toJSON();
107 113 model.content.nbformat = notebook.nbformat;
108 114 model.content.nbformat_minor = notebook.nbformat_minor;
109 115 // time the ajax call for autosave tuning purposes.
110 116 var start = new Date().getTime();
111 117 // We do the call with settings so we can set cache to false.
112 118 var settings = {
113 119 processData : false,
114 120 cache : false,
115 121 type : "PUT",
116 122 data : JSON.stringify(model),
117 123 headers : {'Content-Type': 'application/json'},
118 124 success : $.proxy(notebook.save_notebook_success, this, start),
119 125 error : $.proxy(notebook.save_notebook_error, this)
120 126 };
121 127 if (extra_settings) {
122 128 for (var key in extra_settings) {
123 129 settings[key] = extra_settings[key];
124 130 }
125 131 }
126 132 notebook.events.trigger('notebook_saving.Notebook');
127 133 var url = utils.url_join_encode(
128 134 notebook.base_url,
129 135 'api/notebooks',
130 136 notebook.notebook_path,
131 137 notebook.notebook_name
132 138 );
133 139 $.ajax(url, settings);
134 140 };
135 141
136 142 ContentManager.prototype.save_checkpoint = function() {
137 143 // This is not necessary - integrated into save
138 144 };
139 145
140 146 ContentManager.prototype.restore_checkpoint = function(notebook, id) {
141 147 that = notebook;
142 148 this.events.trigger('notebook_restoring.Notebook', checkpoint);
143 149 var url = utils.url_join_encode(
144 150 this.base_url,
145 151 'api/notebooks',
146 152 this.notebook_path,
147 153 this.notebook_name,
148 154 'checkpoints',
149 155 checkpoint
150 156 );
151 157 $.post(url).done(
152 158 $.proxy(that.restore_checkpoint_success, that)
153 159 ).fail(
154 160 $.proxy(that.restore_checkpoint_error, that)
155 161 );
156 162 };
157 163
158 164 ContentManager.prototype.list_checkpoints = function(notebook) {
159 165 that = notebook;
160 166 var url = utils.url_join_encode(
161 167 that.base_url,
162 168 'api/notebooks',
163 169 that.notebook_path,
164 170 that.notebook_name,
165 171 'checkpoints'
166 172 );
167 173 $.get(url).done(
168 174 $.proxy(that.list_checkpoints_success, that)
169 175 ).fail(
170 176 $.proxy(that.list_checkpoints_error, that)
171 177 );
172 178 };
173 179
174 180 IPython.ContentManager = ContentManager;
175 181
176 182 return {'ContentManager': ContentManager};
177 183 });
General Comments 0
You need to be logged in to leave comments. Login now