##// END OF EJS Templates
update upload and copy...
MinRK -
Show More
@@ -1,191 +1,235 b''
1 """Tornado handlers for the notebooks web service.
1 """Tornado handlers for the notebooks web service.
2
2
3 Authors:
3 Authors:
4
4
5 * Brian Granger
5 * Brian Granger
6 """
6 """
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2008-2011 The IPython Development Team
9 # Copyright (C) 2008-2011 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 import json
19 import json
20
20
21 from tornado import web
21 from tornado import web
22
22
23 from IPython.html.utils import url_path_join
23 from IPython.html.utils import url_path_join
24 from IPython.utils.jsonutil import date_default
24 from IPython.utils.jsonutil import date_default
25
25
26 from IPython.html.base.handlers import IPythonHandler, json_errors
26 from IPython.html.base.handlers import IPythonHandler, json_errors
27
27
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 # Notebook web service handlers
29 # Notebook web service handlers
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31
31
32
32
33 class NotebookHandler(IPythonHandler):
33 class NotebookHandler(IPythonHandler):
34
34
35 SUPPORTED_METHODS = (u'GET', u'PUT', u'PATCH', u'POST', u'DELETE')
35 SUPPORTED_METHODS = (u'GET', u'PUT', u'PATCH', u'POST', u'DELETE')
36
36
37 def notebook_location(self, name, path=''):
37 def notebook_location(self, name, path=''):
38 """Return the full URL location of a notebook based.
38 """Return the full URL location of a notebook based.
39
39
40 Parameters
40 Parameters
41 ----------
41 ----------
42 name : unicode
42 name : unicode
43 The base name of the notebook, such as "foo.ipynb".
43 The base name of the notebook, such as "foo.ipynb".
44 path : unicode
44 path : unicode
45 The URL path of the notebook.
45 The URL path of the notebook.
46 """
46 """
47 return url_path_join(self.base_project_url, 'api', 'notebooks', path, name)
47 return url_path_join(self.base_project_url, 'api', 'notebooks', path, name)
48
48
49 @web.authenticated
49 @web.authenticated
50 @json_errors
50 @json_errors
51 def get(self, path='', name=None):
51 def get(self, path='', name=None):
52 """
52 """
53 GET with path and no notebook lists notebooks in a directory
53 GET with path and no notebook lists notebooks in a directory
54 GET with path and notebook name
54 GET with path and notebook name
55
55
56 GET get checks if a notebook is not named, an returns a list of notebooks
56 GET get checks if a notebook is not named, an returns a list of notebooks
57 in the notebook path given. If a name is given, return
57 in the notebook path given. If a name is given, return
58 the notebook representation"""
58 the notebook representation"""
59 nbm = self.notebook_manager
59 nbm = self.notebook_manager
60 # Check to see if a notebook name was given
60 # Check to see if a notebook name was given
61 if name is None:
61 if name is None:
62 # List notebooks in 'path'
62 # List notebooks in 'path'
63 notebooks = nbm.list_notebooks(path)
63 notebooks = nbm.list_notebooks(path)
64 self.finish(json.dumps(notebooks, default=date_default))
64 self.finish(json.dumps(notebooks, default=date_default))
65 else:
65 else:
66 # get and return notebook representation
66 # get and return notebook representation
67 model = nbm.get_notebook_model(name, path)
67 model = nbm.get_notebook_model(name, path)
68 self.set_header(u'Last-Modified', model[u'last_modified'])
68 self.set_header(u'Last-Modified', model[u'last_modified'])
69 self.finish(json.dumps(model, default=date_default))
69 self.finish(json.dumps(model, default=date_default))
70
70
71 @web.authenticated
71 @web.authenticated
72 @json_errors
72 @json_errors
73 def patch(self, path='', name=None):
73 def patch(self, path='', name=None):
74 """patch is currently used strictly for notebook renaming.
74 """PATCH renames a notebook without re-uploading content."""
75 Changes the notebook name to the name given in data."""
76 nbm = self.notebook_manager
75 nbm = self.notebook_manager
77 if name is None:
76 if name is None:
78 raise web.HTTPError(400, u'Notebook name missing')
77 raise web.HTTPError(400, u'Notebook name missing')
79 model = self.get_json_body()
78 model = self.get_json_body()
80 if model is None:
79 if model is None:
81 raise web.HTTPError(400, u'JSON body missing')
80 raise web.HTTPError(400, u'JSON body missing')
82 model = nbm.update_notebook_model(model, name, path)
81 model = nbm.update_notebook_model(model, name, path)
83 if model[u'name'] != name or model[u'path'] != path:
82 if model[u'name'] != name or model[u'path'] != path:
84 self.set_status(301)
83 self.set_status(301)
85 location = self.notebook_location(model[u'name'], model[u'path'])
84 location = self.notebook_location(model[u'name'], model[u'path'])
86 self.set_header(u'Location', location)
85 self.set_header(u'Location', location)
87 self.set_header(u'Last-Modified', model[u'last_modified'])
86 self.set_header(u'Last-Modified', model[u'last_modified'])
88 self.finish(json.dumps(model, default=date_default))
87 self.finish(json.dumps(model, default=date_default))
89
88
90 @web.authenticated
89 @web.authenticated
91 @json_errors
90 @json_errors
92 def post(self, path='', name=None):
91 def post(self, path='', name=None):
93 """Create a new notebook in the location given by 'notebook_path'."""
92 """Create a new notebook in the specified path.
93
94 POST creates new notebooks.
95
96 POST /api/notebooks/path : new untitled notebook in path
97 POST /api/notebooks/path/notebook.ipynb : new notebook with name in path
98 If content specified upload notebook, otherwise start empty.
99 """
94 nbm = self.notebook_manager
100 nbm = self.notebook_manager
95 model = self.get_json_body()
101 model = self.get_json_body()
96 model = nbm.create_notebook_model(model, path)
102 if name is None:
103 # creating new notebook, model doesn't make sense
104 if model is not None:
105 raise web.HTTPError(400, "Model not valid when creating untitled notebooks.")
106 model = nbm.create_notebook_model(path=path)
107 else:
108 if model is None:
109 self.log.info("Creating new Notebook at %s/%s", path, name)
110 model = {}
111 else:
112 self.log.info("Uploading Notebook to %s/%s", path, name)
113 # set the model name from the URL
114 model['name'] = name
115 model = nbm.create_notebook_model(model, path)
116
97 location = self.notebook_location(model[u'name'], model[u'path'])
117 location = self.notebook_location(model[u'name'], model[u'path'])
98 self.set_header(u'Location', location)
118 self.set_header(u'Location', location)
99 self.set_header(u'Last-Modified', model[u'last_modified'])
119 self.set_header(u'Last-Modified', model[u'last_modified'])
100 self.set_status(201)
120 self.set_status(201)
101 self.finish(json.dumps(model, default=date_default))
121 self.finish(json.dumps(model, default=date_default))
102
122
103 @web.authenticated
123 @web.authenticated
104 @json_errors
124 @json_errors
105 def put(self, path='', name=None):
125 def put(self, path='', name=None):
106 """saves the notebook in the location given by 'notebook_path'."""
126 """saves the notebook in the location given by 'notebook_path'."""
107 nbm = self.notebook_manager
127 nbm = self.notebook_manager
108 model = self.get_json_body()
128 model = self.get_json_body()
109 if model is None:
129 if model is None:
110 raise web.HTTPError(400, u'JSON body missing')
130 raise web.HTTPError(400, u'JSON body missing')
111 nbm.save_notebook_model(model, name, path)
131 nbm.save_notebook_model(model, name, path)
112 self.finish(json.dumps(model, default=date_default))
132 self.finish(json.dumps(model, default=date_default))
113
133
114 @web.authenticated
134 @web.authenticated
115 @json_errors
135 @json_errors
116 def delete(self, path='', name=None):
136 def delete(self, path='', name=None):
117 """delete the notebook in the given notebook path"""
137 """delete the notebook in the given notebook path"""
118 nbm = self.notebook_manager
138 nbm = self.notebook_manager
119 nbm.delete_notebook_model(name, path)
139 nbm.delete_notebook_model(name, path)
120 self.set_status(204)
140 self.set_status(204)
121 self.finish()
141 self.finish()
122
142
143 class NotebookCopyHandler(IPythonHandler):
144
145 SUPPORTED_METHODS = ('POST')
146
147 @web.authenticated
148 @json_errors
149 def post(self, path='', name=None):
150 """Copy an existing notebook."""
151 nbm = self.notebook_manager
152 model = self.get_json_body()
153 if name is None:
154 raise web.HTTPError(400, "Notebook name required")
155 self.log.info("Copying Notebook %s/%s", path, name)
156 model = nbm.copy_notebook(name, path)
157 location = url_path_join(
158 self.base_project_url, 'api', 'notebooks',
159 model['path'], model['name'],
160 )
161 self.set_header(u'Location', location)
162 self.set_header(u'Last-Modified', model[u'last_modified'])
163 self.set_status(201)
164 self.finish(json.dumps(model, default=date_default))
165
123
166
124 class NotebookCheckpointsHandler(IPythonHandler):
167 class NotebookCheckpointsHandler(IPythonHandler):
125
168
126 SUPPORTED_METHODS = ('GET', 'POST')
169 SUPPORTED_METHODS = ('GET', 'POST')
127
170
128 @web.authenticated
171 @web.authenticated
129 @json_errors
172 @json_errors
130 def get(self, path='', name=None):
173 def get(self, path='', name=None):
131 """get lists checkpoints for a notebook"""
174 """get lists checkpoints for a notebook"""
132 nbm = self.notebook_manager
175 nbm = self.notebook_manager
133 checkpoints = nbm.list_checkpoints(name, path)
176 checkpoints = nbm.list_checkpoints(name, path)
134 data = json.dumps(checkpoints, default=date_default)
177 data = json.dumps(checkpoints, default=date_default)
135 self.finish(data)
178 self.finish(data)
136
179
137 @web.authenticated
180 @web.authenticated
138 @json_errors
181 @json_errors
139 def post(self, path='', name=None):
182 def post(self, path='', name=None):
140 """post creates a new checkpoint"""
183 """post creates a new checkpoint"""
141 nbm = self.notebook_manager
184 nbm = self.notebook_manager
142 checkpoint = nbm.create_checkpoint(name, path)
185 checkpoint = nbm.create_checkpoint(name, path)
143 data = json.dumps(checkpoint, default=date_default)
186 data = json.dumps(checkpoint, default=date_default)
144 location = url_path_join(self.base_project_url, u'/api/notebooks',
187 location = url_path_join(self.base_project_url, u'/api/notebooks',
145 path, name, 'checkpoints', checkpoint[u'checkpoint_id'])
188 path, name, 'checkpoints', checkpoint[u'checkpoint_id'])
146 self.set_header(u'Location', location)
189 self.set_header(u'Location', location)
147 self.finish(data)
190 self.finish(data)
148
191
149
192
150 class ModifyNotebookCheckpointsHandler(IPythonHandler):
193 class ModifyNotebookCheckpointsHandler(IPythonHandler):
151
194
152 SUPPORTED_METHODS = ('POST', 'DELETE')
195 SUPPORTED_METHODS = ('POST', 'DELETE')
153
196
154 @web.authenticated
197 @web.authenticated
155 @json_errors
198 @json_errors
156 def post(self, path, name, checkpoint_id):
199 def post(self, path, name, checkpoint_id):
157 """post restores a notebook from a checkpoint"""
200 """post restores a notebook from a checkpoint"""
158 nbm = self.notebook_manager
201 nbm = self.notebook_manager
159 nbm.restore_checkpoint(checkpoint_id, name, path)
202 nbm.restore_checkpoint(checkpoint_id, name, path)
160 self.set_status(204)
203 self.set_status(204)
161 self.finish()
204 self.finish()
162
205
163 @web.authenticated
206 @web.authenticated
164 @json_errors
207 @json_errors
165 def delete(self, path, name, checkpoint_id):
208 def delete(self, path, name, checkpoint_id):
166 """delete clears a checkpoint for a given notebook"""
209 """delete clears a checkpoint for a given notebook"""
167 nbm = self.notebook_manager
210 nbm = self.notebook_manager
168 nbm.delete_checkpoint(checkpoint_id, name, path)
211 nbm.delete_checkpoint(checkpoint_id, name, path)
169 self.set_status(204)
212 self.set_status(204)
170 self.finish()
213 self.finish()
171
214
172 #-----------------------------------------------------------------------------
215 #-----------------------------------------------------------------------------
173 # URL to handler mappings
216 # URL to handler mappings
174 #-----------------------------------------------------------------------------
217 #-----------------------------------------------------------------------------
175
218
176
219
177 _path_regex = r"(?P<path>.*)"
220 _path_regex = r"(?P<path>.*)"
178 _checkpoint_id_regex = r"(?P<checkpoint_id>[\w-]+)"
221 _checkpoint_id_regex = r"(?P<checkpoint_id>[\w-]+)"
179 _notebook_name_regex = r"(?P<name>[^/]+\.ipynb)"
222 _notebook_name_regex = r"(?P<name>[^/]+\.ipynb)"
180 _notebook_path_regex = "%s/%s" % (_path_regex, _notebook_name_regex)
223 _notebook_path_regex = "%s/%s" % (_path_regex, _notebook_name_regex)
181
224
182 default_handlers = [
225 default_handlers = [
226 (r"/api/notebooks/?%s/copy" % _notebook_path_regex, NotebookCopyHandler),
183 (r"/api/notebooks/?%s/checkpoints" % _notebook_path_regex, NotebookCheckpointsHandler),
227 (r"/api/notebooks/?%s/checkpoints" % _notebook_path_regex, NotebookCheckpointsHandler),
184 (r"/api/notebooks/?%s/checkpoints/%s" % (_notebook_path_regex, _checkpoint_id_regex),
228 (r"/api/notebooks/?%s/checkpoints/%s" % (_notebook_path_regex, _checkpoint_id_regex),
185 ModifyNotebookCheckpointsHandler),
229 ModifyNotebookCheckpointsHandler),
186 (r"/api/notebooks/?%s" % _notebook_path_regex, NotebookHandler),
230 (r"/api/notebooks/?%s" % _notebook_path_regex, NotebookHandler),
187 (r"/api/notebooks/?%s/?" % _path_regex, NotebookHandler),
231 (r"/api/notebooks/?%s/?" % _path_regex, NotebookHandler),
188 ]
232 ]
189
233
190
234
191
235
@@ -1,169 +1,167 b''
1 """A base class notebook manager.
1 """A base class notebook manager.
2
2
3 Authors:
3 Authors:
4
4
5 * Brian Granger
5 * Brian Granger
6 * Zach Sailer
6 * Zach Sailer
7 """
7 """
8
8
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10 # Copyright (C) 2011 The IPython Development Team
10 # Copyright (C) 2011 The IPython Development Team
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Imports
17 # Imports
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 import os
20 import os
21
21
22 from IPython.config.configurable import LoggingConfigurable
22 from IPython.config.configurable import LoggingConfigurable
23 from IPython.nbformat import current
23 from IPython.nbformat import current
24 from IPython.utils.traitlets import List, Dict, Unicode, TraitError
24 from IPython.utils.traitlets import List, Dict, Unicode, TraitError
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Classes
27 # Classes
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 class NotebookManager(LoggingConfigurable):
30 class NotebookManager(LoggingConfigurable):
31
31
32 # Todo:
32 # Todo:
33 # The notebook_dir attribute is used to mean a couple of different things:
33 # The notebook_dir attribute is used to mean a couple of different things:
34 # 1. Where the notebooks are stored if FileNotebookManager is used.
34 # 1. Where the notebooks are stored if FileNotebookManager is used.
35 # 2. The cwd of the kernel for a project.
35 # 2. The cwd of the kernel for a project.
36 # Right now we use this attribute in a number of different places and
36 # Right now we use this attribute in a number of different places and
37 # we are going to have to disentangle all of this.
37 # we are going to have to disentangle all of this.
38 notebook_dir = Unicode(os.getcwdu(), config=True, help="""
38 notebook_dir = Unicode(os.getcwdu(), config=True, help="""
39 The directory to use for notebooks.
39 The directory to use for notebooks.
40 """)
40 """)
41
41
42 filename_ext = Unicode(u'.ipynb')
42 filename_ext = Unicode(u'.ipynb')
43
43
44 def path_exists(self, path):
44 def path_exists(self, path):
45 """Does the API-style path (directory) actually exist?
45 """Does the API-style path (directory) actually exist?
46
46
47 Override this method in subclasses.
47 Override this method in subclasses.
48
48
49 Parameters
49 Parameters
50 ----------
50 ----------
51 path : string
51 path : string
52 The
52 The
53
53
54 Returns
54 Returns
55 -------
55 -------
56 exists : bool
56 exists : bool
57 Whether the path does indeed exist.
57 Whether the path does indeed exist.
58 """
58 """
59 raise NotImplementedError
59 raise NotImplementedError
60
60
61 def _notebook_dir_changed(self, name, old, new):
61 def _notebook_dir_changed(self, name, old, new):
62 """Do a bit of validation of the notebook dir."""
62 """Do a bit of validation of the notebook dir."""
63 if not os.path.isabs(new):
63 if not os.path.isabs(new):
64 # If we receive a non-absolute path, make it absolute.
64 # If we receive a non-absolute path, make it absolute.
65 self.notebook_dir = os.path.abspath(new)
65 self.notebook_dir = os.path.abspath(new)
66 return
66 return
67 if os.path.exists(new) and not os.path.isdir(new):
67 if os.path.exists(new) and not os.path.isdir(new):
68 raise TraitError("notebook dir %r is not a directory" % new)
68 raise TraitError("notebook dir %r is not a directory" % new)
69 if not os.path.exists(new):
69 if not os.path.exists(new):
70 self.log.info("Creating notebook dir %s", new)
70 self.log.info("Creating notebook dir %s", new)
71 try:
71 try:
72 os.mkdir(new)
72 os.mkdir(new)
73 except:
73 except:
74 raise TraitError("Couldn't create notebook dir %r" % new)
74 raise TraitError("Couldn't create notebook dir %r" % new)
75
75
76 # Main notebook API
76 # Main notebook API
77
77
78 def increment_filename(self, basename, path=''):
78 def increment_filename(self, basename, path=''):
79 """Increment a notebook filename without the .ipynb to make it unique.
79 """Increment a notebook filename without the .ipynb to make it unique.
80
80
81 Parameters
81 Parameters
82 ----------
82 ----------
83 basename : unicode
83 basename : unicode
84 The name of a notebook without the ``.ipynb`` file extension.
84 The name of a notebook without the ``.ipynb`` file extension.
85 path : unicode
85 path : unicode
86 The URL path of the notebooks directory
86 The URL path of the notebooks directory
87 """
87 """
88 return basename
88 return basename
89
89
90 def list_notebooks(self):
90 def list_notebooks(self, path=''):
91 """Return a list of notebook dicts without content.
91 """Return a list of notebook dicts without content.
92
92
93 This returns a list of dicts, each of the form::
93 This returns a list of dicts, each of the form::
94
94
95 dict(notebook_id=notebook,name=name)
95 dict(notebook_id=notebook,name=name)
96
96
97 This list of dicts should be sorted by name::
97 This list of dicts should be sorted by name::
98
98
99 data = sorted(data, key=lambda item: item['name'])
99 data = sorted(data, key=lambda item: item['name'])
100 """
100 """
101 raise NotImplementedError('must be implemented in a subclass')
101 raise NotImplementedError('must be implemented in a subclass')
102
102
103 def get_notebook_model(self, name, path='', content=True):
103 def get_notebook_model(self, name, path='', content=True):
104 """Get the notebook model with or without content."""
104 """Get the notebook model with or without content."""
105 raise NotImplementedError('must be implemented in a subclass')
105 raise NotImplementedError('must be implemented in a subclass')
106
106
107 def save_notebook_model(self, model, name, path=''):
107 def save_notebook_model(self, model, name, path=''):
108 """Save the notebook model and return the model with no content."""
108 """Save the notebook model and return the model with no content."""
109 raise NotImplementedError('must be implemented in a subclass')
109 raise NotImplementedError('must be implemented in a subclass')
110
110
111 def update_notebook_model(self, model, name, path=''):
111 def update_notebook_model(self, model, name, path=''):
112 """Update the notebook model and return the model with no content."""
112 """Update the notebook model and return the model with no content."""
113 raise NotImplementedError('must be implemented in a subclass')
113 raise NotImplementedError('must be implemented in a subclass')
114
114
115 def delete_notebook_model(self, name, path):
115 def delete_notebook_model(self, name, path=''):
116 """Delete notebook by name and path."""
116 """Delete notebook by name and path."""
117 raise NotImplementedError('must be implemented in a subclass')
117 raise NotImplementedError('must be implemented in a subclass')
118
118
119 def create_notebook_model(self, model=None, path=''):
119 def create_notebook_model(self, model=None, path=''):
120 """Create a new untitled notebook and return its model with no content."""
120 """Create a new untitled notebook and return its model with no content."""
121 untitled = self.increment_filename('Untitled', path)
122 if model is None:
121 if model is None:
123 model = {}
122 model = {}
123 if 'content' not in model:
124 metadata = current.new_metadata(name=u'')
124 metadata = current.new_metadata(name=u'')
125 nb = current.new_notebook(metadata=metadata)
125 model['content'] = current.new_notebook(metadata=metadata)
126 model['content'] = nb
126 if 'name' not in model:
127 model['name'] = name = untitled
127 model['name'] = self.increment_filename('Untitled', path)
128 model['path'] = path
128
129 else:
129 model['path'] = path
130 name = model.setdefault('name', untitled)
130 model = self.save_notebook_model(model, model['name'], model['path'])
131 model['path'] = path
132 model = self.save_notebook_model(model, name, path)
133 return model
131 return model
134
132
135 def copy_notebook(self, name, path='/', content=False):
133 def copy_notebook(self, name, path=''):
136 """Copy an existing notebook and return its new model."""
134 """Copy an existing notebook and return its new model."""
137 model = self.get_notebook_model(name, path)
135 model = self.get_notebook_model(name, path)
138 name = os.path.splitext(name)[0] + '-Copy'
136 name = os.path.splitext(name)[0] + '-Copy'
139 name = self.increment_filename(name, path) + self.filename_ext
137 name = self.increment_filename(name, path) + self.filename_ext
140 model['name'] = name
138 model['name'] = name
141 model = self.save_notebook_model(model, name, path, content=content)
139 model = self.save_notebook_model(model, name, path)
142 return model
140 return model
143
141
144 # Checkpoint-related
142 # Checkpoint-related
145
143
146 def create_checkpoint(self, name, path='/'):
144 def create_checkpoint(self, name, path=''):
147 """Create a checkpoint of the current state of a notebook
145 """Create a checkpoint of the current state of a notebook
148
146
149 Returns a checkpoint_id for the new checkpoint.
147 Returns a checkpoint_id for the new checkpoint.
150 """
148 """
151 raise NotImplementedError("must be implemented in a subclass")
149 raise NotImplementedError("must be implemented in a subclass")
152
150
153 def list_checkpoints(self, name, path='/'):
151 def list_checkpoints(self, name, path=''):
154 """Return a list of checkpoints for a given notebook"""
152 """Return a list of checkpoints for a given notebook"""
155 return []
153 return []
156
154
157 def restore_checkpoint(self, checkpoint_id, name, path='/'):
155 def restore_checkpoint(self, checkpoint_id, name, path=''):
158 """Restore a notebook from one of its checkpoints"""
156 """Restore a notebook from one of its checkpoints"""
159 raise NotImplementedError("must be implemented in a subclass")
157 raise NotImplementedError("must be implemented in a subclass")
160
158
161 def delete_checkpoint(self, checkpoint_id, name, path='/'):
159 def delete_checkpoint(self, checkpoint_id, name, path=''):
162 """delete a checkpoint for a notebook"""
160 """delete a checkpoint for a notebook"""
163 raise NotImplementedError("must be implemented in a subclass")
161 raise NotImplementedError("must be implemented in a subclass")
164
162
165 def log_info(self):
163 def log_info(self):
166 self.log.info(self.info_string())
164 self.log.info(self.info_string())
167
165
168 def info_string(self):
166 def info_string(self):
169 return "Serving notebooks"
167 return "Serving notebooks"
@@ -1,2249 +1,2248 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2011 The IPython Development Team
2 // Copyright (C) 2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // Notebook
9 // Notebook
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13 "use strict";
13 "use strict";
14
14
15 var utils = IPython.utils;
15 var utils = IPython.utils;
16 var key = IPython.utils.keycodes;
16 var key = IPython.utils.keycodes;
17
17
18 /**
18 /**
19 * A notebook contains and manages cells.
19 * A notebook contains and manages cells.
20 *
20 *
21 * @class Notebook
21 * @class Notebook
22 * @constructor
22 * @constructor
23 * @param {String} selector A jQuery selector for the notebook's DOM element
23 * @param {String} selector A jQuery selector for the notebook's DOM element
24 * @param {Object} [options] A config object
24 * @param {Object} [options] A config object
25 */
25 */
26 var Notebook = function (selector, options) {
26 var Notebook = function (selector, options) {
27 var options = options || {};
27 var options = options || {};
28 this._baseProjectUrl = options.baseProjectUrl;
28 this._baseProjectUrl = options.baseProjectUrl;
29 this.notebook_path = options.notebookPath;
29 this.notebook_path = options.notebookPath;
30 this.notebook_name = options.notebookName;
30 this.notebook_name = options.notebookName;
31 this.element = $(selector);
31 this.element = $(selector);
32 this.element.scroll();
32 this.element.scroll();
33 this.element.data("notebook", this);
33 this.element.data("notebook", this);
34 this.next_prompt_number = 1;
34 this.next_prompt_number = 1;
35 this.session = null;
35 this.session = null;
36 this.clipboard = null;
36 this.clipboard = null;
37 this.undelete_backup = null;
37 this.undelete_backup = null;
38 this.undelete_index = null;
38 this.undelete_index = null;
39 this.undelete_below = false;
39 this.undelete_below = false;
40 this.paste_enabled = false;
40 this.paste_enabled = false;
41 this.set_dirty(false);
41 this.set_dirty(false);
42 this.metadata = {};
42 this.metadata = {};
43 this._checkpoint_after_save = false;
43 this._checkpoint_after_save = false;
44 this.last_checkpoint = null;
44 this.last_checkpoint = null;
45 this.checkpoints = [];
45 this.checkpoints = [];
46 this.autosave_interval = 0;
46 this.autosave_interval = 0;
47 this.autosave_timer = null;
47 this.autosave_timer = null;
48 // autosave *at most* every two minutes
48 // autosave *at most* every two minutes
49 this.minimum_autosave_interval = 120000;
49 this.minimum_autosave_interval = 120000;
50 // single worksheet for now
50 // single worksheet for now
51 this.worksheet_metadata = {};
51 this.worksheet_metadata = {};
52 this.control_key_active = false;
52 this.control_key_active = false;
53 this.notebook_name_blacklist_re = /[\/\\:]/;
53 this.notebook_name_blacklist_re = /[\/\\:]/;
54 this.nbformat = 3 // Increment this when changing the nbformat
54 this.nbformat = 3 // Increment this when changing the nbformat
55 this.nbformat_minor = 0 // Increment this when changing the nbformat
55 this.nbformat_minor = 0 // Increment this when changing the nbformat
56 this.style();
56 this.style();
57 this.create_elements();
57 this.create_elements();
58 this.bind_events();
58 this.bind_events();
59 };
59 };
60
60
61 /**
61 /**
62 * Tweak the notebook's CSS style.
62 * Tweak the notebook's CSS style.
63 *
63 *
64 * @method style
64 * @method style
65 */
65 */
66 Notebook.prototype.style = function () {
66 Notebook.prototype.style = function () {
67 $('div#notebook').addClass('border-box-sizing');
67 $('div#notebook').addClass('border-box-sizing');
68 };
68 };
69
69
70 /**
70 /**
71 * Get the root URL of the notebook server.
71 * Get the root URL of the notebook server.
72 *
72 *
73 * @method baseProjectUrl
73 * @method baseProjectUrl
74 * @return {String} The base project URL
74 * @return {String} The base project URL
75 */
75 */
76 Notebook.prototype.baseProjectUrl = function(){
76 Notebook.prototype.baseProjectUrl = function(){
77 return this._baseProjectUrl || $('body').data('baseProjectUrl');
77 return this._baseProjectUrl || $('body').data('baseProjectUrl');
78 };
78 };
79
79
80 Notebook.prototype.notebookName = function() {
80 Notebook.prototype.notebookName = function() {
81 var name = $('body').data('notebookName');
81 var name = $('body').data('notebookName');
82 name = decodeURIComponent(name);
82 name = decodeURIComponent(name);
83 return name;
83 return name;
84 };
84 };
85
85
86 Notebook.prototype.notebookPath = function() {
86 Notebook.prototype.notebookPath = function() {
87 var path = $('body').data('notebookPath');
87 var path = $('body').data('notebookPath');
88 path = decodeURIComponent(path);
88 path = decodeURIComponent(path);
89 return path
89 return path
90 };
90 };
91
91
92 /**
92 /**
93 * Create an HTML and CSS representation of the notebook.
93 * Create an HTML and CSS representation of the notebook.
94 *
94 *
95 * @method create_elements
95 * @method create_elements
96 */
96 */
97 Notebook.prototype.create_elements = function () {
97 Notebook.prototype.create_elements = function () {
98 // We add this end_space div to the end of the notebook div to:
98 // We add this end_space div to the end of the notebook div to:
99 // i) provide a margin between the last cell and the end of the notebook
99 // i) provide a margin between the last cell and the end of the notebook
100 // ii) to prevent the div from scrolling up when the last cell is being
100 // ii) to prevent the div from scrolling up when the last cell is being
101 // edited, but is too low on the page, which browsers will do automatically.
101 // edited, but is too low on the page, which browsers will do automatically.
102 var that = this;
102 var that = this;
103 this.container = $("<div/>").addClass("container").attr("id", "notebook-container");
103 this.container = $("<div/>").addClass("container").attr("id", "notebook-container");
104 var end_space = $('<div/>').addClass('end_space');
104 var end_space = $('<div/>').addClass('end_space');
105 end_space.dblclick(function (e) {
105 end_space.dblclick(function (e) {
106 var ncells = that.ncells();
106 var ncells = that.ncells();
107 that.insert_cell_below('code',ncells-1);
107 that.insert_cell_below('code',ncells-1);
108 });
108 });
109 this.element.append(this.container);
109 this.element.append(this.container);
110 this.container.append(end_space);
110 this.container.append(end_space);
111 $('div#notebook').addClass('border-box-sizing');
111 $('div#notebook').addClass('border-box-sizing');
112 };
112 };
113
113
114 /**
114 /**
115 * Bind JavaScript events: key presses and custom IPython events.
115 * Bind JavaScript events: key presses and custom IPython events.
116 *
116 *
117 * @method bind_events
117 * @method bind_events
118 */
118 */
119 Notebook.prototype.bind_events = function () {
119 Notebook.prototype.bind_events = function () {
120 var that = this;
120 var that = this;
121
121
122 $([IPython.events]).on('set_next_input.Notebook', function (event, data) {
122 $([IPython.events]).on('set_next_input.Notebook', function (event, data) {
123 var index = that.find_cell_index(data.cell);
123 var index = that.find_cell_index(data.cell);
124 var new_cell = that.insert_cell_below('code',index);
124 var new_cell = that.insert_cell_below('code',index);
125 new_cell.set_text(data.text);
125 new_cell.set_text(data.text);
126 that.dirty = true;
126 that.dirty = true;
127 });
127 });
128
128
129 $([IPython.events]).on('set_dirty.Notebook', function (event, data) {
129 $([IPython.events]).on('set_dirty.Notebook', function (event, data) {
130 that.dirty = data.value;
130 that.dirty = data.value;
131 });
131 });
132
132
133 $([IPython.events]).on('select.Cell', function (event, data) {
133 $([IPython.events]).on('select.Cell', function (event, data) {
134 var index = that.find_cell_index(data.cell);
134 var index = that.find_cell_index(data.cell);
135 that.select(index);
135 that.select(index);
136 });
136 });
137
137
138 $([IPython.events]).on('status_autorestarting.Kernel', function () {
138 $([IPython.events]).on('status_autorestarting.Kernel', function () {
139 IPython.dialog.modal({
139 IPython.dialog.modal({
140 title: "Kernel Restarting",
140 title: "Kernel Restarting",
141 body: "The kernel appears to have died. It will restart automatically.",
141 body: "The kernel appears to have died. It will restart automatically.",
142 buttons: {
142 buttons: {
143 OK : {
143 OK : {
144 class : "btn-primary"
144 class : "btn-primary"
145 }
145 }
146 }
146 }
147 });
147 });
148 });
148 });
149
149
150
150
151 $(document).keydown(function (event) {
151 $(document).keydown(function (event) {
152
152
153 // Save (CTRL+S) or (AppleKey+S)
153 // Save (CTRL+S) or (AppleKey+S)
154 //metaKey = applekey on mac
154 //metaKey = applekey on mac
155 if ((event.ctrlKey || event.metaKey) && event.keyCode==83) {
155 if ((event.ctrlKey || event.metaKey) && event.keyCode==83) {
156 that.save_checkpoint();
156 that.save_checkpoint();
157 event.preventDefault();
157 event.preventDefault();
158 return false;
158 return false;
159 } else if (event.which === key.ESC) {
159 } else if (event.which === key.ESC) {
160 // Intercept escape at highest level to avoid closing
160 // Intercept escape at highest level to avoid closing
161 // websocket connection with firefox
161 // websocket connection with firefox
162 IPython.pager.collapse();
162 IPython.pager.collapse();
163 event.preventDefault();
163 event.preventDefault();
164 } else if (event.which === key.SHIFT) {
164 } else if (event.which === key.SHIFT) {
165 // ignore shift keydown
165 // ignore shift keydown
166 return true;
166 return true;
167 }
167 }
168 if (event.which === key.UPARROW && !event.shiftKey) {
168 if (event.which === key.UPARROW && !event.shiftKey) {
169 var cell = that.get_selected_cell();
169 var cell = that.get_selected_cell();
170 if (cell && cell.at_top()) {
170 if (cell && cell.at_top()) {
171 event.preventDefault();
171 event.preventDefault();
172 that.select_prev();
172 that.select_prev();
173 };
173 };
174 } else if (event.which === key.DOWNARROW && !event.shiftKey) {
174 } else if (event.which === key.DOWNARROW && !event.shiftKey) {
175 var cell = that.get_selected_cell();
175 var cell = that.get_selected_cell();
176 if (cell && cell.at_bottom()) {
176 if (cell && cell.at_bottom()) {
177 event.preventDefault();
177 event.preventDefault();
178 that.select_next();
178 that.select_next();
179 };
179 };
180 } else if (event.which === key.ENTER && event.shiftKey) {
180 } else if (event.which === key.ENTER && event.shiftKey) {
181 that.execute_selected_cell();
181 that.execute_selected_cell();
182 return false;
182 return false;
183 } else if (event.which === key.ENTER && event.altKey) {
183 } else if (event.which === key.ENTER && event.altKey) {
184 // Execute code cell, and insert new in place
184 // Execute code cell, and insert new in place
185 that.execute_selected_cell();
185 that.execute_selected_cell();
186 // Only insert a new cell, if we ended up in an already populated cell
186 // Only insert a new cell, if we ended up in an already populated cell
187 if (/\S/.test(that.get_selected_cell().get_text()) == true) {
187 if (/\S/.test(that.get_selected_cell().get_text()) == true) {
188 that.insert_cell_above('code');
188 that.insert_cell_above('code');
189 }
189 }
190 return false;
190 return false;
191 } else if (event.which === key.ENTER && event.ctrlKey) {
191 } else if (event.which === key.ENTER && event.ctrlKey) {
192 that.execute_selected_cell({terminal:true});
192 that.execute_selected_cell({terminal:true});
193 return false;
193 return false;
194 } else if (event.which === 77 && event.ctrlKey && that.control_key_active == false) {
194 } else if (event.which === 77 && event.ctrlKey && that.control_key_active == false) {
195 that.control_key_active = true;
195 that.control_key_active = true;
196 return false;
196 return false;
197 } else if (event.which === 88 && that.control_key_active) {
197 } else if (event.which === 88 && that.control_key_active) {
198 // Cut selected cell = x
198 // Cut selected cell = x
199 that.cut_cell();
199 that.cut_cell();
200 that.control_key_active = false;
200 that.control_key_active = false;
201 return false;
201 return false;
202 } else if (event.which === 67 && that.control_key_active) {
202 } else if (event.which === 67 && that.control_key_active) {
203 // Copy selected cell = c
203 // Copy selected cell = c
204 that.copy_cell();
204 that.copy_cell();
205 that.control_key_active = false;
205 that.control_key_active = false;
206 return false;
206 return false;
207 } else if (event.which === 86 && that.control_key_active) {
207 } else if (event.which === 86 && that.control_key_active) {
208 // Paste below selected cell = v
208 // Paste below selected cell = v
209 that.paste_cell_below();
209 that.paste_cell_below();
210 that.control_key_active = false;
210 that.control_key_active = false;
211 return false;
211 return false;
212 } else if (event.which === 68 && that.control_key_active) {
212 } else if (event.which === 68 && that.control_key_active) {
213 // Delete selected cell = d
213 // Delete selected cell = d
214 that.delete_cell();
214 that.delete_cell();
215 that.control_key_active = false;
215 that.control_key_active = false;
216 return false;
216 return false;
217 } else if (event.which === 65 && that.control_key_active) {
217 } else if (event.which === 65 && that.control_key_active) {
218 // Insert code cell above selected = a
218 // Insert code cell above selected = a
219 that.insert_cell_above('code');
219 that.insert_cell_above('code');
220 that.control_key_active = false;
220 that.control_key_active = false;
221 return false;
221 return false;
222 } else if (event.which === 66 && that.control_key_active) {
222 } else if (event.which === 66 && that.control_key_active) {
223 // Insert code cell below selected = b
223 // Insert code cell below selected = b
224 that.insert_cell_below('code');
224 that.insert_cell_below('code');
225 that.control_key_active = false;
225 that.control_key_active = false;
226 return false;
226 return false;
227 } else if (event.which === 89 && that.control_key_active) {
227 } else if (event.which === 89 && that.control_key_active) {
228 // To code = y
228 // To code = y
229 that.to_code();
229 that.to_code();
230 that.control_key_active = false;
230 that.control_key_active = false;
231 return false;
231 return false;
232 } else if (event.which === 77 && that.control_key_active) {
232 } else if (event.which === 77 && that.control_key_active) {
233 // To markdown = m
233 // To markdown = m
234 that.to_markdown();
234 that.to_markdown();
235 that.control_key_active = false;
235 that.control_key_active = false;
236 return false;
236 return false;
237 } else if (event.which === 84 && that.control_key_active) {
237 } else if (event.which === 84 && that.control_key_active) {
238 // To Raw = t
238 // To Raw = t
239 that.to_raw();
239 that.to_raw();
240 that.control_key_active = false;
240 that.control_key_active = false;
241 return false;
241 return false;
242 } else if (event.which === 49 && that.control_key_active) {
242 } else if (event.which === 49 && that.control_key_active) {
243 // To Heading 1 = 1
243 // To Heading 1 = 1
244 that.to_heading(undefined, 1);
244 that.to_heading(undefined, 1);
245 that.control_key_active = false;
245 that.control_key_active = false;
246 return false;
246 return false;
247 } else if (event.which === 50 && that.control_key_active) {
247 } else if (event.which === 50 && that.control_key_active) {
248 // To Heading 2 = 2
248 // To Heading 2 = 2
249 that.to_heading(undefined, 2);
249 that.to_heading(undefined, 2);
250 that.control_key_active = false;
250 that.control_key_active = false;
251 return false;
251 return false;
252 } else if (event.which === 51 && that.control_key_active) {
252 } else if (event.which === 51 && that.control_key_active) {
253 // To Heading 3 = 3
253 // To Heading 3 = 3
254 that.to_heading(undefined, 3);
254 that.to_heading(undefined, 3);
255 that.control_key_active = false;
255 that.control_key_active = false;
256 return false;
256 return false;
257 } else if (event.which === 52 && that.control_key_active) {
257 } else if (event.which === 52 && that.control_key_active) {
258 // To Heading 4 = 4
258 // To Heading 4 = 4
259 that.to_heading(undefined, 4);
259 that.to_heading(undefined, 4);
260 that.control_key_active = false;
260 that.control_key_active = false;
261 return false;
261 return false;
262 } else if (event.which === 53 && that.control_key_active) {
262 } else if (event.which === 53 && that.control_key_active) {
263 // To Heading 5 = 5
263 // To Heading 5 = 5
264 that.to_heading(undefined, 5);
264 that.to_heading(undefined, 5);
265 that.control_key_active = false;
265 that.control_key_active = false;
266 return false;
266 return false;
267 } else if (event.which === 54 && that.control_key_active) {
267 } else if (event.which === 54 && that.control_key_active) {
268 // To Heading 6 = 6
268 // To Heading 6 = 6
269 that.to_heading(undefined, 6);
269 that.to_heading(undefined, 6);
270 that.control_key_active = false;
270 that.control_key_active = false;
271 return false;
271 return false;
272 } else if (event.which === 79 && that.control_key_active) {
272 } else if (event.which === 79 && that.control_key_active) {
273 // Toggle output = o
273 // Toggle output = o
274 if (event.shiftKey){
274 if (event.shiftKey){
275 that.toggle_output_scroll();
275 that.toggle_output_scroll();
276 } else {
276 } else {
277 that.toggle_output();
277 that.toggle_output();
278 }
278 }
279 that.control_key_active = false;
279 that.control_key_active = false;
280 return false;
280 return false;
281 } else if (event.which === 83 && that.control_key_active) {
281 } else if (event.which === 83 && that.control_key_active) {
282 // Save notebook = s
282 // Save notebook = s
283 that.save_checkpoint();
283 that.save_checkpoint();
284 that.control_key_active = false;
284 that.control_key_active = false;
285 return false;
285 return false;
286 } else if (event.which === 74 && that.control_key_active) {
286 } else if (event.which === 74 && that.control_key_active) {
287 // Move cell down = j
287 // Move cell down = j
288 that.move_cell_down();
288 that.move_cell_down();
289 that.control_key_active = false;
289 that.control_key_active = false;
290 return false;
290 return false;
291 } else if (event.which === 75 && that.control_key_active) {
291 } else if (event.which === 75 && that.control_key_active) {
292 // Move cell up = k
292 // Move cell up = k
293 that.move_cell_up();
293 that.move_cell_up();
294 that.control_key_active = false;
294 that.control_key_active = false;
295 return false;
295 return false;
296 } else if (event.which === 80 && that.control_key_active) {
296 } else if (event.which === 80 && that.control_key_active) {
297 // Select previous = p
297 // Select previous = p
298 that.select_prev();
298 that.select_prev();
299 that.control_key_active = false;
299 that.control_key_active = false;
300 return false;
300 return false;
301 } else if (event.which === 78 && that.control_key_active) {
301 } else if (event.which === 78 && that.control_key_active) {
302 // Select next = n
302 // Select next = n
303 that.select_next();
303 that.select_next();
304 that.control_key_active = false;
304 that.control_key_active = false;
305 return false;
305 return false;
306 } else if (event.which === 76 && that.control_key_active) {
306 } else if (event.which === 76 && that.control_key_active) {
307 // Toggle line numbers = l
307 // Toggle line numbers = l
308 that.cell_toggle_line_numbers();
308 that.cell_toggle_line_numbers();
309 that.control_key_active = false;
309 that.control_key_active = false;
310 return false;
310 return false;
311 } else if (event.which === 73 && that.control_key_active) {
311 } else if (event.which === 73 && that.control_key_active) {
312 // Interrupt kernel = i
312 // Interrupt kernel = i
313 that.session.interrupt_kernel();
313 that.session.interrupt_kernel();
314 that.control_key_active = false;
314 that.control_key_active = false;
315 return false;
315 return false;
316 } else if (event.which === 190 && that.control_key_active) {
316 } else if (event.which === 190 && that.control_key_active) {
317 // Restart kernel = . # matches qt console
317 // Restart kernel = . # matches qt console
318 that.restart_kernel();
318 that.restart_kernel();
319 that.control_key_active = false;
319 that.control_key_active = false;
320 return false;
320 return false;
321 } else if (event.which === 72 && that.control_key_active) {
321 } else if (event.which === 72 && that.control_key_active) {
322 // Show keyboard shortcuts = h
322 // Show keyboard shortcuts = h
323 IPython.quick_help.show_keyboard_shortcuts();
323 IPython.quick_help.show_keyboard_shortcuts();
324 that.control_key_active = false;
324 that.control_key_active = false;
325 return false;
325 return false;
326 } else if (event.which === 90 && that.control_key_active) {
326 } else if (event.which === 90 && that.control_key_active) {
327 // Undo last cell delete = z
327 // Undo last cell delete = z
328 that.undelete();
328 that.undelete();
329 that.control_key_active = false;
329 that.control_key_active = false;
330 return false;
330 return false;
331 } else if ((event.which === 189 || event.which === 173) &&
331 } else if ((event.which === 189 || event.which === 173) &&
332 that.control_key_active) {
332 that.control_key_active) {
333 // how fun! '-' is 189 in Chrome, but 173 in FF and Opera
333 // how fun! '-' is 189 in Chrome, but 173 in FF and Opera
334 // Split cell = -
334 // Split cell = -
335 that.split_cell();
335 that.split_cell();
336 that.control_key_active = false;
336 that.control_key_active = false;
337 return false;
337 return false;
338 } else if (that.control_key_active) {
338 } else if (that.control_key_active) {
339 that.control_key_active = false;
339 that.control_key_active = false;
340 return true;
340 return true;
341 }
341 }
342 return true;
342 return true;
343 });
343 });
344
344
345 var collapse_time = function(time){
345 var collapse_time = function(time){
346 var app_height = $('#ipython-main-app').height(); // content height
346 var app_height = $('#ipython-main-app').height(); // content height
347 var splitter_height = $('div#pager_splitter').outerHeight(true);
347 var splitter_height = $('div#pager_splitter').outerHeight(true);
348 var new_height = app_height - splitter_height;
348 var new_height = app_height - splitter_height;
349 that.element.animate({height : new_height + 'px'}, time);
349 that.element.animate({height : new_height + 'px'}, time);
350 }
350 }
351
351
352 this.element.bind('collapse_pager', function (event,extrap) {
352 this.element.bind('collapse_pager', function (event,extrap) {
353 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
353 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
354 collapse_time(time);
354 collapse_time(time);
355 });
355 });
356
356
357 var expand_time = function(time) {
357 var expand_time = function(time) {
358 var app_height = $('#ipython-main-app').height(); // content height
358 var app_height = $('#ipython-main-app').height(); // content height
359 var splitter_height = $('div#pager_splitter').outerHeight(true);
359 var splitter_height = $('div#pager_splitter').outerHeight(true);
360 var pager_height = $('div#pager').outerHeight(true);
360 var pager_height = $('div#pager').outerHeight(true);
361 var new_height = app_height - pager_height - splitter_height;
361 var new_height = app_height - pager_height - splitter_height;
362 that.element.animate({height : new_height + 'px'}, time);
362 that.element.animate({height : new_height + 'px'}, time);
363 }
363 }
364
364
365 this.element.bind('expand_pager', function (event, extrap) {
365 this.element.bind('expand_pager', function (event, extrap) {
366 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
366 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
367 expand_time(time);
367 expand_time(time);
368 });
368 });
369
369
370 // Firefox 22 broke $(window).on("beforeunload")
370 // Firefox 22 broke $(window).on("beforeunload")
371 // I'm not sure why or how.
371 // I'm not sure why or how.
372 window.onbeforeunload = function (e) {
372 window.onbeforeunload = function (e) {
373 // TODO: Make killing the kernel configurable.
373 // TODO: Make killing the kernel configurable.
374 var kill_kernel = false;
374 var kill_kernel = false;
375 if (kill_kernel) {
375 if (kill_kernel) {
376 that.session.kill_kernel();
376 that.session.kill_kernel();
377 }
377 }
378 // if we are autosaving, trigger an autosave on nav-away.
378 // if we are autosaving, trigger an autosave on nav-away.
379 // still warn, because if we don't the autosave may fail.
379 // still warn, because if we don't the autosave may fail.
380 if (that.dirty) {
380 if (that.dirty) {
381 if ( that.autosave_interval ) {
381 if ( that.autosave_interval ) {
382 // schedule autosave in a timeout
382 // schedule autosave in a timeout
383 // this gives you a chance to forcefully discard changes
383 // this gives you a chance to forcefully discard changes
384 // by reloading the page if you *really* want to.
384 // by reloading the page if you *really* want to.
385 // the timer doesn't start until you *dismiss* the dialog.
385 // the timer doesn't start until you *dismiss* the dialog.
386 setTimeout(function () {
386 setTimeout(function () {
387 if (that.dirty) {
387 if (that.dirty) {
388 that.save_notebook();
388 that.save_notebook();
389 }
389 }
390 }, 1000);
390 }, 1000);
391 return "Autosave in progress, latest changes may be lost.";
391 return "Autosave in progress, latest changes may be lost.";
392 } else {
392 } else {
393 return "Unsaved changes will be lost.";
393 return "Unsaved changes will be lost.";
394 }
394 }
395 };
395 };
396 // Null is the *only* return value that will make the browser not
396 // Null is the *only* return value that will make the browser not
397 // pop up the "don't leave" dialog.
397 // pop up the "don't leave" dialog.
398 return null;
398 return null;
399 };
399 };
400 };
400 };
401
401
402 /**
402 /**
403 * Set the dirty flag, and trigger the set_dirty.Notebook event
403 * Set the dirty flag, and trigger the set_dirty.Notebook event
404 *
404 *
405 * @method set_dirty
405 * @method set_dirty
406 */
406 */
407 Notebook.prototype.set_dirty = function (value) {
407 Notebook.prototype.set_dirty = function (value) {
408 if (value === undefined) {
408 if (value === undefined) {
409 value = true;
409 value = true;
410 }
410 }
411 if (this.dirty == value) {
411 if (this.dirty == value) {
412 return;
412 return;
413 }
413 }
414 $([IPython.events]).trigger('set_dirty.Notebook', {value: value});
414 $([IPython.events]).trigger('set_dirty.Notebook', {value: value});
415 };
415 };
416
416
417 /**
417 /**
418 * Scroll the top of the page to a given cell.
418 * Scroll the top of the page to a given cell.
419 *
419 *
420 * @method scroll_to_cell
420 * @method scroll_to_cell
421 * @param {Number} cell_number An index of the cell to view
421 * @param {Number} cell_number An index of the cell to view
422 * @param {Number} time Animation time in milliseconds
422 * @param {Number} time Animation time in milliseconds
423 * @return {Number} Pixel offset from the top of the container
423 * @return {Number} Pixel offset from the top of the container
424 */
424 */
425 Notebook.prototype.scroll_to_cell = function (cell_number, time) {
425 Notebook.prototype.scroll_to_cell = function (cell_number, time) {
426 var cells = this.get_cells();
426 var cells = this.get_cells();
427 var time = time || 0;
427 var time = time || 0;
428 cell_number = Math.min(cells.length-1,cell_number);
428 cell_number = Math.min(cells.length-1,cell_number);
429 cell_number = Math.max(0 ,cell_number);
429 cell_number = Math.max(0 ,cell_number);
430 var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ;
430 var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ;
431 this.element.animate({scrollTop:scroll_value}, time);
431 this.element.animate({scrollTop:scroll_value}, time);
432 return scroll_value;
432 return scroll_value;
433 };
433 };
434
434
435 /**
435 /**
436 * Scroll to the bottom of the page.
436 * Scroll to the bottom of the page.
437 *
437 *
438 * @method scroll_to_bottom
438 * @method scroll_to_bottom
439 */
439 */
440 Notebook.prototype.scroll_to_bottom = function () {
440 Notebook.prototype.scroll_to_bottom = function () {
441 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
441 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
442 };
442 };
443
443
444 /**
444 /**
445 * Scroll to the top of the page.
445 * Scroll to the top of the page.
446 *
446 *
447 * @method scroll_to_top
447 * @method scroll_to_top
448 */
448 */
449 Notebook.prototype.scroll_to_top = function () {
449 Notebook.prototype.scroll_to_top = function () {
450 this.element.animate({scrollTop:0}, 0);
450 this.element.animate({scrollTop:0}, 0);
451 };
451 };
452
452
453 // Edit Notebook metadata
453 // Edit Notebook metadata
454
454
455 Notebook.prototype.edit_metadata = function () {
455 Notebook.prototype.edit_metadata = function () {
456 var that = this;
456 var that = this;
457 IPython.dialog.edit_metadata(this.metadata, function (md) {
457 IPython.dialog.edit_metadata(this.metadata, function (md) {
458 that.metadata = md;
458 that.metadata = md;
459 }, 'Notebook');
459 }, 'Notebook');
460 };
460 };
461
461
462 // Cell indexing, retrieval, etc.
462 // Cell indexing, retrieval, etc.
463
463
464 /**
464 /**
465 * Get all cell elements in the notebook.
465 * Get all cell elements in the notebook.
466 *
466 *
467 * @method get_cell_elements
467 * @method get_cell_elements
468 * @return {jQuery} A selector of all cell elements
468 * @return {jQuery} A selector of all cell elements
469 */
469 */
470 Notebook.prototype.get_cell_elements = function () {
470 Notebook.prototype.get_cell_elements = function () {
471 return this.container.children("div.cell");
471 return this.container.children("div.cell");
472 };
472 };
473
473
474 /**
474 /**
475 * Get a particular cell element.
475 * Get a particular cell element.
476 *
476 *
477 * @method get_cell_element
477 * @method get_cell_element
478 * @param {Number} index An index of a cell to select
478 * @param {Number} index An index of a cell to select
479 * @return {jQuery} A selector of the given cell.
479 * @return {jQuery} A selector of the given cell.
480 */
480 */
481 Notebook.prototype.get_cell_element = function (index) {
481 Notebook.prototype.get_cell_element = function (index) {
482 var result = null;
482 var result = null;
483 var e = this.get_cell_elements().eq(index);
483 var e = this.get_cell_elements().eq(index);
484 if (e.length !== 0) {
484 if (e.length !== 0) {
485 result = e;
485 result = e;
486 }
486 }
487 return result;
487 return result;
488 };
488 };
489
489
490 /**
490 /**
491 * Count the cells in this notebook.
491 * Count the cells in this notebook.
492 *
492 *
493 * @method ncells
493 * @method ncells
494 * @return {Number} The number of cells in this notebook
494 * @return {Number} The number of cells in this notebook
495 */
495 */
496 Notebook.prototype.ncells = function () {
496 Notebook.prototype.ncells = function () {
497 return this.get_cell_elements().length;
497 return this.get_cell_elements().length;
498 };
498 };
499
499
500 /**
500 /**
501 * Get all Cell objects in this notebook.
501 * Get all Cell objects in this notebook.
502 *
502 *
503 * @method get_cells
503 * @method get_cells
504 * @return {Array} This notebook's Cell objects
504 * @return {Array} This notebook's Cell objects
505 */
505 */
506 // TODO: we are often calling cells as cells()[i], which we should optimize
506 // TODO: we are often calling cells as cells()[i], which we should optimize
507 // to cells(i) or a new method.
507 // to cells(i) or a new method.
508 Notebook.prototype.get_cells = function () {
508 Notebook.prototype.get_cells = function () {
509 return this.get_cell_elements().toArray().map(function (e) {
509 return this.get_cell_elements().toArray().map(function (e) {
510 return $(e).data("cell");
510 return $(e).data("cell");
511 });
511 });
512 };
512 };
513
513
514 /**
514 /**
515 * Get a Cell object from this notebook.
515 * Get a Cell object from this notebook.
516 *
516 *
517 * @method get_cell
517 * @method get_cell
518 * @param {Number} index An index of a cell to retrieve
518 * @param {Number} index An index of a cell to retrieve
519 * @return {Cell} A particular cell
519 * @return {Cell} A particular cell
520 */
520 */
521 Notebook.prototype.get_cell = function (index) {
521 Notebook.prototype.get_cell = function (index) {
522 var result = null;
522 var result = null;
523 var ce = this.get_cell_element(index);
523 var ce = this.get_cell_element(index);
524 if (ce !== null) {
524 if (ce !== null) {
525 result = ce.data('cell');
525 result = ce.data('cell');
526 }
526 }
527 return result;
527 return result;
528 }
528 }
529
529
530 /**
530 /**
531 * Get the cell below a given cell.
531 * Get the cell below a given cell.
532 *
532 *
533 * @method get_next_cell
533 * @method get_next_cell
534 * @param {Cell} cell The provided cell
534 * @param {Cell} cell The provided cell
535 * @return {Cell} The next cell
535 * @return {Cell} The next cell
536 */
536 */
537 Notebook.prototype.get_next_cell = function (cell) {
537 Notebook.prototype.get_next_cell = function (cell) {
538 var result = null;
538 var result = null;
539 var index = this.find_cell_index(cell);
539 var index = this.find_cell_index(cell);
540 if (this.is_valid_cell_index(index+1)) {
540 if (this.is_valid_cell_index(index+1)) {
541 result = this.get_cell(index+1);
541 result = this.get_cell(index+1);
542 }
542 }
543 return result;
543 return result;
544 }
544 }
545
545
546 /**
546 /**
547 * Get the cell above a given cell.
547 * Get the cell above a given cell.
548 *
548 *
549 * @method get_prev_cell
549 * @method get_prev_cell
550 * @param {Cell} cell The provided cell
550 * @param {Cell} cell The provided cell
551 * @return {Cell} The previous cell
551 * @return {Cell} The previous cell
552 */
552 */
553 Notebook.prototype.get_prev_cell = function (cell) {
553 Notebook.prototype.get_prev_cell = function (cell) {
554 // TODO: off-by-one
554 // TODO: off-by-one
555 // nb.get_prev_cell(nb.get_cell(1)) is null
555 // nb.get_prev_cell(nb.get_cell(1)) is null
556 var result = null;
556 var result = null;
557 var index = this.find_cell_index(cell);
557 var index = this.find_cell_index(cell);
558 if (index !== null && index > 1) {
558 if (index !== null && index > 1) {
559 result = this.get_cell(index-1);
559 result = this.get_cell(index-1);
560 }
560 }
561 return result;
561 return result;
562 }
562 }
563
563
564 /**
564 /**
565 * Get the numeric index of a given cell.
565 * Get the numeric index of a given cell.
566 *
566 *
567 * @method find_cell_index
567 * @method find_cell_index
568 * @param {Cell} cell The provided cell
568 * @param {Cell} cell The provided cell
569 * @return {Number} The cell's numeric index
569 * @return {Number} The cell's numeric index
570 */
570 */
571 Notebook.prototype.find_cell_index = function (cell) {
571 Notebook.prototype.find_cell_index = function (cell) {
572 var result = null;
572 var result = null;
573 this.get_cell_elements().filter(function (index) {
573 this.get_cell_elements().filter(function (index) {
574 if ($(this).data("cell") === cell) {
574 if ($(this).data("cell") === cell) {
575 result = index;
575 result = index;
576 };
576 };
577 });
577 });
578 return result;
578 return result;
579 };
579 };
580
580
581 /**
581 /**
582 * Get a given index , or the selected index if none is provided.
582 * Get a given index , or the selected index if none is provided.
583 *
583 *
584 * @method index_or_selected
584 * @method index_or_selected
585 * @param {Number} index A cell's index
585 * @param {Number} index A cell's index
586 * @return {Number} The given index, or selected index if none is provided.
586 * @return {Number} The given index, or selected index if none is provided.
587 */
587 */
588 Notebook.prototype.index_or_selected = function (index) {
588 Notebook.prototype.index_or_selected = function (index) {
589 var i;
589 var i;
590 if (index === undefined || index === null) {
590 if (index === undefined || index === null) {
591 i = this.get_selected_index();
591 i = this.get_selected_index();
592 if (i === null) {
592 if (i === null) {
593 i = 0;
593 i = 0;
594 }
594 }
595 } else {
595 } else {
596 i = index;
596 i = index;
597 }
597 }
598 return i;
598 return i;
599 };
599 };
600
600
601 /**
601 /**
602 * Get the currently selected cell.
602 * Get the currently selected cell.
603 * @method get_selected_cell
603 * @method get_selected_cell
604 * @return {Cell} The selected cell
604 * @return {Cell} The selected cell
605 */
605 */
606 Notebook.prototype.get_selected_cell = function () {
606 Notebook.prototype.get_selected_cell = function () {
607 var index = this.get_selected_index();
607 var index = this.get_selected_index();
608 return this.get_cell(index);
608 return this.get_cell(index);
609 };
609 };
610
610
611 /**
611 /**
612 * Check whether a cell index is valid.
612 * Check whether a cell index is valid.
613 *
613 *
614 * @method is_valid_cell_index
614 * @method is_valid_cell_index
615 * @param {Number} index A cell index
615 * @param {Number} index A cell index
616 * @return True if the index is valid, false otherwise
616 * @return True if the index is valid, false otherwise
617 */
617 */
618 Notebook.prototype.is_valid_cell_index = function (index) {
618 Notebook.prototype.is_valid_cell_index = function (index) {
619 if (index !== null && index >= 0 && index < this.ncells()) {
619 if (index !== null && index >= 0 && index < this.ncells()) {
620 return true;
620 return true;
621 } else {
621 } else {
622 return false;
622 return false;
623 };
623 };
624 }
624 }
625
625
626 /**
626 /**
627 * Get the index of the currently selected cell.
627 * Get the index of the currently selected cell.
628
628
629 * @method get_selected_index
629 * @method get_selected_index
630 * @return {Number} The selected cell's numeric index
630 * @return {Number} The selected cell's numeric index
631 */
631 */
632 Notebook.prototype.get_selected_index = function () {
632 Notebook.prototype.get_selected_index = function () {
633 var result = null;
633 var result = null;
634 this.get_cell_elements().filter(function (index) {
634 this.get_cell_elements().filter(function (index) {
635 if ($(this).data("cell").selected === true) {
635 if ($(this).data("cell").selected === true) {
636 result = index;
636 result = index;
637 };
637 };
638 });
638 });
639 return result;
639 return result;
640 };
640 };
641
641
642
642
643 // Cell selection.
643 // Cell selection.
644
644
645 /**
645 /**
646 * Programmatically select a cell.
646 * Programmatically select a cell.
647 *
647 *
648 * @method select
648 * @method select
649 * @param {Number} index A cell's index
649 * @param {Number} index A cell's index
650 * @return {Notebook} This notebook
650 * @return {Notebook} This notebook
651 */
651 */
652 Notebook.prototype.select = function (index) {
652 Notebook.prototype.select = function (index) {
653 if (this.is_valid_cell_index(index)) {
653 if (this.is_valid_cell_index(index)) {
654 var sindex = this.get_selected_index()
654 var sindex = this.get_selected_index()
655 if (sindex !== null && index !== sindex) {
655 if (sindex !== null && index !== sindex) {
656 this.get_cell(sindex).unselect();
656 this.get_cell(sindex).unselect();
657 };
657 };
658 var cell = this.get_cell(index);
658 var cell = this.get_cell(index);
659 cell.select();
659 cell.select();
660 if (cell.cell_type === 'heading') {
660 if (cell.cell_type === 'heading') {
661 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
661 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
662 {'cell_type':cell.cell_type,level:cell.level}
662 {'cell_type':cell.cell_type,level:cell.level}
663 );
663 );
664 } else {
664 } else {
665 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
665 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
666 {'cell_type':cell.cell_type}
666 {'cell_type':cell.cell_type}
667 );
667 );
668 };
668 };
669 };
669 };
670 return this;
670 return this;
671 };
671 };
672
672
673 /**
673 /**
674 * Programmatically select the next cell.
674 * Programmatically select the next cell.
675 *
675 *
676 * @method select_next
676 * @method select_next
677 * @return {Notebook} This notebook
677 * @return {Notebook} This notebook
678 */
678 */
679 Notebook.prototype.select_next = function () {
679 Notebook.prototype.select_next = function () {
680 var index = this.get_selected_index();
680 var index = this.get_selected_index();
681 this.select(index+1);
681 this.select(index+1);
682 return this;
682 return this;
683 };
683 };
684
684
685 /**
685 /**
686 * Programmatically select the previous cell.
686 * Programmatically select the previous cell.
687 *
687 *
688 * @method select_prev
688 * @method select_prev
689 * @return {Notebook} This notebook
689 * @return {Notebook} This notebook
690 */
690 */
691 Notebook.prototype.select_prev = function () {
691 Notebook.prototype.select_prev = function () {
692 var index = this.get_selected_index();
692 var index = this.get_selected_index();
693 this.select(index-1);
693 this.select(index-1);
694 return this;
694 return this;
695 };
695 };
696
696
697
697
698 // Cell movement
698 // Cell movement
699
699
700 /**
700 /**
701 * Move given (or selected) cell up and select it.
701 * Move given (or selected) cell up and select it.
702 *
702 *
703 * @method move_cell_up
703 * @method move_cell_up
704 * @param [index] {integer} cell index
704 * @param [index] {integer} cell index
705 * @return {Notebook} This notebook
705 * @return {Notebook} This notebook
706 **/
706 **/
707 Notebook.prototype.move_cell_up = function (index) {
707 Notebook.prototype.move_cell_up = function (index) {
708 var i = this.index_or_selected(index);
708 var i = this.index_or_selected(index);
709 if (this.is_valid_cell_index(i) && i > 0) {
709 if (this.is_valid_cell_index(i) && i > 0) {
710 var pivot = this.get_cell_element(i-1);
710 var pivot = this.get_cell_element(i-1);
711 var tomove = this.get_cell_element(i);
711 var tomove = this.get_cell_element(i);
712 if (pivot !== null && tomove !== null) {
712 if (pivot !== null && tomove !== null) {
713 tomove.detach();
713 tomove.detach();
714 pivot.before(tomove);
714 pivot.before(tomove);
715 this.select(i-1);
715 this.select(i-1);
716 };
716 };
717 this.set_dirty(true);
717 this.set_dirty(true);
718 };
718 };
719 return this;
719 return this;
720 };
720 };
721
721
722
722
723 /**
723 /**
724 * Move given (or selected) cell down and select it
724 * Move given (or selected) cell down and select it
725 *
725 *
726 * @method move_cell_down
726 * @method move_cell_down
727 * @param [index] {integer} cell index
727 * @param [index] {integer} cell index
728 * @return {Notebook} This notebook
728 * @return {Notebook} This notebook
729 **/
729 **/
730 Notebook.prototype.move_cell_down = function (index) {
730 Notebook.prototype.move_cell_down = function (index) {
731 var i = this.index_or_selected(index);
731 var i = this.index_or_selected(index);
732 if ( this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) {
732 if ( this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) {
733 var pivot = this.get_cell_element(i+1);
733 var pivot = this.get_cell_element(i+1);
734 var tomove = this.get_cell_element(i);
734 var tomove = this.get_cell_element(i);
735 if (pivot !== null && tomove !== null) {
735 if (pivot !== null && tomove !== null) {
736 tomove.detach();
736 tomove.detach();
737 pivot.after(tomove);
737 pivot.after(tomove);
738 this.select(i+1);
738 this.select(i+1);
739 };
739 };
740 };
740 };
741 this.set_dirty();
741 this.set_dirty();
742 return this;
742 return this;
743 };
743 };
744
744
745
745
746 // Insertion, deletion.
746 // Insertion, deletion.
747
747
748 /**
748 /**
749 * Delete a cell from the notebook.
749 * Delete a cell from the notebook.
750 *
750 *
751 * @method delete_cell
751 * @method delete_cell
752 * @param [index] A cell's numeric index
752 * @param [index] A cell's numeric index
753 * @return {Notebook} This notebook
753 * @return {Notebook} This notebook
754 */
754 */
755 Notebook.prototype.delete_cell = function (index) {
755 Notebook.prototype.delete_cell = function (index) {
756 var i = this.index_or_selected(index);
756 var i = this.index_or_selected(index);
757 var cell = this.get_selected_cell();
757 var cell = this.get_selected_cell();
758 this.undelete_backup = cell.toJSON();
758 this.undelete_backup = cell.toJSON();
759 $('#undelete_cell').removeClass('ui-state-disabled');
759 $('#undelete_cell').removeClass('ui-state-disabled');
760 if (this.is_valid_cell_index(i)) {
760 if (this.is_valid_cell_index(i)) {
761 var ce = this.get_cell_element(i);
761 var ce = this.get_cell_element(i);
762 ce.remove();
762 ce.remove();
763 if (i === (this.ncells())) {
763 if (i === (this.ncells())) {
764 this.select(i-1);
764 this.select(i-1);
765 this.undelete_index = i - 1;
765 this.undelete_index = i - 1;
766 this.undelete_below = true;
766 this.undelete_below = true;
767 } else {
767 } else {
768 this.select(i);
768 this.select(i);
769 this.undelete_index = i;
769 this.undelete_index = i;
770 this.undelete_below = false;
770 this.undelete_below = false;
771 };
771 };
772 $([IPython.events]).trigger('delete.Cell', {'cell': cell, 'index': i});
772 $([IPython.events]).trigger('delete.Cell', {'cell': cell, 'index': i});
773 this.set_dirty(true);
773 this.set_dirty(true);
774 };
774 };
775 return this;
775 return this;
776 };
776 };
777
777
778 /**
778 /**
779 * Insert a cell so that after insertion the cell is at given index.
779 * Insert a cell so that after insertion the cell is at given index.
780 *
780 *
781 * Similar to insert_above, but index parameter is mandatory
781 * Similar to insert_above, but index parameter is mandatory
782 *
782 *
783 * Index will be brought back into the accissible range [0,n]
783 * Index will be brought back into the accissible range [0,n]
784 *
784 *
785 * @method insert_cell_at_index
785 * @method insert_cell_at_index
786 * @param type {string} in ['code','markdown','heading']
786 * @param type {string} in ['code','markdown','heading']
787 * @param [index] {int} a valid index where to inser cell
787 * @param [index] {int} a valid index where to inser cell
788 *
788 *
789 * @return cell {cell|null} created cell or null
789 * @return cell {cell|null} created cell or null
790 **/
790 **/
791 Notebook.prototype.insert_cell_at_index = function(type, index){
791 Notebook.prototype.insert_cell_at_index = function(type, index){
792
792
793 var ncells = this.ncells();
793 var ncells = this.ncells();
794 var index = Math.min(index,ncells);
794 var index = Math.min(index,ncells);
795 index = Math.max(index,0);
795 index = Math.max(index,0);
796 var cell = null;
796 var cell = null;
797
797
798 if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) {
798 if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) {
799 if (type === 'code') {
799 if (type === 'code') {
800 cell = new IPython.CodeCell(this.session);
800 cell = new IPython.CodeCell(this.session);
801 cell.set_input_prompt();
801 cell.set_input_prompt();
802 } else if (type === 'markdown') {
802 } else if (type === 'markdown') {
803 cell = new IPython.MarkdownCell();
803 cell = new IPython.MarkdownCell();
804 } else if (type === 'raw') {
804 } else if (type === 'raw') {
805 cell = new IPython.RawCell();
805 cell = new IPython.RawCell();
806 } else if (type === 'heading') {
806 } else if (type === 'heading') {
807 cell = new IPython.HeadingCell();
807 cell = new IPython.HeadingCell();
808 }
808 }
809
809
810 if(this._insert_element_at_index(cell.element,index)){
810 if(this._insert_element_at_index(cell.element,index)){
811 cell.render();
811 cell.render();
812 this.select(this.find_cell_index(cell));
812 this.select(this.find_cell_index(cell));
813 $([IPython.events]).trigger('create.Cell', {'cell': cell, 'index': index});
813 $([IPython.events]).trigger('create.Cell', {'cell': cell, 'index': index});
814 this.set_dirty(true);
814 this.set_dirty(true);
815 }
815 }
816 }
816 }
817 return cell;
817 return cell;
818
818
819 };
819 };
820
820
821 /**
821 /**
822 * Insert an element at given cell index.
822 * Insert an element at given cell index.
823 *
823 *
824 * @method _insert_element_at_index
824 * @method _insert_element_at_index
825 * @param element {dom element} a cell element
825 * @param element {dom element} a cell element
826 * @param [index] {int} a valid index where to inser cell
826 * @param [index] {int} a valid index where to inser cell
827 * @private
827 * @private
828 *
828 *
829 * return true if everything whent fine.
829 * return true if everything whent fine.
830 **/
830 **/
831 Notebook.prototype._insert_element_at_index = function(element, index){
831 Notebook.prototype._insert_element_at_index = function(element, index){
832 if (element === undefined){
832 if (element === undefined){
833 return false;
833 return false;
834 }
834 }
835
835
836 var ncells = this.ncells();
836 var ncells = this.ncells();
837
837
838 if (ncells === 0) {
838 if (ncells === 0) {
839 // special case append if empty
839 // special case append if empty
840 this.element.find('div.end_space').before(element);
840 this.element.find('div.end_space').before(element);
841 } else if ( ncells === index ) {
841 } else if ( ncells === index ) {
842 // special case append it the end, but not empty
842 // special case append it the end, but not empty
843 this.get_cell_element(index-1).after(element);
843 this.get_cell_element(index-1).after(element);
844 } else if (this.is_valid_cell_index(index)) {
844 } else if (this.is_valid_cell_index(index)) {
845 // otherwise always somewhere to append to
845 // otherwise always somewhere to append to
846 this.get_cell_element(index).before(element);
846 this.get_cell_element(index).before(element);
847 } else {
847 } else {
848 return false;
848 return false;
849 }
849 }
850
850
851 if (this.undelete_index !== null && index <= this.undelete_index) {
851 if (this.undelete_index !== null && index <= this.undelete_index) {
852 this.undelete_index = this.undelete_index + 1;
852 this.undelete_index = this.undelete_index + 1;
853 this.set_dirty(true);
853 this.set_dirty(true);
854 }
854 }
855 return true;
855 return true;
856 };
856 };
857
857
858 /**
858 /**
859 * Insert a cell of given type above given index, or at top
859 * Insert a cell of given type above given index, or at top
860 * of notebook if index smaller than 0.
860 * of notebook if index smaller than 0.
861 *
861 *
862 * default index value is the one of currently selected cell
862 * default index value is the one of currently selected cell
863 *
863 *
864 * @method insert_cell_above
864 * @method insert_cell_above
865 * @param type {string} cell type
865 * @param type {string} cell type
866 * @param [index] {integer}
866 * @param [index] {integer}
867 *
867 *
868 * @return handle to created cell or null
868 * @return handle to created cell or null
869 **/
869 **/
870 Notebook.prototype.insert_cell_above = function (type, index) {
870 Notebook.prototype.insert_cell_above = function (type, index) {
871 index = this.index_or_selected(index);
871 index = this.index_or_selected(index);
872 return this.insert_cell_at_index(type, index);
872 return this.insert_cell_at_index(type, index);
873 };
873 };
874
874
875 /**
875 /**
876 * Insert a cell of given type below given index, or at bottom
876 * Insert a cell of given type below given index, or at bottom
877 * of notebook if index greater thatn number of cell
877 * of notebook if index greater thatn number of cell
878 *
878 *
879 * default index value is the one of currently selected cell
879 * default index value is the one of currently selected cell
880 *
880 *
881 * @method insert_cell_below
881 * @method insert_cell_below
882 * @param type {string} cell type
882 * @param type {string} cell type
883 * @param [index] {integer}
883 * @param [index] {integer}
884 *
884 *
885 * @return handle to created cell or null
885 * @return handle to created cell or null
886 *
886 *
887 **/
887 **/
888 Notebook.prototype.insert_cell_below = function (type, index) {
888 Notebook.prototype.insert_cell_below = function (type, index) {
889 index = this.index_or_selected(index);
889 index = this.index_or_selected(index);
890 return this.insert_cell_at_index(type, index+1);
890 return this.insert_cell_at_index(type, index+1);
891 };
891 };
892
892
893
893
894 /**
894 /**
895 * Insert cell at end of notebook
895 * Insert cell at end of notebook
896 *
896 *
897 * @method insert_cell_at_bottom
897 * @method insert_cell_at_bottom
898 * @param {String} type cell type
898 * @param {String} type cell type
899 *
899 *
900 * @return the added cell; or null
900 * @return the added cell; or null
901 **/
901 **/
902 Notebook.prototype.insert_cell_at_bottom = function (type){
902 Notebook.prototype.insert_cell_at_bottom = function (type){
903 var len = this.ncells();
903 var len = this.ncells();
904 return this.insert_cell_below(type,len-1);
904 return this.insert_cell_below(type,len-1);
905 };
905 };
906
906
907 /**
907 /**
908 * Turn a cell into a code cell.
908 * Turn a cell into a code cell.
909 *
909 *
910 * @method to_code
910 * @method to_code
911 * @param {Number} [index] A cell's index
911 * @param {Number} [index] A cell's index
912 */
912 */
913 Notebook.prototype.to_code = function (index) {
913 Notebook.prototype.to_code = function (index) {
914 var i = this.index_or_selected(index);
914 var i = this.index_or_selected(index);
915 if (this.is_valid_cell_index(i)) {
915 if (this.is_valid_cell_index(i)) {
916 var source_element = this.get_cell_element(i);
916 var source_element = this.get_cell_element(i);
917 var source_cell = source_element.data("cell");
917 var source_cell = source_element.data("cell");
918 if (!(source_cell instanceof IPython.CodeCell)) {
918 if (!(source_cell instanceof IPython.CodeCell)) {
919 var target_cell = this.insert_cell_below('code',i);
919 var target_cell = this.insert_cell_below('code',i);
920 var text = source_cell.get_text();
920 var text = source_cell.get_text();
921 if (text === source_cell.placeholder) {
921 if (text === source_cell.placeholder) {
922 text = '';
922 text = '';
923 }
923 }
924 target_cell.set_text(text);
924 target_cell.set_text(text);
925 // make this value the starting point, so that we can only undo
925 // make this value the starting point, so that we can only undo
926 // to this state, instead of a blank cell
926 // to this state, instead of a blank cell
927 target_cell.code_mirror.clearHistory();
927 target_cell.code_mirror.clearHistory();
928 source_element.remove();
928 source_element.remove();
929 this.set_dirty(true);
929 this.set_dirty(true);
930 };
930 };
931 };
931 };
932 };
932 };
933
933
934 /**
934 /**
935 * Turn a cell into a Markdown cell.
935 * Turn a cell into a Markdown cell.
936 *
936 *
937 * @method to_markdown
937 * @method to_markdown
938 * @param {Number} [index] A cell's index
938 * @param {Number} [index] A cell's index
939 */
939 */
940 Notebook.prototype.to_markdown = function (index) {
940 Notebook.prototype.to_markdown = function (index) {
941 var i = this.index_or_selected(index);
941 var i = this.index_or_selected(index);
942 if (this.is_valid_cell_index(i)) {
942 if (this.is_valid_cell_index(i)) {
943 var source_element = this.get_cell_element(i);
943 var source_element = this.get_cell_element(i);
944 var source_cell = source_element.data("cell");
944 var source_cell = source_element.data("cell");
945 if (!(source_cell instanceof IPython.MarkdownCell)) {
945 if (!(source_cell instanceof IPython.MarkdownCell)) {
946 var target_cell = this.insert_cell_below('markdown',i);
946 var target_cell = this.insert_cell_below('markdown',i);
947 var text = source_cell.get_text();
947 var text = source_cell.get_text();
948 if (text === source_cell.placeholder) {
948 if (text === source_cell.placeholder) {
949 text = '';
949 text = '';
950 };
950 };
951 // The edit must come before the set_text.
951 // The edit must come before the set_text.
952 target_cell.edit();
952 target_cell.edit();
953 target_cell.set_text(text);
953 target_cell.set_text(text);
954 // make this value the starting point, so that we can only undo
954 // make this value the starting point, so that we can only undo
955 // to this state, instead of a blank cell
955 // to this state, instead of a blank cell
956 target_cell.code_mirror.clearHistory();
956 target_cell.code_mirror.clearHistory();
957 source_element.remove();
957 source_element.remove();
958 this.set_dirty(true);
958 this.set_dirty(true);
959 };
959 };
960 };
960 };
961 };
961 };
962
962
963 /**
963 /**
964 * Turn a cell into a raw text cell.
964 * Turn a cell into a raw text cell.
965 *
965 *
966 * @method to_raw
966 * @method to_raw
967 * @param {Number} [index] A cell's index
967 * @param {Number} [index] A cell's index
968 */
968 */
969 Notebook.prototype.to_raw = function (index) {
969 Notebook.prototype.to_raw = function (index) {
970 var i = this.index_or_selected(index);
970 var i = this.index_or_selected(index);
971 if (this.is_valid_cell_index(i)) {
971 if (this.is_valid_cell_index(i)) {
972 var source_element = this.get_cell_element(i);
972 var source_element = this.get_cell_element(i);
973 var source_cell = source_element.data("cell");
973 var source_cell = source_element.data("cell");
974 var target_cell = null;
974 var target_cell = null;
975 if (!(source_cell instanceof IPython.RawCell)) {
975 if (!(source_cell instanceof IPython.RawCell)) {
976 target_cell = this.insert_cell_below('raw',i);
976 target_cell = this.insert_cell_below('raw',i);
977 var text = source_cell.get_text();
977 var text = source_cell.get_text();
978 if (text === source_cell.placeholder) {
978 if (text === source_cell.placeholder) {
979 text = '';
979 text = '';
980 };
980 };
981 // The edit must come before the set_text.
981 // The edit must come before the set_text.
982 target_cell.edit();
982 target_cell.edit();
983 target_cell.set_text(text);
983 target_cell.set_text(text);
984 // make this value the starting point, so that we can only undo
984 // make this value the starting point, so that we can only undo
985 // to this state, instead of a blank cell
985 // to this state, instead of a blank cell
986 target_cell.code_mirror.clearHistory();
986 target_cell.code_mirror.clearHistory();
987 source_element.remove();
987 source_element.remove();
988 this.set_dirty(true);
988 this.set_dirty(true);
989 };
989 };
990 };
990 };
991 };
991 };
992
992
993 /**
993 /**
994 * Turn a cell into a heading cell.
994 * Turn a cell into a heading cell.
995 *
995 *
996 * @method to_heading
996 * @method to_heading
997 * @param {Number} [index] A cell's index
997 * @param {Number} [index] A cell's index
998 * @param {Number} [level] A heading level (e.g., 1 becomes &lt;h1&gt;)
998 * @param {Number} [level] A heading level (e.g., 1 becomes &lt;h1&gt;)
999 */
999 */
1000 Notebook.prototype.to_heading = function (index, level) {
1000 Notebook.prototype.to_heading = function (index, level) {
1001 level = level || 1;
1001 level = level || 1;
1002 var i = this.index_or_selected(index);
1002 var i = this.index_or_selected(index);
1003 if (this.is_valid_cell_index(i)) {
1003 if (this.is_valid_cell_index(i)) {
1004 var source_element = this.get_cell_element(i);
1004 var source_element = this.get_cell_element(i);
1005 var source_cell = source_element.data("cell");
1005 var source_cell = source_element.data("cell");
1006 var target_cell = null;
1006 var target_cell = null;
1007 if (source_cell instanceof IPython.HeadingCell) {
1007 if (source_cell instanceof IPython.HeadingCell) {
1008 source_cell.set_level(level);
1008 source_cell.set_level(level);
1009 } else {
1009 } else {
1010 target_cell = this.insert_cell_below('heading',i);
1010 target_cell = this.insert_cell_below('heading',i);
1011 var text = source_cell.get_text();
1011 var text = source_cell.get_text();
1012 if (text === source_cell.placeholder) {
1012 if (text === source_cell.placeholder) {
1013 text = '';
1013 text = '';
1014 };
1014 };
1015 // The edit must come before the set_text.
1015 // The edit must come before the set_text.
1016 target_cell.set_level(level);
1016 target_cell.set_level(level);
1017 target_cell.edit();
1017 target_cell.edit();
1018 target_cell.set_text(text);
1018 target_cell.set_text(text);
1019 // make this value the starting point, so that we can only undo
1019 // make this value the starting point, so that we can only undo
1020 // to this state, instead of a blank cell
1020 // to this state, instead of a blank cell
1021 target_cell.code_mirror.clearHistory();
1021 target_cell.code_mirror.clearHistory();
1022 source_element.remove();
1022 source_element.remove();
1023 this.set_dirty(true);
1023 this.set_dirty(true);
1024 };
1024 };
1025 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
1025 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
1026 {'cell_type':'heading',level:level}
1026 {'cell_type':'heading',level:level}
1027 );
1027 );
1028 };
1028 };
1029 };
1029 };
1030
1030
1031
1031
1032 // Cut/Copy/Paste
1032 // Cut/Copy/Paste
1033
1033
1034 /**
1034 /**
1035 * Enable UI elements for pasting cells.
1035 * Enable UI elements for pasting cells.
1036 *
1036 *
1037 * @method enable_paste
1037 * @method enable_paste
1038 */
1038 */
1039 Notebook.prototype.enable_paste = function () {
1039 Notebook.prototype.enable_paste = function () {
1040 var that = this;
1040 var that = this;
1041 if (!this.paste_enabled) {
1041 if (!this.paste_enabled) {
1042 $('#paste_cell_replace').removeClass('ui-state-disabled')
1042 $('#paste_cell_replace').removeClass('ui-state-disabled')
1043 .on('click', function () {that.paste_cell_replace();});
1043 .on('click', function () {that.paste_cell_replace();});
1044 $('#paste_cell_above').removeClass('ui-state-disabled')
1044 $('#paste_cell_above').removeClass('ui-state-disabled')
1045 .on('click', function () {that.paste_cell_above();});
1045 .on('click', function () {that.paste_cell_above();});
1046 $('#paste_cell_below').removeClass('ui-state-disabled')
1046 $('#paste_cell_below').removeClass('ui-state-disabled')
1047 .on('click', function () {that.paste_cell_below();});
1047 .on('click', function () {that.paste_cell_below();});
1048 this.paste_enabled = true;
1048 this.paste_enabled = true;
1049 };
1049 };
1050 };
1050 };
1051
1051
1052 /**
1052 /**
1053 * Disable UI elements for pasting cells.
1053 * Disable UI elements for pasting cells.
1054 *
1054 *
1055 * @method disable_paste
1055 * @method disable_paste
1056 */
1056 */
1057 Notebook.prototype.disable_paste = function () {
1057 Notebook.prototype.disable_paste = function () {
1058 if (this.paste_enabled) {
1058 if (this.paste_enabled) {
1059 $('#paste_cell_replace').addClass('ui-state-disabled').off('click');
1059 $('#paste_cell_replace').addClass('ui-state-disabled').off('click');
1060 $('#paste_cell_above').addClass('ui-state-disabled').off('click');
1060 $('#paste_cell_above').addClass('ui-state-disabled').off('click');
1061 $('#paste_cell_below').addClass('ui-state-disabled').off('click');
1061 $('#paste_cell_below').addClass('ui-state-disabled').off('click');
1062 this.paste_enabled = false;
1062 this.paste_enabled = false;
1063 };
1063 };
1064 };
1064 };
1065
1065
1066 /**
1066 /**
1067 * Cut a cell.
1067 * Cut a cell.
1068 *
1068 *
1069 * @method cut_cell
1069 * @method cut_cell
1070 */
1070 */
1071 Notebook.prototype.cut_cell = function () {
1071 Notebook.prototype.cut_cell = function () {
1072 this.copy_cell();
1072 this.copy_cell();
1073 this.delete_cell();
1073 this.delete_cell();
1074 }
1074 }
1075
1075
1076 /**
1076 /**
1077 * Copy a cell.
1077 * Copy a cell.
1078 *
1078 *
1079 * @method copy_cell
1079 * @method copy_cell
1080 */
1080 */
1081 Notebook.prototype.copy_cell = function () {
1081 Notebook.prototype.copy_cell = function () {
1082 var cell = this.get_selected_cell();
1082 var cell = this.get_selected_cell();
1083 this.clipboard = cell.toJSON();
1083 this.clipboard = cell.toJSON();
1084 this.enable_paste();
1084 this.enable_paste();
1085 };
1085 };
1086
1086
1087 /**
1087 /**
1088 * Replace the selected cell with a cell in the clipboard.
1088 * Replace the selected cell with a cell in the clipboard.
1089 *
1089 *
1090 * @method paste_cell_replace
1090 * @method paste_cell_replace
1091 */
1091 */
1092 Notebook.prototype.paste_cell_replace = function () {
1092 Notebook.prototype.paste_cell_replace = function () {
1093 if (this.clipboard !== null && this.paste_enabled) {
1093 if (this.clipboard !== null && this.paste_enabled) {
1094 var cell_data = this.clipboard;
1094 var cell_data = this.clipboard;
1095 var new_cell = this.insert_cell_above(cell_data.cell_type);
1095 var new_cell = this.insert_cell_above(cell_data.cell_type);
1096 new_cell.fromJSON(cell_data);
1096 new_cell.fromJSON(cell_data);
1097 var old_cell = this.get_next_cell(new_cell);
1097 var old_cell = this.get_next_cell(new_cell);
1098 this.delete_cell(this.find_cell_index(old_cell));
1098 this.delete_cell(this.find_cell_index(old_cell));
1099 this.select(this.find_cell_index(new_cell));
1099 this.select(this.find_cell_index(new_cell));
1100 };
1100 };
1101 };
1101 };
1102
1102
1103 /**
1103 /**
1104 * Paste a cell from the clipboard above the selected cell.
1104 * Paste a cell from the clipboard above the selected cell.
1105 *
1105 *
1106 * @method paste_cell_above
1106 * @method paste_cell_above
1107 */
1107 */
1108 Notebook.prototype.paste_cell_above = function () {
1108 Notebook.prototype.paste_cell_above = function () {
1109 if (this.clipboard !== null && this.paste_enabled) {
1109 if (this.clipboard !== null && this.paste_enabled) {
1110 var cell_data = this.clipboard;
1110 var cell_data = this.clipboard;
1111 var new_cell = this.insert_cell_above(cell_data.cell_type);
1111 var new_cell = this.insert_cell_above(cell_data.cell_type);
1112 new_cell.fromJSON(cell_data);
1112 new_cell.fromJSON(cell_data);
1113 };
1113 };
1114 };
1114 };
1115
1115
1116 /**
1116 /**
1117 * Paste a cell from the clipboard below the selected cell.
1117 * Paste a cell from the clipboard below the selected cell.
1118 *
1118 *
1119 * @method paste_cell_below
1119 * @method paste_cell_below
1120 */
1120 */
1121 Notebook.prototype.paste_cell_below = function () {
1121 Notebook.prototype.paste_cell_below = function () {
1122 if (this.clipboard !== null && this.paste_enabled) {
1122 if (this.clipboard !== null && this.paste_enabled) {
1123 var cell_data = this.clipboard;
1123 var cell_data = this.clipboard;
1124 var new_cell = this.insert_cell_below(cell_data.cell_type);
1124 var new_cell = this.insert_cell_below(cell_data.cell_type);
1125 new_cell.fromJSON(cell_data);
1125 new_cell.fromJSON(cell_data);
1126 };
1126 };
1127 };
1127 };
1128
1128
1129 // Cell undelete
1129 // Cell undelete
1130
1130
1131 /**
1131 /**
1132 * Restore the most recently deleted cell.
1132 * Restore the most recently deleted cell.
1133 *
1133 *
1134 * @method undelete
1134 * @method undelete
1135 */
1135 */
1136 Notebook.prototype.undelete = function() {
1136 Notebook.prototype.undelete = function() {
1137 if (this.undelete_backup !== null && this.undelete_index !== null) {
1137 if (this.undelete_backup !== null && this.undelete_index !== null) {
1138 var current_index = this.get_selected_index();
1138 var current_index = this.get_selected_index();
1139 if (this.undelete_index < current_index) {
1139 if (this.undelete_index < current_index) {
1140 current_index = current_index + 1;
1140 current_index = current_index + 1;
1141 }
1141 }
1142 if (this.undelete_index >= this.ncells()) {
1142 if (this.undelete_index >= this.ncells()) {
1143 this.select(this.ncells() - 1);
1143 this.select(this.ncells() - 1);
1144 }
1144 }
1145 else {
1145 else {
1146 this.select(this.undelete_index);
1146 this.select(this.undelete_index);
1147 }
1147 }
1148 var cell_data = this.undelete_backup;
1148 var cell_data = this.undelete_backup;
1149 var new_cell = null;
1149 var new_cell = null;
1150 if (this.undelete_below) {
1150 if (this.undelete_below) {
1151 new_cell = this.insert_cell_below(cell_data.cell_type);
1151 new_cell = this.insert_cell_below(cell_data.cell_type);
1152 } else {
1152 } else {
1153 new_cell = this.insert_cell_above(cell_data.cell_type);
1153 new_cell = this.insert_cell_above(cell_data.cell_type);
1154 }
1154 }
1155 new_cell.fromJSON(cell_data);
1155 new_cell.fromJSON(cell_data);
1156 this.select(current_index);
1156 this.select(current_index);
1157 this.undelete_backup = null;
1157 this.undelete_backup = null;
1158 this.undelete_index = null;
1158 this.undelete_index = null;
1159 }
1159 }
1160 $('#undelete_cell').addClass('ui-state-disabled');
1160 $('#undelete_cell').addClass('ui-state-disabled');
1161 }
1161 }
1162
1162
1163 // Split/merge
1163 // Split/merge
1164
1164
1165 /**
1165 /**
1166 * Split the selected cell into two, at the cursor.
1166 * Split the selected cell into two, at the cursor.
1167 *
1167 *
1168 * @method split_cell
1168 * @method split_cell
1169 */
1169 */
1170 Notebook.prototype.split_cell = function () {
1170 Notebook.prototype.split_cell = function () {
1171 // Todo: implement spliting for other cell types.
1171 // Todo: implement spliting for other cell types.
1172 var cell = this.get_selected_cell();
1172 var cell = this.get_selected_cell();
1173 if (cell.is_splittable()) {
1173 if (cell.is_splittable()) {
1174 var texta = cell.get_pre_cursor();
1174 var texta = cell.get_pre_cursor();
1175 var textb = cell.get_post_cursor();
1175 var textb = cell.get_post_cursor();
1176 if (cell instanceof IPython.CodeCell) {
1176 if (cell instanceof IPython.CodeCell) {
1177 cell.set_text(texta);
1177 cell.set_text(texta);
1178 var new_cell = this.insert_cell_below('code');
1178 var new_cell = this.insert_cell_below('code');
1179 new_cell.set_text(textb);
1179 new_cell.set_text(textb);
1180 } else if (cell instanceof IPython.MarkdownCell) {
1180 } else if (cell instanceof IPython.MarkdownCell) {
1181 cell.set_text(texta);
1181 cell.set_text(texta);
1182 cell.render();
1182 cell.render();
1183 var new_cell = this.insert_cell_below('markdown');
1183 var new_cell = this.insert_cell_below('markdown');
1184 new_cell.edit(); // editor must be visible to call set_text
1184 new_cell.edit(); // editor must be visible to call set_text
1185 new_cell.set_text(textb);
1185 new_cell.set_text(textb);
1186 new_cell.render();
1186 new_cell.render();
1187 }
1187 }
1188 };
1188 };
1189 };
1189 };
1190
1190
1191 /**
1191 /**
1192 * Combine the selected cell into the cell above it.
1192 * Combine the selected cell into the cell above it.
1193 *
1193 *
1194 * @method merge_cell_above
1194 * @method merge_cell_above
1195 */
1195 */
1196 Notebook.prototype.merge_cell_above = function () {
1196 Notebook.prototype.merge_cell_above = function () {
1197 var index = this.get_selected_index();
1197 var index = this.get_selected_index();
1198 var cell = this.get_cell(index);
1198 var cell = this.get_cell(index);
1199 if (!cell.is_mergeable()) {
1199 if (!cell.is_mergeable()) {
1200 return;
1200 return;
1201 }
1201 }
1202 if (index > 0) {
1202 if (index > 0) {
1203 var upper_cell = this.get_cell(index-1);
1203 var upper_cell = this.get_cell(index-1);
1204 if (!upper_cell.is_mergeable()) {
1204 if (!upper_cell.is_mergeable()) {
1205 return;
1205 return;
1206 }
1206 }
1207 var upper_text = upper_cell.get_text();
1207 var upper_text = upper_cell.get_text();
1208 var text = cell.get_text();
1208 var text = cell.get_text();
1209 if (cell instanceof IPython.CodeCell) {
1209 if (cell instanceof IPython.CodeCell) {
1210 cell.set_text(upper_text+'\n'+text);
1210 cell.set_text(upper_text+'\n'+text);
1211 } else if (cell instanceof IPython.MarkdownCell) {
1211 } else if (cell instanceof IPython.MarkdownCell) {
1212 cell.edit();
1212 cell.edit();
1213 cell.set_text(upper_text+'\n'+text);
1213 cell.set_text(upper_text+'\n'+text);
1214 cell.render();
1214 cell.render();
1215 };
1215 };
1216 this.delete_cell(index-1);
1216 this.delete_cell(index-1);
1217 this.select(this.find_cell_index(cell));
1217 this.select(this.find_cell_index(cell));
1218 };
1218 };
1219 };
1219 };
1220
1220
1221 /**
1221 /**
1222 * Combine the selected cell into the cell below it.
1222 * Combine the selected cell into the cell below it.
1223 *
1223 *
1224 * @method merge_cell_below
1224 * @method merge_cell_below
1225 */
1225 */
1226 Notebook.prototype.merge_cell_below = function () {
1226 Notebook.prototype.merge_cell_below = function () {
1227 var index = this.get_selected_index();
1227 var index = this.get_selected_index();
1228 var cell = this.get_cell(index);
1228 var cell = this.get_cell(index);
1229 if (!cell.is_mergeable()) {
1229 if (!cell.is_mergeable()) {
1230 return;
1230 return;
1231 }
1231 }
1232 if (index < this.ncells()-1) {
1232 if (index < this.ncells()-1) {
1233 var lower_cell = this.get_cell(index+1);
1233 var lower_cell = this.get_cell(index+1);
1234 if (!lower_cell.is_mergeable()) {
1234 if (!lower_cell.is_mergeable()) {
1235 return;
1235 return;
1236 }
1236 }
1237 var lower_text = lower_cell.get_text();
1237 var lower_text = lower_cell.get_text();
1238 var text = cell.get_text();
1238 var text = cell.get_text();
1239 if (cell instanceof IPython.CodeCell) {
1239 if (cell instanceof IPython.CodeCell) {
1240 cell.set_text(text+'\n'+lower_text);
1240 cell.set_text(text+'\n'+lower_text);
1241 } else if (cell instanceof IPython.MarkdownCell) {
1241 } else if (cell instanceof IPython.MarkdownCell) {
1242 cell.edit();
1242 cell.edit();
1243 cell.set_text(text+'\n'+lower_text);
1243 cell.set_text(text+'\n'+lower_text);
1244 cell.render();
1244 cell.render();
1245 };
1245 };
1246 this.delete_cell(index+1);
1246 this.delete_cell(index+1);
1247 this.select(this.find_cell_index(cell));
1247 this.select(this.find_cell_index(cell));
1248 };
1248 };
1249 };
1249 };
1250
1250
1251
1251
1252 // Cell collapsing and output clearing
1252 // Cell collapsing and output clearing
1253
1253
1254 /**
1254 /**
1255 * Hide a cell's output.
1255 * Hide a cell's output.
1256 *
1256 *
1257 * @method collapse
1257 * @method collapse
1258 * @param {Number} index A cell's numeric index
1258 * @param {Number} index A cell's numeric index
1259 */
1259 */
1260 Notebook.prototype.collapse = function (index) {
1260 Notebook.prototype.collapse = function (index) {
1261 var i = this.index_or_selected(index);
1261 var i = this.index_or_selected(index);
1262 this.get_cell(i).collapse();
1262 this.get_cell(i).collapse();
1263 this.set_dirty(true);
1263 this.set_dirty(true);
1264 };
1264 };
1265
1265
1266 /**
1266 /**
1267 * Show a cell's output.
1267 * Show a cell's output.
1268 *
1268 *
1269 * @method expand
1269 * @method expand
1270 * @param {Number} index A cell's numeric index
1270 * @param {Number} index A cell's numeric index
1271 */
1271 */
1272 Notebook.prototype.expand = function (index) {
1272 Notebook.prototype.expand = function (index) {
1273 var i = this.index_or_selected(index);
1273 var i = this.index_or_selected(index);
1274 this.get_cell(i).expand();
1274 this.get_cell(i).expand();
1275 this.set_dirty(true);
1275 this.set_dirty(true);
1276 };
1276 };
1277
1277
1278 /** Toggle whether a cell's output is collapsed or expanded.
1278 /** Toggle whether a cell's output is collapsed or expanded.
1279 *
1279 *
1280 * @method toggle_output
1280 * @method toggle_output
1281 * @param {Number} index A cell's numeric index
1281 * @param {Number} index A cell's numeric index
1282 */
1282 */
1283 Notebook.prototype.toggle_output = function (index) {
1283 Notebook.prototype.toggle_output = function (index) {
1284 var i = this.index_or_selected(index);
1284 var i = this.index_or_selected(index);
1285 this.get_cell(i).toggle_output();
1285 this.get_cell(i).toggle_output();
1286 this.set_dirty(true);
1286 this.set_dirty(true);
1287 };
1287 };
1288
1288
1289 /**
1289 /**
1290 * Toggle a scrollbar for long cell outputs.
1290 * Toggle a scrollbar for long cell outputs.
1291 *
1291 *
1292 * @method toggle_output_scroll
1292 * @method toggle_output_scroll
1293 * @param {Number} index A cell's numeric index
1293 * @param {Number} index A cell's numeric index
1294 */
1294 */
1295 Notebook.prototype.toggle_output_scroll = function (index) {
1295 Notebook.prototype.toggle_output_scroll = function (index) {
1296 var i = this.index_or_selected(index);
1296 var i = this.index_or_selected(index);
1297 this.get_cell(i).toggle_output_scroll();
1297 this.get_cell(i).toggle_output_scroll();
1298 };
1298 };
1299
1299
1300 /**
1300 /**
1301 * Hide each code cell's output area.
1301 * Hide each code cell's output area.
1302 *
1302 *
1303 * @method collapse_all_output
1303 * @method collapse_all_output
1304 */
1304 */
1305 Notebook.prototype.collapse_all_output = function () {
1305 Notebook.prototype.collapse_all_output = function () {
1306 var ncells = this.ncells();
1306 var ncells = this.ncells();
1307 var cells = this.get_cells();
1307 var cells = this.get_cells();
1308 for (var i=0; i<ncells; i++) {
1308 for (var i=0; i<ncells; i++) {
1309 if (cells[i] instanceof IPython.CodeCell) {
1309 if (cells[i] instanceof IPython.CodeCell) {
1310 cells[i].output_area.collapse();
1310 cells[i].output_area.collapse();
1311 }
1311 }
1312 };
1312 };
1313 // this should not be set if the `collapse` key is removed from nbformat
1313 // this should not be set if the `collapse` key is removed from nbformat
1314 this.set_dirty(true);
1314 this.set_dirty(true);
1315 };
1315 };
1316
1316
1317 /**
1317 /**
1318 * Expand each code cell's output area, and add a scrollbar for long output.
1318 * Expand each code cell's output area, and add a scrollbar for long output.
1319 *
1319 *
1320 * @method scroll_all_output
1320 * @method scroll_all_output
1321 */
1321 */
1322 Notebook.prototype.scroll_all_output = function () {
1322 Notebook.prototype.scroll_all_output = function () {
1323 var ncells = this.ncells();
1323 var ncells = this.ncells();
1324 var cells = this.get_cells();
1324 var cells = this.get_cells();
1325 for (var i=0; i<ncells; i++) {
1325 for (var i=0; i<ncells; i++) {
1326 if (cells[i] instanceof IPython.CodeCell) {
1326 if (cells[i] instanceof IPython.CodeCell) {
1327 cells[i].output_area.expand();
1327 cells[i].output_area.expand();
1328 cells[i].output_area.scroll_if_long();
1328 cells[i].output_area.scroll_if_long();
1329 }
1329 }
1330 };
1330 };
1331 // this should not be set if the `collapse` key is removed from nbformat
1331 // this should not be set if the `collapse` key is removed from nbformat
1332 this.set_dirty(true);
1332 this.set_dirty(true);
1333 };
1333 };
1334
1334
1335 /**
1335 /**
1336 * Expand each code cell's output area, and remove scrollbars.
1336 * Expand each code cell's output area, and remove scrollbars.
1337 *
1337 *
1338 * @method expand_all_output
1338 * @method expand_all_output
1339 */
1339 */
1340 Notebook.prototype.expand_all_output = function () {
1340 Notebook.prototype.expand_all_output = function () {
1341 var ncells = this.ncells();
1341 var ncells = this.ncells();
1342 var cells = this.get_cells();
1342 var cells = this.get_cells();
1343 for (var i=0; i<ncells; i++) {
1343 for (var i=0; i<ncells; i++) {
1344 if (cells[i] instanceof IPython.CodeCell) {
1344 if (cells[i] instanceof IPython.CodeCell) {
1345 cells[i].output_area.expand();
1345 cells[i].output_area.expand();
1346 cells[i].output_area.unscroll_area();
1346 cells[i].output_area.unscroll_area();
1347 }
1347 }
1348 };
1348 };
1349 // this should not be set if the `collapse` key is removed from nbformat
1349 // this should not be set if the `collapse` key is removed from nbformat
1350 this.set_dirty(true);
1350 this.set_dirty(true);
1351 };
1351 };
1352
1352
1353 /**
1353 /**
1354 * Clear each code cell's output area.
1354 * Clear each code cell's output area.
1355 *
1355 *
1356 * @method clear_all_output
1356 * @method clear_all_output
1357 */
1357 */
1358 Notebook.prototype.clear_all_output = function () {
1358 Notebook.prototype.clear_all_output = function () {
1359 var ncells = this.ncells();
1359 var ncells = this.ncells();
1360 var cells = this.get_cells();
1360 var cells = this.get_cells();
1361 for (var i=0; i<ncells; i++) {
1361 for (var i=0; i<ncells; i++) {
1362 if (cells[i] instanceof IPython.CodeCell) {
1362 if (cells[i] instanceof IPython.CodeCell) {
1363 cells[i].clear_output(true,true,true);
1363 cells[i].clear_output(true,true,true);
1364 // Make all In[] prompts blank, as well
1364 // Make all In[] prompts blank, as well
1365 // TODO: make this configurable (via checkbox?)
1365 // TODO: make this configurable (via checkbox?)
1366 cells[i].set_input_prompt();
1366 cells[i].set_input_prompt();
1367 }
1367 }
1368 };
1368 };
1369 this.set_dirty(true);
1369 this.set_dirty(true);
1370 };
1370 };
1371
1371
1372
1372
1373 // Other cell functions: line numbers, ...
1373 // Other cell functions: line numbers, ...
1374
1374
1375 /**
1375 /**
1376 * Toggle line numbers in the selected cell's input area.
1376 * Toggle line numbers in the selected cell's input area.
1377 *
1377 *
1378 * @method cell_toggle_line_numbers
1378 * @method cell_toggle_line_numbers
1379 */
1379 */
1380 Notebook.prototype.cell_toggle_line_numbers = function() {
1380 Notebook.prototype.cell_toggle_line_numbers = function() {
1381 this.get_selected_cell().toggle_line_numbers();
1381 this.get_selected_cell().toggle_line_numbers();
1382 };
1382 };
1383
1383
1384 // Session related things
1384 // Session related things
1385
1385
1386 /**
1386 /**
1387 * Start a new session and set it on each code cell.
1387 * Start a new session and set it on each code cell.
1388 *
1388 *
1389 * @method start_session
1389 * @method start_session
1390 */
1390 */
1391 Notebook.prototype.start_session = function () {
1391 Notebook.prototype.start_session = function () {
1392 this.session = new IPython.Session(this.notebook_name, this.notebook_path, this);
1392 this.session = new IPython.Session(this.notebook_name, this.notebook_path, this);
1393 this.session.start();
1393 this.session.start();
1394 this.link_cells_to_session();
1394 this.link_cells_to_session();
1395 };
1395 };
1396
1396
1397
1397
1398 /**
1398 /**
1399 * Once a session is started, link the code cells to the session
1399 * Once a session is started, link the code cells to the session
1400 *
1400 *
1401 */
1401 */
1402 Notebook.prototype.link_cells_to_session= function(){
1402 Notebook.prototype.link_cells_to_session= function(){
1403 var ncells = this.ncells();
1403 var ncells = this.ncells();
1404 for (var i=0; i<ncells; i++) {
1404 for (var i=0; i<ncells; i++) {
1405 var cell = this.get_cell(i);
1405 var cell = this.get_cell(i);
1406 if (cell instanceof IPython.CodeCell) {
1406 if (cell instanceof IPython.CodeCell) {
1407 cell.set_session(this.session);
1407 cell.set_session(this.session);
1408 };
1408 };
1409 };
1409 };
1410 };
1410 };
1411
1411
1412 /**
1412 /**
1413 * Prompt the user to restart the IPython kernel.
1413 * Prompt the user to restart the IPython kernel.
1414 *
1414 *
1415 * @method restart_kernel
1415 * @method restart_kernel
1416 */
1416 */
1417 Notebook.prototype.restart_kernel = function () {
1417 Notebook.prototype.restart_kernel = function () {
1418 var that = this;
1418 var that = this;
1419 IPython.dialog.modal({
1419 IPython.dialog.modal({
1420 title : "Restart kernel or continue running?",
1420 title : "Restart kernel or continue running?",
1421 body : $("<p/>").html(
1421 body : $("<p/>").html(
1422 'Do you want to restart the current kernel? You will lose all variables defined in it.'
1422 'Do you want to restart the current kernel? You will lose all variables defined in it.'
1423 ),
1423 ),
1424 buttons : {
1424 buttons : {
1425 "Continue running" : {},
1425 "Continue running" : {},
1426 "Restart" : {
1426 "Restart" : {
1427 "class" : "btn-danger",
1427 "class" : "btn-danger",
1428 "click" : function() {
1428 "click" : function() {
1429 that.session.restart_kernel();
1429 that.session.restart_kernel();
1430 }
1430 }
1431 }
1431 }
1432 }
1432 }
1433 });
1433 });
1434 };
1434 };
1435
1435
1436 /**
1436 /**
1437 * Run the selected cell.
1437 * Run the selected cell.
1438 *
1438 *
1439 * Execute or render cell outputs.
1439 * Execute or render cell outputs.
1440 *
1440 *
1441 * @method execute_selected_cell
1441 * @method execute_selected_cell
1442 * @param {Object} options Customize post-execution behavior
1442 * @param {Object} options Customize post-execution behavior
1443 */
1443 */
1444 Notebook.prototype.execute_selected_cell = function (options) {
1444 Notebook.prototype.execute_selected_cell = function (options) {
1445 // add_new: should a new cell be added if we are at the end of the nb
1445 // add_new: should a new cell be added if we are at the end of the nb
1446 // terminal: execute in terminal mode, which stays in the current cell
1446 // terminal: execute in terminal mode, which stays in the current cell
1447 var default_options = {terminal: false, add_new: true};
1447 var default_options = {terminal: false, add_new: true};
1448 $.extend(default_options, options);
1448 $.extend(default_options, options);
1449 var that = this;
1449 var that = this;
1450 var cell = that.get_selected_cell();
1450 var cell = that.get_selected_cell();
1451 var cell_index = that.find_cell_index(cell);
1451 var cell_index = that.find_cell_index(cell);
1452 if (cell instanceof IPython.CodeCell) {
1452 if (cell instanceof IPython.CodeCell) {
1453 cell.execute();
1453 cell.execute();
1454 }
1454 }
1455 if (default_options.terminal) {
1455 if (default_options.terminal) {
1456 cell.select_all();
1456 cell.select_all();
1457 } else {
1457 } else {
1458 if ((cell_index === (that.ncells()-1)) && default_options.add_new) {
1458 if ((cell_index === (that.ncells()-1)) && default_options.add_new) {
1459 that.insert_cell_below('code');
1459 that.insert_cell_below('code');
1460 // If we are adding a new cell at the end, scroll down to show it.
1460 // If we are adding a new cell at the end, scroll down to show it.
1461 that.scroll_to_bottom();
1461 that.scroll_to_bottom();
1462 } else {
1462 } else {
1463 that.select(cell_index+1);
1463 that.select(cell_index+1);
1464 };
1464 };
1465 };
1465 };
1466 this.set_dirty(true);
1466 this.set_dirty(true);
1467 };
1467 };
1468
1468
1469 /**
1469 /**
1470 * Execute all cells below the selected cell.
1470 * Execute all cells below the selected cell.
1471 *
1471 *
1472 * @method execute_cells_below
1472 * @method execute_cells_below
1473 */
1473 */
1474 Notebook.prototype.execute_cells_below = function () {
1474 Notebook.prototype.execute_cells_below = function () {
1475 this.execute_cell_range(this.get_selected_index(), this.ncells());
1475 this.execute_cell_range(this.get_selected_index(), this.ncells());
1476 this.scroll_to_bottom();
1476 this.scroll_to_bottom();
1477 };
1477 };
1478
1478
1479 /**
1479 /**
1480 * Execute all cells above the selected cell.
1480 * Execute all cells above the selected cell.
1481 *
1481 *
1482 * @method execute_cells_above
1482 * @method execute_cells_above
1483 */
1483 */
1484 Notebook.prototype.execute_cells_above = function () {
1484 Notebook.prototype.execute_cells_above = function () {
1485 this.execute_cell_range(0, this.get_selected_index());
1485 this.execute_cell_range(0, this.get_selected_index());
1486 };
1486 };
1487
1487
1488 /**
1488 /**
1489 * Execute all cells.
1489 * Execute all cells.
1490 *
1490 *
1491 * @method execute_all_cells
1491 * @method execute_all_cells
1492 */
1492 */
1493 Notebook.prototype.execute_all_cells = function () {
1493 Notebook.prototype.execute_all_cells = function () {
1494 this.execute_cell_range(0, this.ncells());
1494 this.execute_cell_range(0, this.ncells());
1495 this.scroll_to_bottom();
1495 this.scroll_to_bottom();
1496 };
1496 };
1497
1497
1498 /**
1498 /**
1499 * Execute a contiguous range of cells.
1499 * Execute a contiguous range of cells.
1500 *
1500 *
1501 * @method execute_cell_range
1501 * @method execute_cell_range
1502 * @param {Number} start Index of the first cell to execute (inclusive)
1502 * @param {Number} start Index of the first cell to execute (inclusive)
1503 * @param {Number} end Index of the last cell to execute (exclusive)
1503 * @param {Number} end Index of the last cell to execute (exclusive)
1504 */
1504 */
1505 Notebook.prototype.execute_cell_range = function (start, end) {
1505 Notebook.prototype.execute_cell_range = function (start, end) {
1506 for (var i=start; i<end; i++) {
1506 for (var i=start; i<end; i++) {
1507 this.select(i);
1507 this.select(i);
1508 this.execute_selected_cell({add_new:false});
1508 this.execute_selected_cell({add_new:false});
1509 };
1509 };
1510 };
1510 };
1511
1511
1512 // Persistance and loading
1512 // Persistance and loading
1513
1513
1514 /**
1514 /**
1515 * Getter method for this notebook's name.
1515 * Getter method for this notebook's name.
1516 *
1516 *
1517 * @method get_notebook_name
1517 * @method get_notebook_name
1518 * @return {String} This notebook's name
1518 * @return {String} This notebook's name
1519 */
1519 */
1520 Notebook.prototype.get_notebook_name = function () {
1520 Notebook.prototype.get_notebook_name = function () {
1521 var nbname = this.notebook_name.substring(0,this.notebook_name.length-6);
1521 var nbname = this.notebook_name.substring(0,this.notebook_name.length-6);
1522 return nbname;
1522 return nbname;
1523 };
1523 };
1524
1524
1525 /**
1525 /**
1526 * Setter method for this notebook's name.
1526 * Setter method for this notebook's name.
1527 *
1527 *
1528 * @method set_notebook_name
1528 * @method set_notebook_name
1529 * @param {String} name A new name for this notebook
1529 * @param {String} name A new name for this notebook
1530 */
1530 */
1531 Notebook.prototype.set_notebook_name = function (name) {
1531 Notebook.prototype.set_notebook_name = function (name) {
1532 this.notebook_name = name;
1532 this.notebook_name = name;
1533 };
1533 };
1534
1534
1535 /**
1535 /**
1536 * Check that a notebook's name is valid.
1536 * Check that a notebook's name is valid.
1537 *
1537 *
1538 * @method test_notebook_name
1538 * @method test_notebook_name
1539 * @param {String} nbname A name for this notebook
1539 * @param {String} nbname A name for this notebook
1540 * @return {Boolean} True if the name is valid, false if invalid
1540 * @return {Boolean} True if the name is valid, false if invalid
1541 */
1541 */
1542 Notebook.prototype.test_notebook_name = function (nbname) {
1542 Notebook.prototype.test_notebook_name = function (nbname) {
1543 nbname = nbname || '';
1543 nbname = nbname || '';
1544 if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) {
1544 if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) {
1545 return true;
1545 return true;
1546 } else {
1546 } else {
1547 return false;
1547 return false;
1548 };
1548 };
1549 };
1549 };
1550
1550
1551 /**
1551 /**
1552 * Load a notebook from JSON (.ipynb).
1552 * Load a notebook from JSON (.ipynb).
1553 *
1553 *
1554 * This currently handles one worksheet: others are deleted.
1554 * This currently handles one worksheet: others are deleted.
1555 *
1555 *
1556 * @method fromJSON
1556 * @method fromJSON
1557 * @param {Object} data JSON representation of a notebook
1557 * @param {Object} data JSON representation of a notebook
1558 */
1558 */
1559 Notebook.prototype.fromJSON = function (data) {
1559 Notebook.prototype.fromJSON = function (data) {
1560 var content = data.content;
1560 var content = data.content;
1561 var ncells = this.ncells();
1561 var ncells = this.ncells();
1562 var i;
1562 var i;
1563 for (i=0; i<ncells; i++) {
1563 for (i=0; i<ncells; i++) {
1564 // Always delete cell 0 as they get renumbered as they are deleted.
1564 // Always delete cell 0 as they get renumbered as they are deleted.
1565 this.delete_cell(0);
1565 this.delete_cell(0);
1566 };
1566 };
1567 // Save the metadata and name.
1567 // Save the metadata and name.
1568 this.metadata = content.metadata;
1568 this.metadata = content.metadata;
1569 this.notebook_name = data.name;
1569 this.notebook_name = data.name;
1570 // Only handle 1 worksheet for now.
1570 // Only handle 1 worksheet for now.
1571 var worksheet = content.worksheets[0];
1571 var worksheet = content.worksheets[0];
1572 if (worksheet !== undefined) {
1572 if (worksheet !== undefined) {
1573 if (worksheet.metadata) {
1573 if (worksheet.metadata) {
1574 this.worksheet_metadata = worksheet.metadata;
1574 this.worksheet_metadata = worksheet.metadata;
1575 }
1575 }
1576 var new_cells = worksheet.cells;
1576 var new_cells = worksheet.cells;
1577 ncells = new_cells.length;
1577 ncells = new_cells.length;
1578 var cell_data = null;
1578 var cell_data = null;
1579 var new_cell = null;
1579 var new_cell = null;
1580 for (i=0; i<ncells; i++) {
1580 for (i=0; i<ncells; i++) {
1581 cell_data = new_cells[i];
1581 cell_data = new_cells[i];
1582 // VERSIONHACK: plaintext -> raw
1582 // VERSIONHACK: plaintext -> raw
1583 // handle never-released plaintext name for raw cells
1583 // handle never-released plaintext name for raw cells
1584 if (cell_data.cell_type === 'plaintext'){
1584 if (cell_data.cell_type === 'plaintext'){
1585 cell_data.cell_type = 'raw';
1585 cell_data.cell_type = 'raw';
1586 }
1586 }
1587
1587
1588 new_cell = this.insert_cell_below(cell_data.cell_type);
1588 new_cell = this.insert_cell_below(cell_data.cell_type);
1589 new_cell.fromJSON(cell_data);
1589 new_cell.fromJSON(cell_data);
1590 };
1590 };
1591 };
1591 };
1592 if (content.worksheets.length > 1) {
1592 if (content.worksheets.length > 1) {
1593 IPython.dialog.modal({
1593 IPython.dialog.modal({
1594 title : "Multiple worksheets",
1594 title : "Multiple worksheets",
1595 body : "This notebook has " + data.worksheets.length + " worksheets, " +
1595 body : "This notebook has " + data.worksheets.length + " worksheets, " +
1596 "but this version of IPython can only handle the first. " +
1596 "but this version of IPython can only handle the first. " +
1597 "If you save this notebook, worksheets after the first will be lost.",
1597 "If you save this notebook, worksheets after the first will be lost.",
1598 buttons : {
1598 buttons : {
1599 OK : {
1599 OK : {
1600 class : "btn-danger"
1600 class : "btn-danger"
1601 }
1601 }
1602 }
1602 }
1603 });
1603 });
1604 }
1604 }
1605 };
1605 };
1606
1606
1607 /**
1607 /**
1608 * Dump this notebook into a JSON-friendly object.
1608 * Dump this notebook into a JSON-friendly object.
1609 *
1609 *
1610 * @method toJSON
1610 * @method toJSON
1611 * @return {Object} A JSON-friendly representation of this notebook.
1611 * @return {Object} A JSON-friendly representation of this notebook.
1612 */
1612 */
1613 Notebook.prototype.toJSON = function () {
1613 Notebook.prototype.toJSON = function () {
1614 var cells = this.get_cells();
1614 var cells = this.get_cells();
1615 var ncells = cells.length;
1615 var ncells = cells.length;
1616 var cell_array = new Array(ncells);
1616 var cell_array = new Array(ncells);
1617 for (var i=0; i<ncells; i++) {
1617 for (var i=0; i<ncells; i++) {
1618 cell_array[i] = cells[i].toJSON();
1618 cell_array[i] = cells[i].toJSON();
1619 };
1619 };
1620 var data = {
1620 var data = {
1621 // Only handle 1 worksheet for now.
1621 // Only handle 1 worksheet for now.
1622 worksheets : [{
1622 worksheets : [{
1623 cells: cell_array,
1623 cells: cell_array,
1624 metadata: this.worksheet_metadata
1624 metadata: this.worksheet_metadata
1625 }],
1625 }],
1626 metadata : this.metadata
1626 metadata : this.metadata
1627 };
1627 };
1628 return data;
1628 return data;
1629 };
1629 };
1630
1630
1631 /**
1631 /**
1632 * Start an autosave timer, for periodically saving the notebook.
1632 * Start an autosave timer, for periodically saving the notebook.
1633 *
1633 *
1634 * @method set_autosave_interval
1634 * @method set_autosave_interval
1635 * @param {Integer} interval the autosave interval in milliseconds
1635 * @param {Integer} interval the autosave interval in milliseconds
1636 */
1636 */
1637 Notebook.prototype.set_autosave_interval = function (interval) {
1637 Notebook.prototype.set_autosave_interval = function (interval) {
1638 var that = this;
1638 var that = this;
1639 // clear previous interval, so we don't get simultaneous timers
1639 // clear previous interval, so we don't get simultaneous timers
1640 if (this.autosave_timer) {
1640 if (this.autosave_timer) {
1641 clearInterval(this.autosave_timer);
1641 clearInterval(this.autosave_timer);
1642 }
1642 }
1643
1643
1644 this.autosave_interval = this.minimum_autosave_interval = interval;
1644 this.autosave_interval = this.minimum_autosave_interval = interval;
1645 if (interval) {
1645 if (interval) {
1646 this.autosave_timer = setInterval(function() {
1646 this.autosave_timer = setInterval(function() {
1647 if (that.dirty) {
1647 if (that.dirty) {
1648 that.save_notebook();
1648 that.save_notebook();
1649 }
1649 }
1650 }, interval);
1650 }, interval);
1651 $([IPython.events]).trigger("autosave_enabled.Notebook", interval);
1651 $([IPython.events]).trigger("autosave_enabled.Notebook", interval);
1652 } else {
1652 } else {
1653 this.autosave_timer = null;
1653 this.autosave_timer = null;
1654 $([IPython.events]).trigger("autosave_disabled.Notebook");
1654 $([IPython.events]).trigger("autosave_disabled.Notebook");
1655 };
1655 };
1656 };
1656 };
1657
1657
1658 /**
1658 /**
1659 * Save this notebook on the server.
1659 * Save this notebook on the server.
1660 *
1660 *
1661 * @method save_notebook
1661 * @method save_notebook
1662 */
1662 */
1663 Notebook.prototype.save_notebook = function () {
1663 Notebook.prototype.save_notebook = function () {
1664 // Create a JSON model to be sent to the server.
1664 // Create a JSON model to be sent to the server.
1665 var model = {};
1665 var model = {};
1666 model.name = this.notebook_name;
1666 model.name = this.notebook_name;
1667 model.path = this.notebook_path;
1667 model.path = this.notebook_path;
1668 model.content = this.toJSON();
1668 model.content = this.toJSON();
1669 model.content.nbformat = this.nbformat;
1669 model.content.nbformat = this.nbformat;
1670 model.content.nbformat_minor = this.nbformat_minor;
1670 model.content.nbformat_minor = this.nbformat_minor;
1671 // time the ajax call for autosave tuning purposes.
1671 // time the ajax call for autosave tuning purposes.
1672 var start = new Date().getTime();
1672 var start = new Date().getTime();
1673 // We do the call with settings so we can set cache to false.
1673 // We do the call with settings so we can set cache to false.
1674 var settings = {
1674 var settings = {
1675 processData : false,
1675 processData : false,
1676 cache : false,
1676 cache : false,
1677 type : "PUT",
1677 type : "PUT",
1678 data : JSON.stringify(model),
1678 data : JSON.stringify(model),
1679 headers : {'Content-Type': 'application/json'},
1679 headers : {'Content-Type': 'application/json'},
1680 success : $.proxy(this.save_notebook_success, this, start),
1680 success : $.proxy(this.save_notebook_success, this, start),
1681 error : $.proxy(this.save_notebook_error, this)
1681 error : $.proxy(this.save_notebook_error, this)
1682 };
1682 };
1683 $([IPython.events]).trigger('notebook_saving.Notebook');
1683 $([IPython.events]).trigger('notebook_saving.Notebook');
1684 var url = utils.url_path_join(
1684 var url = utils.url_path_join(
1685 this.baseProjectUrl(),
1685 this.baseProjectUrl(),
1686 'api/notebooks',
1686 'api/notebooks',
1687 this.notebookPath(),
1687 this.notebookPath(),
1688 this.notebook_name
1688 this.notebook_name
1689 );
1689 );
1690 $.ajax(url, settings);
1690 $.ajax(url, settings);
1691 };
1691 };
1692
1692
1693 /**
1693 /**
1694 * Success callback for saving a notebook.
1694 * Success callback for saving a notebook.
1695 *
1695 *
1696 * @method save_notebook_success
1696 * @method save_notebook_success
1697 * @param {Integer} start the time when the save request started
1697 * @param {Integer} start the time when the save request started
1698 * @param {Object} data JSON representation of a notebook
1698 * @param {Object} data JSON representation of a notebook
1699 * @param {String} status Description of response status
1699 * @param {String} status Description of response status
1700 * @param {jqXHR} xhr jQuery Ajax object
1700 * @param {jqXHR} xhr jQuery Ajax object
1701 */
1701 */
1702 Notebook.prototype.save_notebook_success = function (start, data, status, xhr) {
1702 Notebook.prototype.save_notebook_success = function (start, data, status, xhr) {
1703 this.set_dirty(false);
1703 this.set_dirty(false);
1704 $([IPython.events]).trigger('notebook_saved.Notebook');
1704 $([IPython.events]).trigger('notebook_saved.Notebook');
1705 this._update_autosave_interval(start);
1705 this._update_autosave_interval(start);
1706 if (this._checkpoint_after_save) {
1706 if (this._checkpoint_after_save) {
1707 this.create_checkpoint();
1707 this.create_checkpoint();
1708 this._checkpoint_after_save = false;
1708 this._checkpoint_after_save = false;
1709 };
1709 };
1710 };
1710 };
1711
1711
1712 /**
1712 /**
1713 * update the autosave interval based on how long the last save took
1713 * update the autosave interval based on how long the last save took
1714 *
1714 *
1715 * @method _update_autosave_interval
1715 * @method _update_autosave_interval
1716 * @param {Integer} timestamp when the save request started
1716 * @param {Integer} timestamp when the save request started
1717 */
1717 */
1718 Notebook.prototype._update_autosave_interval = function (start) {
1718 Notebook.prototype._update_autosave_interval = function (start) {
1719 var duration = (new Date().getTime() - start);
1719 var duration = (new Date().getTime() - start);
1720 if (this.autosave_interval) {
1720 if (this.autosave_interval) {
1721 // new save interval: higher of 10x save duration or parameter (default 30 seconds)
1721 // new save interval: higher of 10x save duration or parameter (default 30 seconds)
1722 var interval = Math.max(10 * duration, this.minimum_autosave_interval);
1722 var interval = Math.max(10 * duration, this.minimum_autosave_interval);
1723 // round to 10 seconds, otherwise we will be setting a new interval too often
1723 // round to 10 seconds, otherwise we will be setting a new interval too often
1724 interval = 10000 * Math.round(interval / 10000);
1724 interval = 10000 * Math.round(interval / 10000);
1725 // set new interval, if it's changed
1725 // set new interval, if it's changed
1726 if (interval != this.autosave_interval) {
1726 if (interval != this.autosave_interval) {
1727 this.set_autosave_interval(interval);
1727 this.set_autosave_interval(interval);
1728 }
1728 }
1729 }
1729 }
1730 };
1730 };
1731
1731
1732 /**
1732 /**
1733 * Failure callback for saving a notebook.
1733 * Failure callback for saving a notebook.
1734 *
1734 *
1735 * @method save_notebook_error
1735 * @method save_notebook_error
1736 * @param {jqXHR} xhr jQuery Ajax object
1736 * @param {jqXHR} xhr jQuery Ajax object
1737 * @param {String} status Description of response status
1737 * @param {String} status Description of response status
1738 * @param {String} error_msg HTTP error message
1738 * @param {String} error_msg HTTP error message
1739 */
1739 */
1740 Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) {
1740 Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) {
1741 $([IPython.events]).trigger('notebook_save_failed.Notebook');
1741 $([IPython.events]).trigger('notebook_save_failed.Notebook');
1742 };
1742 };
1743
1743
1744 Notebook.prototype.new_notebook = function(){
1744 Notebook.prototype.new_notebook = function(){
1745 var path = this.notebookPath();
1745 var path = this.notebookPath();
1746 var settings = {
1746 var settings = {
1747 processData : false,
1747 processData : false,
1748 cache : false,
1748 cache : false,
1749 type : "POST",
1749 type : "POST",
1750 dataType : "json",
1750 dataType : "json",
1751 success:$.proxy(function (data, status, xhr){
1751 success:$.proxy(function (data, status, xhr){
1752 var notebook_name = data.name;
1752 var notebook_name = data.name;
1753 window.open(
1753 window.open(
1754 utils.url_path_join(
1754 utils.url_path_join(
1755 this.baseProjectUrl(),
1755 this.baseProjectUrl(),
1756 'notebooks',
1756 'notebooks',
1757 this.notebookPath(),
1757 this.notebookPath(),
1758 notebook_name
1758 notebook_name
1759 ),
1759 ),
1760 '_blank'
1760 '_blank'
1761 );
1761 );
1762 }, this)
1762 }, this)
1763 };
1763 };
1764 var url = this.baseProjectUrl() + 'api/notebooks' + path;
1764 var url = this.baseProjectUrl() + 'api/notebooks' + path;
1765 $.ajax(url,settings);
1765 $.ajax(url,settings);
1766 };
1766 };
1767
1767
1768
1768
1769 Notebook.prototype.copy_notebook = function(){
1769 Notebook.prototype.copy_notebook = function(){
1770 var path = this.notebookPath();
1770 var path = this.notebookPath();
1771 var name = {'name': this.notebook_name}
1772 var settings = {
1771 var settings = {
1773 processData : false,
1772 processData : false,
1774 cache : false,
1773 cache : false,
1775 type : "POST",
1774 type : "POST",
1776 data: JSON.stringify(name),
1777 dataType : "json",
1778 success:$.proxy(function (data, status, xhr){
1775 success:$.proxy(function (data, status, xhr){
1779 notebook_name = data.name;
1776 var notebook_name = data.name;
1780 window.open(utils.url_path_join(
1777 window.open(utils.url_path_join(
1781 this._baseProjectUrl,
1778 this._baseProjectUrl,
1782 'notebooks',
1779 'notebooks',
1783 this.notebookPath(),
1780 this.notebookPath(),
1784 notebook_name
1781 notebook_name
1785 ));
1782 ));
1786 }, this)
1783 }, this)
1787 };
1784 };
1788 var url = utils.url_path_join(
1785 var url = utils.url_path_join(
1789 this._baseProjectUrl,
1786 this._baseProjectUrl,
1790 'notebooks',
1787 'api/notebooks',
1791 path
1788 path,
1789 this.notebook_name,
1790 'copy'
1792 );
1791 );
1793 $.ajax(url,settings);
1792 $.ajax(url,settings);
1794 };
1793 };
1795
1794
1796 Notebook.prototype.notebook_rename = function (nbname) {
1795 Notebook.prototype.notebook_rename = function (nbname) {
1797 var that = this;
1796 var that = this;
1798 var new_name = nbname + '.ipynb'
1797 var new_name = nbname + '.ipynb'
1799 var name = {'name': new_name};
1798 var name = {'name': new_name};
1800 var settings = {
1799 var settings = {
1801 processData : false,
1800 processData : false,
1802 cache : false,
1801 cache : false,
1803 type : "PATCH",
1802 type : "PATCH",
1804 data : JSON.stringify(name),
1803 data : JSON.stringify(name),
1805 dataType: "json",
1804 dataType: "json",
1806 headers : {'Content-Type': 'application/json'},
1805 headers : {'Content-Type': 'application/json'},
1807 success : $.proxy(that.rename_success, this),
1806 success : $.proxy(that.rename_success, this),
1808 error : $.proxy(that.rename_error, this)
1807 error : $.proxy(that.rename_error, this)
1809 };
1808 };
1810 $([IPython.events]).trigger('notebook_rename.Notebook');
1809 $([IPython.events]).trigger('notebook_rename.Notebook');
1811 var url = utils.url_path_join(
1810 var url = utils.url_path_join(
1812 this.baseProjectUrl(),
1811 this.baseProjectUrl(),
1813 'api/notebooks',
1812 'api/notebooks',
1814 this.notebookPath(),
1813 this.notebookPath(),
1815 this.notebook_name
1814 this.notebook_name
1816 );
1815 );
1817 $.ajax(url, settings);
1816 $.ajax(url, settings);
1818 };
1817 };
1819
1818
1820
1819
1821 Notebook.prototype.rename_success = function (json, status, xhr) {
1820 Notebook.prototype.rename_success = function (json, status, xhr) {
1822 this.notebook_name = json.name
1821 this.notebook_name = json.name
1823 var name = this.notebook_name
1822 var name = this.notebook_name
1824 var path = json.path
1823 var path = json.path
1825 this.session.notebook_rename(name, path);
1824 this.session.notebook_rename(name, path);
1826 $([IPython.events]).trigger('notebook_renamed.Notebook');
1825 $([IPython.events]).trigger('notebook_renamed.Notebook');
1827 }
1826 }
1828
1827
1829 Notebook.prototype.rename_error = function (json, status, xhr) {
1828 Notebook.prototype.rename_error = function (json, status, xhr) {
1830 var that = this;
1829 var that = this;
1831 var dialog = $('<div/>').append(
1830 var dialog = $('<div/>').append(
1832 $("<p/>").addClass("rename-message")
1831 $("<p/>").addClass("rename-message")
1833 .html('This notebook name already exists.')
1832 .html('This notebook name already exists.')
1834 )
1833 )
1835 IPython.dialog.modal({
1834 IPython.dialog.modal({
1836 title: "Notebook Rename Error!",
1835 title: "Notebook Rename Error!",
1837 body: dialog,
1836 body: dialog,
1838 buttons : {
1837 buttons : {
1839 "Cancel": {},
1838 "Cancel": {},
1840 "OK": {
1839 "OK": {
1841 class: "btn-primary",
1840 class: "btn-primary",
1842 click: function () {
1841 click: function () {
1843 IPython.save_widget.rename_notebook();
1842 IPython.save_widget.rename_notebook();
1844 }}
1843 }}
1845 },
1844 },
1846 open : function (event, ui) {
1845 open : function (event, ui) {
1847 var that = $(this);
1846 var that = $(this);
1848 // Upon ENTER, click the OK button.
1847 // Upon ENTER, click the OK button.
1849 that.find('input[type="text"]').keydown(function (event, ui) {
1848 that.find('input[type="text"]').keydown(function (event, ui) {
1850 if (event.which === utils.keycodes.ENTER) {
1849 if (event.which === utils.keycodes.ENTER) {
1851 that.find('.btn-primary').first().click();
1850 that.find('.btn-primary').first().click();
1852 }
1851 }
1853 });
1852 });
1854 that.find('input[type="text"]').focus();
1853 that.find('input[type="text"]').focus();
1855 }
1854 }
1856 });
1855 });
1857 }
1856 }
1858
1857
1859 /**
1858 /**
1860 * Request a notebook's data from the server.
1859 * Request a notebook's data from the server.
1861 *
1860 *
1862 * @method load_notebook
1861 * @method load_notebook
1863 * @param {String} notebook_naem and path A notebook to load
1862 * @param {String} notebook_naem and path A notebook to load
1864 */
1863 */
1865 Notebook.prototype.load_notebook = function (notebook_name, notebook_path) {
1864 Notebook.prototype.load_notebook = function (notebook_name, notebook_path) {
1866 var that = this;
1865 var that = this;
1867 this.notebook_name = notebook_name;
1866 this.notebook_name = notebook_name;
1868 this.notebook_path = notebook_path;
1867 this.notebook_path = notebook_path;
1869 // We do the call with settings so we can set cache to false.
1868 // We do the call with settings so we can set cache to false.
1870 var settings = {
1869 var settings = {
1871 processData : false,
1870 processData : false,
1872 cache : false,
1871 cache : false,
1873 type : "GET",
1872 type : "GET",
1874 dataType : "json",
1873 dataType : "json",
1875 success : $.proxy(this.load_notebook_success,this),
1874 success : $.proxy(this.load_notebook_success,this),
1876 error : $.proxy(this.load_notebook_error,this),
1875 error : $.proxy(this.load_notebook_error,this),
1877 };
1876 };
1878 $([IPython.events]).trigger('notebook_loading.Notebook');
1877 $([IPython.events]).trigger('notebook_loading.Notebook');
1879 var url = utils.url_path_join(
1878 var url = utils.url_path_join(
1880 this._baseProjectUrl,
1879 this._baseProjectUrl,
1881 'api/notebooks',
1880 'api/notebooks',
1882 this.notebookPath(),
1881 this.notebookPath(),
1883 this.notebook_name
1882 this.notebook_name
1884 );
1883 );
1885 $.ajax(url, settings);
1884 $.ajax(url, settings);
1886 };
1885 };
1887
1886
1888 /**
1887 /**
1889 * Success callback for loading a notebook from the server.
1888 * Success callback for loading a notebook from the server.
1890 *
1889 *
1891 * Load notebook data from the JSON response.
1890 * Load notebook data from the JSON response.
1892 *
1891 *
1893 * @method load_notebook_success
1892 * @method load_notebook_success
1894 * @param {Object} data JSON representation of a notebook
1893 * @param {Object} data JSON representation of a notebook
1895 * @param {String} status Description of response status
1894 * @param {String} status Description of response status
1896 * @param {jqXHR} xhr jQuery Ajax object
1895 * @param {jqXHR} xhr jQuery Ajax object
1897 */
1896 */
1898 Notebook.prototype.load_notebook_success = function (data, status, xhr) {
1897 Notebook.prototype.load_notebook_success = function (data, status, xhr) {
1899 this.fromJSON(data);
1898 this.fromJSON(data);
1900 if (this.ncells() === 0) {
1899 if (this.ncells() === 0) {
1901 this.insert_cell_below('code');
1900 this.insert_cell_below('code');
1902 };
1901 };
1903 this.set_dirty(false);
1902 this.set_dirty(false);
1904 this.select(0);
1903 this.select(0);
1905 this.scroll_to_top();
1904 this.scroll_to_top();
1906 if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) {
1905 if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) {
1907 var msg = "This notebook has been converted from an older " +
1906 var msg = "This notebook has been converted from an older " +
1908 "notebook format (v"+data.orig_nbformat+") to the current notebook " +
1907 "notebook format (v"+data.orig_nbformat+") to the current notebook " +
1909 "format (v"+data.nbformat+"). The next time you save this notebook, the " +
1908 "format (v"+data.nbformat+"). The next time you save this notebook, the " +
1910 "newer notebook format will be used and older versions of IPython " +
1909 "newer notebook format will be used and older versions of IPython " +
1911 "may not be able to read it. To keep the older version, close the " +
1910 "may not be able to read it. To keep the older version, close the " +
1912 "notebook without saving it.";
1911 "notebook without saving it.";
1913 IPython.dialog.modal({
1912 IPython.dialog.modal({
1914 title : "Notebook converted",
1913 title : "Notebook converted",
1915 body : msg,
1914 body : msg,
1916 buttons : {
1915 buttons : {
1917 OK : {
1916 OK : {
1918 class : "btn-primary"
1917 class : "btn-primary"
1919 }
1918 }
1920 }
1919 }
1921 });
1920 });
1922 } else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) {
1921 } else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) {
1923 var that = this;
1922 var that = this;
1924 var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor;
1923 var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor;
1925 var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor;
1924 var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor;
1926 var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " +
1925 var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " +
1927 this_vs + ". You can still work with this notebook, but some features " +
1926 this_vs + ". You can still work with this notebook, but some features " +
1928 "introduced in later notebook versions may not be available."
1927 "introduced in later notebook versions may not be available."
1929
1928
1930 IPython.dialog.modal({
1929 IPython.dialog.modal({
1931 title : "Newer Notebook",
1930 title : "Newer Notebook",
1932 body : msg,
1931 body : msg,
1933 buttons : {
1932 buttons : {
1934 OK : {
1933 OK : {
1935 class : "btn-danger"
1934 class : "btn-danger"
1936 }
1935 }
1937 }
1936 }
1938 });
1937 });
1939
1938
1940 }
1939 }
1941
1940
1942 // Create the session after the notebook is completely loaded to prevent
1941 // Create the session after the notebook is completely loaded to prevent
1943 // code execution upon loading, which is a security risk.
1942 // code execution upon loading, which is a security risk.
1944 if (this.session == null) {
1943 if (this.session == null) {
1945 this.start_session();
1944 this.start_session();
1946 }
1945 }
1947 // load our checkpoint list
1946 // load our checkpoint list
1948 IPython.notebook.list_checkpoints();
1947 IPython.notebook.list_checkpoints();
1949 $([IPython.events]).trigger('notebook_loaded.Notebook');
1948 $([IPython.events]).trigger('notebook_loaded.Notebook');
1950 };
1949 };
1951
1950
1952 /**
1951 /**
1953 * Failure callback for loading a notebook from the server.
1952 * Failure callback for loading a notebook from the server.
1954 *
1953 *
1955 * @method load_notebook_error
1954 * @method load_notebook_error
1956 * @param {jqXHR} xhr jQuery Ajax object
1955 * @param {jqXHR} xhr jQuery Ajax object
1957 * @param {String} textStatus Description of response status
1956 * @param {String} textStatus Description of response status
1958 * @param {String} errorThrow HTTP error message
1957 * @param {String} errorThrow HTTP error message
1959 */
1958 */
1960 Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) {
1959 Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) {
1961 if (xhr.status === 400) {
1960 if (xhr.status === 400) {
1962 var msg = errorThrow;
1961 var msg = errorThrow;
1963 } else if (xhr.status === 500) {
1962 } else if (xhr.status === 500) {
1964 var msg = "An unknown error occurred while loading this notebook. " +
1963 var msg = "An unknown error occurred while loading this notebook. " +
1965 "This version can load notebook formats " +
1964 "This version can load notebook formats " +
1966 "v" + this.nbformat + " or earlier.";
1965 "v" + this.nbformat + " or earlier.";
1967 }
1966 }
1968 IPython.dialog.modal({
1967 IPython.dialog.modal({
1969 title: "Error loading notebook",
1968 title: "Error loading notebook",
1970 body : msg,
1969 body : msg,
1971 buttons : {
1970 buttons : {
1972 "OK": {}
1971 "OK": {}
1973 }
1972 }
1974 });
1973 });
1975 }
1974 }
1976
1975
1977 /********************* checkpoint-related *********************/
1976 /********************* checkpoint-related *********************/
1978
1977
1979 /**
1978 /**
1980 * Save the notebook then immediately create a checkpoint.
1979 * Save the notebook then immediately create a checkpoint.
1981 *
1980 *
1982 * @method save_checkpoint
1981 * @method save_checkpoint
1983 */
1982 */
1984 Notebook.prototype.save_checkpoint = function () {
1983 Notebook.prototype.save_checkpoint = function () {
1985 this._checkpoint_after_save = true;
1984 this._checkpoint_after_save = true;
1986 this.save_notebook();
1985 this.save_notebook();
1987 };
1986 };
1988
1987
1989 /**
1988 /**
1990 * Add a checkpoint for this notebook.
1989 * Add a checkpoint for this notebook.
1991 * for use as a callback from checkpoint creation.
1990 * for use as a callback from checkpoint creation.
1992 *
1991 *
1993 * @method add_checkpoint
1992 * @method add_checkpoint
1994 */
1993 */
1995 Notebook.prototype.add_checkpoint = function (checkpoint) {
1994 Notebook.prototype.add_checkpoint = function (checkpoint) {
1996 var found = false;
1995 var found = false;
1997 for (var i = 0; i < this.checkpoints.length; i++) {
1996 for (var i = 0; i < this.checkpoints.length; i++) {
1998 var existing = this.checkpoints[i];
1997 var existing = this.checkpoints[i];
1999 if (existing.checkpoint_id == checkpoint.checkpoint_id) {
1998 if (existing.checkpoint_id == checkpoint.checkpoint_id) {
2000 found = true;
1999 found = true;
2001 this.checkpoints[i] = checkpoint;
2000 this.checkpoints[i] = checkpoint;
2002 break;
2001 break;
2003 }
2002 }
2004 }
2003 }
2005 if (!found) {
2004 if (!found) {
2006 this.checkpoints.push(checkpoint);
2005 this.checkpoints.push(checkpoint);
2007 }
2006 }
2008 this.last_checkpoint = this.checkpoints[this.checkpoints.length - 1];
2007 this.last_checkpoint = this.checkpoints[this.checkpoints.length - 1];
2009 };
2008 };
2010
2009
2011 /**
2010 /**
2012 * List checkpoints for this notebook.
2011 * List checkpoints for this notebook.
2013 *
2012 *
2014 * @method list_checkpoints
2013 * @method list_checkpoints
2015 */
2014 */
2016 Notebook.prototype.list_checkpoints = function () {
2015 Notebook.prototype.list_checkpoints = function () {
2017 var url = utils.url_path_join(
2016 var url = utils.url_path_join(
2018 this.baseProjectUrl(),
2017 this.baseProjectUrl(),
2019 'api/notebooks',
2018 'api/notebooks',
2020 this.notebookPath(),
2019 this.notebookPath(),
2021 this.notebook_name,
2020 this.notebook_name,
2022 'checkpoints'
2021 'checkpoints'
2023 );
2022 );
2024 $.get(url).done(
2023 $.get(url).done(
2025 $.proxy(this.list_checkpoints_success, this)
2024 $.proxy(this.list_checkpoints_success, this)
2026 ).fail(
2025 ).fail(
2027 $.proxy(this.list_checkpoints_error, this)
2026 $.proxy(this.list_checkpoints_error, this)
2028 );
2027 );
2029 };
2028 };
2030
2029
2031 /**
2030 /**
2032 * Success callback for listing checkpoints.
2031 * Success callback for listing checkpoints.
2033 *
2032 *
2034 * @method list_checkpoint_success
2033 * @method list_checkpoint_success
2035 * @param {Object} data JSON representation of a checkpoint
2034 * @param {Object} data JSON representation of a checkpoint
2036 * @param {String} status Description of response status
2035 * @param {String} status Description of response status
2037 * @param {jqXHR} xhr jQuery Ajax object
2036 * @param {jqXHR} xhr jQuery Ajax object
2038 */
2037 */
2039 Notebook.prototype.list_checkpoints_success = function (data, status, xhr) {
2038 Notebook.prototype.list_checkpoints_success = function (data, status, xhr) {
2040 var data = $.parseJSON(data);
2039 var data = $.parseJSON(data);
2041 this.checkpoints = data;
2040 this.checkpoints = data;
2042 if (data.length) {
2041 if (data.length) {
2043 this.last_checkpoint = data[data.length - 1];
2042 this.last_checkpoint = data[data.length - 1];
2044 } else {
2043 } else {
2045 this.last_checkpoint = null;
2044 this.last_checkpoint = null;
2046 }
2045 }
2047 $([IPython.events]).trigger('checkpoints_listed.Notebook', [data]);
2046 $([IPython.events]).trigger('checkpoints_listed.Notebook', [data]);
2048 };
2047 };
2049
2048
2050 /**
2049 /**
2051 * Failure callback for listing a checkpoint.
2050 * Failure callback for listing a checkpoint.
2052 *
2051 *
2053 * @method list_checkpoint_error
2052 * @method list_checkpoint_error
2054 * @param {jqXHR} xhr jQuery Ajax object
2053 * @param {jqXHR} xhr jQuery Ajax object
2055 * @param {String} status Description of response status
2054 * @param {String} status Description of response status
2056 * @param {String} error_msg HTTP error message
2055 * @param {String} error_msg HTTP error message
2057 */
2056 */
2058 Notebook.prototype.list_checkpoints_error = function (xhr, status, error_msg) {
2057 Notebook.prototype.list_checkpoints_error = function (xhr, status, error_msg) {
2059 $([IPython.events]).trigger('list_checkpoints_failed.Notebook');
2058 $([IPython.events]).trigger('list_checkpoints_failed.Notebook');
2060 };
2059 };
2061
2060
2062 /**
2061 /**
2063 * Create a checkpoint of this notebook on the server from the most recent save.
2062 * Create a checkpoint of this notebook on the server from the most recent save.
2064 *
2063 *
2065 * @method create_checkpoint
2064 * @method create_checkpoint
2066 */
2065 */
2067 Notebook.prototype.create_checkpoint = function () {
2066 Notebook.prototype.create_checkpoint = function () {
2068 var url = utils.url_path_join(
2067 var url = utils.url_path_join(
2069 this.baseProjectUrl(),
2068 this.baseProjectUrl(),
2070 'api/notebooks',
2069 'api/notebooks',
2071 this.notebookPath(),
2070 this.notebookPath(),
2072 this.notebook_name,
2071 this.notebook_name,
2073 'checkpoints'
2072 'checkpoints'
2074 );
2073 );
2075 $.post(url).done(
2074 $.post(url).done(
2076 $.proxy(this.create_checkpoint_success, this)
2075 $.proxy(this.create_checkpoint_success, this)
2077 ).fail(
2076 ).fail(
2078 $.proxy(this.create_checkpoint_error, this)
2077 $.proxy(this.create_checkpoint_error, this)
2079 );
2078 );
2080 };
2079 };
2081
2080
2082 /**
2081 /**
2083 * Success callback for creating a checkpoint.
2082 * Success callback for creating a checkpoint.
2084 *
2083 *
2085 * @method create_checkpoint_success
2084 * @method create_checkpoint_success
2086 * @param {Object} data JSON representation of a checkpoint
2085 * @param {Object} data JSON representation of a checkpoint
2087 * @param {String} status Description of response status
2086 * @param {String} status Description of response status
2088 * @param {jqXHR} xhr jQuery Ajax object
2087 * @param {jqXHR} xhr jQuery Ajax object
2089 */
2088 */
2090 Notebook.prototype.create_checkpoint_success = function (data, status, xhr) {
2089 Notebook.prototype.create_checkpoint_success = function (data, status, xhr) {
2091 var data = $.parseJSON(data);
2090 var data = $.parseJSON(data);
2092 this.add_checkpoint(data);
2091 this.add_checkpoint(data);
2093 $([IPython.events]).trigger('checkpoint_created.Notebook', data);
2092 $([IPython.events]).trigger('checkpoint_created.Notebook', data);
2094 };
2093 };
2095
2094
2096 /**
2095 /**
2097 * Failure callback for creating a checkpoint.
2096 * Failure callback for creating a checkpoint.
2098 *
2097 *
2099 * @method create_checkpoint_error
2098 * @method create_checkpoint_error
2100 * @param {jqXHR} xhr jQuery Ajax object
2099 * @param {jqXHR} xhr jQuery Ajax object
2101 * @param {String} status Description of response status
2100 * @param {String} status Description of response status
2102 * @param {String} error_msg HTTP error message
2101 * @param {String} error_msg HTTP error message
2103 */
2102 */
2104 Notebook.prototype.create_checkpoint_error = function (xhr, status, error_msg) {
2103 Notebook.prototype.create_checkpoint_error = function (xhr, status, error_msg) {
2105 $([IPython.events]).trigger('checkpoint_failed.Notebook');
2104 $([IPython.events]).trigger('checkpoint_failed.Notebook');
2106 };
2105 };
2107
2106
2108 Notebook.prototype.restore_checkpoint_dialog = function (checkpoint) {
2107 Notebook.prototype.restore_checkpoint_dialog = function (checkpoint) {
2109 var that = this;
2108 var that = this;
2110 var checkpoint = checkpoint || this.last_checkpoint;
2109 var checkpoint = checkpoint || this.last_checkpoint;
2111 if ( ! checkpoint ) {
2110 if ( ! checkpoint ) {
2112 console.log("restore dialog, but no checkpoint to restore to!");
2111 console.log("restore dialog, but no checkpoint to restore to!");
2113 return;
2112 return;
2114 }
2113 }
2115 var body = $('<div/>').append(
2114 var body = $('<div/>').append(
2116 $('<p/>').addClass("p-space").text(
2115 $('<p/>').addClass("p-space").text(
2117 "Are you sure you want to revert the notebook to " +
2116 "Are you sure you want to revert the notebook to " +
2118 "the latest checkpoint?"
2117 "the latest checkpoint?"
2119 ).append(
2118 ).append(
2120 $("<strong/>").text(
2119 $("<strong/>").text(
2121 " This cannot be undone."
2120 " This cannot be undone."
2122 )
2121 )
2123 )
2122 )
2124 ).append(
2123 ).append(
2125 $('<p/>').addClass("p-space").text("The checkpoint was last updated at:")
2124 $('<p/>').addClass("p-space").text("The checkpoint was last updated at:")
2126 ).append(
2125 ).append(
2127 $('<p/>').addClass("p-space").text(
2126 $('<p/>').addClass("p-space").text(
2128 Date(checkpoint.last_modified)
2127 Date(checkpoint.last_modified)
2129 ).css("text-align", "center")
2128 ).css("text-align", "center")
2130 );
2129 );
2131
2130
2132 IPython.dialog.modal({
2131 IPython.dialog.modal({
2133 title : "Revert notebook to checkpoint",
2132 title : "Revert notebook to checkpoint",
2134 body : body,
2133 body : body,
2135 buttons : {
2134 buttons : {
2136 Revert : {
2135 Revert : {
2137 class : "btn-danger",
2136 class : "btn-danger",
2138 click : function () {
2137 click : function () {
2139 that.restore_checkpoint(checkpoint.checkpoint_id);
2138 that.restore_checkpoint(checkpoint.checkpoint_id);
2140 }
2139 }
2141 },
2140 },
2142 Cancel : {}
2141 Cancel : {}
2143 }
2142 }
2144 });
2143 });
2145 }
2144 }
2146
2145
2147 /**
2146 /**
2148 * Restore the notebook to a checkpoint state.
2147 * Restore the notebook to a checkpoint state.
2149 *
2148 *
2150 * @method restore_checkpoint
2149 * @method restore_checkpoint
2151 * @param {String} checkpoint ID
2150 * @param {String} checkpoint ID
2152 */
2151 */
2153 Notebook.prototype.restore_checkpoint = function (checkpoint) {
2152 Notebook.prototype.restore_checkpoint = function (checkpoint) {
2154 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
2153 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
2155 var url = utils.url_path_join(
2154 var url = utils.url_path_join(
2156 this.baseProjectUrl(),
2155 this.baseProjectUrl(),
2157 'api/notebooks',
2156 'api/notebooks',
2158 this.notebookPath(),
2157 this.notebookPath(),
2159 this.notebook_name,
2158 this.notebook_name,
2160 'checkpoints',
2159 'checkpoints',
2161 checkpoint
2160 checkpoint
2162 );
2161 );
2163 $.post(url).done(
2162 $.post(url).done(
2164 $.proxy(this.restore_checkpoint_success, this)
2163 $.proxy(this.restore_checkpoint_success, this)
2165 ).fail(
2164 ).fail(
2166 $.proxy(this.restore_checkpoint_error, this)
2165 $.proxy(this.restore_checkpoint_error, this)
2167 );
2166 );
2168 };
2167 };
2169
2168
2170 /**
2169 /**
2171 * Success callback for restoring a notebook to a checkpoint.
2170 * Success callback for restoring a notebook to a checkpoint.
2172 *
2171 *
2173 * @method restore_checkpoint_success
2172 * @method restore_checkpoint_success
2174 * @param {Object} data (ignored, should be empty)
2173 * @param {Object} data (ignored, should be empty)
2175 * @param {String} status Description of response status
2174 * @param {String} status Description of response status
2176 * @param {jqXHR} xhr jQuery Ajax object
2175 * @param {jqXHR} xhr jQuery Ajax object
2177 */
2176 */
2178 Notebook.prototype.restore_checkpoint_success = function (data, status, xhr) {
2177 Notebook.prototype.restore_checkpoint_success = function (data, status, xhr) {
2179 $([IPython.events]).trigger('checkpoint_restored.Notebook');
2178 $([IPython.events]).trigger('checkpoint_restored.Notebook');
2180 this.load_notebook(this.notebook_name, this.notebook_path);
2179 this.load_notebook(this.notebook_name, this.notebook_path);
2181 };
2180 };
2182
2181
2183 /**
2182 /**
2184 * Failure callback for restoring a notebook to a checkpoint.
2183 * Failure callback for restoring a notebook to a checkpoint.
2185 *
2184 *
2186 * @method restore_checkpoint_error
2185 * @method restore_checkpoint_error
2187 * @param {jqXHR} xhr jQuery Ajax object
2186 * @param {jqXHR} xhr jQuery Ajax object
2188 * @param {String} status Description of response status
2187 * @param {String} status Description of response status
2189 * @param {String} error_msg HTTP error message
2188 * @param {String} error_msg HTTP error message
2190 */
2189 */
2191 Notebook.prototype.restore_checkpoint_error = function (xhr, status, error_msg) {
2190 Notebook.prototype.restore_checkpoint_error = function (xhr, status, error_msg) {
2192 $([IPython.events]).trigger('checkpoint_restore_failed.Notebook');
2191 $([IPython.events]).trigger('checkpoint_restore_failed.Notebook');
2193 };
2192 };
2194
2193
2195 /**
2194 /**
2196 * Delete a notebook checkpoint.
2195 * Delete a notebook checkpoint.
2197 *
2196 *
2198 * @method delete_checkpoint
2197 * @method delete_checkpoint
2199 * @param {String} checkpoint ID
2198 * @param {String} checkpoint ID
2200 */
2199 */
2201 Notebook.prototype.delete_checkpoint = function (checkpoint) {
2200 Notebook.prototype.delete_checkpoint = function (checkpoint) {
2202 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
2201 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
2203 var url = utils.url_path_join(
2202 var url = utils.url_path_join(
2204 this.baseProjectUrl(),
2203 this.baseProjectUrl(),
2205 'api/notebooks',
2204 'api/notebooks',
2206 this.notebookPath(),
2205 this.notebookPath(),
2207 this.notebook_name,
2206 this.notebook_name,
2208 'checkpoints',
2207 'checkpoints',
2209 checkpoint
2208 checkpoint
2210 );
2209 );
2211 $.ajax(url, {
2210 $.ajax(url, {
2212 type: 'DELETE',
2211 type: 'DELETE',
2213 success: $.proxy(this.delete_checkpoint_success, this),
2212 success: $.proxy(this.delete_checkpoint_success, this),
2214 error: $.proxy(this.delete_notebook_error,this)
2213 error: $.proxy(this.delete_notebook_error,this)
2215 });
2214 });
2216 };
2215 };
2217
2216
2218 /**
2217 /**
2219 * Success callback for deleting a notebook checkpoint
2218 * Success callback for deleting a notebook checkpoint
2220 *
2219 *
2221 * @method delete_checkpoint_success
2220 * @method delete_checkpoint_success
2222 * @param {Object} data (ignored, should be empty)
2221 * @param {Object} data (ignored, should be empty)
2223 * @param {String} status Description of response status
2222 * @param {String} status Description of response status
2224 * @param {jqXHR} xhr jQuery Ajax object
2223 * @param {jqXHR} xhr jQuery Ajax object
2225 */
2224 */
2226 Notebook.prototype.delete_checkpoint_success = function (data, status, xhr) {
2225 Notebook.prototype.delete_checkpoint_success = function (data, status, xhr) {
2227 $([IPython.events]).trigger('checkpoint_deleted.Notebook', data);
2226 $([IPython.events]).trigger('checkpoint_deleted.Notebook', data);
2228 this.load_notebook(this.notebook_name, this.notebook_path);
2227 this.load_notebook(this.notebook_name, this.notebook_path);
2229 };
2228 };
2230
2229
2231 /**
2230 /**
2232 * Failure callback for deleting a notebook checkpoint.
2231 * Failure callback for deleting a notebook checkpoint.
2233 *
2232 *
2234 * @method delete_checkpoint_error
2233 * @method delete_checkpoint_error
2235 * @param {jqXHR} xhr jQuery Ajax object
2234 * @param {jqXHR} xhr jQuery Ajax object
2236 * @param {String} status Description of response status
2235 * @param {String} status Description of response status
2237 * @param {String} error_msg HTTP error message
2236 * @param {String} error_msg HTTP error message
2238 */
2237 */
2239 Notebook.prototype.delete_checkpoint_error = function (xhr, status, error_msg) {
2238 Notebook.prototype.delete_checkpoint_error = function (xhr, status, error_msg) {
2240 $([IPython.events]).trigger('checkpoint_delete_failed.Notebook');
2239 $([IPython.events]).trigger('checkpoint_delete_failed.Notebook');
2241 };
2240 };
2242
2241
2243
2242
2244 IPython.Notebook = Notebook;
2243 IPython.Notebook = Notebook;
2245
2244
2246
2245
2247 return IPython;
2246 return IPython;
2248
2247
2249 }(IPython));
2248 }(IPython));
@@ -1,397 +1,396 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2011 The IPython Development Team
2 // Copyright (C) 2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // NotebookList
9 // NotebookList
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13 "use strict";
13 "use strict";
14
14
15 var utils = IPython.utils;
15 var utils = IPython.utils;
16
16
17 var NotebookList = function (selector) {
17 var NotebookList = function (selector) {
18 this.selector = selector;
18 this.selector = selector;
19 if (this.selector !== undefined) {
19 if (this.selector !== undefined) {
20 this.element = $(selector);
20 this.element = $(selector);
21 this.style();
21 this.style();
22 this.bind_events();
22 this.bind_events();
23 }
23 }
24 this.notebooks_list = [];
24 this.notebooks_list = [];
25 this.sessions = {};
25 this.sessions = {};
26 };
26 };
27
27
28 NotebookList.prototype.baseProjectUrl = function () {
28 NotebookList.prototype.baseProjectUrl = function () {
29 return $('body').data('baseProjectUrl');
29 return $('body').data('baseProjectUrl');
30 };
30 };
31
31
32 NotebookList.prototype.notebookPath = function() {
32 NotebookList.prototype.notebookPath = function() {
33 return $('body').data('notebookPath');
33 return $('body').data('notebookPath');
34 };
34 };
35
35
36 NotebookList.prototype.style = function () {
36 NotebookList.prototype.style = function () {
37 $('#notebook_toolbar').addClass('list_toolbar');
37 $('#notebook_toolbar').addClass('list_toolbar');
38 $('#drag_info').addClass('toolbar_info');
38 $('#drag_info').addClass('toolbar_info');
39 $('#notebook_buttons').addClass('toolbar_buttons');
39 $('#notebook_buttons').addClass('toolbar_buttons');
40 $('#notebook_list_header').addClass('list_header');
40 $('#notebook_list_header').addClass('list_header');
41 this.element.addClass("list_container");
41 this.element.addClass("list_container");
42 };
42 };
43
43
44
44
45 NotebookList.prototype.bind_events = function () {
45 NotebookList.prototype.bind_events = function () {
46 var that = this;
46 var that = this;
47 $('#refresh_notebook_list').click(function () {
47 $('#refresh_notebook_list').click(function () {
48 that.load_list();
48 that.load_list();
49 });
49 });
50 this.element.bind('dragover', function () {
50 this.element.bind('dragover', function () {
51 return false;
51 return false;
52 });
52 });
53 this.element.bind('drop', function(event){
53 this.element.bind('drop', function(event){
54 that.handelFilesUpload(event,'drop');
54 that.handelFilesUpload(event,'drop');
55 return false;
55 return false;
56 });
56 });
57 };
57 };
58
58
59 NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) {
59 NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) {
60 var that = this;
60 var that = this;
61 var files;
61 var files;
62 if(dropOrForm =='drop'){
62 if(dropOrForm =='drop'){
63 files = event.originalEvent.dataTransfer.files;
63 files = event.originalEvent.dataTransfer.files;
64 } else
64 } else
65 {
65 {
66 files = event.originalEvent.target.files;
66 files = event.originalEvent.target.files;
67 }
67 }
68 for (var i = 0; i < files.length; i++) {
68 for (var i = 0; i < files.length; i++) {
69 var f = files[i];
69 var f = files[i];
70 var reader = new FileReader();
70 var reader = new FileReader();
71 reader.readAsText(f);
71 reader.readAsText(f);
72 var fname = f.name.split('.');
72 var fname = f.name.split('.');
73 var nbname = fname.slice(0,-1).join('.');
73 var nbname = fname.slice(0,-1).join('.');
74 var nbformat = fname.slice(-1)[0];
74 var nbformat = fname.slice(-1)[0];
75 if (nbformat === 'ipynb') {nbformat = 'json';}
75 if (nbformat === 'ipynb') {nbformat = 'json';}
76 if (nbformat === 'py' || nbformat === 'json') {
76 if (nbformat === 'py' || nbformat === 'json') {
77 var item = that.new_notebook_item(0);
77 var item = that.new_notebook_item(0);
78 that.add_name_input(nbname, item);
78 that.add_name_input(nbname, item);
79 item.data('nbformat', nbformat);
79 item.data('nbformat', nbformat);
80 // Store the notebook item in the reader so we can use it later
80 // Store the notebook item in the reader so we can use it later
81 // to know which item it belongs to.
81 // to know which item it belongs to.
82 $(reader).data('item', item);
82 $(reader).data('item', item);
83 reader.onload = function (event) {
83 reader.onload = function (event) {
84 var nbitem = $(event.target).data('item');
84 var nbitem = $(event.target).data('item');
85 that.add_notebook_data(event.target.result, nbitem);
85 that.add_notebook_data(event.target.result, nbitem);
86 that.add_upload_button(nbitem);
86 that.add_upload_button(nbitem);
87 };
87 };
88 }
88 }
89 }
89 }
90 return false;
90 return false;
91 };
91 };
92
92
93 NotebookList.prototype.clear_list = function () {
93 NotebookList.prototype.clear_list = function () {
94 this.element.children('.list_item').remove();
94 this.element.children('.list_item').remove();
95 };
95 };
96
96
97 NotebookList.prototype.load_sessions = function(){
97 NotebookList.prototype.load_sessions = function(){
98 var that = this;
98 var that = this;
99 var settings = {
99 var settings = {
100 processData : false,
100 processData : false,
101 cache : false,
101 cache : false,
102 type : "GET",
102 type : "GET",
103 dataType : "json",
103 dataType : "json",
104 success : $.proxy(that.sessions_loaded, this)
104 success : $.proxy(that.sessions_loaded, this)
105 };
105 };
106 var url = this.baseProjectUrl() + 'api/sessions';
106 var url = this.baseProjectUrl() + 'api/sessions';
107 $.ajax(url,settings);
107 $.ajax(url,settings);
108 };
108 };
109
109
110
110
111 NotebookList.prototype.sessions_loaded = function(data){
111 NotebookList.prototype.sessions_loaded = function(data){
112 this.sessions = {};
112 this.sessions = {};
113 var len = data.length;
113 var len = data.length;
114 if (len > 0) {
114 if (len > 0) {
115 for (var i=0; i<len; i++) {
115 for (var i=0; i<len; i++) {
116 var nb_path;
116 var nb_path;
117 if (!data[i].notebook.path) {
117 if (!data[i].notebook.path) {
118 nb_path = data[i].notebook.name;
118 nb_path = data[i].notebook.name;
119 }
119 }
120 else {
120 else {
121 nb_path = utils.url_path_join(
121 nb_path = utils.url_path_join(
122 data[i].notebook.path,
122 data[i].notebook.path,
123 data[i].notebook.name
123 data[i].notebook.name
124 );
124 );
125 }
125 }
126 this.sessions[nb_path] = data[i].id;
126 this.sessions[nb_path] = data[i].id;
127 }
127 }
128 }
128 }
129 this.load_list();
129 this.load_list();
130 };
130 };
131
131
132 NotebookList.prototype.load_list = function () {
132 NotebookList.prototype.load_list = function () {
133 var that = this;
133 var that = this;
134 var settings = {
134 var settings = {
135 processData : false,
135 processData : false,
136 cache : false,
136 cache : false,
137 type : "GET",
137 type : "GET",
138 dataType : "json",
138 dataType : "json",
139 success : $.proxy(this.list_loaded, this),
139 success : $.proxy(this.list_loaded, this),
140 error : $.proxy( function(){
140 error : $.proxy( function(){
141 that.list_loaded([], null, null, {msg:"Error connecting to server."});
141 that.list_loaded([], null, null, {msg:"Error connecting to server."});
142 },this)
142 },this)
143 };
143 };
144
144
145 var url = utils.url_path_join(
145 var url = utils.url_path_join(
146 this.baseProjectUrl(),
146 this.baseProjectUrl(),
147 'api',
147 'api',
148 'notebooks',
148 'notebooks',
149 this.notebookPath()
149 this.notebookPath()
150 );
150 );
151 $.ajax(url, settings);
151 $.ajax(url, settings);
152 };
152 };
153
153
154
154
155 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
155 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
156 var message = 'Notebook list empty.';
156 var message = 'Notebook list empty.';
157 if (param !== undefined && param.msg) {
157 if (param !== undefined && param.msg) {
158 message = param.msg;
158 message = param.msg;
159 }
159 }
160 var len = data.length;
160 var len = data.length;
161 this.clear_list();
161 this.clear_list();
162 if (len === 0) {
162 if (len === 0) {
163 $(this.new_notebook_item(0))
163 $(this.new_notebook_item(0))
164 .append(
164 .append(
165 $('<div style="margin:auto;text-align:center;color:grey"/>')
165 $('<div style="margin:auto;text-align:center;color:grey"/>')
166 .text(message)
166 .text(message)
167 );
167 );
168 }
168 }
169 for (var i=0; i<len; i++) {
169 for (var i=0; i<len; i++) {
170 var name = data[i].name;
170 var name = data[i].name;
171 var path = this.notebookPath();
171 var path = this.notebookPath();
172 var nbname = name.split(".")[0];
172 var nbname = name.split(".")[0];
173 var item = this.new_notebook_item(i);
173 var item = this.new_notebook_item(i);
174 this.add_link(path, nbname, item);
174 this.add_link(path, nbname, item);
175 name = utils.url_path_join(this.notebookPath(), name);
175 name = utils.url_path_join(this.notebookPath(), name);
176 if(this.sessions[name] === undefined){
176 if(this.sessions[name] === undefined){
177 this.add_delete_button(item);
177 this.add_delete_button(item);
178 } else {
178 } else {
179 this.add_shutdown_button(item,this.sessions[name]);
179 this.add_shutdown_button(item,this.sessions[name]);
180 }
180 }
181 }
181 }
182 };
182 };
183
183
184
184
185 NotebookList.prototype.new_notebook_item = function (index) {
185 NotebookList.prototype.new_notebook_item = function (index) {
186 var item = $('<div/>').addClass("list_item").addClass("row-fluid");
186 var item = $('<div/>').addClass("list_item").addClass("row-fluid");
187 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
187 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
188 // item.css('border-top-style','none');
188 // item.css('border-top-style','none');
189 item.append($("<div/>").addClass("span12").append(
189 item.append($("<div/>").addClass("span12").append(
190 $("<a/>").addClass("item_link").append(
190 $("<a/>").addClass("item_link").append(
191 $("<span/>").addClass("item_name")
191 $("<span/>").addClass("item_name")
192 )
192 )
193 ).append(
193 ).append(
194 $('<div/>').addClass("item_buttons btn-group pull-right")
194 $('<div/>').addClass("item_buttons btn-group pull-right")
195 ));
195 ));
196
196
197 if (index === -1) {
197 if (index === -1) {
198 this.element.append(item);
198 this.element.append(item);
199 } else {
199 } else {
200 this.element.children().eq(index).after(item);
200 this.element.children().eq(index).after(item);
201 }
201 }
202 return item;
202 return item;
203 };
203 };
204
204
205
205
206 NotebookList.prototype.add_link = function (path, nbname, item) {
206 NotebookList.prototype.add_link = function (path, nbname, item) {
207 item.data('nbname', nbname);
207 item.data('nbname', nbname);
208 item.data('path', path);
208 item.data('path', path);
209 item.find(".item_name").text(nbname);
209 item.find(".item_name").text(nbname);
210 item.find("a.item_link")
210 item.find("a.item_link")
211 .attr('href',
211 .attr('href',
212 utils.url_path_join(
212 utils.url_path_join(
213 this.baseProjectUrl(),
213 this.baseProjectUrl(),
214 "notebooks",
214 "notebooks",
215 this.notebookPath(),
215 this.notebookPath(),
216 nbname + ".ipynb"
216 nbname + ".ipynb"
217 )
217 )
218 ).attr('target','_blank');
218 ).attr('target','_blank');
219 };
219 };
220
220
221
221
222 NotebookList.prototype.add_name_input = function (nbname, item) {
222 NotebookList.prototype.add_name_input = function (nbname, item) {
223 item.data('nbname', nbname);
223 item.data('nbname', nbname);
224 item.find(".item_name").empty().append(
224 item.find(".item_name").empty().append(
225 $('<input/>')
225 $('<input/>')
226 .addClass("nbname_input")
226 .addClass("nbname_input")
227 .attr('value', nbname)
227 .attr('value', nbname)
228 .attr('size', '30')
228 .attr('size', '30')
229 .attr('type', 'text')
229 .attr('type', 'text')
230 );
230 );
231 };
231 };
232
232
233
233
234 NotebookList.prototype.add_notebook_data = function (data, item) {
234 NotebookList.prototype.add_notebook_data = function (data, item) {
235 item.data('nbdata', data);
235 item.data('nbdata', data);
236 };
236 };
237
237
238
238
239 NotebookList.prototype.add_shutdown_button = function (item, session) {
239 NotebookList.prototype.add_shutdown_button = function (item, session) {
240 var that = this;
240 var that = this;
241 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini").
241 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini").
242 click(function (e) {
242 click(function (e) {
243 var settings = {
243 var settings = {
244 processData : false,
244 processData : false,
245 cache : false,
245 cache : false,
246 type : "DELETE",
246 type : "DELETE",
247 dataType : "json",
247 dataType : "json",
248 success : function () {
248 success : function () {
249 that.load_sessions();
249 that.load_sessions();
250 }
250 }
251 };
251 };
252 var url = utils.url_path_join(
252 var url = utils.url_path_join(
253 that.baseProjectUrl(),
253 that.baseProjectUrl(),
254 'api/sessions',
254 'api/sessions',
255 session
255 session
256 );
256 );
257 $.ajax(url, settings);
257 $.ajax(url, settings);
258 return false;
258 return false;
259 });
259 });
260 // var new_buttons = item.find('a'); // shutdown_button;
260 // var new_buttons = item.find('a'); // shutdown_button;
261 item.find(".item_buttons").html("").append(shutdown_button);
261 item.find(".item_buttons").html("").append(shutdown_button);
262 };
262 };
263
263
264 NotebookList.prototype.add_delete_button = function (item) {
264 NotebookList.prototype.add_delete_button = function (item) {
265 var new_buttons = $('<span/>').addClass("btn-group pull-right");
265 var new_buttons = $('<span/>').addClass("btn-group pull-right");
266 var notebooklist = this;
266 var notebooklist = this;
267 var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
267 var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
268 click(function (e) {
268 click(function (e) {
269 // $(this) is the button that was clicked.
269 // $(this) is the button that was clicked.
270 var that = $(this);
270 var that = $(this);
271 // We use the nbname and notebook_id from the parent notebook_item element's
271 // We use the nbname and notebook_id from the parent notebook_item element's
272 // data because the outer scopes values change as we iterate through the loop.
272 // data because the outer scopes values change as we iterate through the loop.
273 var parent_item = that.parents('div.list_item');
273 var parent_item = that.parents('div.list_item');
274 var nbname = parent_item.data('nbname');
274 var nbname = parent_item.data('nbname');
275 var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
275 var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
276 IPython.dialog.modal({
276 IPython.dialog.modal({
277 title : "Delete notebook",
277 title : "Delete notebook",
278 body : message,
278 body : message,
279 buttons : {
279 buttons : {
280 Delete : {
280 Delete : {
281 class: "btn-danger",
281 class: "btn-danger",
282 click: function() {
282 click: function() {
283 var settings = {
283 var settings = {
284 processData : false,
284 processData : false,
285 cache : false,
285 cache : false,
286 type : "DELETE",
286 type : "DELETE",
287 dataType : "json",
287 dataType : "json",
288 success : function (data, status, xhr) {
288 success : function (data, status, xhr) {
289 parent_item.remove();
289 parent_item.remove();
290 }
290 }
291 };
291 };
292 var url = utils.url_path_join(
292 var url = utils.url_path_join(
293 notebooklist.baseProjectUrl(),
293 notebooklist.baseProjectUrl(),
294 'api/notebooks',
294 'api/notebooks',
295 notebooklist.notebookPath(),
295 notebooklist.notebookPath(),
296 nbname + '.ipynb'
296 nbname + '.ipynb'
297 );
297 );
298 $.ajax(url, settings);
298 $.ajax(url, settings);
299 }
299 }
300 },
300 },
301 Cancel : {}
301 Cancel : {}
302 }
302 }
303 });
303 });
304 return false;
304 return false;
305 });
305 });
306 item.find(".item_buttons").html("").append(delete_button);
306 item.find(".item_buttons").html("").append(delete_button);
307 };
307 };
308
308
309
309
310 NotebookList.prototype.add_upload_button = function (item) {
310 NotebookList.prototype.add_upload_button = function (item) {
311 var that = this;
311 var that = this;
312 var upload_button = $('<button/>').text("Upload")
312 var upload_button = $('<button/>').text("Upload")
313 .addClass('btn btn-primary btn-mini upload_button')
313 .addClass('btn btn-primary btn-mini upload_button')
314 .click(function (e) {
314 .click(function (e) {
315 var nbname = item.find('.item_name > input').attr('value');
315 var nbname = item.find('.item_name > input').val();
316 var nbformat = item.data('nbformat');
316 var nbformat = item.data('nbformat');
317 var nbdata = item.data('nbdata');
317 var nbdata = item.data('nbdata');
318 var content_type = 'application/json';
318 var content_type = 'application/json';
319 if (nbformat === 'json') {
319 if (nbformat === 'json') {
320 // pass
320 // pass
321 } else if (nbformat === 'py') {
321 } else if (nbformat === 'py') {
322 // TODO: re-enable non-ipynb upload
322 // TODO: re-enable non-ipynb upload
323 }
323 }
324 var model = {
324 var model = {
325 content : JSON.parse(nbdata),
325 content : JSON.parse(nbdata),
326 name : nbname,
327 path : that.notebookPath()
328 };
326 };
329 var settings = {
327 var settings = {
330 processData : false,
328 processData : false,
331 cache : false,
329 cache : false,
332 type : 'POST',
330 type : 'POST',
333 dataType : 'json',
331 dataType : 'json',
334 data : JSON.stringify(model),
332 data : JSON.stringify(model),
335 headers : {'Content-Type': content_type},
333 headers : {'Content-Type': content_type},
336 success : function (data, status, xhr) {
334 success : function (data, status, xhr) {
337 that.add_link(data, nbname, item);
335 that.add_link(data, nbname, item);
338 that.add_delete_button(item);
336 that.add_delete_button(item);
339 },
337 },
340 error : function (data, status, xhr) {
338 error : function (data, status, xhr) {
341 console.log(data, status);
339 console.log(data, status);
342 }
340 }
343 };
341 };
344
342
345 var url = utils.url_path_join(
343 var url = utils.url_path_join(
346 that.baseProjectUrl(),
344 that.baseProjectUrl(),
347 'api/notebooks',
345 'api/notebooks',
348 that.notebookPath()
346 that.notebookPath(),
347 nbname + '.ipynb'
349 );
348 );
350 $.ajax(url, settings);
349 $.ajax(url, settings);
351 return false;
350 return false;
352 });
351 });
353 var cancel_button = $('<button/>').text("Cancel")
352 var cancel_button = $('<button/>').text("Cancel")
354 .addClass("btn btn-mini")
353 .addClass("btn btn-mini")
355 .click(function (e) {
354 .click(function (e) {
356 console.log('cancel click');
355 console.log('cancel click');
357 item.remove();
356 item.remove();
358 return false;
357 return false;
359 });
358 });
360 item.find(".item_buttons").empty()
359 item.find(".item_buttons").empty()
361 .append(upload_button)
360 .append(upload_button)
362 .append(cancel_button);
361 .append(cancel_button);
363 };
362 };
364
363
365
364
366 NotebookList.prototype.new_notebook = function(){
365 NotebookList.prototype.new_notebook = function(){
367 var path = this.notebookPath();
366 var path = this.notebookPath();
368 var settings = {
367 var settings = {
369 processData : false,
368 processData : false,
370 cache : false,
369 cache : false,
371 type : "POST",
370 type : "POST",
372 dataType : "json",
371 dataType : "json",
373 success:$.proxy(function (data, status, xhr){
372 success:$.proxy(function (data, status, xhr){
374 var notebook_name = data.name;
373 var notebook_name = data.name;
375 window.open(
374 window.open(
376 utils.url_path_join(
375 utils.url_path_join(
377 this.baseProjectUrl(),
376 this.baseProjectUrl(),
378 'notebooks',
377 'notebooks',
379 path,
378 path,
380 notebook_name),
379 notebook_name),
381 '_blank'
380 '_blank'
382 );
381 );
383 }, this)
382 }, this)
384 };
383 };
385 var url = utils.url_path_join(
384 var url = utils.url_path_join(
386 this.baseProjectUrl(),
385 this.baseProjectUrl(),
387 'api/notebooks',
386 'api/notebooks',
388 path
387 path
389 );
388 );
390 $.ajax(url, settings);
389 $.ajax(url, settings);
391 };
390 };
392
391
393 IPython.NotebookList = NotebookList;
392 IPython.NotebookList = NotebookList;
394
393
395 return IPython;
394 return IPython;
396
395
397 }(IPython));
396 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now