~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 */
1099.1.68 by William Grant
Move the remaining images to the new framework, in the new ivle.webapp.core
58
type_icons_path = "+media/ivle.webapp.core/images/mime";
59
type_icons_path_large = "+media/ivle.webapp.core/images/mime/large";
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
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",
578 by dcoles
fileservice: Added code to allow browing of past revisons in the File Browser
68
    "modified": "modified.png",
918 by wagrant
browser: Expose svn update functionality. This brings a need for an
69
    "conflicted": "conflicted.png",
578 by dcoles
fileservice: Added code to allow browing of past revisons in the File Browser
70
    "revision": "revision.png"
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
71
};
72
213 by mattgiuca
Fileservice / Files (Python and JS files):
73
/* Mapping SVN status to "nice" strings */
74
svn_nice = {
75
    "unversioned": "Temporary file",
76
    "normal": "Permanent file",
77
    "added": "Temporary file (scheduled to be added)",
78
    "missing": "Permanent file (missing)",
79
    "deleted": "Permanent file (scheduled for deletion)",
80
    "replaced": "Permanent file (replaced)",
81
    "modified": "Permanent file (modified)",
82
    "merged": "Permanent file (merged)",
578 by dcoles
fileservice: Added code to allow browing of past revisons in the File Browser
83
    "conflicted": "Permanent file (conflicted)",
84
    "revision": "Past Permanent file (revision)"
213 by mattgiuca
Fileservice / Files (Python and JS files):
85
};
86
230 by mattgiuca
Removed "unversioned" icon from subversion status. Now unversioned files do
87
default_svn_icon = null;
213 by mattgiuca
Fileservice / Files (Python and JS files):
88
default_svn_nice = "Unknown status";
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
89
1099.1.68 by William Grant
Move the remaining images to the new framework, in the new ivle.webapp.core
90
svn_icons_path = "+media/ivle.webapp.core/images/svn";
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
91
1099.1.68 by William Grant
Move the remaining images to the new framework, in the new ivle.webapp.core
92
published_icon = "+media/ivle.webapp.core/images/interface/published.png";
269 by mattgiuca
browser: Added publish functionality to JavaScript client side.
93
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
94
/* List of MIME types considered "executable" by the system.
95
 * Executable files offer a "run" link, implying that the "serve"
96
 * application can interpret them.
97
 */
98
types_exec = [
489 by agdimech
browser.js, editor.js: Fixed the syntax error which was resulting due to a trailing comma.
99
    "text/x-python"
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
100
];
101
211 by mattgiuca
fileservice/listing: Slight change to date format.
102
103
/* Global variables */
104
602 by mattgiuca
util.js: Added shallow_copy_object function.
105
/** The listing object returned by the server as JSON */
106
file_listing = null;
107
current_file = null;
910 by wagrant
filebrowser_lib: Send the current revision along with the listing if we
108
current_revision = null;
211 by mattgiuca
fileservice/listing: Slight change to date format.
109
current_path = "";
110
597 by mattgiuca
Major refactor of actions in File browser.
111
/** Filenames of all files selected
112
 * (Only used by dir listings, but still needs to be [] for files, so that
113
 * update_actions knows that nothing is selected).
114
 */
115
selected_files = [];
116
622 by mattgiuca
browser.js: Reinstated upload_callback (previously removed from listing.js).
117
upload_callback_count = 0;      /* See upload_callback */
118
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
119
/** Calls the server using Ajax, performing an action on the server side.
120
 * Receives the response from the server and performs a refresh of the page
121
 * contents, updating it to display the returned data (such as a directory
122
 * listing, file preview, or editor pane).
123
 * Always makes a POST request.
124
 * No return value.
125
 *
126
 * \param action String. Name of the action to perform, as defined in the
127
 *     fileservice API.
128
 * \param path URL path to make the request to, within the application.
129
 * \param args Argument object, as described in util.parse_url and friends.
130
 *      This should contain the arguments to the action, but NOT the action
131
 *      itself. (Also a minor side-effect; the "args" object will be mutated
132
 *      to include the action attribute).
133
 * \param content_type String, optional.
134
 *      May be "application/x-www-form-urlencoded" or "multipart/form-data".
135
 *      Defaults to "application/x-www-form-urlencoded".
136
 *      "multipart/form-data" is recommended for large uploads.
137
 */
385 by mattgiuca
browser/editor: Clicking "Save" in the editor does not cause a page refresh
138
function do_action(action, path, args, content_type, ignore_response)
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
139
{
140
    args.action = action;
559 by mattgiuca
Major JavaScript refactor: util.ajax_call is now asynchronous, not
141
    /* Callback action, when the server returns */
142
    var callback = function(response)
143
        {
144
            /* Check for action errors reported by the server, and report them
145
             * to the user */
146
            var error = response.getResponseHeader("X-IVLE-Action-Error");
147
            if (error != null)
785 by mattgiuca
fileservice_lib/__init__.py: X-IVLE-Action-Error HTTP response header is now
148
                /* Note: This header (in particular) comes URI-encoded, to
149
                 * allow multi-line error messages. Decode */
150
                alert("Error: " + decodeURIComponent(error.toString()) + ".");
559 by mattgiuca
Major JavaScript refactor: util.ajax_call is now asynchronous, not
151
            /* Now read the response and set up the page accordingly */
152
            if (ignore_response != true)
789 by mattgiuca
browser.js: Replaced the old "The server returned an invalid directory
153
                handle_response(path, response, true);
559 by mattgiuca
Major JavaScript refactor: util.ajax_call is now asynchronous, not
154
        }
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
155
    /* Call the server and perform the action. This mutates the server. */
559 by mattgiuca
Major JavaScript refactor: util.ajax_call is now asynchronous, not
156
    ajax_call(callback, service_app, path, args, "POST", content_type);
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
157
}
158
159
/** Calls the server using Ajax, requesting a directory listing. This should
160
 * not modify the server in any way. Receives the response from the server and
161
 * performs a refresh of the page contents, updating it to display the
162
 * returned data (such as a directory listing, file preview, or editor pane).
163
 * Called "navigate", can also be used for a simple refresh.
164
 * Always makes a GET request.
165
 * No return value.
166
 */
