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

« back to all changes in this revision

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

  • Committer: dcoles
  • Date: 2008-07-03 04:20:54 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:803
Setup: Modularised setup.py so it is now no longer over 1000 lines. This should 
allow us to get in there and tidy up each module much easier. Also removed 
updatejails since this functionality seems to be duplicated with remakeuser.py 
and remakealluser.py scripts.

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
 
        window.onbeforeunload = null;
10
 
    }
11
 
}
12
 
 
13
 
function save_file(filename)
14
 
{
15
 
    data = editbox.getCode();
 
1
function save_file()
 
2
{
 
3
    var savebutton = document.getElementById("save_button");
 
4
    var filename = document.getElementById("save_filename").value;
 
5
    data = editAreaLoader.getValue("editbox");
16
6
    /* Do NOT refresh the page contents (causes problems for editarea and is
17
7
     * unnecessary). */
18
8
    do_action("putfile", filename,
19
9
              {"path":".", "data":data, "overwrite":"true"},
20
10
              "multipart/form-data", true);
21
 
    disable_save_if_safe();
22
 
}
23
 
 
24
 
function save_file_as(default_filename)
25
 
{
26
 
    filename = prompt("Path to save to:", default_filename);
27
 
    if (!filename) return;
28
 
 
29
 
    /* The filename will be path_joined with the app name, so needs to not
30
 
     * be absolute, lest it clobber the app name. */
31
 
    if (filename.charAt(0) == "/") filename = filename.substring(1);
32
 
    ajax_call(save_file_as_callback, "fileservice", filename, {}, "POST");
33
 
}
34
 
 
35
 
function save_file_as_callback(response)
36
 
{
37
 
    if (response.status == 404 || confirm("Are you sure you want to overwrite " + filename + "?"))
38
 
        save_file(filename);
39
 
}
40
 
 
41
 
/* Return a warning to be used in window.onbeforeunload. */
42
 
function confirm_beforeunload() {
43
 
    return 'If you continue, any unsaved changes to the current file will be lost.';
 
11
    savebutton.setAttribute("value", "Saved");
 
12
    // XXX Do not disable for now; there is a problem getting the callback
 
13
    // to edit_text.
 
14
    //savebutton.setAttribute("disabled", "disabled");
44
15
}
45
16
 
46
17
function edit_text()
47
18
{
48
19
    var savebutton = document.getElementById("save_button");
49
 
    savebutton.disabled = false;
50
 
    window.onbeforeunload = confirm_beforeunload;
 
20
    savebutton.setAttribute("value", "Save");
 
21
    savebutton.removeAttribute("disabled");
51
22
}
52
23
 
53
 
/** Presents the "editor heading" inserting it into a given element at
54
 
 *  the front. Note that the save widget is handled by the Python.
 
24
/** Presents the "editor heading" (the part with the save box)
 
25
 * inserting it into a given element at the front.
55
26
 */
56
27
function present_editorhead(elem, path, handler_type)
57
28
{
58
29
    var div = document.getElementById("actions2");
59
30
 
 
31
    /* Set up minimal interface */
 
32
    var p = dom_make_text_elem("p", "Save as: ");
 
33
    var pathname = document.createElement("input");
 
34
    pathname.setAttribute("type", "text");
 
35
    pathname.setAttribute("size", "30");
 
36
    pathname.setAttribute("id", "save_filename");
 
37
    pathname.setAttribute("value", path);
 
38
    p.appendChild(pathname);
 
39
    var savebutton = document.createElement("input");
 
40
    savebutton.setAttribute("id", "save_button");
 
41
    savebutton.setAttribute("type", "button");
 
42
    savebutton.setAttribute("value", "Saved");
 
43
    // XXX Do not disable for now; there is a problem getting the callback
 
44
    // to edit_text.
 
45
    //savebutton.setAttribute("disabled", "disabled");
 
46
    savebutton.setAttribute("onclick", "save_file()");
 
47
    p.appendChild(savebutton);
 
48
    var t = document.createTextNode(" ");
 
49
    p.appendChild(t);
 
50
    div.appendChild(p);
 
51
 
60
52
    /* Print a warning message if this is not actually a text file.
61
53
     */
62
54
    if (handler_type != "text")
69
61
    }
70
62
}
71
63
 
72
 
function highlighting_changed(select)
73
 
{
74
 
    editbox.edit(editbox.getCode(), select.value);
75
 
}
76
 
 
77
 
function initialise_codepress()
78
 
{
79
 
    editbox.addChangeHandler(edit_text);
80
 
     
81
 
    /* We can only safely disable the save button on the first load.
82
 
     * Syntax highlighting changes will also get this function called.
83
 
     * We unfortunately need the change handler added each time.
84
 
     */
85
 
    if (!initialise_codepress.already)
86
 
    {
87
 
        disable_save_if_safe();
88
 
        initialise_codepress.already = true;
89
 
    }
90
 
}
91
 
 
92
64
/** Presents the text editor.
93
65
 */
94
66
function handle_text(path, text, handler_type)
101
73
    present_editorhead(files, path, handler_type);
102
74
 
103
75
    var div = document.createElement("div");
104
 
    div.style.height = '100%';
105
76
    files.appendChild(div);
106
77
    var txt_elem = dom_make_text_elem("textarea",
107
78
        text.toString())
108
79
    div.appendChild(txt_elem);
109
80
    txt_elem.setAttribute("id", "editbox");
110
 
    language = language_from_mime(current_file.type);
111
 
 
112
 
    // Assume plaintext if no type can be determined.
113
 
    language = language ? language : "text";
114
 
    document.getElementById("highlighting_select").value = language;
115
 
 
116
 
    txt_elem.className = "codepress " + language;
117
81
    txt_elem.setAttribute("onchange", "edit_text()");
118
82
    /* TODO: Make CSS height: 100% work */
119
83
    txt_elem.setAttribute("rows", "35");
120
 
    CodePress.run();
121
 
 
122
 
    window.onbeforeunload = confirm_beforeunload;
123
 
 
124
 
    /* And set a callback so we know that the editor iframe is loaded so we
125
 
     * can set a callback so we know when to enable the save button.
126
 
     * We also take this opportunity to disable the save button, if
127
 
     * the browser is likely to reenable it as needed. */
128
 
    editbox.onload = initialise_codepress
129
 
}
130
 
 
131
 
function language_from_mime(mime)
132
 
{
133
 
    return {'text/x-python': 'python',
134
 
            'application/javascript': 'javascript',
135
 
            'text/css': 'css',
136
 
            'text/plain': 'text',
137
 
            'text/html': 'html',
138
 
            'application/xhtml+xml': 'html'}[mime];
139
 
}
 
84
 
 
85
    /* Load EditArea into the editbox */
 
86
    editAreaLoader.init({
 
87
        id : "editbox",
 
88
        syntax: "python",
 
89
        toolbar: "search, go_to_line, |, undo, redo, |, select_font, |, syntax_selection, |, highlight, |, help",
 
90
        start_highlight: true,
 
91
        allow_toggle: false,
 
92
        allow_resize: false,
 
93
        replace_tab_by_spaces: 4,
 
94
        change_callback: "edit_text"
 
95
    });
 
96
}
 
97