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

170 by mattgiuca
browser: Added CSS and JS files (not much in them).
1
/* IVLE - Informatics Virtual Learning Environment
2
 * Copyright (C) 2007-2008 The University of Melbourne
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 2 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
 *
18
 * Module: File Browser (client)
19
 * Author: Matt Giuca
20
 * Date: 11/1/2008
21
 */
22
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
23
/* Url names for apps */
24
this_app = "files";
25
service_app = "fileservice";
26
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
27
/* Mapping MIME types onto handlers.
28
 * "text" : When navigating to a text file, the text editor is opened.
29
 * "image" : When navigating to an image, the image is displayed (rather than
30
 *              going to the text editor).
31
 * "audio" : When navigating to an audio file, a "play" button is presented.
32
 * "binary" : When navigating to a binary file, offer it as a download through
33
 *              "serve".
34
 *
35
 * If a file is not on the list, its default action is determined by the first
36
 * part of its content type, where "text/*", "image/*" and "audio/*" are
37
 * treated as above, and other types are simply treated as binary.
38
 */
39
type_handlers = {
40
    "application/x-javascript" : "text",
41
    "application/javascript" : "text",
42
    "application/json" : "text",
43
    "application/xml" : "text",
44
};
45
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
46
/* Mapping MIME types to icons, just the file's basename */
47
type_icons = {
48
    "text/directory": "dir.png",
49
    "text/x-python": "py.png",
50
};
51
52
default_type_icon = "txt.png";
53
54
/* Relative to IVLE root */
55
type_icons_path = "media/images/mime";
56
type_icons_path_large = "media/images/mime/large";
57
58
/* Mapping SVN status to icons, just the file's basename */
59
svn_icons = {
60
    "unversioned": "unversioned.png",
61
    "normal": "normal.png",
62
    "modified": "modified.png",
63
};
64
65
default_svn_icon = "normal.png";
66
67
svn_icons_path = "media/images/svn";
68
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
69
/* List of MIME types considered "executable" by the system.
70
 * Executable files offer a "run" link, implying that the "serve"
71
 * application can interpret them.
72
 */
73
types_exec = [
74
    "text/x-python",
75
];
76
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
77
/** Calls the server using Ajax, performing an action on the server side.
78
 * Receives the response from the server and performs a refresh of the page
79
 * contents, updating it to display the returned data (such as a directory
80
 * listing, file preview, or editor pane).
81
 * Always makes a POST request.
82
 * No return value.
83
 *
84
 * \param action String. Name of the action to perform, as defined in the
85
 *     fileservice API.
86
 * \param path URL path to make the request to, within the application.
87
 * \param args Argument object, as described in util.parse_url and friends.
88
 *      This should contain the arguments to the action, but NOT the action
89
 *      itself. (Also a minor side-effect; the "args" object will be mutated
90
 *      to include the action attribute).
91
 * \param content_type String, optional.
92
 *      May be "application/x-www-form-urlencoded" or "multipart/form-data".
93
 *      Defaults to "application/x-www-form-urlencoded".
94
 *      "multipart/form-data" is recommended for large uploads.
95
 */
96
function do_action(action, path, args, content_type)
97
{
98
    args.action = action;
99
    /* Call the server and perform the action. This mutates the server. */
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
100
    response = ajax_call(service_app, path, args, "POST", content_type);
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
101
    /* Check for action errors reported by the server, and report them to the
102
     * user */
182 by mattgiuca
browser.js: Now correctly implements do_action and navigate. Able to perform
103
    error = response.getResponseHeader("X-IVLE-Action-Error");
104
    if (error != null)
105
        alert("Error: " + error.toString() + ".");
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
106
    /* Now read the response and set up the page accordingly */
107
    handle_response(path, response);
108
}
109
110
/** Calls the server using Ajax, requesting a directory listing. This should
111
 * not modify the server in any way. Receives the response from the server and
112
 * performs a refresh of the page contents, updating it to display the
113
 * returned data (such as a directory listing, file preview, or editor pane).
114
 * Called "navigate", can also be used for a simple refresh.
115
 * Always makes a GET request.
116
 * No return value.
117
 */
118
function navigate(path)
119
{
120
    /* Call the server and request the listing. This mutates the server. */
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
121
    response = ajax_call(service_app, path, null, "GET");
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
122
    /* Now read the response and set up the page accordingly */
123
    handle_response(path, response);
124
}
125
126
/** Given an HTTP response object, cleans up and rebuilds the contents of the
127
 * page using the response data. This does not navigate away from the page, it
128
 * merely rebuilds most of the data.
129
 * Note that depending on the type of data returned, this could result in a
130
 * directory listing, an image preview, an editor pane, etc.
131
 * Figures out the type and calls the appropriate function.
132
 * \param path URL path which the request was made for. This can (among other
133
 * things) be used to update the URL in the location bar.
134
 * \param response XMLHttpRequest object returned by the server. Should
135
 * contain all the response data.
136
 */