603 by mattgiuca
Console now starts up in the user's home directory.
167
function navigate(path)
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
168
{
559 by mattgiuca
Major JavaScript refactor: util.ajax_call is now asynchronous, not
169
    callback = function(response)
170
        {
171
            /* Read the response and set up the page accordingly */
789 by mattgiuca
browser.js: Replaced the old "The server returned an invalid directory
172
            handle_response(path, response, false, url.args);
559 by mattgiuca
Major JavaScript refactor: util.ajax_call is now asynchronous, not
173
        }
578 by dcoles
fileservice: Added code to allow browing of past revisons in the File Browser
174
    /* Get any query strings */
175
    url = parse_url(window.location.href);
176
    
602 by mattgiuca
util.js: Added shallow_copy_object function.
177
    /* Call the server and request the listing. */
578 by dcoles
fileservice: Added code to allow browing of past revisons in the File Browser
178
    ajax_call(callback, service_app, path, url.args, "GET");
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
179
}
180
597 by mattgiuca
Major refactor of actions in File browser.
181
/* Refreshes the current view.
182
 * Calls navigate on the current path.
183
 */
184
function refresh()
185
{
865 by wagrant
browser: Warn users before running, serving and refreshing if the file
186
    if (maybe_save('All changes since the last save will be lost!'))
187
        navigate(current_path);
597 by mattgiuca
Major refactor of actions in File browser.
188
}
189
211 by mattgiuca
fileservice/listing: Slight change to date format.
190
/** Determines the "handler type" from a MIME type.
191
 * The handler type is a string, either "text", "image", "audio" or "binary".
192
 */
193
function get_handler_type(content_type)
194
{
195
    if (!content_type)
196
        return null;
197
    if (content_type in type_handlers)
198
        return type_handlers[content_type];
199
    else
200
    {   /* Based on the first part of the MIME type */
201
        var handler_type = content_type.split('/')[0];
202
        if (handler_type != "text" && handler_type != "image" &&
203
            handler_type != "audio")
204
            handler_type = "binary";
205
        return handler_type;
206
    }
207
}
208
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
209
/** Given an HTTP response object, cleans up and rebuilds the contents of the
210
 * page using the response data. This does not navigate away from the page, it
211
 * merely rebuilds most of the data.
212
 * Note that depending on the type of data returned, this could result in a
213
 * directory listing, an image preview, an editor pane, etc.
214
 * Figures out the type and calls the appropriate function.
215
 * \param path URL path which the request was made for. This can (among other
216
 * things) be used to update the URL in the location bar.
217
 * \param response XMLHttpRequest object returned by the server. Should
218
 * contain all the response data.
789 by mattgiuca
browser.js: Replaced the old "The server returned an invalid directory
219
 * \param is_action Boolean. True if this is the response to an action, false
220
 * if this is the response to a simple listing. This is used in handling the
221
 * error.
602 by mattgiuca
util.js: Added shallow_copy_object function.
222
 * \param url_args Arguments dict, for the arguments passed to the URL
223
 * in the browser's address bar (will be forwarded along).
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
224
 */
