~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";
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
25
edit_app = "edit";
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
26
service_app = "fileservice";
205 by mattgiuca
browser.js: Added appropriate handlers for error, text, and binary data.
27
serve_app = "serve";
28
download_app = "download";
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
29
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
30
/* Mapping MIME types onto handlers.
31
 * "text" : When navigating to a text file, the text editor is opened.
32
 * "image" : When navigating to an image, the image is displayed (rather than
33
 *              going to the text editor).
34
 * "audio" : When navigating to an audio file, a "play" button is presented.
35
 * "binary" : When navigating to a binary file, offer it as a download through
36
 *              "serve".
37
 *
38
 * If a file is not on the list, its default action is determined by the first
39
 * part of its content type, where "text/*", "image/*" and "audio/*" are
40
 * treated as above, and other types are simply treated as binary.
41
 */
42
type_handlers = {
43
    "application/x-javascript" : "text",
44
    "application/javascript" : "text",
45
    "application/json" : "text",
46
    "application/xml" : "text",
47
};
48
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
49
/* Mapping MIME types to icons, just the file's basename */
50
type_icons = {
51
    "text/directory": "dir.png",
52
    "text/x-python": "py.png",
53
};
54
55
default_type_icon = "txt.png";
56
57
/* Relative to IVLE root */
58
type_icons_path = "media/images/mime";
59
type_icons_path_large = "media/images/mime/large";
60
61
/* Mapping SVN status to icons, just the file's basename */
62
svn_icons = {
63
    "unversioned": "unversioned.png",
64
    "normal": "normal.png",
65
    "modified": "modified.png",
66
};
67
68
default_svn_icon = "normal.png";
69
70
svn_icons_path = "media/images/svn";
71
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
72
/* List of MIME types considered "executable" by the system.
73
 * Executable files offer a "run" link, implying that the "serve"
74
 * application can interpret them.
75
 */
76
types_exec = [
77
    "text/x-python",
78
];
79
211 by mattgiuca
fileservice/listing: Slight change to date format.
80
81
/* Global variables */
82
83
current_path = "";
84
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
85
/** Calls the server using Ajax, performing an action on the server side.
86
 * Receives the response from the server and performs a refresh of the page
87
 * contents, updating it to display the returned data (such as a directory
88
 * listing, file preview, or editor pane).
89
 * Always makes a POST request.
90
 * No return value.
91
 *
92
 * \param action String. Name of the action to perform, as defined in the
93
 *     fileservice API.
94
 * \param path URL path to make the request to, within the application.
95
 * \param args Argument object, as described in util.parse_url and friends.
96
 *      This should contain the arguments to the action, but NOT the action
97
 *      itself. (Also a minor side-effect; the "args" object will be mutated
98
 *      to include the action attribute).
99
 * \param content_type String, optional.
100
 *      May be "application/x-www-form-urlencoded" or "multipart/form-data".
101
 *      Defaults to "application/x-www-form-urlencoded".
102
 *      "multipart/form-data" is recommended for large uploads.
103
 */
104
function do_action(action, path, args, content_type)
105
{
106
    args.action = action;
107
    /* 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!)
108
    response = ajax_call(service_app, path, args, "POST", content_type);
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
109
    /* Check for action errors reported by the server, and report them to the
110
     * user */
182 by mattgiuca
browser.js: Now correctly implements do_action and navigate. Able to perform
111
    error = response.getResponseHeader("X-IVLE-Action-Error");
112
    if (error != null)
113
        alert("Error: " + error.toString() + ".");
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
114
    /* Now read the response and set up the page accordingly */
115
    handle_response(path, response);
116
}
117
118
/** Calls the server using Ajax, requesting a directory listing. This should
119
 * not modify the server in any way. Receives the response from the server and
120
 * performs a refresh of the page contents, updating it to display the
121
 * returned data (such as a directory listing, file preview, or editor pane).
122
 * Called "navigate", can also be used for a simple refresh.
123
 * Always makes a GET request.
124
 * No return value.
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
125
 * \param editmode Optional boolean. If true, then the user navigated here
126
 * with an "edit" URL so we should favour using the editor.
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
127
 */
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
128
function navigate(path, editmode)
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
129
{
130
    /* 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!)
131
    response = ajax_call(service_app, path, null, "GET");
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
132
    /* Now read the response and set up the page accordingly */
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
133
    handle_response(path, response, editmode);
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
134
}
135
211 by mattgiuca
fileservice/listing: Slight change to date format.
136
/** Determines the "handler type" from a MIME type.
137
 * The handler type is a string, either "text", "image", "audio" or "binary".
138
 */
