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

« back to all changes in this revision

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

  • Committer: mattgiuca
  • Date: 2007-12-07 03:52:26 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:13
notes/misc.txt: Added notes from today's meeting. (Steven Bird)
notes/README: Explain that this is markdown.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
function disable_save_if_safe()
2
 
{
3
 
    /* If this is defined, this engine supports change notification, so is able
4
 
     * to enable the button again. Disable it for them. */
5
 
    if(editbox.editor.addChangeHandler)
6
 
    {
7
 
        var savebutton = document.getElementById("save_button");
8
 
        savebutton.disabled = true;
9
 
    }
10
 
}
11
 
 
12
 
function save_file(filename)
13
 
{
14
 
    data = editbox.getCode();
15
 
    /* Do NOT refresh the page contents (causes problems for editarea and is
16
 
     * unnecessary). */
17
 
    do_action("putfile", filename,
18
 
              {"path":".", "data":data, "overwrite":"true"},
19
 
              "multipart/form-data", true);
20
 
    disable_save_if_safe();
21
 
}
22
 
 
23
 
function save_file_as(default_filename)
24
 
{
25
 
    filename = prompt("Path to save to:", default_filename);
26
 
    if (!filename) return;
27
 
 
28
 
    /* The filename will be path_joined with the app name, so needs to not
29
 
     * be absolute, lest it clobber the app name. */
30
 
    if (filename.charAt(0) == "/") filename = filename.substring(1);
31
 
    ajax_call(save_file_as_callback, "fileservice", filename, {}, "POST");
32
 
}
33
 
 
34
 
function save_file_as_callback(response)
35
 
{
36
 
    if (response.status == 404 || confirm("Are you sure you want to overwrite " + filename + "?"))
37
 
        save_file(filename);
38
 
}
39
 
 
40
 
function edit_text()
41
 
{
42
 
    var savebutton = document.getElementById("save_button");
43
 
    savebutton.disabled = false;
44
 
}
45
 
 
46
 
/** Presents the "editor heading" inserting it into a given element at
47
 
 *  the front. Note that the save widget is handled by the Python.
48
 
 */
49
 
function present_editorhead(elem, path, handler_type)
50
 
{
51
 
    var div = document.getElementById("actions2");
52
 
 
53
 
    /* Print a warning message if this is not actually a text file.
54
 
     */
55
 
    if (handler_type != "text")
56
 
    {
57
 
        var warn = dom_make_text_elem("p",
58
 
            "Warning: You are editing a binary " +
59
 
            "file, which explains any strange characters you may see. If " +
60
 
            "you save this file, you could corrupt it.");
61
 
        div.appendChild(warn);
62
 
    }
63
 
}
64
 
 
65
 
function highlighting_changed(select)
66
 
{
67
 
    editbox.edit(editbox.getCode(), select.value);
68
 
}
69
 
 
70
 
/** Presents the text editor.
71
 
 */
72
 
function handle_text(path, text, handler_type)
73
 
{
74
 
    /* Create a textarea with the text in it
75
 
     * (The makings of a primitive editor).
76
 
     */
77
 
    var files = document.getElementById("filesbody");
78
 
    /* Put our UI at the top */
79
 
    present_editorhead(files, path, handler_type);
80
 
 
81
 
    var div = document.createElement("div");
82
 
    div.style.height = '100%';
83
 
    files.appendChild(div);
84
 
    var txt_elem = dom_make_text_elem("textarea",
85
 
        text.toString())
86
 
    div.appendChild(txt_elem);
87
 
    txt_elem.setAttribute("id", "editbox");
88
 
    language = language_from_mime(current_file.type);
89
 
 
90
 
    // Assume plaintext if no type can be determined.
91
 
    language = language ? language : "text";
92
 
    document.getElementById("highlighting_select").value = language;
93
 
 
94
 
    txt_elem.className = "codepress " + language;
95
 
    txt_elem.setAttribute("onchange", "edit_text()");
96
 
    /* TODO: Make CSS height: 100% work */
97
 
    txt_elem.setAttribute("rows", "35");
98
 
    CodePress.run();
99
 
 
100
 
    /* And set a callback so we know that the editor iframe is loaded so we
101
 
     * can set a callback so we know when to enable the save button.
102
 
     * We also take this opportunity to disable the save button, if
103
 
     * the browser is likely to reenable it as needed. */
104
 
    editbox.onload = function() {editbox.addChangeHandler(edit_text);
105
 
                                 disable_save_if_safe(); };
106
 
}
107
 
 
108
 
function language_from_mime(mime)
109
 
{
110
 
    return {'text/x-python': 'python',
111
 
            'application/javascript': 'javascript',
112
 
            'text/css': 'css',
113
 
            'text/plain': 'text',
114
 
            'text/html': 'html',
115
 
            'application/xhtml+xml': 'html'}[mime];
116
 
}