789 by mattgiuca
browser.js: Replaced the old "The server returned an invalid directory
225
function handle_response(path, response, is_action, url_args)
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
226
{
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
227
    /* TODO: Set location bar to "path" */
211 by mattgiuca
fileservice/listing: Slight change to date format.
228
    current_path = path;
189 by mattgiuca
browser.js: Top-level handler now presents the path nav panel, not dir
229
230
    /* Clear away the existing page contents */
231
    clearpage();
232
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
233
    /* Check the status, and if not 200, read the error and handle this as an
234
     * error. */
235
    if (response.status != 200)
236
    {
237
        var error = response.getResponseHeader("X-IVLE-Return-Error");
238
        if (error == null)
239
            error = response.statusText;
240
        handle_error(error);
241
        return;
242
    }
243
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
244
    var subjects = null;
245
    var top_level_dir = path==username;
246
    if (top_level_dir)
247
    {
248
        var req = ajax_call(null, "userservice", "get_enrolments", null, "GET")
249
        subjects = decode_response(req);
250
    }
251
252
602 by mattgiuca
util.js: Added shallow_copy_object function.
253
    /* This will always return a listing, whether it is a dir or a file.
254
     */
255
    var listing = response.responseText;
256
    /* The listing SHOULD be valid JSON text. Parse it into an object. */
257
    try
258
    {
259
        listing = JSON.parse(listing);
260
        file_listing = listing.listing;     /* Global */
261
    }
262
    catch (e)
263
    {
789 by mattgiuca
browser.js: Replaced the old "The server returned an invalid directory
264
        if (is_action)
265
        {
266
            var err = document.createElement("div");
267
            var p = dom_make_text_elem("p", "Error: "
268
                    + "There was an unexpected server error processing "
269
                    + "the selected command.");
270
            err.appendChild(p);
271
            p = dom_make_text_elem("p", "If the problem persists, please "
272
                    + "contact the system administrator.")
273
            err.appendChild(p);
274
            p = document.createElement("p");
275
            var refresh = document.createElement("input");
276
            refresh.setAttribute("type", "button");
277
            refresh.setAttribute("value", "Back to file view");
278
            refresh.setAttribute("onclick", "refresh()");
279
            p.appendChild(refresh);
280
            err.appendChild(p);
281
            handle_error(err);
282
        }
283
        else
284
        {
285
            var err = document.createElement("div");
286
            var p = dom_make_text_elem("p", "Error: "
287
                    + "There was an unexpected server error retrieving "
288
                    + "the requested file or directory.");
289
            err.appendChild(p);
290
            p = dom_make_text_elem("p", "If the problem persists, please "
291
                    + "contact the system administrator.")
292
            err.appendChild(p);
293
            handle_error(err);
294
        }
602 by mattgiuca
util.js: Added shallow_copy_object function.
295
        return;
296
    }
297
    /* Get "." out, it's special */
298
    current_file = file_listing["."];     /* Global */
299
    delete file_listing["."];
300
910 by wagrant
filebrowser_lib: Send the current revision along with the listing if we
301
    if ('revision' in listing)
302
    {
303
        current_revision = listing.revision;
304
    }
305
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
306
    /* Check if this is a directory listing or file contents */
222 by mattgiuca
browser.js:
307
    var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
602 by mattgiuca
util.js: Added shallow_copy_object function.
308
    if (isdir)
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
309
    {
980 by dcoles
Files: "Junk" is now only shown when other (non-subject/stuff) files are in the
310
        setup_for_listing();
311
        home_listing(listing, subjects, path);
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
312
    }
313
    else
314
    {
602 by mattgiuca
util.js: Added shallow_copy_object function.
315
        /* Need to make a 2nd ajax call, this time get the actual file
316
         * contents */
317
        callback = function(response)
318
            {
319
                /* Read the response and set up the page accordingly */
320
                handle_contents_response(path, response);
321
            }
322
        /* Call the server and request the listing. */
323
        if (url_args)
324
            args = shallow_clone_object(url_args);
325
        else
326
            args = {};
327
        /* This time, get the contents of the file, not its metadata */
328
        args['return'] = "contents";
329
        ajax_call(callback, service_app, path, args, "GET");
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
330
    }
597 by mattgiuca
Major refactor of actions in File browser.
331
    update_actions(isdir);
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
332
}
333
602 by mattgiuca
util.js: Added shallow_copy_object function.
334
function handle_contents_response(path, response)
335
{
336
    /* Treat this as an ordinary file. Get the file type. */
337
    var content_type = response.getResponseHeader("Content-Type");
338
    var handler_type = get_handler_type(content_type);
339
    would_be_handler_type = handler_type;
340
    /* handler_type should now be set to either
341
     * "text", "image", "audio" or "binary". */
342
    switch (handler_type)
343
    {
344
    case "text":
345
        handle_text(path, response.responseText,
346
            would_be_handler_type);
347
        break;
348
    case "image":
349
        /* TODO: Custom image handler */
350
        handle_binary(path, response.responseText);
351
        break;
352
    case "audio":
353
        /* TODO: Custom audio handler */
354
        handle_binary(path, response.responseText);
355
        break;
356
    case "binary":
357
        handle_binary(path);
358
        break;
359
    }
360
}
361
622 by mattgiuca
browser.js: Reinstated upload_callback (previously removed from listing.js).
362
/* Called when a form upload comes back (from an iframe).
363
 * Refreshes the page.
364
 */
365
function upload_callback()
366
{
367
    /* This has a pretty nasty hack, which happens to work.
368
     * upload_callback is set as the "onload" callback for the iframe which
369
     * receives the response from the server for uploading a file.
370
     * This means it gets called twice. Once when initialising the iframe, and
371
     * a second time when the actual response comes back.
372
     * All we want to do is call navigate to refresh the page. But we CAN'T do
373
     * that on the first load or it will just go into an infinite cycle of
374
     * refreshing. We need to refresh the page ONLY on the second refresh.
375
     * upload_callback_count is reset to 0 just before the iframe is created.
376
     */
377
    upload_callback_count++;
378
    if (upload_callback_count >= 2)
838 by wagrant
browser.js: Clear the file upload path when an upload finishes.
379
    {
1089 by chadnickbok
Fixes Issue #14
380
        myFrame = frames['upload_iframe'].document;
381
        data = myFrame.firstChild.childNodes[1].firstChild.firstChild.nodeValue;
382
        data = JSON.parse(data);
383
        if ('Error' in data)
384
            alert("Error: " + decodeURIComponent(data['Error']));
838 by wagrant
browser.js: Clear the file upload path when an upload finishes.
385
        document.getElementsByName('data')[0].value = '';
622 by mattgiuca
browser.js: Reinstated upload_callback (previously removed from listing.js).
386
        refresh();
838 by wagrant
browser.js: Clear the file upload path when an upload finishes.
387
    }
622 by mattgiuca
browser.js: Reinstated upload_callback (previously removed from listing.js).
388
}
389
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
390
/** Deletes all "dynamic" content on the page.
391
 * This returns the page back to the state it is in when the HTML arrives to
392
 * the browser, ready for another handler to populate it.
393
 */