137
function handle_response(path, response)
138
{
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
139
    /* TODO: Set location bar to "path" */
189 by mattgiuca
browser.js: Top-level handler now presents the path nav panel, not dir
140
141
    /* Clear away the existing page contents */
142
    clearpage();
143
    /* Display the path at the top, for navigation */
144
    presentpath(path);
145
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
146
    /* Check the status, and if not 200, read the error and handle this as an
147
     * error. */
148
    if (response.status != 200)
149
    {
150
        var error = response.getResponseHeader("X-IVLE-Return-Error");
151
        if (error == null)
152
            error = response.statusText;
153
        handle_error(error);
154
        return;
155
    }
156
157
    /* Check if this is a directory listing or file contents */
158
    if (response.getResponseHeader("X-IVLE-Return") == "Dir")
159
    {
160
        var listing = response.responseText;
161
        /* The listing SHOULD be valid JSON text. Parse it into an object. */
162
        try
163
        {
164
            listing = JSON.parse(listing);
165
        }
166
        catch (e)
167
        {
168
            handle_error("The server returned an invalid directory listing");
169
            return;
170
        }
171
        handle_dir_listing(path, listing);
172
    }
173
    else
174
    {
175
        /* Treat this as an ordinary file. Get the file type. */
176
        var content_type = response.getResponseHeader("Content-Type");
177
        var handler_type;
178
        if (content_type in type_handlers)
179
            handler_type = type_handlers[content_type];
180
        else
181
        {   /* Based on the first part of the MIME type */
182
            handler_type = content_type.split('/')[0];
183
            if (handler_type != "text" && handler_type != "image" &&
184
                handler_type != "audio")
185
                handler_type = "binary";
186
        }
187
        /* handler_type should now be set to either
188
         * "text", "image", "audio" or "binary". */
189
        switch (handler_type)
190
        {
191
        case "text":
192
            handle_text(path, response.responseText);
193
            break;
194
        case "image":
195
            /* TODO: Custom image handler */
196
            handle_binary(path, response.responseText);
197
            break;
198
        case "audio":
199
            /* TODO: Custom audio handler */
200
            handle_binary(path, response.responseText);
201
            break;
202
        case "binary":
203
            handle_binary(path);
204
            break;
205
        }
206
    }
207
}
208
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
209
/** Deletes all "dynamic" content on the page.
210
 * This returns the page back to the state it is in when the HTML arrives to
211
 * the browser, ready for another handler to populate it.
212
 */
213
function clearpage()
214
{
215
    /* Note: For now clear just enough to repopulate with a dir listing.
216
     * Later, will have to clear more to make way for other handlers.
217
     * Possibly have a "full clear" for all handlers, and special
218
     * less-violent clearers for each handler if the same handler is going to
219
     * be used that was used last time. */
220
    dom_removechildren(document.getElementById("path"));
221
    dom_removechildren(document.getElementById("files"));
222
    dom_removechildren(document.getElementById("sidepanel"));
223
}
224
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
225
/*** HANDLERS for different types of responses (such as dir listing, file,
226
 * etc). */
227
228
function handle_error(message)
229
{
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
230
    /* TODO: Find a better place to put this message. */
189 by mattgiuca
browser.js: Top-level handler now presents the path nav panel, not dir
231
    var files = document.getElementById("files");
232
    var tr = document.createElement("tr");
233
    var td = document.createElement("td");
234
    tr.appendChild(td);
235
    var td = document.createElement("td");
236
    tr.appendChild(td);
237
    var td = document.createElement("td");
238
    tr.appendChild(td);
239
    var txt_elem = dom_make_text_elem("td", "Error: "
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
240
        + message.toString() + ".")
241
    txt_elem.setAttribute("class", "error");
189 by mattgiuca
browser.js: Top-level handler now presents the path nav panel, not dir
242
    tr.appendChild(txt_elem);
243
    var td = document.createElement("td");
244
    tr.appendChild(td);
245
    var td = document.createElement("td");
246
    tr.appendChild(td);
247
    files.appendChild(tr);
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
248
}
249
250
/** Presents a path list (address bar inside the page) for clicking.
251
 */
252
function presentpath(path)
253
{
254
    var dom_path = document.getElementById("path");
255
    var navlist = [];
256
    var href_path = make_path(this_app);
257
258
    /* Create all of the paths */
259
    for each (dir in path.split("/"))
260
    {
261
        if (dir == "") continue;
262
        navlist.push(dir);
263
        /* Make an 'a' element */
264
        href_path = path_join(href_path, dir);
265
        var link = dom_make_link_elem("a", dir, href_path,
266
                "navigate(" + href_path + ")");
267
        dom_path.appendChild(link);
268
        dom_path.appendChild(document.createTextNode("/"));
269
    }
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
270
}
271
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
272
/** Given a mime type, returns the path to the icon.
273
 * \param type String, Mime type.
274
 * \param sizelarge Boolean, optional.
275
 * \return Path to the icon. Has applied make_path, so it is relative to site
276
 * root.
277
 */
