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

« back to all changes in this revision

Viewing changes to www/media/browser/editor.js

  • Committer: mattgiuca
  • Date: 2008-02-14 00:49:06 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:450
Set svn:ignore on a a few more things.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
function disable_save()
2
 
{
3
 
    var savebutton = document.getElementById("save_button");
4
 
    savebutton.disabled = true;
5
 
    window.onbeforeunload = null;
6
 
}
7
 
 
8
 
function save_file(filename)
9
 
{
10
 
    if (using_codemirror)
11
 
        data = codemirror.getCode();
12
 
    else
13
 
        data = document.getElementById("editbox").value;
14
 
 
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
 
 
 
1
saved_status = null;
 
2
 
 
3
function save_file()
 
4
{
 
5
    filename = document.getElementById("save_filename").value;
 
6
    data = editAreaLoader.getValue("editbox");
18
7
    /* Do NOT refresh the page contents (causes problems for editarea and is
19
8
     * unnecessary). */
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"},
27
 
                  "multipart/form-data");
28
 
        disable_save();
29
 
    }
30
 
}
31
 
 
32
 
function save_file_as(default_filename)
33
 
{
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);
47
 
}
48
 
 
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.';
 
9
    do_action("putfile", filename, {"path":".", "data":data}, null, true);
 
10
    saved_status.data = "Saved.";
52
11
}
53
12
 
54
13
function edit_text()
55
14
{
56
 
    var savebutton = document.getElementById("save_button");
57
 
    savebutton.disabled = false;
58
 
    window.onbeforeunload = confirm_beforeunload;
 
15
    saved_status.data = "Not saved.";
59
16
}
60
17
 
61
 
function highlighting_changed(select)
 
18
/** Presents the "editor heading" (the part with the save box)
 
19
 * inserting it into a given element at the front.
 
20
 */
 
21
function present_editorhead(elem, path, handler_type)
62
22
{
63
 
    codemirror_language(select.value);
 
23
    var div = document.createElement("div");
 
24
    /* Insert as the head element */
 
25
    elem.insertBefore(div, elem.firstChild)
 
26
    div.setAttribute("id", "editorhead");
 
27
 
 
28
    /* Set up minimal interface */
 
29
    var p = dom_make_text_elem("p", "Path: ");
 
30
    var pathname = document.createElement("input");
 
31
    pathname.setAttribute("type", "text");
 
32
    pathname.setAttribute("size", "30");
 
33
    pathname.setAttribute("id", "save_filename");
 
34
    pathname.setAttribute("value", path);
 
35
    p.appendChild(pathname);
 
36
    var savebutton = document.createElement("input");
 
37
    savebutton.setAttribute("type", "button");
 
38
    savebutton.setAttribute("value", "Save");
 
39
    savebutton.setAttribute("onclick", "save_file()");
 
40
    p.appendChild(savebutton);
 
41
    var t = document.createTextNode(" ");
 
42
    p.appendChild(t);
 
43
    saved_status = document.createTextNode("Saved.");
 
44
    //p.appendChild(saved_status);
 
45
    div.appendChild(p);
 
46
 
 
47
    /* Print a warning message if this is not actually a text file.
 
48
     */
 
49
    if (handler_type != "text")
 
50
    {
 
51
        var warn = dom_make_text_elem("p",
 
52
            "Warning: You are editing a binary " +
 
53
            "file, which explains any strange characters you may see. If " +
 
54
            "you save this file, you could corrupt it.");
 
55
        div.appendChild(warn);
 
56
    }
64
57
}
65
58
 
66
59
/** Presents the text editor.
67
60
 */
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)
 