394
function clearpage()
395
{
203 by mattgiuca
browser: Removed all directory-listing specific HTML from the Python-generated
396
    dom_removechildren(document.getElementById("filesbody"));
397
}
398
865 by wagrant
browser: Warn users before running, serving and refreshing if the file
399
/* Checks if a file needs to be saved. If it does, the user will be asked
400
 * if they want to continue anyway. The caller must specify a warning
401
 * sentence which indicates the consequences of continuing.
402
 * Returns true if we should continue, and false if we should not.
403
 */
404
function maybe_save(warning)
405
{
406
    if (warning == null) warning = '';
407
    if (current_file.isdir) return true;
408
    if (document.getElementById("save_button").disabled) return true;
409
    return confirm("This file has unsaved changes. " + warning +
410
                   "\nAre you sure you wish to continue?");
411
}
412
203 by mattgiuca
browser: Removed all directory-listing specific HTML from the Python-generated
413
/** Deletes all "dynamic" content on the page necessary to navigate from
414
 * one directory listing to another (does not clear as much as clearpage
415
 * does).
416
 * This is the equivalent of calling clearpage() then
417
 * setup_for_dir_listing(), assuming the page is already on a dir listing.
418
 */
419
function clearpage_dir()
420
{
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
421
    dom_removechildren(document.getElementById("path"));
422
    dom_removechildren(document.getElementById("files"));
423
    dom_removechildren(document.getElementById("sidepanel"));
424
}
425
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
426
/*** HANDLERS for different types of responses (such as dir listing, file,
427
 * etc). */
428
789 by mattgiuca
browser.js: Replaced the old "The server returned an invalid directory
429
/* handle_error.
430
 * message may either be a string, or a DOM node, which will be placed inside
431
 * a div.
432
 */
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
433
function handle_error(message)
434
{
205 by mattgiuca
browser.js: Added appropriate handlers for error, text, and binary data.
435
    var files = document.getElementById("filesbody");
789 by mattgiuca
browser.js: Replaced the old "The server returned an invalid directory
436
    var txt_elem;
437
    if (typeof(message) == "string")
438
    {
439
        txt_elem = dom_make_text_elem("div", "Error: "
440
                   + message.toString() + ".")
441
    }
442
    else
443
    {
444
        /* Assume message is a DOM node */
445
        txt_elem = document.createElement("div");
446
        txt_elem.appendChild(message);
447
    }
205 by mattgiuca
browser.js: Added appropriate handlers for error, text, and binary data.
448
    txt_elem.setAttribute("class", "padding error");
449
    files.appendChild(txt_elem);
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
450
}
451
910 by wagrant
filebrowser_lib: Send the current revision along with the listing if we
452
/** Given a path, filename and optional revision, returns a URL to open that
453
 *  revision of that file.
454
 */
455
function build_revision_url(path, filename, revision)
456
{
457
    bits = {'path': app_path(this_app, path, filename)};
458
    if (current_revision)
459
    {
460
        bits['query_string'] = 'r=' + revision;
461
    }
462
    return build_url(bits);
463
}
464
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
465
/** Given a mime type, returns the path to the icon.
466
 * \param type String, Mime type.
467
 * \param sizelarge Boolean, optional.
468
 * \return Path to the icon. Has applied make_path, so it is relative to site
469
 * root.
470
 */
471
function mime_type_to_icon(type, sizelarge)
472
{
473
    var filename;
474
    if (type in type_icons)
475
        filename = type_icons[type];
476
    else
477
        filename = default_type_icon;
478
    if (sizelarge)
479
        return make_path(path_join(type_icons_path_large, filename));
480
    else
481
        return make_path(path_join(type_icons_path, filename));
482
}
483
484
/** Given an svnstatus, returns the path to the icon.
485
 * \param type String, svn status.
486
 * \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
487
 * root. May return null to indicate no SVN icon.
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
488
 */
489
function svnstatus_to_icon(svnstatus)
490
{
491
    var filename;
492
    if (svnstatus in svn_icons)
493
        filename = svn_icons[svnstatus];
494
    else
495
        filename = default_svn_icon;
230 by mattgiuca
Removed "unversioned" icon from subversion status. Now unversioned files do
496
    if (filename == null) return null;
190 by mattgiuca
util: Added function dom_make_img, creates <img> elements.
497
    return make_path(path_join(svn_icons_path, filename));
498
}
499
213 by mattgiuca
Fileservice / Files (Python and JS files):
500
/** Given an svnstatus, returns the "nice" string.
501
 */
502
function svnstatus_to_string(svnstatus)
503
{
504
    if (svnstatus in svn_nice)
505
        return svn_nice[svnstatus];
506
    else
507
        return default_svn_nice;
508
}
509
183 by mattgiuca
browser: Added top level of response handling. Now determines handler type and
510
/** Displays a download link to the binary file.
511
 */