278
function mime_type_to_icon(type, sizelarge)
279
{
280
    var filename;
281
    if (type in type_icons)
282
        filename = type_icons[type];
283
    else
284
        filename = default_type_icon;
285
    if (sizelarge)
286
        return make_path(path_join(type_icons_path_large, filename));
287
    else
288
        return make_path(path_join(type_icons_path, filename));
289
}
290
291
/** Given an svnstatus, returns the path to the icon.
292
 * \param type String, svn status.
293
 * \return Path to the icon. Has applied make_path, so it is relative to site
294
 * root.
295
 */
296
function svnstatus_to_icon(svnstatus)
297
{
298
    var filename;
299
    if (svnstatus in svn_icons)
300
        filename = svn_icons[svnstatus];
301
    else
302
        filename = default_svn_icon;
303
    return make_path(path_join(svn_icons_path, filename));
304
}
305
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
306
/** Presents the directory listing.
307
 */
308
function handle_dir_listing(path, listing)
309
{
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
310
    var row_toggle = 1;
311
    /* Nav through the top-level of the JSON to the actual listing object. */
312
    var listing = listing.listing;
313
314
    /* Get "." out, it's special */
315
    var thisdir = listing["."];
316
    delete listing["."];
317
    /* Is this dir under svn? */
318
    var under_subversion = "svnstatus" in thisdir;
319
320
    var files = document.getElementById("files");
321
    var file;
322
    var row;
323
    var td;
324
    var checkbox;
325
326
    /* Create all of the files */
327
    for (var filename in listing)
328
    {
329
        file = listing[filename];
330
        /* Make a 'tr' element */
331
        row = document.createElement("tr");
332
        /* Column 1: Selection checkbox */
333
        row.setAttribute("class", "row" + row_toggle.toString())
334
        row_toggle = row_toggle == 1 ? 2 : 1;
335
        td = document.createElement("td");
336
        checkbox = document.createElement("input");
337
        checkbox.setAttribute("type", "checkbox");
338
        checkbox.setAttribute("title", "Select this file");
339
        td.appendChild(checkbox);
340
        row.appendChild(td);
341
        if (file.isdir)
342
        {
343
            /* Column 2: Filetype and subversion icons. */
344
            td = document.createElement("td");
345
            td.setAttribute("class", "thincol");
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
346
            td.appendChild(dom_make_img(mime_type_to_icon("text/directory"),
347
                22, 22, file.type));
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
348
            row.appendChild(td);
349
            td = document.createElement("td");
350
            td.setAttribute("class", "thincol");
351
            if (under_subversion)
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
352
                td.appendChild(dom_make_img(svnstatus_to_icon(file.svnstatus),
353
                    22, 22, file.svnstatus));
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
354
            row.appendChild(td);
355
            /* Column 3: Filename */
356
            row.appendChild(dom_make_link_elem("td", filename,
357
                make_path(path_join(this_app, path, filename)),
358
                "navigate(" + path_join(path, filename) + ")"));
359
        }
360
        else
361
        {
362
            /* Column 2: Filetype and subversion icons. */
363
            td = document.createElement("td");
364
            td.setAttribute("class", "thincol");
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
365
            td.appendChild(dom_make_img(mime_type_to_icon(file.type),
366
                22, 22, file.type));
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
367
            row.appendChild(td);
368
            td = document.createElement("td");
369
            td.setAttribute("class", "thincol");
370
            if (under_subversion)
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
371
                td.appendChild(dom_make_img(svnstatus_to_icon(file.svnstatus),
372
                    22, 22, file.svnstatus));
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
373
            row.appendChild(td);
374
            /* Column 3: Filename */
375
            row.appendChild(dom_make_text_elem("td", filename));
376
        }
377
        /* Column 4: Size */
378
        row.appendChild(dom_make_text_elem("td", nice_filesize(file.size)));
379
        /* Column 4: Date */
380
        row.appendChild(dom_make_text_elem("td", file.mtime_nice));
381
        files.appendChild(row);
382
    }
383
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
384
}
385
386
/** Presents the text editor.
387
 */
388
function handle_text(path, text)
389
{
390
    /* TODO */
391
    alert(text);
392
}
393
394
/** Displays a download link to the binary file.
395
 */
396
function handle_binary(path)
397
{
398
    /* TODO */
399
    alert(path);
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
400
}
401
170 by mattgiuca
browser: Added CSS and JS files (not much in them).
402
/** Called when the page loads initially.
403
 */
404
window.onload = function()
405
{
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
406
    /* Navigate (internally) to the path in the URL bar.
407
     * This causes the page to be populated with whatever is at that address,
408
     * whether it be a directory or a file.
409
     */
410
    var path = parse_url(window.location.href).path;
411
    /* Strip out root_dir + "/files" from the front of the path */
412
    strip_chars = make_path(this_app).length + 1;
413
    path = path.substr(strip_chars);
414
415
    navigate(path);
170 by mattgiuca
browser: Added CSS and JS files (not much in them).
416
}