source: trunk/www.guidonia.net/wp/wp-includes/js/codepress/codepress.js@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 4.4 KB
Line 
1/*
2 * CodePress - Real Time Syntax Highlighting Editor written in JavaScript - http://codepress.org/
3 *
4 * Copyright (C) 2006 Fernando M.A.d.S. <fermads@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it under the terms of the
7 * GNU Lesser General Public License as published by the Free Software Foundation.
8 *
9 * Read the full licence: http://www.opensource.org/licenses/lgpl-license.php
10 */
11
12CodePress = function(obj) {
13 var self = document.createElement('iframe');
14 self.textarea = obj;
15 self.textarea.disabled = true;
16 self.textarea.style.overflow = 'hidden';
17 self.style.height = self.textarea.clientHeight +'px';
18 self.style.width = self.textarea.clientWidth +'px';
19 self.textarea.style.overflow = 'auto';
20 self.style.border = '1px solid gray';
21 self.frameBorder = 0; // remove IE internal iframe border
22 self.style.visibility = 'hidden';
23 self.style.position = 'absolute';
24 self.options = self.textarea.className;
25
26 self.initialize = function() {
27 self.editor = self.contentWindow.CodePress;
28 self.editor.body = self.contentWindow.document.getElementsByTagName('body')[0];
29 self.editor.setCode(self.textarea.value);
30 self.setOptions();
31 self.editor.syntaxHighlight('init');
32 self.textarea.style.display = 'none';
33 self.style.position = 'static';
34 self.style.visibility = 'visible';
35 self.style.display = 'inline';
36 }
37
38 // obj can by a textarea id or a string (code)
39 self.edit = function(obj,language) {
40 if(obj) self.textarea.value = document.getElementById(obj) ? document.getElementById(obj).value : obj;
41 if(!self.textarea.disabled) return;
42 self.language = language ? language : self.getLanguage();
43 self.src = CodePress.path+'codepress.html?language='+self.language+'&ts='+(new Date).getTime();
44 if(self.attachEvent) self.attachEvent('onload',self.initialize);
45 else self.addEventListener('load',self.initialize,false);
46 }
47
48 self.getLanguage = function() {
49 for (language in CodePress.languages)
50 if(self.options.match('\\b'+language+'\\b'))
51 return CodePress.languages[language] ? language : 'generic';
52 }
53
54 self.setOptions = function() {
55 if(self.options.match('autocomplete-off')) self.toggleAutoComplete();
56 if(self.options.match('readonly-on')) self.toggleReadOnly();
57 if(self.options.match('linenumbers-off')) self.toggleLineNumbers();
58 }
59
60 self.getCode = function() {
61 return self.textarea.disabled ? self.editor.getCode() : self.textarea.value;
62 }
63
64 self.setCode = function(code) {
65 self.textarea.disabled ? self.editor.setCode(code) : self.textarea.value = code;
66 }
67
68 self.toggleAutoComplete = function() {
69 self.editor.autocomplete = (self.editor.autocomplete) ? false : true;
70 }
71
72 self.toggleReadOnly = function() {
73 self.textarea.readOnly = (self.textarea.readOnly) ? false : true;
74 if(self.style.display != 'none') // prevent exception on FF + iframe with display:none
75 self.editor.readOnly(self.textarea.readOnly ? true : false);
76 }
77
78 self.toggleLineNumbers = function() {
79 var cn = self.editor.body.className;
80 self.editor.body.className = (cn==''||cn=='show-line-numbers') ? 'hide-line-numbers' : 'show-line-numbers';
81 }
82
83 self.toggleEditor = function() {
84 if(self.textarea.disabled) {
85 self.textarea.value = self.getCode();
86 self.textarea.disabled = false;
87 self.style.display = 'none';
88 self.textarea.style.display = 'inline';
89 }
90 else {
91 self.textarea.disabled = true;
92 self.setCode(self.textarea.value);
93 self.editor.syntaxHighlight('init');
94 self.style.display = 'inline';
95 self.textarea.style.display = 'none';
96 }
97 }
98
99 self.edit();
100 return self;
101}
102
103CodePress.languages = {
104 csharp : 'C#',
105 css : 'CSS',
106 generic : 'Generic',
107 html : 'HTML',
108 java : 'Java',
109 javascript : 'JavaScript',
110 perl : 'Perl',
111 ruby : 'Ruby',
112 php : 'PHP',
113 text : 'Text',
114 sql : 'SQL',
115 vbscript : 'VBScript'
116}
117
118
119CodePress.run = function() {
120 // Modified for WordPress compat to prevent loading on webkit and to
121 // reference a codepress_path which is specified externally.
122 if (navigator.userAgent.toLowerCase().indexOf('webkit') != -1)
123 return;
124 CodePress.path = codepress_path;
125 var t = document.getElementsByTagName('textarea'), i, id;
126 for(i=0,n=t.length;i<n;i++) {
127 if(t[i].className.match('codepress')) {
128 id = t[i].id;
129 t[i].id = id+'_cp';
130 eval(id+' = new CodePress(t[i])');
131 t[i].parentNode.insertBefore(eval(id), t[i]);
132 }
133 }
134}
135
136if(window.attachEvent) window.attachEvent('onload',CodePress.run);
137else window.addEventListener('DOMContentLoaded',CodePress.run,false);
Note: See TracBrowser for help on using the repository browser.