~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/media/common/codepress/codepress.js

  • Committer: mattgiuca
  • Date: 2008-07-07 12:01:03 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:820
lib: Added new package pulldown_subj, a collection of modules designed to
    pull student subject enrolments from the server.
    Note that the actual code to do this is not included (since that is
    specific to the organisation running IVLE) - just a pluggable interface
    and an example plugin module.
configure.py: Added new config option: subject_pulldown_modules, which allows
    you to specify which modules are plugged in here.
    (Actually that was added accidentally in a previous commit; but this
    revision fixes some comments).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
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.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.addChangeHandler = function(handler) {
 
69
                return self.editor.addChangeHandler ? self.editor.addChangeHandler(handler) : false;
 
70
        }
 
71
 
 
72
        self.toggleAutoComplete = function() {
 
73
                self.editor.autocomplete = (self.editor.autocomplete) ? false : true;
 
74
        }
 
75
        
 
76
        self.toggleReadOnly = function() {
 
77
                self.textarea.readOnly = (self.textarea.readOnly) ? false : true;
 
78
                if(self.style.display != 'none') // prevent exception on FF + iframe with display:none
 
79
                        self.editor.readOnly(self.textarea.readOnly ? true : false);
 
80
        }
 
81
        
 
82
        self.toggleLineNumbers = function() {
 
83
                var cn = self.editor.body.className;
 
84
                self.editor.body.className = (cn==''||cn=='show-line-numbers') ? 'hide-line-numbers' : 'show-line-numbers';
 
85
        }
 
86
        
 
87
        self.toggleEditor = function() {
 
88
                if(self.textarea.disabled) {
 
89
                        self.textarea.value = self.getCode();
 
90
                        self.textarea.disabled = false;
 
91
                        self.style.display = 'none';
 
92
                        self.textarea.style.display = 'inline';
 
93
                }
 
94
                else {
 
95
                        self.textarea.disabled = true;
 
96
                        self.setCode(self.textarea.value);
 
97
                        self.editor.syntaxHighlight('init');
 
98
                        self.style.display = 'inline';
 
99
                        self.textarea.style.display = 'none';
 
100
                }
 
101
        }
 
102
 
 
103
        self.edit();
 
104
        return self;
 
105
}
 
106
 
 
107
CodePress.languages = { 
 
108
        csharp : 'C#', 
 
109
        css : 'CSS', 
 
110
        generic : 'Generic',
 
111
        html : 'HTML',
 
112
        java : 'Java', 
 
113
        javascript : 'JavaScript', 
 
114
        perl : 'Perl', 
 
115
        ruby : 'Ruby',  
 
116
        php : 'PHP', 
 
117
        python : 'Python', 
 
118
        text : 'Text', 
 
119
        sql : 'SQL',
 
120
        vbscript : 'VBScript'
 
121
}
 
122
 
 
123
 
 
124
CodePress.run = function() {
 
125
        s = document.getElementsByTagName('script');
 
126
        for(var i=0,n=s.length;i<n;i++) {
 
127
                if(s[i].src.match('codepress.js')) {
 
128
                        CodePress.path = s[i].src.replace('codepress.js','');
 
129
                }
 
130
        }
 
131
        t = document.getElementsByTagName('textarea');
 
132
        for(var i=0,n=t.length;i<n;i++) {
 
133
                if(t[i].className.match('codepress')) {
 
134
                        id = t[i].id;
 
135
                        t[i].id = id+'_cp';
 
136
                        eval(id+' = new CodePress(t[i])');
 
137
                        t[i].parentNode.insertBefore(eval(id), t[i]);
 
138
                } 
 
139
        }
 
140
}
 
141
 
 
142
if(window.attachEvent) window.attachEvent('onload',CodePress.run);
 
143
else window.addEventListener('DOMContentLoaded',CodePress.run,false);