From 86aafe6dc9c08dc875ae698c2d01f9ee0e0dc75d 2014-11-20 19:52:35
From: Thomas Kluyver <takowl@gmail.com>
Date: 2014-11-20 19:52:35
Subject: [PATCH] Update text editor for new contents API

---

diff --git a/IPython/html/static/texteditor/js/editor.js b/IPython/html/static/texteditor/js/editor.js
index 8f5d8e8..a373a41 100644
--- a/IPython/html/static/texteditor/js/editor.js
+++ b/IPython/html/static/texteditor/js/editor.js
@@ -21,56 +21,46 @@ function($,
         
         this.codemirror = CodeMirror($(this.selector)[0]);
         
-        this.save_enabled = true;
-    };
-    
-    // TODO: Remove this once the contents API is refactored to just use paths
-    Editor.prototype._split_path = function() {
-        var ix = this.file_path.lastIndexOf("/");
-        if (ix === -1) {
-            return ['', this.file_path];
-        } else {
-            return [
-                this.file_path.substring(0, ix),
-                this.file_path.substring(ix+1)
-            ];
-        }
+        this.save_enabled = false;
     };
     
     Editor.prototype.load = function() {
-        var split_path = this._split_path();
+        var that = this;
         var cm = this.codemirror;
-        this.contents.load(split_path[0], split_path[1], {
-            success: function(model) {
-                if (model.type === "file" && model.format === "text") {
-                    cm.setValue(model.content);
-                    
-                    // Find and load the highlighting mode
-                    var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
-                    if (modeinfo) {
-                        utils.requireCodeMirrorMode(modeinfo.mode, function() {
-                            cm.setOption('mode', modeinfo.mode);
-                        });
-                    }
-                } else {
-                    this.codemirror.setValue("Error! Not a text file. Saving disabled.");
-                    this.save_enabled = false;
+        this.contents.get(this.file_path, {type: 'file', format: 'text'})
+            .then(function(model) {
+                cm.setValue(model.content);
+                
+                // Find and load the highlighting mode
+                var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
+                if (modeinfo) {
+                    utils.requireCodeMirrorMode(modeinfo.mode, function() {
+                        cm.setOption('mode', modeinfo.mode);
+                    });
                 }
+                that.save_enabled = true;
+            },
+            function(error) {
+                cm.setValue("Error! " + error.message +
+                                "\nSaving disabled.");
+                that.save_enabled = false;
             }
-        });
+        );
     };
 
     Editor.prototype.save = function() {
-        var split_path = this._split_path();
+        if (!this.save_enabled) {
+            console.log("Not saving, save disabled");
+            return;
+        }
         var model = {
-            path: split_path[0],
-            name: split_path[1],
+            path: this.file_path,
             type: 'file',
             format: 'text',
             content: this.codemirror.getValue(),
         };
         var that = this;
-        this.contents.save(split_path[0], split_path[1], model, {
+        this.contents.save(this.file_path, model, {
             success: function() {
                 that.events.trigger("save_succeeded.TextEditor");
             }
diff --git a/IPython/html/texteditor/handlers.py b/IPython/html/texteditor/handlers.py
index 199c545..a202685 100644
--- a/IPython/html/texteditor/handlers.py
+++ b/IPython/html/texteditor/handlers.py
@@ -5,23 +5,22 @@
 # Distributed under the terms of the Modified BSD License.
 
 from tornado import web
-from ..base.handlers import IPythonHandler, file_path_regex
+from ..base.handlers import IPythonHandler, path_regex
 from ..utils import url_escape
 
 class EditorHandler(IPythonHandler):
-    """Render the terminal interface."""
+    """Render the text editor interface."""
     @web.authenticated
-    def get(self, path, name):
+    def get(self, path):
         path = path.strip('/')
-        if not self.contents_manager.file_exists(name, path):
-            raise web.HTTPError(404, u'File does not exist: %s/%s' % (path, name))
+        if not self.contents_manager.file_exists(path):
+            raise web.HTTPError(404, u'File does not exist: %s' % path)
 
-        file_path = url_escape(path) + "/" + url_escape(name)
         self.write(self.render_template('texteditor.html',
-            file_path=file_path,
+            file_path=url_escape(path),
             )
         )
 
 default_handlers = [
-    (r"/texteditor%s" % file_path_regex, EditorHandler),
+    (r"/texteditor%s" % path_regex, EditorHandler),
 ]
\ No newline at end of file