139
function get_handler_type(content_type)
140
{
141
    if (!content_type)
142
        return null;
143
    if (content_type in type_handlers)
144
        return type_handlers[content_type];
145
    else
146
    {   /* Based on the first part of the MIME type */
147
        var handler_type = content_type.split('/')[0];
148
        if (handler_type != "text" && handler_type != "image" &&
149
            handler_type != "audio")
150
            handler_type = "binary";
151
        return handler_type;
152
    }
153
}
154
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
155
/** Given an HTTP response object, cleans up and rebuilds the contents of the
156
 * page using the response data. This does not navigate away from the page, it
157
 * merely rebuilds most of the data.
158
 * Note that depending on the type of data returned, this could result in a
159
 * directory listing, an image preview, an editor pane, etc.
160
 * Figures out the type and calls the appropriate function.
161
 * \param path URL path which the request was made for. This can (among other
162
 * things) be used to update the URL in the location bar.
163
 * \param response XMLHttpRequest object returned by the server. Should
164
 * contain all the response data.
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
165
 * \param editmode Optional boolean. If true, then the user navigated here
166
 * with an "edit" URL so we should favour using the editor.
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
167
 */
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
168
function handle_response(path, response, editmode)
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
169
{
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
170
    /* TODO: Set location bar to "path" */
211 by mattgiuca
fileservice/listing: Slight change to date format.
171
    current_path = path;
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
172
    settitle(path);
189 by mattgiuca
browser.js: Top-level handler now presents the path nav panel, not dir
173
174
    /* Clear away the existing page contents */
175
    clearpage();
176
    /* Display the path at the top, for navigation */
177
    presentpath(path);
178
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
179
    /* Check the status, and if not 200, read the error and handle this as an
180
     * error. */
181
    if (response.status != 200)
182
    {
183
        var error = response.getResponseHeader("X-IVLE-Return-Error");
184
        if (error == null)
185
            error = response.statusText;
186
        handle_error(error);
187
        return;
188
    }
189
190
    /* Check if this is a directory listing or file contents */
191
    if (response.getResponseHeader("X-IVLE-Return") == "Dir")
192
    {
193
        var listing = response.responseText;
194
        /* The listing SHOULD be valid JSON text. Parse it into an object. */
195
        try
196
        {
197
            listing = JSON.parse(listing);
198
        }
199
        catch (e)
200
        {
201
            handle_error("The server returned an invalid directory listing");
202
            return;
203
        }
204
        handle_dir_listing(path, listing);
205
    }
206
    else
207
    {
208
        /* Treat this as an ordinary file. Get the file type. */
209
        var content_type = response.getResponseHeader("Content-Type");
211 by mattgiuca
fileservice/listing: Slight change to date format.
210
        var handler_type = get_handler_type(content_type);
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
211
        /* If we're in "edit mode", always treat this file as text */
212
        would_be_handler_type = handler_type;
213
        if (editmode) handler_type = "text";
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
214
        /* handler_type should now be set to either
215
         * "text", "image", "audio" or "binary". */
216
        switch (handler_type)
217
        {
218
        case "text":
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
219
            handle_text(path, response.responseText, would_be_handler_type);
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
220
            break;
221
        case "image":
222
            /* TODO: Custom image handler */
223
            handle_binary(path, response.responseText);
224
            break;
225
        case "audio":
226
            /* TODO: Custom audio handler */
227
            handle_binary(path, response.responseText);
228
            break;
229
        case "binary":
230
            handle_binary(path);
231
            break;
232
        }
233
    }
234
}
235
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
236
/** Deletes all "dynamic" content on the page.
237
 * This returns the page back to the state it is in when the HTML arrives to
238
 * the browser, ready for another handler to populate it.
239
 */
