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

« back to all changes in this revision

Viewing changes to ivle/webapp/filesystem/browser/media/editor.js

  • Committer: William Grant
  • Date: 2010-07-28 04:12:08 UTC
  • mfrom: (1790.1.8 codemirror-srsly)
  • Revision ID: grantw@unimelb.edu.au-20100728041208-mciagtog1785oje4
Move from CodePress to CodeMirror. It's now an external dependency, too, so you'll need to install it yourself.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
function disable_save_if_safe()
 
1
function disable_save()
2
2
{
3
 
    /* If we are using CodePress, we can only safely disable the save button
4
 
     * (indicating that there are no changes to save) if the engine supports
5
 
     * change notification, so the button can be enabled again.
6
 
     *
7
 
     * Our non-CodePress mode just uses normal textarea events, so is always
8
 
     * fine.
9
 
     */
10
 
    if((!using_codepress) || editbox.editor.addChangeHandler)
11
 
    {
12
 
        var savebutton = document.getElementById("save_button");
13
 
        savebutton.disabled = true;
14
 
        window.onbeforeunload = null;
15
 
    }
 
3
    var savebutton = document.getElementById("save_button");
 
4
    savebutton.disabled = true;
 
5
    window.onbeforeunload = null;
16
6
}
17
7
 
18
8
function save_file(filename)
19
9
{
20
 
    if (using_codepress)
21
 
        data = editbox.getCode();
 
10
    if (using_codemirror)
 
11
        data = codemirror.getCode();
22
12
    else
23
13
        data = document.getElementById("editbox").value;
24
14
 
35
25
        do_action("putfile", filename,
36
26
                  {"path":".", "data":data, "overwrite":"true"},
37
27
                  "multipart/form-data");
38
 
        disable_save_if_safe();
 
28
        disable_save();
39
29
    }
40
30
}
41
31
 
89
79
 
90
80
function highlighting_changed(select)
91
81
{
92
 
    editbox.edit(editbox.getCode(), select.value);
93
 
}
94
 
 
95
 
function initialise_codepress()
96
 
{
97
 
    editbox.addChangeHandler(edit_text);
98
 
    editbox.addSaveHandler(function() {document.getElementById("save_button").click()});
99
 
     
100
 
    /* We can only safely disable the save button on the first load.
101
 
     * Syntax highlighting changes will also get this function called.
102
 
     * We unfortunately need the change handler added each time.
103
 
     */
104
 
    if (!initialise_codepress.already)
105
 
    {
106
 
        disable_save_if_safe();
107
 
        initialise_codepress.already = true;
108
 
    }
 
82
    codemirror_language(select.value);
109
83
}
110
84
 
111
85
/** Presents the text editor.
144
118
    txt_elem.style.height = "100%";
145
119
    window.onbeforeunload = confirm_beforeunload;
146
120
 
147
 
    /* XXX: Lord, please forgive me for browser sniffing.
148
 
            CodePress only works properly in real Gecko at the moment,
149
 
            so we must go to great and evil lengths to sniff it out.
150
 
            It's by no means a complete check, but it has to support
151
 
            more browsers than the previous situation.
152
 
            This should be killed ASAP when we fix/replace CodePress.
153
 
     */
154
 
    using_codepress = (navigator.userAgent.match('Gecko') &&
155
 
                       !navigator.userAgent.match('WebKit') &&
156
 
                       !navigator.userAgent.match('KHTML') &&
157
 
                       !navigator.userAgent.match('Presto'))
 
121
    /* Always use CodeMirror (unless we find a good reason not to!) */
 
122
    using_codemirror = true;
158
123
 
159
 
    if (using_codepress)
 
124
    if (using_codemirror)
160
125
    {
161
 
        /* This is probably real Gecko. Try to fire up CodePress.
162
 
         * If it fails we'll have a horrible mess, so we'll hope.
163
 
         */
164
 
        txt_elem.className = "codepress autocomplete-off " + language;
165
 
        CodePress.run();
 
126
        /* CodeMirror */
 
127
        using_codemirror = true;
 
128
        codemirror = new CodeMirror.fromTextArea(txt_elem, {
 
129
            path: codemirrorpath,
 
130
            stylesheet: [
 
131
                    codemirrorpath + "/contrib/python/css/pythoncolors.css",
 
132
                    codemirrorpath + "/css/xmlcolors.css",
 
133
                    codemirrorpath + "/css/jscolors.css",
 
134
                    codemirrorpath + "/css/csscolors.css"
 
135
                    ],
 
136
            basefiles: ["js/util.js",
 
137
                    "js/stringstream.js",
 
138
                    "js/select.js",
 
139
                    "js/undo.js",
 
140
                    "js/editor.js",
 
141
                    "js/tokenize.js"
 
142
                    ],
 
143
            parserfile: ["contrib/python/js/parsepython.js",
 
144
                    "js/parsexml.js",
 
145
                    "js/parsecss.js",
 
146
                    "js/tokenizejavascript.js",
 
147
                    "js/parsejavascript.js",
 
148
                    "js/parsehtmlmixed.js",
 
149
                    "js/parsedummy.js"
 
150
                    ],
 
151
            onChange: edit_text,
 
152
            indentUnit: 4,
 
153
            tabMode: "spaces",
 
154
            lineNumbers: true,
 
155
            initCallback: function() {codemirror_language(language);},
 
156
            saveFunction: function() {document.getElementById("save_button").click();}
 
157
        });
166
158
 
167
 
        /* And set a callback so we know that the editor iframe is loaded so
168
 
         * we can set a callback so we know when to enable the save button.
169
 
         * We also take this opportunity to disable the save button, if
170
 
         * the browser is likely to reenable it as needed. */
171
 
        editbox.onload = initialise_codepress;
172
159
    }
173
 
    else
174
 
    {
175
 
        /* Not using CodePress, so we can already disable the Save button. */
176
 
        disable_save_if_safe();
 
160
 
 
161
    /* Not using CodePress, so we can already disable the Save button. */
 
162
    disable_save();
 
163
 
 
164
}
 
165
 
 
166
function codemirror_language(lang)
 
167
{
 
168
    if(lang == 'python') {
 
169
        codemirror.setParser("PythonParser")
 
170
    } else if(lang == 'html') {
 
171
        codemirror.setParser("HTMLMixedParser")
 
172
    } else if(lang == 'css') {
 
173
        codemirror.setParser("CSSParser")
 
174
    } else if(lang == 'javascript') {
 
175
        codemirror.setParser("JSParser")
 
176
    } else {
 
177
        codemirror.setParser("DummyParser")
177
178
    }
178
179
}
179
180