~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-06-23 10:34:43 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:787
fileservice_lib/action.py:
    * Fixed TODOs, and comments.
    * putfile: Added "overwrite" optional parameter. If true, overwrites,
      otherwise error. Note: This changes the default behaviour (used to
      overwrite by default).
editor.js: Call to putfile has overwrite: true, as necessary to keep the old
    behaviour.
listing.js: Comment on "new file".

Note: This commit changes the behaviour of "new file" so it has an error if
the file already exists.

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
1
function save_file()
13
2
{
 
3
    var savebutton = document.getElementById("save_button");
14
4
    var filename = document.getElementById("save_filename").value;
15
 
    data = editbox.getCode();
 
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
 
              {"path":".", "data":data, "overwrite":"true"},
20
 
              "multipart/form-data", true);
21
 
    disable_save_if_safe();
 
9
              {"path":".", "data":data, "overwrite":"true"}, null, true);
 
10
    savebutton.setAttribute("value", "Saved");
 
11
    // XXX Do not disable for now; there is a problem getting the callback
 
12
    // to edit_text.
 
13
    //savebutton.setAttribute("disabled", "disabled");
22
14
}
23
15
 
24
16
function edit_text()
25
17
{
26
18
    var savebutton = document.getElementById("save_button");
27
 
    savebutton.disabled = false;
 
19
    savebutton.setAttribute("value", "Save");
 
20
    savebutton.removeAttribute("disabled");
28
21
}
29
22
 
30
 
/** Presents the "editor heading" inserting it into a given element at
31
 
 *  the front. Note that the save widget is handled by the Python.
 
23
/** Presents the "editor heading" (the part with the save box)
 
24
 * inserting it into a given element at the front.
32
25
 */
33
26
function present_editorhead(elem, path, handler_type)
34
27
{
35
28
    var div = document.getElementById("actions2");
36
29
 
 
30
    /* Set up minimal interface */
 
31
    var p = dom_make_text_elem("p", "Save as: ");
 
32
    var pathname = document.createElement("input");
 
33
    pathname.setAttribute("type", "text");
 
34
    pathname.setAttribute("size", "30");
 
35
    pathname.setAttribute("id", "save_filename");
 
36
    pathname.setAttribute("value", path);
 
37
    p.appendChild(pathname);
 
38
    var savebutton = document.createElement("input");
 
39
    savebutton.setAttribute("id", "save_button");
 
40
    savebutton.setAttribute("type", "button");
 
41
    savebutton.setAttribute("value", "Saved");
 
42
    savebutton.setAttribute("disabled", "disabled");
 
43
    savebutton.setAttribute("onclick", "save_file()");
 
44
    p.appendChild(savebutton);
 
45
    var t = document.createTextNode(" ");
 
46
    p.appendChild(t);
 
47
    div.appendChild(p);
 
48
 
37
49
    /* Print a warning message if this is not actually a text file.
38
50
     */
39
51
    if (handler_type != "text")
58
70
    present_editorhead(files, path, handler_type);
59
71
 
60
72
    var div = document.createElement("div");
61
 
    div.style.height = '100%';
62
73
    files.appendChild(div);
63
74
    var txt_elem = dom_make_text_elem("textarea",
64
75
        text.toString())
65
76
    div.appendChild(txt_elem);
66
77
    txt_elem.setAttribute("id", "editbox");
67
 
    language = language_from_mime(current_file.type)
68
 
 
69
 
    // Assume plaintext if no type can be determined.
70
 
    txt_elem.className = "codepress " + (language ? language : 'text');
71
78
    txt_elem.setAttribute("onchange", "edit_text()");
72
79
    /* TODO: Make CSS height: 100% work */
73
80
    txt_elem.setAttribute("rows", "35");
74
 
    CodePress.run();
75
 
 
76
 
    /* And set a callback so we know that the editor iframe is loaded so we
77
 
     * can set a callback so we know when to enable the save button.
78
 
     * We also take this opportunity to disable the save button, if
79
 
     * the browser is likely to reenable it as needed. */
80
 
    editbox.onload = function() {editbox.addChangeHandler(edit_text);
81
 
                                 disable_save_if_safe(); };
82
 
}
83
 
 
84
 
function language_from_mime(mime)
85
 
{
86
 
    return {'text/x-python': 'python',
87
 
            'application/javascript': 'javascript',
88
 
            'text/css': 'css',
89
 
            'text/plain': 'text',
90
 
            'text/html': 'html',
91
 
            'application/xhtml+xml': 'html'}[mime];
92
 
}
 
81
 
 
82
    /* Load EditArea into the editbox */
 
83
    editAreaLoader.init({
 
84
        id : "editbox",
 
85
        syntax: "python",
 
86
        toolbar: "search, go_to_line, |, undo, redo, |, select_font, |, syntax_selection, |, highlight, |, help",
 
87
        start_highlight: true,
 
88
        allow_toggle: false,
 
89
        allow_resize: false,
 
90
        replace_tab_by_spaces: 4,
 
91
        change_callback: "edit_text"
 
92
    });
 
93
}
 
94