240
function clearpage()
241
{
203 by mattgiuca
browser: Removed all directory-listing specific HTML from the Python-generated
242
    dom_removechildren(document.getElementById("path"));
243
    dom_removechildren(document.getElementById("filesbody"));
244
}
245
246
/** Deletes all "dynamic" content on the page necessary to navigate from
247
 * one directory listing to another (does not clear as much as clearpage
248
 * does).
249
 * This is the equivalent of calling clearpage() then
250
 * setup_for_dir_listing(), assuming the page is already on a dir listing.
251
 */
252
function clearpage_dir()
253
{
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
254
    dom_removechildren(document.getElementById("path"));
255
    dom_removechildren(document.getElementById("files"));
256
    dom_removechildren(document.getElementById("sidepanel"));
257
}
258
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
259
/** Sets the mode to either "file browser" or "text editor" mode.
260
 * This modifies the window icon, and selected tab.
261
 * \param editmode If True, editor mode. Else, file browser mode.
262
 */
263
function setmode(editmode)
264
{
265
    /* Find the DOM elements for the file browser and editor tabs */
266
    var tabs = document.getElementById("apptabs");
267
    var tab_files = null;
268
    var tab_edit = null;
269
    var a;
270
    var href;
271
    for (var i=0; i<tabs.childNodes.length; i++)
272
    {
273
        /* Find the href of the link within */
274
        if (!tabs.childNodes[i].getElementsByTagName) continue;
275
        a = tabs.childNodes[i].getElementsByTagName("a");
276
        if (a.length == 0) continue;
277
        href = a[0].getAttribute("href");
278
        if (href == null) continue;
279
        if (endswith(href, this_app))
280
            tab_files = tabs.childNodes[i];
281
        else if (endswith(href, edit_app))
282
            tab_edit = tabs.childNodes[i];
283
    }
284
285
    if (editmode)
286
    {
287
        tab_files.removeAttribute("class");
288
        tab_edit.setAttribute("class", "thisapp");
289
    }
290
    else
291
    {
292
        tab_edit.removeAttribute("class");
293
        tab_files.setAttribute("class", "thisapp");
294
    }
295
}
296
297
function settitle(path)
298
{
299
    document.title = path_basename(path) + " - IVLE";
300
}
301
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
302
/*** HANDLERS for different types of responses (such as dir listing, file,
303
 * etc). */
304
305
function handle_error(message)
306
{
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
307
    setmode(false);
205 by mattgiuca
browser.js: Added appropriate handlers for error, text, and binary data.
308
    var files = document.getElementById("filesbody");
309
    var txt_elem = dom_make_text_elem("div", "Error: "
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
310
        + message.toString() + ".")
205 by mattgiuca
browser.js: Added appropriate handlers for error, text, and binary data.
311
    txt_elem.setAttribute("class", "padding error");
312
    files.appendChild(txt_elem);
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
313
}
314
315
/** Presents a path list (address bar inside the page) for clicking.
316
 */
317
function presentpath(path)
318
{
319
    var dom_path = document.getElementById("path");
320
    var href_path = make_path(this_app);
200 by mattgiuca
fileservice.listing: Now returns a nicer date format for mtime_nice.
321
    var nav_path = "";
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
322
323
    /* Create all of the paths */
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
324
    for each (var dir in path.split("/"))
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
325
    {
326
        if (dir == "") continue;
327
        /* Make an 'a' element */
328
        href_path = path_join(href_path, dir);
200 by mattgiuca
fileservice.listing: Now returns a nicer date format for mtime_nice.
329
        nav_path = path_join(nav_path, dir);
330
        var link = dom_make_link_elem("a", dir, "Navigate to " + nav_path,
331
                href_path, "navigate(" + href_path + ")");
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
332
        dom_path.appendChild(link);
333
        dom_path.appendChild(document.createTextNode("/"));
334
    }
207 by mattgiuca
browser.js: Removed trailing slash from the path list
335
    dom_path.removeChild(dom_path.lastChild);
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
336
}
337
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
338
/** Given a mime type, returns the path to the icon.
339
 * \param type String, Mime type.
340
 * \param sizelarge Boolean, optional.
341
 * \return Path to the icon. Has applied make_path, so it is relative to site
342
 * root.
343
 */