61
function handle_text(path, text, handler_type)
88
62
{
89
63
    /* Create a textarea with the text in it
90
64
     * (The makings of a primitive editor).
91
65
     */
 
66
    setmode(true);
 
67
 
92
68
    var files = document.getElementById("filesbody");
 
69
    /* Put our UI at the top */
 
70
    present_editorhead(files, path, handler_type);
93
71
 
94
72
    var div = document.createElement("div");
95
 
    div.style.height = '100%';
96
73
    files.appendChild(div);
97
 
    var txt_elem = document.createElement("textarea");
98
 
    txt_elem.value = response_text.toString();
 
74
    var txt_elem = dom_make_text_elem("textarea",
 
75
        text.toString())
99
76
    div.appendChild(txt_elem);
100
77
    txt_elem.setAttribute("id", "editbox");
101
 
    language = language_from_mime(content_type);
102
 
 
103
 
    // Assume plaintext if no type can be determined.
104
 
    language = language ? language : "text";
105
 
    document.getElementById("highlighting_select").value = language;
106
 
 
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
 
 
115
 
    txt_elem.style.width = "100%";
116
 
    txt_elem.style.height = "100%";
117
 
    txt_elem.style.padding = "0";
118
 
    window.onbeforeunload = confirm_beforeunload;
119
 
 
120
 
    /* Always use CodeMirror (unless we find a good reason not to!) */
121
 
    using_codemirror = true;
122
 
 
123
 
    if (using_codemirror)
124
 
    {
125
 
        /* CodeMirror */
126
 
        using_codemirror = true;
127
 
        codemirror = new CodeMirror.fromTextArea(txt_elem, {
128
 
            path: codemirrorpath,
129
 
            stylesheet: [
130
 
                    codemirrorpath + "/contrib/python/css/pythoncolors.css",
131
 
                    codemirrorpath + "/css/xmlcolors.css",
132
 
                    codemirrorpath + "/css/jscolors.css",
133
 
                    codemirrorpath + "/css/csscolors.css"
134
 
                    ],
135
 
            basefiles: ["js/util.js",
136
 
                    "js/stringstream.js",
137
 
                    "js/select.js",
138
 
                    "js/undo.js",
139
 
                    "js/editor.js",
140
 
                    "js/tokenize.js"
141
 
                    ],
142
 
            parserfile: ["contrib/python/js/parsepython.js",
143
 
                    "js/parsexml.js",
144
 
                    "js/parsecss.js",
145
 
                    "js/tokenizejavascript.js",
146
 
                    "js/parsejavascript.js",
147
 
                    "js/parsehtmlmixed.js",
148
 
                    "js/parsedummy.js"
149
 
                    ],
150
 
            onChange: edit_text,
151
 
            indentUnit: 4,
152
 
            width: "auto", // Fixes issue with > 100% width
153
 
            tabMode: "spaces",
154
 
            lineNumbers: true,
155
 
            initCallback: function() {codemirror_language(language);},
156
 
            saveFunction: function() {document.getElementById("save_button").click();}
157
 
        });
158
 
 
159
 
    }
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")
178
 
    }
179
 
 
180
 
    // Show actions bar
181
 
    $("#actions2_file").show();
182
 
}
183
 
 
184
 
function language_from_mime(mime)
185
 
{
186
 
    return {'text/x-python': 'python',
187
 
            'application/x-javascript': 'javascript',
188
 
            'application/javascript': 'javascript',
189
 
            'text/css': 'css',
190
 
            'text/plain': 'text',
191
 
            'text/html': 'html',
192
 
            'application/xml': 'html',
193
 
            'application/xhtml+xml': 'html'}[mime];
194
 
}
 
78
    txt_elem.setAttribute("onchange", "edit_text()");
 
79
    /* TODO: Make CSS height: 100% work */
 
80
    txt_elem.setAttribute("rows", "35");
 
81
 
 
82
    /* Load EditArea into the editbox */
 
83
    editAreaLoader.init({
 
84
        id : "editbox",
 
85
        syntax: "python",
 
86
        start_highlight: true,
 
87
        allow_toggle: false,
 
88
        allow_resize: false,
 
89
        replace_tab_by_spaces: 4,
 
90
    });
 
91
}
 
92