2
* CodePress - Real Time Syntax Highlighting Editor written in JavaScript - http://codepress.org/
4
* Copyright (C) 2006 Fernando M.A.d.S. <fermads@gmail.com>
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.
9
* Read the full licence: http://www.opensource.org/licenses/lgpl-license.php
12
CodePress = function(obj) {
13
var self = document.createElement('iframe');
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;
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);
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';
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);
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';
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();
60
self.getCode = function() {
61
return self.textarea.disabled ? self.editor.getCode() : self.textarea.value;
64
self.setCode = function(code) {
65
self.textarea.disabled ? self.editor.setCode(code) : self.textarea.value = code;
68
self.addChangeHandler = function(handler) {
69
return self.editor.addChangeHandler ? self.editor.addChangeHandler(handler) : false;
72
self.addSaveHandler = function(handler) {
73
return self.editor.addSaveHandler ? self.editor.addSaveHandler(handler) : false;
76
self.toggleAutoComplete = function() {
77
self.editor.autocomplete = (self.editor.autocomplete) ? false : true;
80
self.toggleReadOnly = function() {
81
self.textarea.readOnly = (self.textarea.readOnly) ? false : true;
82
if(self.style.display != 'none') // prevent exception on FF + iframe with display:none
83
self.editor.readOnly(self.textarea.readOnly ? true : false);
86
self.toggleLineNumbers = function() {
87
var cn = self.editor.body.className;
88
self.editor.body.className = (cn==''||cn=='show-line-numbers') ? 'hide-line-numbers' : 'show-line-numbers';
91
self.toggleEditor = function() {
92
if(self.textarea.disabled) {
93
self.textarea.value = self.getCode();
94
self.textarea.disabled = false;
95
self.style.display = 'none';
96
self.textarea.style.display = 'inline';
99
self.textarea.disabled = true;
100
self.setCode(self.textarea.value);
101
self.editor.syntaxHighlight('init');
102
self.style.display = 'inline';
103
self.textarea.style.display = 'none';
111
CodePress.languages = {
117
javascript : 'JavaScript',
124
vbscript : 'VBScript'
128
CodePress.run = function() {
129
s = document.getElementsByTagName('script');
130
for(var i=0,n=s.length;i<n;i++) {
131
if(s[i].src.match('codepress.js')) {
132
CodePress.path = s[i].src.replace('codepress.js','');
135
t = document.getElementsByTagName('textarea');
136
for(var i=0,n=t.length;i<n;i++) {
137
if(t[i].className.match('codepress')) {
140
eval(id+' = new CodePress(t[i])');
141
t[i].parentNode.insertBefore(eval(id), t[i]);
146
if(window.attachEvent) window.attachEvent('onload',CodePress.run);
147
else window.addEventListener('DOMContentLoaded',CodePress.run,false);