~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-05 01:41:15 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:409
Moved www/conf and www/common to a new directory lib. This separates the "web"
part of IVLE from what is becoming less web oriented (at least from Apache's
standpoint).
Modified setup.py to install this lib directory correctly and write conf in
the right place. Also adds the lib directory to ivle.pth.

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