Quick fix in CM.
[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     content_wrapper.style.width = options.width;
161     content_wrapper.style.height = options.height;
162     
163     iframe_container.style.position = 'absolute';
164     iframe_container.style.top = '0px';
165     iframe_container.style.right = '0px';
166     iframe_container.style.bottom = '0px';
167     iframe_container.style.left = '0px';
168     
169     if (options.lineNumbers) {
170        iframe_container.style.left = '28px';
171        
172        var nums = document.createElement("DIV"),
173           scroller = document.createElement("DIV");
174
175         nums.style.position = "absolute";
176         nums.style.height = "100%";
177
178         nums.style.top = "0px";
179         nums.style.left = "0px";
180         nums.style.overflow = "hidden";
181
182         scroller.className = "CodeMirror-line-numbers";
183         nums.appendChild(scroller);
184         content_wrapper.appendChild(nums);
185
186         iframe_container.style.right = nums.width;        
187     }   
188     
189     place(content_wrapper);
190
191     // Link back to this object, so that the editor can fetch options
192     // and add a reference to itself.
193     frame.CodeMirror = this;
194     this.win = frame.contentWindow;
195
196     if (typeof options.parserfile == "string")
197       options.parserfile = [options.parserfile];
198     if (typeof options.stylesheet == "string")
199       options.stylesheet = [options.stylesheet];
200
201     var html = ["<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html><head>"];
202     // Hack to work around a bunch of IE8-specific problems.
203     html.push("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=EmulateIE7\"/>");
204     forEach(options.stylesheet, function(file) {
205       html.push("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + file + "\"/>");
206     });
207     forEach(options.basefiles.concat(options.parserfile), function(file) {
208       html.push("<script type=\"text/javascript\" src=\"" + options.path + file + "\"></script>");
209     });
210     html.push("</head><body style=\"border-width: 0;\" class=\"editbox\" spellcheck=\"" +
211               (options.disableSpellcheck ? "false" : "true") + "\"></body></html>");
212
213     var doc = this.win.document;
214     doc.open();
215     doc.write(html.join(""));
216     doc.close();
217   }
218
219   CodeMirror.prototype = {
220     init: function() {
221       if (this.options.initCallback) this.options.initCallback(this);
222       if (this.options.lineNumbers) applyLineNumbers(this.frame);
223       if (this.options.reindentOnLoad) this.reindent();
224     },
225
226     getCode: function() {return this.editor.getCode();},
227     setCode: function(code) {this.editor.importCode(code);},
228     selection: function() {return this.editor.selectedText();},
229     reindent: function() {this.editor.reindent();},
230     reindentSelection: function() {this.editor.reindentSelection(null);},
231
232     focus: function() {
233       this.win.focus();
234       if (this.editor.selectionSnapshot) // IE hack
235         this.win.select.selectCoords(this.win, this.editor.selectionSnapshot);
236     },
237     replaceSelection: function(text) {
238       this.focus();
239       this.editor.replaceSelection(text);
240       return true;
241     },
242     replaceChars: function(text, start, end) {
243       this.editor.replaceChars(text, start, end);
244     },
245     getSearchCursor: function(string, fromCursor) {
246       return this.editor.getSearchCursor(string, fromCursor);
247     },
248
249     undo: function() {this.editor.history.undo();},
250     redo: function() {this.editor.history.redo();},
251     historySize: function() {return this.editor.history.historySize();},
252     clearHistory: function() {this.editor.history.clear();},
253
254     grabKeys: function(callback, filter) {this.editor.grabKeys(callback, filter);},
255     ungrabKeys: function() {this.editor.ungrabKeys();},
256
257     setParser: function(name) {this.editor.setParser(name);},
258
259     cursorPosition: function(start) {
260       if (this.win.select.ie_selection) this.focus();
261       return this.editor.cursorPosition(start);
262     },
263     firstLine: function() {return this.editor.firstLine();},
264     lastLine: function() {return this.editor.lastLine();},
265     nextLine: function(line) {return this.editor.nextLine(line);},
266     prevLine: function(line) {return this.editor.prevLine(line);},
267     lineContent: function(line) {return this.editor.lineContent(line);},
268     setLineContent: function(line, content) {this.editor.setLineContent(line, content);},
269     insertIntoLine: function(line, position, content) {this.editor.insertIntoLine(line, position, content);},
270     selectLines: function(startLine, startOffset, endLine, endOffset) {
271       this.win.focus();
272       this.editor.selectLines(startLine, startOffset, endLine, endOffset);
273     },
274     nthLine: function(n) {
275       var line = this.firstLine();
276       for (; n > 1 && line !== false; n--)
277         line = this.nextLine(line);
278       return line;
279     },
280     lineNumber: function(line) {
281       var num = 0;
282       while (line !== false) {
283         num++;
284         line = this.prevLine(line);
285       }
286       return num;
287     },
288
289     // Old number-based line interface
290     jumpToLine: function(n) {
291       this.selectLines(this.nthLine(n), 0);
292       this.win.focus();
293     },
294     currentLine: function() {
295       return this.lineNumber(this.cursorPosition().line);
296     }
297   };
298
299   CodeMirror.InvalidLineHandle = {toString: function(){return "CodeMirror.InvalidLineHandle";}};
300
301   CodeMirror.replace = function(element) {
302     if (typeof element == "string")
303       element = document.getElementById(element);
304     return function(newElement) {
305       element.parentNode.replaceChild(newElement, element);
306     };
307   };
308
309   CodeMirror.fromTextArea = function(area, options) {
310     if (typeof area == "string")
311       area = document.getElementById(area);
312
313     options = options || {};
314     if (area.style.width && options.width == null)
315       options.width = area.style.width;
316     if (area.style.height && options.height == null)
317       options.height = area.style.height;
318     if (options.content == null) options.content = area.value;
319
320     if (area.form) {
321       function updateField() {
322         area.value = mirror.getCode();
323       }
324       if (typeof area.form.addEventListener == "function")
325         area.form.addEventListener("submit", updateField, false);
326       else
327         area.form.attachEvent("onsubmit", updateField);
328     }
329
330     function insert(frame) {
331       if (area.nextSibling)
332         area.parentNode.insertBefore(frame, area.nextSibling);
333       else
334         area.parentNode.appendChild(frame);
335     }
336
337     area.style.display = "none";
338     var mirror = new CodeMirror(insert, options);
339     return mirror;
340   };
341
342   CodeMirror.isProbablySupported = function() {
343     // This is rather awful, but can be useful.
344     var match;
345     if (window.opera)
346       return Number(window.opera.version()) >= 9.52;
347     else if (/Apple Computers, Inc/.test(navigator.vendor) && (match = navigator.userAgent.match(/Version\/(\d+(?:\.\d+)?)\./)))
348       return Number(match[1]) >= 3;
349     else if (document.selection && window.ActiveXObject && (match = navigator.userAgent.match(/MSIE (\d+(?:\.\d*)?)\b/)))
350       return Number(match[1]) >= 6;
351     else if (match = navigator.userAgent.match(/gecko\/(\d{8})/i))
352       return Number(match[1]) >= 20050901;
353     else if (match = navigator.userAgent.match(/AppleWebKit\/(\d+)/))
354       return Number(match[1]) >= 525;
355     else
356       return null;
357   };
358
359   return CodeMirror;
360 })();