Project

General

Profile

1 460 Ruebenwurz
/*
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
12
CodePress = 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.style.visibility = 'hidden';
22
	self.style.position = 'absolute';
23
	self.options = self.textarea.className;
24
25
	self.initialize = function() {
26
		self.editor = self.contentWindow.CodePress;
27
		self.editor.body = self.contentWindow.document.getElementsByTagName('body')[0];
28
		self.editor.setCode(self.textarea.value);
29
		self.setOptions();
30
		self.editor.syntaxHighlight('init');
31
		self.textarea.style.display = 'none';
32
		self.style.position = 'static';
33
		self.style.visibility = 'visible';
34
		self.style.display = 'inline';
35
	}
36
37
	self.edit = function(id,language) {
38
		if(id) self.textarea.value = document.getElementById(id).value;
39
		if(!self.textarea.disabled) return;
40
		self.language = language ? language : self.options.replace(/ ?codepress ?| ?readonly-on ?| ?autocomplete-off ?| ?linenumbers-off ?/g,'');
41
		if(!CodePress.languages[self.language]) self.language = 'generic';
42
		self.src = CodePress.path+'codepress.html?engine='+CodePress.engine+'&language='+self.language+'&ts='+(new Date).getTime();
43
		if(self.attachEvent) self.attachEvent('onload',self.initialize);
44
		else self.addEventListener('load',self.initialize,false);
45
	}
46
47
	self.setOptions = function() {
48
		if(self.options.match('autocomplete-off')) self.toggleAutoComplete();
49
		if(self.options.match('readonly-on')) self.toggleReadOnly();
50
		if(self.options.match('linenumbers-off')) self.toggleLineNumbers();
51
	}
52
53
	self.getCode = function() {
54
		return self.textarea.disabled ? self.editor.getCode() : self.textarea.value;
55
	}
56
57
	self.setCode = function(code) {
58
		self.textarea.disabled ? self.editor.setCode(code) : self.textarea.value = code;
59
	}
60
61
	self.toggleAutoComplete = function() {
62
		self.editor.autocomplete = (self.editor.autocomplete) ? false : true;
63
	}
64
65
	self.toggleReadOnly = function() {
66
		self.textarea.readOnly = (self.textarea.readOnly) ? false : true;
67
		if(self.style.display != 'none') // prevent exception on FF + iframe with display:none
68
			self.editor.readOnly(self.textarea.readOnly ? true : false);
69
	}
70
71
	self.toggleLineNumbers = function() {
72
		var cn = self.editor.body.className;
73
		self.editor.body.className = (cn==''||cn=='show-line-numbers') ? 'hide-line-numbers' : 'show-line-numbers';
74
	}
75
76
	self.toggleEditor = function() {
77
		if(self.textarea.disabled) {
78
			self.textarea.value = self.getCode();
79
			self.textarea.disabled = false;
80
			self.style.display = 'none';
81
			self.textarea.style.display = 'inline';
82
		}
83
		else {
84
			self.textarea.disabled = true;
85
			self.setCode(self.textarea.value);
86
			self.editor.syntaxHighlight('init');
87
			self.style.display = 'inline';
88
			self.textarea.style.display = 'none';
89
		}
90
	}
91
92
	self.edit();
93
	return self;
94
}
95
96
CodePress.languages = {
97
	css : 'CSS',
98
	generic : 'Generic',
99
	html : 'HTML',
100
	java : 'Java',
101
	javascript : 'JavaScript',
102
	perl : 'Perl',
103
	ruby : 'Ruby',
104
	php : 'PHP',
105
	text : 'Text',
106
	sql : 'SQL'
107
}
108
109
CodePress.getEngine = function()	{
110
	var engine = 'older';
111
	var ua = navigator.userAgent;
112
	if(ua.match('MSIE')) engine = 'msie';
113
	else if(ua.match('KHTML')) engine = 'khtml';
114
	else if(ua.match('Opera')) engine = 'opera';
115
	else if(ua.match('Gecko')) engine = 'gecko';
116
	return engine;
117
}
118
119
CodePress.run = function() {
120
	CodePress.engine = CodePress.getEngine();
121
	s = document.getElementsByTagName('script');
122
	for(var i=0,n=s.length;i<n;i++) {
123
		if(s[i].src.match('codepress.js')) {
124
			CodePress.path = s[i].src.replace('codepress.js','');
125
		}
126
	}
127
	t = document.getElementsByTagName('textarea');
128
	for(var i=0,n=t.length;i<n;i++) {
129
		if(t[i].className.match('codepress')) {
130
			id = t[i].id;
131
			t[i].id = id+'_cp';
132
			eval(id+' = new CodePress(t[i])');
133
			t[i].parentNode.insertBefore(eval(id), t[i]);
134
		}
135
	}
136
}
137
138
if(window.attachEvent) window.attachEvent('onload',CodePress.run);
139
else window.addEventListener('DOMContentLoaded',CodePress.run,false);