~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
385 by mattgiuca
browser/editor: Clicking "Save" in the editor does not cause a page refresh
25
    /* Do NOT refresh the page contents (causes problems for editarea and is
26
     * unnecessary). */
906 by wagrant
editor.js: Warn before saving if the open file is an old revision.
27
    if (current_file.svnstatus != "revision" ||
28
        confirm("You are currently viewing an older version of this file. " +
29
                "Saving will overwrite the current version. " +
30
                "Are you sure you want to continue?"))
31
    {
32
        do_action("putfile", filename,
33
                  {"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
34
                  "multipart/form-data");
906 by wagrant
editor.js: Warn before saving if the open file is an old revision.
35
        disable_save_if_safe();
36
    }
221 by mattgiuca
editor: Made saving work.
37
}
38
836 by wagrant
Split the editor's Save and Save As functionality. This makes things
39
function save_file_as(default_filename)
40
{
839 by wagrant
Prompt on Save As if it would overwrite.
41
    filename = prompt("Path to save to:", default_filename);
42
    if (!filename) return;
43
44
    /* The filename will be path_joined with the app name, so needs to not
45
     * be absolute, lest it clobber the app name. */
46
    if (filename.charAt(0) == "/") filename = filename.substring(1);
47
    ajax_call(save_file_as_callback, "fileservice", filename, {}, "POST");
48
}
49
50
function save_file_as_callback(response)
51
{
52
    if (response.status == 404 || confirm("Are you sure you want to overwrite " + filename + "?"))
53
        save_file(filename);
836 by wagrant
Split the editor's Save and Save As functionality. This makes things
54
}
55
856 by wagrant
editor.js: Prompt before allowing users to navigate away from a
56
/* Return a warning to be used in window.onbeforeunload. */
57
function confirm_beforeunload() {
58
    return 'If you continue, any unsaved changes to the current file will be lost.';
59
}
60
221 by mattgiuca
editor: Made saving work.
61
function edit_text()
62
{
778 by mattgiuca
editor.js: The Save button now enables when the text is changed, and disables
63
    var savebutton = document.getElementById("save_button");
809 by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is
64
    savebutton.disabled = false;
856 by wagrant
editor.js: Prompt before allowing users to navigate away from a
65
    window.onbeforeunload = confirm_beforeunload;
221 by mattgiuca
editor: Made saving work.
66
}
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
67
806 by William Grant
www/apps/browser/__init__.py: Display the save widget in actions2 for
68
/** Presents the "editor heading" inserting it into a given element at
69
 *  the front. Note that the save widget is handled by the Python.
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
70
 */
384 by mattgiuca
editor.js: Rewrote the way the editor and surrounding UI are inserted. The top
71
function present_editorhead(elem, path, handler_type)
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
72
{
777 by mattgiuca
browser: Now hides the "More actions" box altogether if the current file is
73
    var div = document.getElementById("actions2");
221 by mattgiuca
editor: Made saving work.
74
75
    /* Print a warning message if this is not actually a text file.
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
76
     */
77
    if (handler_type != "text")
78
    {
79
        var warn = dom_make_text_elem("p",
80
            "Warning: You are editing a binary " +
81
            "file, which explains any strange characters you may see. If " +
82
            "you save this file, you could corrupt it.");
83
        div.appendChild(warn);
84
    }
384 by mattgiuca
editor.js: Rewrote the way the editor and surrounding UI are inserted. The top
85
}
86
837 by wagrant
Add a highlighting-language selector to the editor.
87
function highlighting_changed(select)
88
{
89
    editbox.edit(editbox.getCode(), select.value);
90
}
91
866 by wagrant
browser: Only disable the save button on the first load of the editor.
92
function initialise_codepress()
93
{
94
    editbox.addChangeHandler(edit_text);
998 by wagrant
editor: Add a hook to CodePress to catch Ctrl+S. Use that to trigger
95
    editbox.addSaveHandler(function() {document.getElementById("save_button").click()});
866 by wagrant
browser: Only disable the save button on the first load of the editor.
96
     
97
    /* We can only safely disable the save button on the first load.
98
     * Syntax highlighting changes will also get this function called.
99
     * We unfortunately need the change handler added each time.
100
     */
101
    if (!initialise_codepress.already)
102
    {
103
        disable_save_if_safe();
104
        initialise_codepress.already = true;
105
    }
106
}
107
384 by mattgiuca
editor.js: Rewrote the way the editor and surrounding UI are inserted. The top
108
/** Presents the text editor.
109
 */