512
function handle_binary(path)
513
{
205 by mattgiuca
browser.js: Added appropriate handlers for error, text, and binary data.
514
    var files = document.getElementById("filesbody");
515
    var div = document.createElement("div");
516
    files.appendChild(div);
517
    div.setAttribute("class", "padding");
224 by mattgiuca
util.js: Removed urlencoding support from "encoded_app_path" (now called
518
    var download_link = app_path(download_app, path);
205 by mattgiuca
browser.js: Added appropriate handlers for error, text, and binary data.
519
    var par1 = dom_make_text_elem("p",
520
        "The file " + path + " is a binary file. To download this file, " +
521
        "click the following link:");
522
    var par2 = dom_make_link_elem("p",
523
        "Download " + path, "Download " + path, download_link);
524
    div.appendChild(par1);
525
    div.appendChild(par2);
181 by mattgiuca
browser.js: Added functions do_action, navigate, handle_response (stubs).
526
}
527
863 by wagrant
browser: Let set_action_state take an Array of names as well. We now
528
/* Enable or disable actions1 moreactions actions. Takes either a single
529
 * name, or an array of them.*/
908 by wagrant
browser: Link breadcrumbs to the current revision.
530
function set_action_state(names, which, allow_on_revision)
862 by wagrant
browser: Refactor the client-side actions1 processing! Kill off 100 lines
531
{
863 by wagrant
browser: Let set_action_state take an Array of names as well. We now
532
    if (!(names instanceof Array)) names = Array(names);
533
534
    for (var i=0; i < names.length; i++)
535
    {
536
        element = document.getElementById('act_' + names[i]);
908 by wagrant
browser: Link breadcrumbs to the current revision.
537
        if (which &&
538
            !(current_file.svnstatus == 'revision' && !allow_on_revision))
863 by wagrant
browser: Let set_action_state take an Array of names as well. We now
539
        {
540
            /* Enabling */
541
            element.setAttribute("class", "choice");
542
            element.removeAttribute("disabled");
543
        }
544
        else
545
        {
546
            /* Disabling */
547
            element.setAttribute("class", "disabled");
548
            element.setAttribute("disabled", "disabled");
549
        }
862 by wagrant
browser: Refactor the client-side actions1 processing! Kill off 100 lines
550
    }
551
}
552
1086 by chadnickbok
This commit fixes issue #10 and part of issue #9
553
/* Updates the list of available actions based on files selected */
597 by mattgiuca
Major refactor of actions in File browser.
554
function update_actions()
555
{
556
    var file;
557
    var numsel = selected_files.length;
1086 by chadnickbok
This commit fixes issue #10 and part of issue #9
558
    var svn_selection = false;
559
    
560
    if (numsel > 0)
561
    {
562
        svn_selection = true;
563
        for (var i = 0; i < selected_files.length; i++){
564
            if (file_listing[selected_files[i]]["svnstatus"] == "unversioned")
565
            {
566
                svn_selection = false;        
567
            }
568
        }
569
    }
570
    
597 by mattgiuca
Major refactor of actions in File browser.
571
    if (numsel <= 1)
572
    {
573
        if (numsel == 0)
574
        {
575
            /* Display information about the current directory instead */
576
            filename = path_basename(current_path);
602 by mattgiuca
util.js: Added shallow_copy_object function.
577
            file = current_file;
597 by mattgiuca
Major refactor of actions in File browser.
578
        }
579
        else if (numsel == 1)
580
        {
581
            filename = selected_files[0];
582
            file = file_listing[filename];
583
        }
584
585
        /* Update each action node in the topbar.
586
         * This includes enabling/disabling actions as appropriate, and
587
         * setting href/onclick attributes. */
588
    }
589
590
    /* Open */
591
    /* Available if exactly one file is selected */
592
    var open = document.getElementById("act_open");
593
    if (numsel == 1)
594
    {
595
        open.setAttribute("class", "choice");
596
        if (file.isdir)
597
            open.setAttribute("title",
598
                "Navigate to this directory in the file browser");
599
        else
600
            open.setAttribute("title",
601
                "Edit or view this file");
910 by wagrant
filebrowser_lib: Send the current revision along with the listing if we
602
        open.setAttribute("href", build_revision_url(current_path, filename,
603
                                                     current_revision));
597 by mattgiuca
Major refactor of actions in File browser.
604
    }
605
    else
606
    {
607
        open.setAttribute("class", "disabled");
608
        open.removeAttribute("title");
609
        open.removeAttribute("href");
610
    }
611
612
    /* Serve */
775 by mattgiuca
browser.js: Updated logic to decide when to enable/disable certain actions.
613
    /* Available if zero or one files are selected,
597 by mattgiuca
Major refactor of actions in File browser.
614
     * and only if this is a file, not a directory */
615
    var serve = document.getElementById("act_serve");
908 by wagrant
browser: Link breadcrumbs to the current revision.
616
    if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
597 by mattgiuca
Major refactor of actions in File browser.
617
    {
618
        serve.setAttribute("class", "choice");
865 by wagrant
browser: Warn users before running, serving and refreshing if the file
619
        serve.setAttribute("onclick",
620
              "return maybe_save('The last saved version will be served.')");
775 by mattgiuca
browser.js: Updated logic to decide when to enable/disable certain actions.
621
        if (numsel == 0)
622
            serve.setAttribute("href",
623
                app_path(serve_app, current_path));
624
        else
625
            serve.setAttribute("href",
626
                app_path(serve_app, current_path, filename));
597 by mattgiuca
Major refactor of actions in File browser.
627
    }
628
    else
629
    {
630
        serve.setAttribute("class", "disabled");
631
        serve.removeAttribute("href");
865 by wagrant
browser: Warn users before running, serving and refreshing if the file
632
        serve.removeAttribute("onclick");
597 by mattgiuca
Major refactor of actions in File browser.
633
    }
634
635
    /* Run */
636
    /* Available if exactly one file is selected,
637
     * and it is a Python file.
638
     */
713 by dcoles
browser: Added console and run button to allow Python files to be executed in
639
    var run = document.getElementById("act_run");
640
     
1086 by chadnickbok
This commit fixes issue #10 and part of issue #9
641
    if (numsel <= 1 && !file.isdir && file.type == "text/x-python" 
642
            && current_file.svnstatus != 'revision')
873 by dcoles
Filebrowser: Clean up the javascript code a bit.
643
    {
644
        if (numsel == 0)
645
        {
646
            // In the edit window
647
            var localpath = path_join('/home', current_path);
648
        }
649
        else
650
        {
651
            // In the browser window
652
            var localpath = path_join('/home', current_path, filename);
653
        }
654
        run.setAttribute("class", "choice");
713 by dcoles
browser: Added console and run button to allow Python files to be executed in
655
        run.setAttribute("onclick", "runfile('" + localpath + "')");
656
    }
657
    else
658
    {
659
        run.setAttribute("class", "disabled");
660
        run.removeAttribute("onclick");
661
    }
597 by mattgiuca
Major refactor of actions in File browser.
662
663
    /* Download */
908 by wagrant
browser: Link breadcrumbs to the current revision.
664
    /* Always available for current files.
597 by mattgiuca
Major refactor of actions in File browser.
665
     * If 0 files selected, download the current file or directory as a ZIP.
666
     * If 1 directory selected, download it as a ZIP.
667
     * If 1 non-directory selected, download it.
668
     * If >1 files selected, download them all as a ZIP.
669
     */
670
    var download = document.getElementById("act_download");
908 by wagrant
browser: Link breadcrumbs to the current revision.
671
    if (current_file.svnstatus == 'revision')
672
    {
673
        download.setAttribute("class", "disabled");
674
        download.removeAttribute("onclick");
675
    }
676
    else if (numsel <= 1)
677
    {
678
        download.setAttribute("class", "choice")
597 by mattgiuca
Major refactor of actions in File browser.
679
        if (numsel == 0)
680
        {
681
            download.setAttribute("href",
682
                app_path(download_app, current_path));
683
            if (file.isdir)
684
                download.setAttribute("title",
685
                    "Download the current directory as a ZIP file");
686
            else
687
                download.setAttribute("title",
688
                    "Download the current file");
689
        }
690
        else
691
        {
692
            download.setAttribute("href",
693
                app_path(download_app, current_path, filename));
694
            if (file.isdir)
695
                download.setAttribute("title",
696
                    "Download the selected directory as a ZIP file");
697
            else
698
                download.setAttribute("title",
699
                    "Download the selected file");
700
        }
701
    }
702
    else
703
    {
704
        /* Make a query string with all the files to download */
705
        var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
706
        for (var i=0; i<numsel; i++)
707
            dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
708
        dlpath = dlpath.substr(0, dlpath.length-1);
908 by wagrant
browser: Link breadcrumbs to the current revision.
709
        download.setAttribute("class", "choice")
597 by mattgiuca
Major refactor of actions in File browser.
710
        download.setAttribute("href", dlpath);
711
        download.setAttribute("title",
712
            "Download the selected files as a ZIP file");
713
    }
714
715
    /* Refresh - No changes required */
716
717
    /* Publish and Submit */
718
    /* If this directory is under subversion and selected/unselected file is a
719
     * directory. */
720
    var publish = document.getElementById("act_publish");
721
    var submit = document.getElementById("act_submit");
862 by wagrant
browser: Refactor the client-side actions1 processing! Kill off 100 lines
722
    var pubcond = numsel <= 1 && file.isdir;
723
    if (pubcond)
597 by mattgiuca
Major refactor of actions in File browser.
724
    {
727 by dcoles
Browser: Added UI code to let you publish and unpublish folders
725
        /* If this dir is already published, call it "Unpublish" */
726
        if (file.published)
727
        {
728
            publish.setAttribute("value", "unpublish");
729
            publish.setAttribute("title" ,"Make it so this directory "
730
                + "can not be seen by anyone on the web");
731
            publish.textContent = "Unpublish";
732
        } else {
733
            publish.setAttribute("value", "publish");
734
            publish.setAttribute("title","Make it so this directory "
735
                + "can be seen by anyone on the web");
736
            publish.textContent = "Publish";
737
        }
862 by wagrant
browser: Refactor the client-side actions1 processing! Kill off 100 lines
738
    }
863 by wagrant
browser: Let set_action_state take an Array of names as well. We now
739
    set_action_state(["publish", "submit"], pubcond);
597 by mattgiuca
Major refactor of actions in File browser.
740
741
    /* Share */
862 by wagrant
browser: Refactor the client-side actions1 processing! Kill off 100 lines
742
    /* If exactly 1 non-directory file is selected, and its parent
597 by mattgiuca
Major refactor of actions in File browser.
743
     * directory is published.
744
     */
862 by wagrant
browser: Refactor the client-side actions1 processing! Kill off 100 lines
745
    set_action_state("share", numsel == 1 && !file.isdir &&
746
                     current_file.published);
597 by mattgiuca
Major refactor of actions in File browser.
747
748
    /* Rename */
749
    /* If exactly 1 file is selected */
862 by wagrant
browser: Refactor the client-side actions1 processing! Kill off 100 lines
750
    set_action_state("rename", numsel == 1);
597 by mattgiuca
Major refactor of actions in File browser.
751
752
    /* Delete, cut, copy */
753
    /* If >= 1 file is selected */
863 by wagrant
browser: Let set_action_state take an Array of names as well. We now
754
    set_action_state(["delete", "cut", "copy"], numsel >= 1);
597 by mattgiuca
Major refactor of actions in File browser.
755
756
    /* Paste, new file, new directory, upload */
775 by mattgiuca
browser.js: Updated logic to decide when to enable/disable certain actions.
757
    /* Disable if the current file is not a directory */
863 by wagrant
browser: Let set_action_state take an Array of names as well. We now
758
    set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
597 by mattgiuca
Major refactor of actions in File browser.
759
760
    /* Subversion actions */
829 by wagrant
Add an svndiff action, and give it an option in the action listbox.
761
    /* These are only useful if we are in a versioned directory and have some
762
     * files selected. */
1086 by chadnickbok
This commit fixes issue #10 and part of issue #9
763
    set_action_state(["svnadd",], numsel >= 1 && current_file.svnstatus);
764
    /* And these are only usefull is ALL the selected files are versioned */
765
    set_action_state(["svnremove", "svnrevert", "svncommit", "svncopy", 
766
            "svncut"], numsel >= 1 && current_file.svnstatus && svn_selection);
767
    
918 by wagrant
browser: Expose svn update functionality. This brings a need for an
768
    /* Diff, log and update only support one path at the moment, so we must
769
     * have 0 or 1 versioned files selected. If 0, the directory must be
770
     * versioned. */
881 by wagrant
browser.js: Support svndiff and svnlog for versioned directories. Also
771
    single_versioned_path = (
772
         (
773
          (numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
774
          (numsel == 0 && (svnst = current_file.svnstatus))
775
         ) && svnst != "unversioned");
918 by wagrant
browser: Expose svn update functionality. This brings a need for an
776
    set_action_state(["svndiff", "svnupdate"], single_versioned_path);
908 by wagrant
browser: Link breadcrumbs to the current revision.
777
919 by wagrant
fileservice_lib: Implement "svn resolved".
778
    /* We can resolve if we have a file selected and it is conflicted. */
779
    set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
780
908 by wagrant
browser: Link breadcrumbs to the current revision.
781
    /* Log should be available for revisions as well. */
782
    set_action_state("svnlog", single_versioned_path, true);
862 by wagrant
browser: Refactor the client-side actions1 processing! Kill off 100 lines
783
777 by mattgiuca
browser: Now hides the "More actions" box altogether if the current file is
784
    /* There is currently nothing on the More Actions menu of use
785
     * when the current file is not a directory. Hence, just remove
786
     * it entirely.
787
     * (This makes some of the above decisions somewhat redundant).
834 by wagrant
Let the client decide whether to show file or directory actions2, as
788
     * We also take this opportunity to show the appropriate actions2
789
     * bar for this path. It should either be a save or upload widget.
777 by mattgiuca
browser: Now hides the "More actions" box altogether if the current file is
790
     */
834 by wagrant
Let the client decide whether to show file or directory actions2, as
791
    if (current_file.isdir)
792
    {
793
        var actions2_directory = document.getElementById("actions2_directory");
794
        actions2_directory.setAttribute("style", "display: inline;");
911 by wagrant
browser: Hide 'More Actions' by default. It will be shown by JS when
795
        var moreactions = document.getElementById("moreactions_area");
796
        moreactions.setAttribute("style", "display: inline;");
834 by wagrant
Let the client decide whether to show file or directory actions2, as
797
    }
798
    else
799
    {
800
        var actions2_file = document.getElementById("actions2_file");
801
        actions2_file.setAttribute("style", "display: inline;");
777 by mattgiuca
browser: Now hides the "More actions" box altogether if the current file is
802
    }
803
597 by mattgiuca
Major refactor of actions in File browser.
804
    return;
805
}
806
807
/** Event handler for when an item of the "More actions..." dropdown box is
808
 * selected. Performs the selected action. */
809
function handle_moreactions()
810
{
811
    var moreactions = document.getElementById("moreactions");
812
    if (moreactions.value == "top")
813
        return;
814
    var selectedaction = moreactions.value;
815
    /* Reset to "More actions..." */
816
    moreactions.selectedIndex = 0;
817
607 by mattgiuca
browser/browser.js: All the actions in "more actions" now actually take
818
    /* If 0 files selected, filename is the name of the current dir.
819
     * If 1 file selected, filename is that file.
820
     */
821
    if (selected_files.length == 0)
822
        filename = path_basename(current_path);
823
    else if (selected_files.length == 1)
824
        filename = selected_files[0];
825
    else
826
        filename = null;
827
597 by mattgiuca
Major refactor of actions in File browser.
828
    /* Now handle the selected action */
607 by mattgiuca
browser/browser.js: All the actions in "more actions" now actually take
829
    switch(selectedaction)
830
    {
831
    case "publish":
832
        action_publish(selected_files);
833
        break;
834
    case "unpublish":
835
        action_unpublish(selected_files);
836
        break;
837
    case "share":
1099.1.204 by William Grant
"Share this file" in the file browser points to the new-style publicserveview.
838
        window.open(public_app_path("~" + current_path, filename), 'share')
607 by mattgiuca
browser/browser.js: All the actions in "more actions" now actually take
839
        break;
840
    case "submit":
841
        // TODO
842
        alert("Not yet implemented: Submit");
843
        break;
844
    case "rename":
845
        action_rename(filename);
846
        break;
847
    case "delete":
921 by wagrant
fileservice_lib, browser: Rename the old remove action to delete, to
848
        action_delete(selected_files);
607 by mattgiuca
browser/browser.js: All the actions in "more actions" now actually take
849
        break;
850
    case "copy":
851
        action_copy(selected_files);
852
        break;
853
    case "cut":
854
        action_cut(selected_files);
855
        break;
856
    case "paste":
857
        action_paste();
858
        break;
859
    case "newfile":
611 by mattgiuca
"New File" now works. (This is a MUCH better replacement for having to go to
860
        action_newfile();
607 by mattgiuca
browser/browser.js: All the actions in "more actions" now actually take
861
        break;
862
    case "mkdir":
863
        action_mkdir();
864
        break;
865
    case "upload":
866
        show_uploadpanel(true);
867
        break;
868
    case "svnadd":
869
        action_add(selected_files);
870
        break;
921 by wagrant
fileservice_lib, browser: Rename the old remove action to delete, to
871
    case "svnremove":
872
        action_remove(selected_files);
873
        break;
607 by mattgiuca
browser/browser.js: All the actions in "more actions" now actually take
874
    case "svnrevert":
875
        action_revert(selected_files);
876
        break;
829 by wagrant
Add an svndiff action, and give it an option in the action listbox.
877
    case "svndiff":
881 by wagrant
browser.js: Support svndiff and svnlog for versioned directories. Also
878
        window.location = path_join(app_path('diff'), current_path, selected_files[0] || '');
829 by wagrant
Add an svndiff action, and give it an option in the action listbox.
879
        break;
918 by wagrant
browser: Expose svn update functionality. This brings a need for an
880
    case "svnupdate":
881
        action_update(selected_files);
882
        break;
919 by wagrant
fileservice_lib: Implement "svn resolved".
883
    case "svnresolved":
884
        action_resolved(selected_files);
885
        break;
607 by mattgiuca
browser/browser.js: All the actions in "more actions" now actually take
886
    case "svncommit":
887
        action_commit(selected_files);
888
        break;
832 by wagrant
Add svnlog to the file actions dropdown.
889
    case "svnlog":
881 by wagrant
browser.js: Support svndiff and svnlog for versioned directories. Also
890
        window.location = path_join(app_path('svnlog'), current_path, selected_files[0] || '');
832 by wagrant
Add svnlog to the file actions dropdown.
891
        break;
1086 by chadnickbok
This commit fixes issue #10 and part of issue #9
892
    case "svncopy":
893
        action_svncopy(selected_files);
894
        break;
895
    case "svncut":
896
        action_svncut(selected_files);
897
        break;
607 by mattgiuca
browser/browser.js: All the actions in "more actions" now actually take
898
    }
597 by mattgiuca
Major refactor of actions in File browser.
899
}
900
713 by dcoles
browser: Added console and run button to allow Python files to be executed in
901
/** User clicks "Run" button.
902
 * Do an Ajax call and print the test output.
903
 */
904
function runfile(localpath)
905
{
865 by wagrant
browser: Warn users before running, serving and refreshing if the file
906
    if (!maybe_save('The last saved version will be run.')) return false;
907
713 by dcoles
browser: Added console and run button to allow Python files to be executed in
908
    /* Dump the entire file to the console */
909
    var callback = function()
910
    {
911
        console_enter_line("execfile('" + localpath + "')", "block");
912
    }
913
    start_server(callback)
914
    return;
915
}
916
170 by mattgiuca
browser: Added CSS and JS files (not much in them).
917
/** Called when the page loads initially.
918
 */
849 by dcoles
Browser: Ported Browser to the new scripts_init framework. Started to decouple
919
function browser_init()
170 by mattgiuca
browser: Added CSS and JS files (not much in them).
920
{
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
921
    /* Navigate (internally) to the path in the URL bar.
922
     * This causes the page to be populated with whatever is at that address,
923
     * whether it be a directory or a file.
924
     */
925
    var path = parse_url(window.location.href).path;
926
    /* 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.
927
    var strip = make_path(this_app);
928
    if (path.substr(0, strip.length) == strip)
929
        path = path.substr(strip.length+1);
930
    else
931
    {
932
        /* See if this is an edit path */
933
        strip = make_path(edit_app);
934
        if (path.substr(0, strip.length) == strip)
935
        {
936
            path = path.substr(strip.length+1);
937
        }
938
    }
188 by mattgiuca
browser.js: Can now (shakily) handle directory listings. (lots of code!)
939
200 by mattgiuca
fileservice.listing: Now returns a nicer date format for mtime_nice.
940
    if (path.length == 0)
941
    {
942
        /* Navigate to the user's home directory by default */
943
        /* TEMP? */
944
        path = username;
945
    }
946
603 by mattgiuca
Console now starts up in the user's home directory.
947
    navigate(path);
170 by mattgiuca
browser: Added CSS and JS files (not much in them).
948
}