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

809 by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is
1
function disable_save_if_safe()
2
{
1308.1.3 by William Grant
Disable the save button if not using CodePress.
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)
809 by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is
11
    {
12
        var savebutton = document.getElementById("save_button");
13
        savebutton.disabled = true;
856 by wagrant
editor.js: Prompt before allowing users to navigate away from a
14
        window.onbeforeunload = null;
809 by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is
15
    }
16
}
17
836 by wagrant
Split the editor's Save and Save As functionality. This makes things
18
function save_file(filename)
221 by mattgiuca
editor: Made saving work.
19
{
1308.1.4 by William Grant
Get the text to be saved from the textarea if not CodePressing.
20
    if (using_codepress)
21
        data = editbox.getCode();
22
    else
23
        data = document.getElementById("editbox").value;
24
1791.1.3 by David Coles
File Browser: Convert newline characters read from textareas to single LF.
25
    /* Convert newlines to a single LF (mainly for IE's CRLFs) */
26
    data = data.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
27
385 by mattgiuca
browser/editor: Clicking "Save" in the editor does not cause a page refresh
28
    /* Do NOT refresh the page contents (causes problems for editarea and is
29
     * unnecessary). */
906 by wagrant
editor.js: Warn before saving if the open file is an old revision.
30
    if (current_file.svnstatus != "revision" ||
31
        confirm("You are currently viewing an older version of this file. " +
32
                "Saving will overwrite the current version. " +
33
                "Are you sure you want to continue?"))
34
    {
35
        do_action("putfile", filename,
36
                  {"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
37
                  "multipart/form-data");
906 by wagrant
editor.js: Warn before saving if the open file is an old revision.
38
        disable_save_if_safe();
39
    }
221 by mattgiuca
editor: Made saving work.
40
}
41
836 by wagrant
Split the editor's Save and Save As functionality. This makes things
42
function save_file_as(default_filename)
43
{
839 by wagrant
Prompt on Save As if it would overwrite.
44
    filename = prompt("Path to save to:", default_filename);
45
    if (!filename) return;
46
47
    /* The filename will be path_joined with the app name, so needs to not
48
     * be absolute, lest it clobber the app name. */
49
    if (filename.charAt(0) == "/") filename = filename.substring(1);
50
    ajax_call(save_file_as_callback, "fileservice", filename, {}, "POST");
51
}
52
53
function save_file_as_callback(response)
54
{
55
    if (response.status == 404 || confirm("Are you sure you want to overwrite " + filename + "?"))
56
        save_file(filename);
836 by wagrant
Split the editor's Save and Save As functionality. This makes things
57
}
58
856 by wagrant
editor.js: Prompt before allowing users to navigate away from a
59
/* Return a warning to be used in window.onbeforeunload. */
60
function confirm_beforeunload() {
61
    return 'If you continue, any unsaved changes to the current file will be lost.';
62
}
63
221 by mattgiuca
editor: Made saving work.
64
function edit_text()
65
{
778 by mattgiuca
editor.js: The Save button now enables when the text is changed, and disables
66
    var savebutton = document.getElementById("save_button");
809 by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is
67
    savebutton.disabled = false;
856 by wagrant
editor.js: Prompt before allowing users to navigate away from a
68
    window.onbeforeunload = confirm_beforeunload;
221 by mattgiuca
editor: Made saving work.
69
}
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
70
806 by William Grant
www/apps/browser/__init__.py: Display the save widget in actions2 for
71
/** Presents the "editor heading" inserting it into a given element at
72
 *  the front. Note that the save widget is handled by the Python.
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
73
 */
384 by mattgiuca
editor.js: Rewrote the way the editor and surrounding UI are inserted. The top
74
function present_editorhead(elem, path, handler_type)
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
75
{
777 by mattgiuca
browser: Now hides the "More actions" box altogether if the current file is
76
    var div = document.getElementById("actions2");
221 by mattgiuca
editor: Made saving work.
77
78
    /* Print a warning message if this is not actually a text file.
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
79
     */
80
    if (handler_type != "text")
81
    {
82
        var warn = dom_make_text_elem("p",
83
            "Warning: You are editing a binary " +
84
            "file, which explains any strange characters you may see. If " +
85
            "you save this file, you could corrupt it.");
86
        div.appendChild(warn);
87
    }
384 by mattgiuca
editor.js: Rewrote the way the editor and surrounding UI are inserted. The top
88
}
89
837 by wagrant
Add a highlighting-language selector to the editor.
90
function highlighting_changed(select)
91
{
92
    editbox.edit(editbox.getCode(), select.value);
93
}
94
866 by wagrant
browser: Only disable the save button on the first load of the editor.
95
function initialise_codepress()
96
{
97
    editbox.addChangeHandler(edit_text);
998 by wagrant
editor: Add a hook to CodePress to catch Ctrl+S. Use that to trigger
98
    editbox.addSaveHandler(function() {document.getElementById("save_button").click()});
866 by wagrant
browser: Only disable the save button on the first load of the editor.
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
    }
109
}
110
384 by mattgiuca
editor.js: Rewrote the way the editor and surrounding UI are inserted. The top
111
/** Presents the text editor.
112
 */
