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

1790.1.1 by David Coles
Editor: Migrate to CodeMirror. Syntax highlighting hardcoded to Python for the
1
function disable_save()
809 by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is
2
{
1790.1.1 by David Coles
Editor: Migrate to CodeMirror. Syntax highlighting hardcoded to Python for the
3
    var savebutton = document.getElementById("save_button");
4
    savebutton.disabled = true;
5
    window.onbeforeunload = null;
809 by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is
6
}
7
836 by wagrant
Split the editor's Save and Save As functionality. This makes things
8
function save_file(filename)
221 by mattgiuca
editor: Made saving work.
9
{
1790.1.1 by David Coles
Editor: Migrate to CodeMirror. Syntax highlighting hardcoded to Python for the
10
    if (using_codemirror)
11
        data = codemirror.getCode();
1308.1.4 by William Grant
Get the text to be saved from the textarea if not CodePressing.
12
    else
13
        data = document.getElementById("editbox").value;
14
1791.1.3 by David Coles
File Browser: Convert newline characters read from textareas to single LF.
15
    /* Convert newlines to a single LF (mainly for IE's CRLFs) */
16
    data = data.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
17
385 by mattgiuca
browser/editor: Clicking "Save" in the editor does not cause a page refresh
18
    /* Do NOT refresh the page contents (causes problems for editarea and is
19
     * unnecessary). */
906 by wagrant
editor.js: Warn before saving if the open file is an old revision.
20
    if (current_file.svnstatus != "revision" ||
21
        confirm("You are currently viewing an older version of this file. " +
22
                "Saving will overwrite the current version. " +
23
                "Are you sure you want to continue?"))
24
    {
25
        do_action("putfile", filename,
26
                  {"path":".", "data":data, "overwrite":"true"},
1326.1.2 by David Coles
Small refactor of browser's JavaScript to allow do_action to be called from
27
                  "multipart/form-data");
1790.1.1 by David Coles
Editor: Migrate to CodeMirror. Syntax highlighting hardcoded to Python for the
28
        disable_save();
906 by wagrant
editor.js: Warn before saving if the open file is an old revision.
29
    }
221 by mattgiuca
editor: Made saving work.
30
}
31
836 by wagrant
Split the editor's Save and Save As functionality. This makes things
32
function save_file_as(default_filename)
33
{
839 by wagrant
Prompt on Save As if it would overwrite.
34
    filename = prompt("Path to save to:", default_filename);
35
    if (!filename) return;
36
37
    /* The filename will be path_joined with the app name, so needs to not
38
     * be absolute, lest it clobber the app name. */
39
    if (filename.charAt(0) == "/") filename = filename.substring(1);
40
    ajax_call(save_file_as_callback, "fileservice", filename, {}, "POST");
41
}
42
43
function save_file_as_callback(response)
44
{
45
    if (response.status == 404 || confirm("Are you sure you want to overwrite " + filename + "?"))
46
        save_file(filename);
836 by wagrant
Split the editor's Save and Save As functionality. This makes things
47
}
48
856 by wagrant
editor.js: Prompt before allowing users to navigate away from a
49
/* Return a warning to be used in window.onbeforeunload. */
50
function confirm_beforeunload() {
51
    return 'If you continue, any unsaved changes to the current file will be lost.';
52
}
53
221 by mattgiuca
editor: Made saving work.
54
function edit_text()
55
{
778 by mattgiuca
editor.js: The Save button now enables when the text is changed, and disables
56
    var savebutton = document.getElementById("save_button");
809 by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is
57
    savebutton.disabled = false;
856 by wagrant
editor.js: Prompt before allowing users to navigate away from a
58
    window.onbeforeunload = confirm_beforeunload;
221 by mattgiuca
editor: Made saving work.
59
}
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
60
837 by wagrant
Add a highlighting-language selector to the editor.
61
function highlighting_changed(select)
62
{
1790.1.3 by David Coles
Editor: Add line numbers and support for Text, HTML and Python syntax
63
    codemirror_language(select.value);
866 by wagrant
browser: Only disable the save button on the first load of the editor.
64
}
65
384 by mattgiuca
editor.js: Rewrote the way the editor and surrounding UI are inserted. The top
66
/** Presents the text editor.
67
 */