344
function mime_type_to_icon(type, sizelarge)
345
{
346
    var filename;
347
    if (type in type_icons)
348
        filename = type_icons[type];
349
    else
350
        filename = default_type_icon;
351
    if (sizelarge)
352
        return make_path(path_join(type_icons_path_large, filename));
353
    else
354
        return make_path(path_join(type_icons_path, filename));
355
}
356
357
/** Given an svnstatus, returns the path to the icon.
358
 * \param type String, svn status.
359
 * \return Path to the icon. Has applied make_path, so it is relative to site
360
 * root.
361
 */
362
function svnstatus_to_icon(svnstatus)
363
{
364
    var filename;
365
    if (svnstatus in svn_icons)
366
        filename = svn_icons[svnstatus];
367
    else
368
        filename = default_svn_icon;
369
    return make_path(path_join(svn_icons_path, filename));
370
}
371
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
372
/** Presents the text editor.
373
 */
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
374
function handle_text(path, text, handler_type)
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
375
{
205 by mattgiuca
browser.js: Added appropriate handlers for error, text, and binary data.
376
    /* Create a textarea with the text in it
377
     * (The makings of a primitive editor).
378
     */
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
379
380
    setmode(true);
205 by mattgiuca
browser.js: Added appropriate handlers for error, text, and binary data.
381
    var files = document.getElementById("filesbody");
382
    var div = document.createElement("div");
383
    files.appendChild(div);
384
    div.setAttribute("class", "padding");
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
385
    /* First, print a warning message if this is not actually a text file.
386
     */
387
    if (handler_type != "text")
388
    {
389
        var warn = dom_make_text_elem("p",
390
            "Warning: You are editing a binary " +
391
            "file, which explains any strange characters you may see. If " +
392
            "you save this file, you could corrupt it.");
393
        div.appendChild(warn);
394
    }
205 by mattgiuca
browser.js: Added appropriate handlers for error, text, and binary data.
395
    var txt_elem = dom_make_text_elem("textarea",
396
        text.toString())
397
    div.appendChild(txt_elem);
398
    txt_elem.setAttribute("id", "editbox");
399
    /* TODO: Make CSS height: 100% work */
400
    txt_elem.setAttribute("rows", "20");
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
401
}
402
403
/** Displays a download link to the binary file.
404
 */
405
function handle_binary(path)
406
{
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
407
    setmode(false);
205 by mattgiuca
browser.js: Added appropriate handlers for error, text, and binary data.
408
    var files = document.getElementById("filesbody");
409
    var div = document.createElement("div");
410
    files.appendChild(div);
411
    div.setAttribute("class", "padding");
412
    var download_link = make_path(path_join(download_app, path));
413
    var par1 = dom_make_text_elem("p",
414
        "The file " + path + " is a binary file. To download this file, " +
415
        "click the following link:");
416
    var par2 = dom_make_link_elem("p",
417
        "Download " + path, "Download " + path, download_link);
418
    div.appendChild(par1);
419
    div.appendChild(par2);
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
420
}
421
170 by mattgiuca
browser: Added CSS and JS files (not much in them).
422
/** Called when the page loads initially.
423
 */
424
window.onload = function()
425
{
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
426
    /* Navigate (internally) to the path in the URL bar.
427
     * This causes the page to be populated with whatever is at that address,
428
     * whether it be a directory or a file.
429
     */
430
    var path = parse_url(window.location.href).path;
431
    /* Strip out root_dir + "/files" from the front of the path */
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
432
    var strip = make_path(this_app);
433
    var editmode = false;
434
    if (path.substr(0, strip.length) == strip)
435
        path = path.substr(strip.length+1);
436
    else
437
    {
438
        /* See if this is an edit path */
439
        strip = make_path(edit_app);
440
        if (path.substr(0, strip.length) == strip)
441
        {
442
            path = path.substr(strip.length+1);
443
            editmode = true;
444
        }
445
    }
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
446
200 by mattgiuca
fileservice.listing: Now returns a nicer date format for mtime_nice.
447
    if (path.length == 0)
448
    {
449
        /* Navigate to the user's home directory by default */
450
        /* TEMP? */
451
        path = username;
452
    }
453
208 by mattgiuca
dispatch.html, ivle.css: "apptabs" is now an ID, not a class.
454
    navigate(path, editmode);
170 by mattgiuca
browser: Added CSS and JS files (not much in them).
455
}