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