##// END OF EJS Templates
Refactor editor into Editor class
Thomas Kluyver -
Show More
@@ -0,0 +1,80
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
3
4 define([
5 'jquery',
6 'base/js/utils',
7 'codemirror/lib/codemirror',
8 'codemirror/mode/meta',
9 ],
10 function($,
11 utils,
12 CodeMirror
13 ) {
14 var Editor = function(selector, options) {
15 this.selector = selector;
16 this.contents = options.contents;
17 this.events = options.events;
18 this.base_url = options.base_url;
19 this.file_path = options.file_path;
20
21 this.codemirror = CodeMirror($(this.selector)[0]);
22
23 this.save_enabled = true;
24 };
25
26 // TODO: Remove this once the contents API is refactored to just use paths
27 Editor.prototype._split_path = function() {
28 var ix = this.file_path.lastIndexOf("/");
29 if (ix === -1) {
30 return ['', this.file_path];
31 } else {
32 return [
33 this.file_path.substring(0, ix),
34 this.file_path.substring(ix+1)
35 ];
36 }
37 };
38
39 Editor.prototype.load = function() {
40 var split_path = this._split_path();
41 var cm = this.codemirror;
42 this.contents.load(split_path[0], split_path[1], {
43 success: function(model) {
44 if (model.type === "file" && model.format === "text") {
45 cm.setValue(model.content);
46
47 // Find and load the highlighting mode
48 var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
49 if (modeinfo) {
50 utils.requireCodeMirrorMode(modeinfo.mode, function() {
51 cm.setOption('mode', modeinfo.mode);
52 });
53 }
54 } else {
55 this.codemirror.setValue("Error! Not a text file. Saving disabled.");
56 this.save_enabled = false;
57 }
58 }
59 });
60 };
61
62 Editor.prototype.save = function() {
63 var split_path = this._split_path();
64 var model = {
65 path: split_path[0],
66 name: split_path[1],
67 type: 'file',
68 format: 'text',
69 content: this.codemirror.getValue(),
70 };
71 var that = this;
72 this.contents.save(split_path[0], split_path[1], model, {
73 success: function() {
74 that.events.trigger("save_succeeded.TextEditor");
75 }
76 });
77 };
78
79 return {Editor: Editor};
80 });
@@ -1,72 +1,47
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 require([
4 require([
5 'jquery',
5 'jquery',
6 'base/js/namespace',
6 'base/js/utils',
7 'base/js/utils',
7 'base/js/page',
8 'base/js/page',
8 'base/js/events',
9 'base/js/events',
9 'contents',
10 'contents',
10 'codemirror/lib/codemirror',
11 'texteditor/js/editor',
11 'texteditor/js/menubar',
12 'texteditor/js/menubar',
12 'codemirror/mode/meta',
13 'custom/custom',
13 'custom/custom',
14 ], function(
14 ], function(
15 $,
15 $,
16 IPython,
16 utils,
17 utils,
17 page,
18 page,
18 events,
19 events,
19 contents,
20 contents,
20 CodeMirror,
21 editor,
21 menubar
22 menubar
22 ){
23 ){
23 page = new page.Page();
24 page = new page.Page();
24
25
25 var base_url = utils.get_body_data('baseUrl');
26 var base_url = utils.get_body_data('baseUrl');
26 contents = new contents.Contents({base_url: base_url});
27
28 var file_path = utils.get_body_data('filePath');
27 var file_path = utils.get_body_data('filePath');
29 var ix = file_path.lastIndexOf("/");
28 contents = new contents.Contents({base_url: base_url});
30 var dir_path, basename;
29
31 if (ix == -1) {
30 var editor = new editor.Editor('#texteditor-container', {
32 dir_path = '';
31 base_url: base_url,
33 basename = file_path;
32 events: events,
34 } else {
33 contents: contents,
35 dir_path = file_path.substring(0, ix);
34 file_path: file_path,
36 basename = file_path.substring(ix+1);
37 }
38 contents.load(dir_path, basename, {
39 success: function(model) {
40 page.show();
41 if (model.type === "file" && model.format === "text") {
42 console.log(modeinfo);
43 var cm = CodeMirror($('#texteditor-container')[0], {
44 value: model.content,
45 });
46
47 var menus = new menubar.MenuBar('#menubar', {
48 base_url: base_url,
49 codemirror: cm,
50 contents: contents,
51 events: events,
52 file_path: file_path
53 });
54
55 // Find and load the highlighting mode
56 var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
57 if (modeinfo) {
58 utils.requireCodeMirrorMode(modeinfo.mode, function() {
59 cm.setOption('mode', modeinfo.mode);
60 });
61 }
62
63 // Attach to document for debugging
64 document.cm_instance = cm;
65 } else {
66 $('#texteditor-container').append(
67 $('<p/>').text(dir_path + " is not a text file")
68 );
69 }
70 }
71 });
35 });
36
37 // Make it available for debugging
38 IPython.editor = editor;
39
40 var menus = new menubar.MenuBar('#menubar', {
41 base_url: base_url,
42 editor: editor,
43 });
44
45 editor.load();
46 page.show();
72 });
47 });
@@ -1,70 +1,46
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 'bootstrap',
8 'bootstrap',
9 ], function(IPython, $, utils, bootstrap) {
9 ], function(IPython, $, utils, bootstrap) {
10 "use strict";
10 "use strict";
11
11
12 var MenuBar = function (selector, options) {
12 var MenuBar = function (selector, options) {
13 // Constructor
13 // Constructor
14 //
14 //
15 // A MenuBar Class to generate the menubar of IPython notebook
15 // A MenuBar Class to generate the menubar of IPython notebook
16 //
16 //
17 // Parameters:
17 // Parameters:
18 // selector: string
18 // selector: string
19 // options: dictionary
19 // options: dictionary
20 // Dictionary of keyword arguments.
20 // Dictionary of keyword arguments.
21 // codemirror: CodeMirror instance
21 // codemirror: CodeMirror instance
22 // contents: ContentManager instance
22 // contents: ContentManager instance
23 // events: $(Events) instance
23 // events: $(Events) instance
24 // base_url : string
24 // base_url : string
25 // file_path : string
25 // file_path : string
26 options = options || {};
26 options = options || {};
27 this.base_url = options.base_url || utils.get_body_data("baseUrl");
27 this.base_url = options.base_url || utils.get_body_data("baseUrl");
28 this.selector = selector;
28 this.selector = selector;
29 this.codemirror = options.codemirror;
29 this.editor = options.editor;
30 this.contents = options.contents;
31 this.events = options.events;
32 this.file_path = options.file_path;
33
30
34 if (this.selector !== undefined) {
31 if (this.selector !== undefined) {
35 this.element = $(selector);
32 this.element = $(selector);
36 this.bind_events();
33 this.bind_events();
37 }
34 }
38 };
35 };
39
36
40 MenuBar.prototype.bind_events = function () {
37 MenuBar.prototype.bind_events = function () {
41 // File
38 // File
42 var that = this;
39 var that = this;
43 this.element.find('#save_file').click(function () {
40 this.element.find('#save_file').click(function () {
44 var ix = that.file_path.lastIndexOf("/");
41 that.editor.save();
45 var dir_path, basename;
46 if (ix === -1) {
47 dir_path = '';
48 basename = that.file_path;
49 } else {
50 dir_path = that.file_path.substring(0, ix);
51 basename = that.file_path.substring(ix+1);
52 }
53 var model = {
54 path: dir_path,
55 name: basename,
56 type: 'file',
57 format: 'text',
58 content: that.codemirror.getValue(),
59 };
60 console.log(model);
61 that.contents.save(dir_path, basename, model, {
62 success: function() {
63 that.events.trigger("save_succeeded.TextEditor");
64 }
65 });
66 });
42 });
67 };
43 };
68
44
69 return {'MenuBar': MenuBar};
45 return {'MenuBar': MenuBar};
70 });
46 });
General Comments 0
You need to be logged in to leave comments. Login now