##// END OF EJS Templates
codemirror: move some code outside of helpers for reuse
ergo -
r874:6903ccc9 default
parent child Browse files
Show More
@@ -29,58 +29,19 b' cmLog.setLevel(Logger.OFF);'
29 //global cache for inline forms
29 //global cache for inline forms
30 var userHintsCache = {};
30 var userHintsCache = {};
31
31
32
32 // global timer, used to cancel async loading
33 var initCodeMirror = function(textAreadId, resetUrl, focus, options) {
33 var CodeMirrorLoadUserHintTimer;
34 var ta = $('#' + textAreadId).get(0);
35 if (focus === undefined) {
36 focus = true;
37 }
38
39 // default options
40 var codeMirrorOptions = {
41 mode: "null",
42 lineNumbers: true,
43 indentUnit: 4,
44 autofocus: focus
45 };
46
47 if (options !== undefined) {
48 // extend with custom options
49 codeMirrorOptions = $.extend(true, codeMirrorOptions, options);
50 }
51
52 var myCodeMirror = CodeMirror.fromTextArea(ta, codeMirrorOptions);
53
34
54 $('#reset').on('click', function(e) {
55 window.location = resetUrl;
56 });
57
58 return myCodeMirror;
59 };
60
61 var initCommentBoxCodeMirror = function(textAreaId, triggerActions){
62 var initialHeight = 100;
63
64 // global timer, used to cancel async loading
65 var loadUserHintTimer;
66
67 if (typeof userHintsCache === "undefined") {
68 userHintsCache = {};
69 cmLog.debug('Init empty cache for mentions');
70 }
71 if (!$(textAreaId).get(0)) {
72 cmLog.debug('Element for textarea not found', textAreaId);
73 return;
74 }
75 var escapeRegExChars = function(value) {
35 var escapeRegExChars = function(value) {
76 return value.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
36 return value.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
77 };
37 };
38
78 /**
39 /**
79 * Load hints from external source returns an array of objects in a format
40 * Load hints from external source returns an array of objects in a format
80 * that hinting lib requires
41 * that hinting lib requires
81 * @returns {Array}
42 * @returns {Array}
82 */
43 */
83 var loadUserHints = function(query, triggerHints) {
44 var CodeMirrorLoadUserHints = function(query, triggerHints) {
84 cmLog.debug('Loading mentions users via AJAX');
45 cmLog.debug('Loading mentions users via AJAX');
85 var _users = [];
46 var _users = [];
86 $.ajax({
47 $.ajax({
@@ -130,7 +91,7 b' var initCommentBoxCodeMirror = function('
130 * @param context
91 * @param context
131 * @returns {Array}
92 * @returns {Array}
132 */
93 */
133 var filterUsers = function(users, context) {
94 var CodeMirrorFilterUsers = function(users, context) {
134 var MAX_LIMIT = 10;
95 var MAX_LIMIT = 10;
135 var filtered_users = [];
96 var filtered_users = [];
136 var curWord = context.string;
97 var curWord = context.string;
@@ -162,6 +123,124 b' var initCommentBoxCodeMirror = function('
162 return filtered_users;
123 return filtered_users;
163 };
124 };
164
125
126 var CodeMirrorMentionHint = function(editor, callback, options) {
127 var cur = editor.getCursor();
128 var curLine = editor.getLine(cur.line).slice(0, cur.ch);
129
130 // match on @ +1char
131 var tokenMatch = new RegExp(
132 '(^@| @)([a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]*)$').exec(curLine);
133
134 var tokenStr = '';
135 if (tokenMatch !== null && tokenMatch.length > 0){
136 tokenStr = tokenMatch[0].strip();
137 } else {
138 // skip if we didn't match our token
139 return;
140 }
141
142 var context = {
143 start: (cur.ch - tokenStr.length) + 1,
144 end: cur.ch,
145 string: tokenStr.slice(1),
146 type: null
147 };
148
149 // case when we put the @sign in fron of a string,
150 // eg <@ we put it here>sometext then we need to prepend to text
151 if (context.end > cur.ch) {
152 context.start = context.start + 1; // we add to the @ sign
153 context.end = cur.ch; // don't eat front part just append
154 context.string = context.string.slice(1, cur.ch - context.start);
155 }
156
157 cmLog.debug('Mention context', context);
158
159 var triggerHints = function(userHints){
160 return callback({
161 list: CodeMirrorFilterUsers(userHints, context),
162 from: CodeMirror.Pos(cur.line, context.start),
163 to: CodeMirror.Pos(cur.line, context.end)
164 });
165 };
166
167 var queryBasedHintsCache = undefined;
168 // if we have something in the cache, try to fetch the query based cache
169 if (userHintsCache !== {}){
170 queryBasedHintsCache = userHintsCache[context.string];
171 }
172
173 if (queryBasedHintsCache !== undefined) {
174 cmLog.debug('Users loaded from cache');
175 triggerHints(queryBasedHintsCache);
176 } else {
177 // this takes care for async loading, and then displaying results
178 // and also propagates the userHintsCache
179 window.clearTimeout(CodeMirrorLoadUserHintTimer);
180 CodeMirrorLoadUserHintTimer = setTimeout(function() {
181 CodeMirrorLoadUserHints(context.string, triggerHints);
182 }, 300);
183 }
184 };
185
186 var CodeMirrorCompleteAfter = function(cm, pred) {
187 var options = {
188 completeSingle: false,
189 async: true,
190 closeOnUnfocus: true
191 };
192 var cur = cm.getCursor();
193 setTimeout(function() {
194 if (!cm.state.completionActive) {
195 cmLog.debug('Trigger mentions hinting');
196 CodeMirror.showHint(cm, CodeMirror.hint.mentions, options);
197 }
198 }, 100);
199
200 // tell CodeMirror we didn't handle the key
201 // trick to trigger on a char but still complete it
202 return CodeMirror.Pass;
203 };
204
205 var initCodeMirror = function(textAreadId, resetUrl, focus, options) {
206 var ta = $('#' + textAreadId).get(0);
207 if (focus === undefined) {
208 focus = true;
209 }
210
211 // default options
212 var codeMirrorOptions = {
213 mode: "null",
214 lineNumbers: true,
215 indentUnit: 4,
216 autofocus: focus
217 };
218
219 if (options !== undefined) {
220 // extend with custom options
221 codeMirrorOptions = $.extend(true, codeMirrorOptions, options);
222 }
223
224 var myCodeMirror = CodeMirror.fromTextArea(ta, codeMirrorOptions);
225
226 $('#reset').on('click', function(e) {
227 window.location = resetUrl;
228 });
229
230 return myCodeMirror;
231 };
232
233 var initCommentBoxCodeMirror = function(textAreaId, triggerActions){
234 var initialHeight = 100;
235
236 if (typeof userHintsCache === "undefined") {
237 userHintsCache = {};
238 cmLog.debug('Init empty cache for mentions');
239 }
240 if (!$(textAreaId).get(0)) {
241 cmLog.debug('Element for textarea not found', textAreaId);
242 return;
243 }
165 /**
244 /**
166 * Filter action based on typed in text
245 * Filter action based on typed in text
167 * @param actions
246 * @param actions
@@ -200,25 +279,6 b' var initCommentBoxCodeMirror = function('
200 return filtered_actions;
279 return filtered_actions;
201 };
280 };
202
281
203 var completeAfter = function(cm, pred) {
204 var options = {
205 completeSingle: false,
206 async: true,
207 closeOnUnfocus: true
208 };
209 var cur = cm.getCursor();
210 setTimeout(function() {
211 if (!cm.state.completionActive) {
212 cmLog.debug('Trigger mentions hinting');
213 CodeMirror.showHint(cm, CodeMirror.hint.mentions, options);
214 }
215 }, 100);
216
217 // tell CodeMirror we didn't handle the key
218 // trick to trigger on a char but still complete it
219 return CodeMirror.Pass;
220 };
221
222 var submitForm = function(cm, pred) {
282 var submitForm = function(cm, pred) {
223 $(cm.display.input.textarea.form).submit();
283 $(cm.display.input.textarea.form).submit();
224 return CodeMirror.Pass;
284 return CodeMirror.Pass;
@@ -238,7 +298,7 b' var initCommentBoxCodeMirror = function('
238 };
298 };
239
299
240 var extraKeys = {
300 var extraKeys = {
241 "'@'": completeAfter,
301 "'@'": CodeMirrorCompleteAfter,
242 Tab: function(cm) {
302 Tab: function(cm) {
243 // space indent instead of TABS
303 // space indent instead of TABS
244 var spaces = new Array(cm.getOption("indentUnit") + 1).join(" ");
304 var spaces = new Array(cm.getOption("indentUnit") + 1).join(" ");
@@ -285,66 +345,6 b' var initCommentBoxCodeMirror = function('
285 self.setSize(null, height);
345 self.setSize(null, height);
286 });
346 });
287
347
288 var mentionHint = function(editor, callback, options) {
289 var cur = editor.getCursor();
290 var curLine = editor.getLine(cur.line).slice(0, cur.ch);
291
292 // match on @ +1char
293 var tokenMatch = new RegExp(
294 '(^@| @)([a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]*)$').exec(curLine);
295
296 var tokenStr = '';
297 if (tokenMatch !== null && tokenMatch.length > 0){
298 tokenStr = tokenMatch[0].strip();
299 } else {
300 // skip if we didn't match our token
301 return;
302 }
303
304 var context = {
305 start: (cur.ch - tokenStr.length) + 1,
306 end: cur.ch,
307 string: tokenStr.slice(1),
308 type: null
309 };
310
311 // case when we put the @sign in fron of a string,
312 // eg <@ we put it here>sometext then we need to prepend to text
313 if (context.end > cur.ch) {
314 context.start = context.start + 1; // we add to the @ sign
315 context.end = cur.ch; // don't eat front part just append
316 context.string = context.string.slice(1, cur.ch - context.start);
317 }
318
319 cmLog.debug('Mention context', context);
320
321 var triggerHints = function(userHints){
322 return callback({
323 list: filterUsers(userHints, context),
324 from: CodeMirror.Pos(cur.line, context.start),
325 to: CodeMirror.Pos(cur.line, context.end)
326 });
327 };
328
329 var queryBasedHintsCache = undefined;
330 // if we have something in the cache, try to fetch the query based cache
331 if (userHintsCache !== {}){
332 queryBasedHintsCache = userHintsCache[context.string];
333 }
334
335 if (queryBasedHintsCache !== undefined) {
336 cmLog.debug('Users loaded from cache');
337 triggerHints(queryBasedHintsCache);
338 } else {
339 // this takes care for async loading, and then displaying results
340 // and also propagates the userHintsCache
341 window.clearTimeout(loadUserHintTimer);
342 loadUserHintTimer = setTimeout(function() {
343 loadUserHints(context.string, triggerHints);
344 }, 300);
345 }
346 };
347
348 var actionHint = function(editor, options) {
348 var actionHint = function(editor, options) {
349 var cur = editor.getCursor();
349 var cur = editor.getCursor();
350 var curLine = editor.getLine(cur.line).slice(0, cur.ch);
350 var curLine = editor.getLine(cur.line).slice(0, cur.ch);
@@ -408,7 +408,7 b' var initCommentBoxCodeMirror = function('
408 to: CodeMirror.Pos(cur.line, context.end)
408 to: CodeMirror.Pos(cur.line, context.end)
409 };
409 };
410 };
410 };
411 CodeMirror.registerHelper("hint", "mentions", mentionHint);
411 CodeMirror.registerHelper("hint", "mentions", CodeMirrorMentionHint);
412 CodeMirror.registerHelper("hint", "actions", actionHint);
412 CodeMirror.registerHelper("hint", "actions", actionHint);
413 return cm;
413 return cm;
414 };
414 };
General Comments 0
You need to be logged in to leave comments. Login now