1791.2.7 by David Coles
Change handle_content_response so that we only fetch file contents inside-
68
function handle_text(path, content_type, url_args)
69
{
70
    /* Need to make a 2nd ajax call, this time get the actual file
71
     * contents */
72
    callback = function(response)
73
        {
74
            /* Read the response and set up the page accordingly */
75
            handle_text_response(path, content_type, response.responseText);
76
        }
77
    /* Call the server and request the listing. */
78
    if (url_args)
79
        args = shallow_clone_object(url_args);
80
    else
81
        args = {};
82
    /* This time, get the contents of the file, not its metadata */
83
    args['return'] = "contents";
84
    ajax_call(callback, service_app, path, args, "GET");
85
}
86
87
function handle_text_response(path, content_type, response_text)
384 by mattgiuca
editor.js: Rewrote the way the editor and surrounding UI are inserted. The top
88
{
89
    /* Create a textarea with the text in it
90
     * (The makings of a primitive editor).
91
     */
92
    var files = document.getElementById("filesbody");
93
94
    var div = document.createElement("div");
809 by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is
95
    div.style.height = '100%';
384 by mattgiuca
editor.js: Rewrote the way the editor and surrounding UI are inserted. The top
96
    files.appendChild(div);
1312 by William Grant
Use textarea.value to set the content of the editor's textarea.
97
    var txt_elem = document.createElement("textarea");
1791.2.7 by David Coles
Change handle_content_response so that we only fetch file contents inside-
98
    txt_elem.value = response_text.toString();
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
99
    div.appendChild(txt_elem);
100
    txt_elem.setAttribute("id", "editbox");
1791.2.7 by David Coles
Change handle_content_response so that we only fetch file contents inside-
101
    language = language_from_mime(content_type);
810 by William Grant
Merge another bit of killall-editarea - automatic filetype detection.
102
103
    // Assume plaintext if no type can be determined.
837 by wagrant
Add a highlighting-language selector to the editor.
104
    language = language ? language : "text";
105
    document.getElementById("highlighting_select").value = language;
106
1308.1.5 by William Grant
Make the save button change handlers behave properly with a textarea.
107
    $(txt_elem).change(edit_text);
108
109
    /* This isn't ideal, as Opera seems to fire it even for non-textual keys.
110
     * But IE and WebKit don't, so this will behave properly in most browsers.
111
     * This makes me sad.
112
     */
113
    $(txt_elem).keypress(edit_text);
114
1308.1.2 by William Grant
Make the editor textarea take up the full size.
115
    txt_elem.style.width = "100%";
116
    txt_elem.style.height = "100%";
856 by wagrant
editor.js: Prompt before allowing users to navigate away from a
117
    window.onbeforeunload = confirm_beforeunload;
118
1790.1.1 by David Coles
Editor: Migrate to CodeMirror. Syntax highlighting hardcoded to Python for the
119
    /* Always use CodeMirror (unless we find a good reason not to!) */
120
    using_codemirror = true;
121
122
    if (using_codemirror)
123
    {
124
        /* CodeMirror */
125
        using_codemirror = true;
126
        codemirror = new CodeMirror.fromTextArea(txt_elem, {
1790.1.5 by William Grant
Add external CodeMirror dep, and use it instead of the embedded copy.
127
            path: codemirrorpath,
128
            stylesheet: [
129
                    codemirrorpath + "/contrib/python/css/pythoncolors.css",
130
                    codemirrorpath + "/css/xmlcolors.css",
131
                    codemirrorpath + "/css/jscolors.css",
132
                    codemirrorpath + "/css/csscolors.css"
1790.1.3 by David Coles
Editor: Add line numbers and support for Text, HTML and Python syntax
133
                    ],
1790.1.1 by David Coles
Editor: Migrate to CodeMirror. Syntax highlighting hardcoded to Python for the
134
            basefiles: ["js/util.js",
135
                    "js/stringstream.js",
136
                    "js/select.js",
137
                    "js/undo.js",
138
                    "js/editor.js",
1790.1.3 by David Coles
Editor: Add line numbers and support for Text, HTML and Python syntax
139
                    "js/tokenize.js"
140
                    ],
141
            parserfile: ["contrib/python/js/parsepython.js",
142
                    "js/parsexml.js",
143
                    "js/parsecss.js",
144
                    "js/tokenizejavascript.js",
145
                    "js/parsejavascript.js",
146
                    "js/parsehtmlmixed.js",
147
                    "js/parsedummy.js"
148
                    ],
1790.1.1 by David Coles
Editor: Migrate to CodeMirror. Syntax highlighting hardcoded to Python for the
149
            onChange: edit_text,
150
            indentUnit: 4,
1790.1.3 by David Coles
Editor: Add line numbers and support for Text, HTML and Python syntax
151
            tabMode: "spaces",
152
            lineNumbers: true,
1790.1.6 by William Grant
Readd a Ctrl+S hook, as we had with CodePress.
153
            initCallback: function() {codemirror_language(language);},
154
            saveFunction: function() {document.getElementById("save_button").click();}
1790.1.1 by David Coles
Editor: Migrate to CodeMirror. Syntax highlighting hardcoded to Python for the
155
        });
1790.1.3 by David Coles
Editor: Add line numbers and support for Text, HTML and Python syntax
156
1790.1.1 by David Coles
Editor: Migrate to CodeMirror. Syntax highlighting hardcoded to Python for the
157
    }
158
159
    /* Not using CodePress, so we can already disable the Save button. */
160
    disable_save();
161
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
162
}
163
1790.1.3 by David Coles
Editor: Add line numbers and support for Text, HTML and Python syntax
164
function codemirror_language(lang)
165
{
166
    if(lang == 'python') {
167
        codemirror.setParser("PythonParser")
168
    } else if(lang == 'html') {
169
        codemirror.setParser("HTMLMixedParser")
1790.1.4 by David Coles
Editor: Enable syntax support for Javascript and CSS.
170
    } else if(lang == 'css') {
171
        codemirror.setParser("CSSParser")
172
    } else if(lang == 'javascript') {
173
        codemirror.setParser("JSParser")
1790.1.3 by David Coles
Editor: Add line numbers and support for Text, HTML and Python syntax
174
    } else {
175
        codemirror.setParser("DummyParser")
176
    }
1791.2.2 by David Coles
File Browser: Don't show actions2_file for anything but text editor and
177
178
    // Show actions bar
179
    $("#actions2_file").show();
1790.1.3 by David Coles
Editor: Add line numbers and support for Text, HTML and Python syntax
180
}
181
810 by William Grant
Merge another bit of killall-editarea - automatic filetype detection.
182
function language_from_mime(mime)
183
{
184
    return {'text/x-python': 'python',
1205 by William Grant
Treat application/x-javascript the same as application/javascript.
185
            'application/x-javascript': 'javascript',
810 by William Grant
Merge another bit of killall-editarea - automatic filetype detection.
186
            'application/javascript': 'javascript',
187
            'text/css': 'css',
188
            'text/plain': 'text',
189
            'text/html': 'html',
987 by wagrant
editor: Use HTML highlighting for XML by default.
190
            'application/xml': 'html',
810 by William Grant
Merge another bit of killall-editarea - automatic filetype detection.
191
            'application/xhtml+xml': 'html'}[mime];
192
}