Redmine locale fix. Some RAL tweaks. Added line numbers to code-mirror.
[redakcja.git] / project / static / js / lib / codemirror / codemirror.js
1 /* CodeMirror main module
2  *
3  * Implements the CodeMirror constructor and prototype, which take care
4  * of initializing the editor frame, and providing the outside interface.
5  */
6
7 // The CodeMirrorConfig object is used to specify a default
8 // configuration. If you specify such an object before loading this
9 // file, the values you put into it will override the defaults given
10 // below. You can also assign to it after loading.
11 var CodeMirrorConfig = window.CodeMirrorConfig || {};
12
13 var CodeMirror = (function(){
14   function setDefaults(object, defaults) {
15     for (var option in defaults) {
16       if (!object.hasOwnProperty(option))
17         object[option] = defaults[option];
18     }
19   }
20   function forEach(array, action) {
21     for (var i = 0; i < array.length; i++)
22       action(array[i]);
23   }
24
25   // These default options can be overridden by passing a set of
26   // options to a specific CodeMirror constructor. See manual.html for
27   // their meaning.
28   setDefaults(CodeMirrorConfig, {
29     stylesheet: "",
30     path: "",
31     parserfile: [],
32     basefiles: ["util.js", "stringstream.js", "select.js", "undo.js", "editor.js", "tokenize.js"],
33     iframeClass: null,
34     passDelay: 200,
35     passTime: 50,
36     continuousScanning: false,
37     saveFunction: null,
38     onChange: null,
39     undoDepth: 50,
40     undoDelay: 800,
41     disableSpellcheck: true,
42     textWrapping: true,
43     readOnly: false,
44     width: "100%",
45     height: "300px",
46     autoMatchParens: false,
47     parserConfig: null,
48     tabMode: "indent", // or "spaces", "default", "shift"
49     reindentOnLoad: false,
50     activeTokens: null,
51     cursorActivity: null,
52     lineNumbers: false,
53     indentUnit: 2
54   });
55
56   function wrapLineNumberDiv(place) {
57     return function(node) {
58         
59       var container = document.createElement("DIV"),
60           nums = document.createElement("DIV"),
61           scroller = document.createElement("DIV");
62       
63       nums.style.position = "absolute";
64       nums.style.height = "100%";      
65       if (nums.style.setExpression) {
66         try {nums.style.setExpression("height", "this.previousSibling.offsetHeight + 'px'");}
67         catch(e) {} // Seems to throw 'Not Implemented' on some IE8 versions
68       }
69       nums.style.top = "0px";
70       nums.style.overflow = "hidden";      
71
72       container.style.position = "absolute";
73       container.style.left = "0px";
74       container.style.right = "0px";
75       container.style.bottom = "0px";
76       container.style.top = "0px";
77
78       node.style.position = "absolute";
79       node.style.top = "0px";
80       node.style.right = "0px";
81       node.style.bottom = "0px";
82       node.style.left = "16px"
83
84       place(container);
85       container.appendChild(node);     
86       container.appendChild(nums);
87       scroller.className = "CodeMirror-line-numbers";
88       nums.appendChild(scroller);
89     }
90   }
91
92   function applyLineNumbers(frame) {
93     var win = frame.contentWindow, doc = win.document,
94         nums = frame.parentNode.nextSibling, scroller = nums.firstChild;
95
96     var nextNum = 1, barWidth = null;
97     function sizeBar() {
98       if (!frame.offsetWidth || !win.Editor) {
99         for (var cur = frame; cur.parentNode; cur = cur.parentNode) {
100           if (cur != document) {
101             clearInterval(sizeInterval);
102             return;
103           }
104         }
105       }
106
107       if (nums.offsetWidth != barWidth) {
108         barWidth = nums.offsetWidth;
109         // nums.style.left = "-" + (frame.parentNode.style.marginLeft = barWidth + "px");
110       }
111     }
112     function update() {
113       var diff = 20 + Math.max(doc.body.offsetHeight, frame.offsetHeight) - scroller.offsetHeight;
114       for (var n = Math.ceil(diff / 10); n > 0; n--) {
115         var div = document.createElement("DIV");
116         div.appendChild(document.createTextNode(nextNum++));
117         scroller.appendChild(div);
118       }
119       nums.scrollTop = doc.body.scrollTop || doc.documentElement.scrollTop || 0;
120     }
121     sizeBar();
122     update();
123     win.addEventHandler(win, "scroll", update);
124     win.addEventHandler(win, "resize", update);
125     var sizeInterval = setInterval(sizeBar, 500);
126   }
127
128   function CodeMirror(place, options) {
129     // Backward compatibility for deprecated options.
130     if (options.dumbTabs) options.tabMode = "spaces";
131     else if (options.normalTab) options.tabMode = "default";
132
133     // Use passed options, if any, to override defaults.
134     this.options = options = options || {};
135     setDefaults(options, CodeMirrorConfig);
136
137     var frame = this.frame = document.createElement("IFRAME");
138     if (options.iframeClass) frame.className = options.iframeClass;
139     frame.frameBorder = 0;
140     frame.src = "javascript:false;";
141     frame.style.border = "0";
142     frame.style.width = "100%";
143     frame.style.height = "100%";
144     // display: block occasionally suppresses some Firefox bugs, so we
145     // always add it, redundant as it sounds.
146     frame.style.display = "block";
147
148     if (place.appendChild) {
149       var node = place;
150       place = function(n){node.appendChild(n);};
151     }
152
153     var iframe_container = document.createElement("DIV");
154     iframe_container.appendChild(frame);
155
156     var content_wrapper = document.createElement("DIV");
157     content_wrapper.appendChild(iframe_container);
158     content_wrapper.style.position = 'relative';
159     content_wrapper.className = 'CodeMirror-content-wrapper';
160     
161     iframe_container.style.position = 'absolute';
162     iframe_container.style.top = '0px';
163     iframe_container.style.right = '0px';
164     iframe_container.style.bottom = '0px';
165     iframe_container.style.left = '28px';
166     
167     if (options.lineNumbers) {
168        var nums = document.createElement("DIV"),
169           scroller = document.createElement("DIV");
170
171         nums.style.position = "absolute";
172         nums.style.height = "100%";
173
174         nums.style.top = "0px";
175         nums.style.left = "0px";
176         nums.style.overflow = "hidden";
177
178         scroller.className = "CodeMirror-line-numbers";
179         nums.appendChild(scroller);
180         content_wrapper.appendChild(nums);
181
182         iframe_container.style.right = nums.width;        
183     }   
184     
185     place(content_wrapper);
186
187     // Link back to this object, so that the editor can fetch options
188     // and add a reference to itself.
189     frame.CodeMirror = this;
190     this.win = frame.contentWindow;
191
192     if (typeof options.parserfile == "string")
193       options.parserfile = [options.parserfile];
194     if (typeof options.stylesheet == "string")
195       options.stylesheet = [options.stylesheet];
196
197     var html = ["<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html><head>"];
198     // Hack to work around a bunch of IE8-specific problems.
199     html.push("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=EmulateIE7\"/>");
200     forEach(options.stylesheet, function(file) {
201       html.push("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + file + "\"/>");
202     });
203     forEach(options.basefiles.concat(options.parserfile), function(file) {
204       html.push("<script type=\"text/javascript\" src=\"" + options.path + file + "\"></script>");
205     });
206     html.push("</head><body style=\"border-width: 0;\" class=\"editbox\" spellcheck=\"" +
207               (options.disableSpellcheck ? "false" : "true") + "\"></body></html>");
208
209     var doc = this.win.document;
210     doc.open();
211     doc.write(html.join(""));
212     doc.close();
213   }
214
215   CodeMirror.prototype = {
216     init: function() {
217       if (this.options.initCallback) this.options.initCallback(this);
218       if (this.options.lineNumbers) applyLineNumbers(this.frame);
219       if (this.options.reindentOnLoad) this.reindent();
220     },
221
222     getCode: function() {return this.editor.getCode();},
223     setCode: function(code) {this.editor.importCode(code);},
224     selection: function() {return this.editor.selectedText();},
225     reindent: function() {this.editor.reindent();},
226     reindentSelection: function() {this.editor.reindentSelection(null);},
227
228     focus: function() {
229       this.win.focus();
230       if (this.editor.selectionSnapshot) // IE hack
231         this.win.select.selectCoords(this.win, this.editor.selectionSnapshot);
232     },
233     replaceSelection: function(text) {
234       this.focus();
235       this.editor.replaceSelection(text);
236       return true;
237     },
238     replaceChars: function(text, start, end) {
239       this.editor.replaceChars(text, start, end);
240     },
241     getSearchCursor: function(string, fromCursor) {
242       return this.editor.getSearchCursor(string, fromCursor);
243     },
244
245     undo: function() {this.editor.history.undo();},
246     redo: function() {this.editor.history.redo();},
247     historySize: function() {return this.editor.history.historySize();},
248     clearHistory: function() {this.editor.history.clear();},
249
250     grabKeys: function(callback, filter) {this.editor.grabKeys(callback, filter);},
251     ungrabKeys: function() {this.editor.ungrabKeys();},
252
253     setParser: function(name) {this.editor.setParser(name);},
254
255     cursorPosition: function(start) {
256       if (this.win.select.ie_selection) this.focus();
257       return this.editor.cursorPosition(start);
258     },
259     firstLine: function() {return this.editor.firstLine();},
260     lastLine: function() {return this.editor.lastLine();},
261     nextLine: function(line) {return this.editor.nextLine(line);},
262     prevLine: function(line) {return this.editor.prevLine(line);},
263     lineContent: function(line) {return this.editor.lineContent(line);},
264     setLineContent: function(line, content) {this.editor.setLineContent(line, content);},
265     insertIntoLine: function(line, position, content) {this.editor.insertIntoLine(line, position, content);},
266     selectLines: function(startLine, startOffset, endLine, endOffset) {
267       this.win.focus();
268       this.editor.selectLines(startLine, startOffset, endLine, endOffset);
269     },
270     nthLine: function(n) {
271       var line = this.firstLine();
272       for (; n > 1 && line !== false; n--)
273         line = this.nextLine(line);
274       return line;
275     },
276     lineNumber: function(line) {
277       var num = 0;
278       while (line !== false) {
279         num++;
280         line = this.prevLine(line);
281       }
282       return num;
283     },
284
285     // Old number-based line interface
286     jumpToLine: function(n) {
287       this.selectLines(this.nthLine(n), 0);
288       this.win.focus();
289     },
290     currentLine: function() {
291       return this.lineNumber(this.cursorPosition().line);
292     }
293   };
294
295   CodeMirror.InvalidLineHandle = {toString: function(){return "CodeMirror.InvalidLineHandle";}};
296
297   CodeMirror.replace = function(element) {
298     if (typeof element == "string")
299       element = document.getElementById(element);
300     return function(newElement) {
301       element.parentNode.replaceChild(newElement, element);
302     };
303   };
304
305   CodeMirror.fromTextArea = function(area, options) {
306     if (typeof area == "string")
307       area = document.getElementById(area);
308
309     options = options || {};
310     if (area.style.width && options.width == null)
311       options.width = area.style.width;
312     if (area.style.height && options.height == null)
313       options.height = area.style.height;
314     if (options.content == null) options.content = area.value;
315
316     if (area.form) {
317       function updateField() {
318         area.value = mirror.getCode();
319       }
320       if (typeof area.form.addEventListener == "function")
321         area.form.addEventListener("submit", updateField, false);
322       else
323         area.form.attachEvent("onsubmit", updateField);
324     }
325
326     function insert(frame) {
327       if (area.nextSibling)
328         area.parentNode.insertBefore(frame, area.nextSibling);
329       else
330         area.parentNode.appendChild(frame);
331     }
332
333     area.style.display = "none";
334     var mirror = new CodeMirror(insert, options);
335     return mirror;
336   };
337
338   CodeMirror.isProbablySupported = function() {
339     // This is rather awful, but can be useful.
340     var match;
341     if (window.opera)
342       return Number(window.opera.version()) >= 9.52;
343     else if (/Apple Computers, Inc/.test(navigator.vendor) && (match = navigator.userAgent.match(/Version\/(\d+(?:\.\d+)?)\./)))
344       return Number(match[1]) >= 3;
345     else if (document.selection && window.ActiveXObject && (match = navigator.userAgent.match(/MSIE (\d+(?:\.\d*)?)\b/)))
346       return Number(match[1]) >= 6;
347     else if (match = navigator.userAgent.match(/gecko\/(\d{8})/i))
348       return Number(match[1]) >= 20050901;
349     else if (match = navigator.userAgent.match(/AppleWebKit\/(\d+)/))
350       return Number(match[1]) >= 525;
351     else
352       return null;
353   };
354
355   return CodeMirror;
356 })();