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