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

« back to all changes in this revision

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

  • Committer: mattgiuca
  • Date: 2008-01-11 06:52:24 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:183
browser: Added top level of response handling. Now determines handler type and
calls the appropriate handler. (Handlers just alert at the moment).

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 * Date: 11/1/2008
21
21
 */
22
22
 
 
23
/* Mapping MIME types onto handlers.
 
24
 * "text" : When navigating to a text file, the text editor is opened.
 
25
 * "image" : When navigating to an image, the image is displayed (rather than
 
26
 *              going to the text editor).
 
27
 * "audio" : When navigating to an audio file, a "play" button is presented.
 
28
 * "binary" : When navigating to a binary file, offer it as a download through
 
29
 *              "serve".
 
30
 *
 
31
 * If a file is not on the list, its default action is determined by the first
 
32
 * part of its content type, where "text/*", "image/*" and "audio/*" are
 
33
 * treated as above, and other types are simply treated as binary.
 
34
 */
 
35
type_handlers = {
 
36
    "application/x-javascript" : "text",
 
37
    "application/javascript" : "text",
 
38
    "application/json" : "text",
 
39
    "application/xml" : "text",
 
40
};
 
41
 
 
42
/* List of MIME types considered "executable" by the system.
 
43
 * Executable files offer a "run" link, implying that the "serve"
 
44
 * application can interpret them.
 
45
 */
 
46
types_exec = [
 
47
    "text/x-python",
 
48
];
 
49
 
23
50
/** Calls the server using Ajax, performing an action on the server side.
24
51
 * Receives the response from the server and performs a refresh of the page
25
52
 * contents, updating it to display the returned data (such as a directory
82
109
 */
83
110
function handle_response(path, response)
84
111
{
 
112
    /* TODO: Set location bar to "path" */
 
113
    /* Check the status, and if not 200, read the error and handle this as an
 
114
     * error. */
 
115
    if (response.status != 200)
 
116
    {
 
117
        var error = response.getResponseHeader("X-IVLE-Return-Error");
 
118
        if (error == null)
 
119
            error = response.statusText;
 
120
        handle_error(error);
 
121
        return;
 
122
    }
 
123
 
 
124
    /* Check if this is a directory listing or file contents */
 
125
    if (response.getResponseHeader("X-IVLE-Return") == "Dir")
 
126
    {
 
127
        var listing = response.responseText;
 
128
        /* The listing SHOULD be valid JSON text. Parse it into an object. */
 
129
        try
 
130
        {
 
131
            listing = JSON.parse(listing);
 
132
        }
 
133
        catch (e)
 
134
        {
 
135
            handle_error("The server returned an invalid directory listing");
 
136
            return;
 
137
        }
 
138
        handle_dir_listing(path, listing);
 
139
    }
 
140
    else
 
141
    {
 
142
        /* Treat this as an ordinary file. Get the file type. */
 
143
        var content_type = response.getResponseHeader("Content-Type");
 
144
        var handler_type;
 
145
        if (content_type in type_handlers)
 
146
            handler_type = type_handlers[content_type];
 
147
        else
 
148
        {   /* Based on the first part of the MIME type */
 
149
            handler_type = content_type.split('/')[0];
 
150
            if (handler_type != "text" && handler_type != "image" &&
 
151
                handler_type != "audio")
 
152
                handler_type = "binary";
 
153
        }
 
154
        /* handler_type should now be set to either
 
155
         * "text", "image", "audio" or "binary". */
 
156
        switch (handler_type)
 
157
        {
 
158
        case "text":
 
159
            handle_text(path, response.responseText);
 
160
            break;
 
161
        case "image":
 
162
            /* TODO: Custom image handler */
 
163
            handle_binary(path, response.responseText);
 
164
            break;
 
165
        case "audio":
 
166
            /* TODO: Custom audio handler */
 
167
            handle_binary(path, response.responseText);
 
168
            break;
 
169
        case "binary":
 
170
            handle_binary(path);
 
171
            break;
 
172
        }
 
173
    }
 
174
}
 
175
 
 
176
/*** HANDLERS for different types of responses (such as dir listing, file,
 
177
 * etc). */
 
178
 
 
179
function handle_error(message)
 
180
{
 
181
    /* TODO: Rather than alert, rebuild the page into a page showing an error
 
182
     * message */
 
183
    alert("Error: " + message.toString() + ".");
 
184
}
 
185
 
 
186
/** Presents the directory listing.
 
187
 */
 
188
function handle_dir_listing(path, listing)
 
189
{
 
190
    /* TODO */
 
191
    alert(listing);
 
192
}
 
193
 
 
194
/** Presents the text editor.
 
195
 */
 
196
function handle_text(path, text)
 
197
{
 
198
    /* TODO */
 
199
    alert(text);
 
200
}
 
201
 
 
202
/** Displays a download link to the binary file.
 
203
 */
 
204
function handle_binary(path)
 
205
{
 
206
    /* TODO */
 
207
    alert(path);
85
208
}
86
209
 
87
210
/** Called when the page loads initially.