##// END OF EJS Templates
DOC: Add comment.
Scott Sanderson -
Show More
@@ -1,193 +1,195 b''
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3
4 4 define([
5 5 'jquery',
6 6 'base/js/utils',
7 7 'codemirror/lib/codemirror',
8 8 'codemirror/mode/meta',
9 9 'codemirror/addon/comment/comment',
10 10 'codemirror/addon/dialog/dialog',
11 11 'codemirror/addon/edit/closebrackets',
12 12 'codemirror/addon/edit/matchbrackets',
13 13 'codemirror/addon/search/searchcursor',
14 14 'codemirror/addon/search/search',
15 15 'codemirror/keymap/emacs',
16 16 'codemirror/keymap/sublime',
17 17 'codemirror/keymap/vim',
18 18 ],
19 19 function($,
20 20 utils,
21 21 CodeMirror
22 22 ) {
23 23 "use strict";
24 24
25 25 var Editor = function(selector, options) {
26 26 var that = this;
27 27 this.selector = selector;
28 28 this.contents = options.contents;
29 29 this.events = options.events;
30 30 this.base_url = options.base_url;
31 31 this.file_path = options.file_path;
32 32 this.config = options.config;
33 33 this.codemirror = new CodeMirror($(this.selector)[0]);
34 34 this.generation = -1;
35 35
36 36 // It appears we have to set commands on the CodeMirror class, not the
37 37 // instance. I'd like to be wrong, but since there should only be one CM
38 38 // instance on the page, this is good enough for now.
39 39 CodeMirror.commands.save = $.proxy(this.save, this);
40 40
41 41 this.save_enabled = false;
42 42
43 43 this.config.loaded.then(function () {
44 44 // load codemirror config
45 45 var cfg = that.config.data.Editor || {};
46 46 var cmopts = $.extend(true, {}, // true = recursive copy
47 47 Editor.default_codemirror_options,
48 48 cfg.codemirror_options || {}
49 49 );
50 50 that._set_codemirror_options(cmopts);
51 51 that.events.trigger('config_changed.Editor', {config: that.config});
52 52 });
53 53 };
54 54
55 55 // default CodeMirror options
56 56 Editor.default_codemirror_options = {
57 57 extraKeys: {
58 58 "Tab" : "indentMore",
59 59 },
60 60 indentUnit: 4,
61 61 theme: "ipython",
62 62 lineNumbers: true,
63 63 lineWrapping: true,
64 64 };
65 65
66 66 Editor.prototype.load = function() {
67 67 /** load the file */
68 68 var that = this;
69 69 var cm = this.codemirror;
70 70 return this.contents.get(this.file_path, {type: 'file', format: 'text'})
71 71 .then(function(model) {
72 72 cm.setValue(model.content);
73 73
74 74 // Setting the file's initial value creates a history entry,
75 75 // which we don't want.
76 76 cm.clearHistory();
77 77 that._set_mode_for_model(model);
78 78 that.save_enabled = true;
79 79 that.generation = cm.changeGeneration();
80 80 that.events.trigger("file_loaded.Editor", model);
81 81 },
82 82 function(error) {
83 83 that.events.trigger("file_load_failed.Editor", error);
84 84 if (error.xhr.responseJSON.reason === 'bad format') {
85 85 window.location = utils.url_path_join(
86 86 that.base_url,
87 87 'files',
88 88 that.file_path
89 89 );
90 90 }
91 91 cm.setValue("Error! " + error.message +
92 92 "\nSaving disabled.");
93 93 that.save_enabled = false;
94 94 }
95 95 );
96 96 };
97 97
98 98 Editor.prototype._set_mode_for_model = function (model) {
99 99 /** Set the CodeMirror mode based on the file model */
100 100
101 101 // Find and load the highlighting mode,
102 102 // first by mime-type, then by file extension
103
103 104 var modeinfo;
105 // mimetype is unset on file rename
104 106 if (model.mimetype) {
105 107 modeinfo = CodeMirror.findModeByMIME(model.mimetype);
106 108 }
107 109 if (!modeinfo || modeinfo.mode === "null") {
108 110 // find by mime failed, use find by ext
109 111 var ext_idx = model.name.lastIndexOf('.');
110 112
111 113 if (ext_idx > 0) {
112 114 // CodeMirror.findModeByExtension wants extension without '.'
113 115 modeinfo = CodeMirror.findModeByExtension(model.name.slice(ext_idx + 1));
114 116 }
115 117 }
116 118 if (modeinfo) {
117 119 this.set_codemirror_mode(modeinfo);
118 120 }
119 121 };
120 122
121 123 Editor.prototype.set_codemirror_mode = function (modeinfo) {
122 124 /** set the codemirror mode from a modeinfo struct */
123 125 var that = this;
124 126 utils.requireCodeMirrorMode(modeinfo, function () {
125 127 that.codemirror.setOption('mode', modeinfo.mode);
126 128 that.events.trigger("mode_changed.Editor", modeinfo);
127 129 });
128 130 };
129 131
130 132 Editor.prototype.get_filename = function () {
131 133 return utils.url_path_split(this.file_path)[1];
132 134 };
133 135
134 136 Editor.prototype.rename = function (new_name) {
135 137 /** rename the file */
136 138 var that = this;
137 139 var parent = utils.url_path_split(this.file_path)[0];
138 140 var new_path = utils.url_path_join(parent, new_name);
139 141 return this.contents.rename(this.file_path, new_path).then(
140 142 function (model) {
141 143 that.file_path = model.path;
142 144 that.events.trigger('file_renamed.Editor', model);
143 145 that._set_mode_for_model(model);
144 146 }
145 147 );
146 148 };
147 149
148 150 Editor.prototype.save = function () {
149 151 /** save the file */
150 152 if (!this.save_enabled) {
151 153 console.log("Not saving, save disabled");
152 154 return;
153 155 }
154 156 var model = {
155 157 path: this.file_path,
156 158 type: 'file',
157 159 format: 'text',
158 160 content: this.codemirror.getValue(),
159 161 };
160 162 var that = this;
161 163 // record change generation for isClean
162 164 this.generation = this.codemirror.changeGeneration();
163 165 return this.contents.save(this.file_path, model).then(function(data) {
164 166 that.events.trigger("file_saved.Editor", data);
165 167 });
166 168 };
167 169
168 170 Editor.prototype._set_codemirror_options = function (options) {
169 171 // update codemirror options from a dict
170 172 var codemirror = this.codemirror;
171 173 $.map(options, function (value, opt) {
172 174 if (value === null) {
173 175 value = CodeMirror.defaults[opt];
174 176 }
175 177 codemirror.setOption(opt, value);
176 178 });
177 179 };
178 180
179 181 Editor.prototype.update_codemirror_options = function (options) {
180 182 /** update codemirror options locally and save changes in config */
181 183 var that = this;
182 184 this._set_codemirror_options(options);
183 185 return this.config.update({
184 186 Editor: {
185 187 codemirror_options: options
186 188 }
187 189 }).then(
188 190 that.events.trigger('config_changed.Editor', {config: that.config})
189 191 );
190 192 };
191 193
192 194 return {Editor: Editor};
193 195 });
General Comments 0
You need to be logged in to leave comments. Login now