110
function handle_text(path, text, handler_type)
111
{
112
    /* Create a textarea with the text in it
113
     * (The makings of a primitive editor).
114
     */
115
    var files = document.getElementById("filesbody");
116
    /* Put our UI at the top */
117
    present_editorhead(files, path, handler_type);
118
119
    var div = document.createElement("div");
809 by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is
120
    div.style.height = '100%';
384 by mattgiuca
editor.js: Rewrote the way the editor and surrounding UI are inserted. The top
121
    files.appendChild(div);
1312 by William Grant
Use textarea.value to set the content of the editor's textarea.
122
    var txt_elem = document.createElement("textarea");
123
    txt_elem.value = text.toString();
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
124
    div.appendChild(txt_elem);
125
    txt_elem.setAttribute("id", "editbox");
837 by wagrant
Add a highlighting-language selector to the editor.
126
    language = language_from_mime(current_file.type);
810 by William Grant
Merge another bit of killall-editarea - automatic filetype detection.
127
128
    // Assume plaintext if no type can be determined.
837 by wagrant
Add a highlighting-language selector to the editor.
129
    language = language ? language : "text";
130
    document.getElementById("highlighting_select").value = language;
131
1308.1.5 by William Grant
Make the save button change handlers behave properly with a textarea.
132
    $(txt_elem).change(edit_text);
133
134
    /* This isn't ideal, as Opera seems to fire it even for non-textual keys.
135
     * But IE and WebKit don't, so this will behave properly in most browsers.
136
     * This makes me sad.
137
     */
138
    $(txt_elem).keypress(edit_text);
139
1308.1.2 by William Grant
Make the editor textarea take up the full size.
140
    txt_elem.style.width = "100%";
141
    txt_elem.style.height = "100%";
856 by wagrant
editor.js: Prompt before allowing users to navigate away from a
142
    window.onbeforeunload = confirm_beforeunload;
143
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.
144
    /* XXX: Lord, please forgive me for browser sniffing.
145
            CodePress only works properly in real Gecko at the moment,
146
            so we must go to great and evil lengths to sniff it out.
147
            It's by no means a complete check, but it has to support
148
            more browsers than the previous situation.
149
            This should be killed ASAP when we fix/replace CodePress.
150
     */
1308.1.3 by William Grant
Disable the save button if not using CodePress.
151
    using_codepress = (navigator.userAgent.match('Gecko') &&
152
                       !navigator.userAgent.match('WebKit') &&
1316 by David Coles
Degrade text editor to textarea in KHTML
153
                       !navigator.userAgent.match('KHTML') &&
1308.1.3 by William Grant
Disable the save button if not using CodePress.
154
                       !navigator.userAgent.match('Presto'))
155
156
    if (using_codepress)
157
    {
158
        /* This is probably real Gecko. Try to fire up CodePress.
159
         * If it fails we'll have a horrible mess, so we'll hope.
160
         */
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.
161
        txt_elem.className = "codepress autocomplete-off " + language;
162
        CodePress.run();
163
164
        /* And set a callback so we know that the editor iframe is loaded so
165
         * we can set a callback so we know when to enable the save button.
166
         * We also take this opportunity to disable the save button, if
167
         * the browser is likely to reenable it as needed. */
168
        editbox.onload = initialise_codepress;
169
    }
1308.1.3 by William Grant
Disable the save button if not using CodePress.
170
    else
171
    {
172
        /* Not using CodePress, so we can already disable the Save button. */
173
        disable_save_if_safe();
174
    }
220 by mattgiuca
browser: Removed 3 buttons which didn't do anything.
175
}
176
810 by William Grant
Merge another bit of killall-editarea - automatic filetype detection.
177
function language_from_mime(mime)
178
{
179
    return {'text/x-python': 'python',
1205 by William Grant
Treat application/x-javascript the same as application/javascript.
180
            'application/x-javascript': 'javascript',
810 by William Grant
Merge another bit of killall-editarea - automatic filetype detection.
181
            'application/javascript': 'javascript',
182
            'text/css': 'css',
183
            'text/plain': 'text',
184
            'text/html': 'html',
987 by wagrant
editor: Use HTML highlighting for XML by default.
185
            'application/xml': 'html',
810 by William Grant
Merge another bit of killall-editarea - automatic filetype detection.
186
            'application/xhtml+xml': 'html'}[mime];
187
}