113
function handle_text(path, text, handler_type)
114
{
115
    /* Create a textarea with the text in it
116
     * (The makings of a primitive editor).
117
     */
118
    var files = document.getElementById("filesbody");
119
    /* Put our UI at the top */
120
    present_editorhead(files, path, handler_type);
121
122
    var div = document.createElement("div");
809 by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is
123
    div.style.height = '100%';
384 by mattgiuca
editor.js: Rewrote the way the editor and surrounding UI are inserted. The top
124
    files.appendChild(div);
1312 by William Grant
Use textarea.value to set the content of the editor's textarea.
125
    var txt_elem = document.createElement("textarea");
126
    txt_elem.value = text.toString();
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
127
    div.appendChild(txt_elem);
128
    txt_elem.setAttribute("id", "editbox");
837 by wagrant
Add a highlighting-language selector to the editor.
129
    language = language_from_mime(current_file.type);
810 by William Grant
Merge another bit of killall-editarea - automatic filetype detection.
130
131
    // Assume plaintext if no type can be determined.
837 by wagrant
Add a highlighting-language selector to the editor.
132
    language = language ? language : "text";
133
    document.getElementById("highlighting_select").value = language;
134
1308.1.5 by William Grant
Make the save button change handlers behave properly with a textarea.
135
    $(txt_elem).change(edit_text);
136
137
    /* This isn't ideal, as Opera seems to fire it even for non-textual keys.
138
     * But IE and WebKit don't, so this will behave properly in most browsers.
139
     * This makes me sad.
140
     */
141
    $(txt_elem).keypress(edit_text);
142
1308.1.2 by William Grant
Make the editor textarea take up the full size.
143
    txt_elem.style.width = "100%";
144
    txt_elem.style.height = "100%";
856 by wagrant
editor.js: Prompt before allowing users to navigate away from a
145
    window.onbeforeunload = confirm_beforeunload;
146
1308.1.1 by William Grant
Only start CodePress when we are running in something that claims to be Gecko, and isn't WebKit or Opera.
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
     */
1308.1.3 by William Grant
Disable the save button if not using CodePress.
154
    using_codepress = (navigator.userAgent.match('Gecko') &&
155
                       !navigator.userAgent.match('WebKit') &&
1316 by David Coles
Degrade text editor to textarea in KHTML
156
                       !navigator.userAgent.match('KHTML') &&
1308.1.3 by William Grant
Disable the save button if not using CodePress.
157
                       !navigator.userAgent.match('Presto'))
158
159
    if (using_codepress)
160
    {
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
         */
1308.1.1 by William Grant
Only start CodePress when we are running in something that claims to be Gecko, and isn't WebKit or Opera.
164
        txt_elem.className = "codepress autocomplete-off " + language;
165
        CodePress.run();
166
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
    }
1308.1.3 by William Grant
Disable the save button if not using CodePress.
173
    else
174
    {
175
        /* Not using CodePress, so we can already disable the Save button. */
176
        disable_save_if_safe();
177
    }
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
178
}
179
810 by William Grant
Merge another bit of killall-editarea - automatic filetype detection.
180
function language_from_mime(mime)
181
{
182
    return {'text/x-python': 'python',
1205 by William Grant
Treat application/x-javascript the same as application/javascript.
183
            'application/x-javascript': 'javascript',
810 by William Grant
Merge another bit of killall-editarea - automatic filetype detection.
184
            'application/javascript': 'javascript',
185
            'text/css': 'css',
186
            'text/plain': 'text',
187
            'text/html': 'html',
987 by wagrant
editor: Use HTML highlighting for XML by default.
188
            'application/xml': 'html',
810 by William Grant
Merge another bit of killall-editarea - automatic filetype detection.
189
            'application/xhtml+xml': 'html'}[mime];
190
}