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

« back to all changes in this revision

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

  • Committer: mattgiuca
  • Date: 2007-12-19 07:02:43 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:86
apps/server: Now calls trampoline-python.
bin/trampoline-python: Commented out throttle call (didn't work).
        Set jail relative to my test jail (will have to change).

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
23
 
/* Url names for apps */
24
 
this_app = "files";
25
 
edit_app = "edit";
26
 
service_app = "fileservice";
27
 
serve_app = "serve";
28
 
download_app = "download";
29
 
 
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
 
 
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": null,
64
 
    "normal": "normal.png",
65
 
    "added": "added.png",
66
 
    "missing": "missing.png",
67
 
    "deleted": "deleted.png",
68
 
    "modified": "modified.png",
69
 
    "conflicted": "conflicted.png",
70
 
    "revision": "revision.png"
71
 
};
72
 
 
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)",
83
 
    "conflicted": "Permanent file (conflicted)",
84
 
    "revision": "Past Permanent file (revision)"
85
 
};
86
 
 
87
 
default_svn_icon = null;
88
 
default_svn_nice = "Unknown status";
89
 
 
90
 
svn_icons_path = "media/images/svn";
91
 
 
92
 
published_icon = "media/images/interface/published.png";
93
 
 
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 = [
99
 
    "text/x-python"
100
 
];
101
 
 
102
 
 
103
 
/* Global variables */
104
 
 
105
 
/** The listing object returned by the server as JSON */
106
 
file_listing = null;
107
 
current_file = null;
108
 
current_revision = null;
109
 
current_path = "";
110
 
 
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
 
 
117
 
upload_callback_count = 0;      /* See upload_callback */
118
 
 
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
 
 */
138
 
function do_action(action, path, args, content_type, ignore_response)
139
 
{
140
 
    args.action = action;
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)
148
 
                /* Note: This header (in particular) comes URI-encoded, to
149
 
                 * allow multi-line error messages. Decode */
150
 
                alert("Error: " + decodeURIComponent(error.toString()) + ".");
151
 
            /* Now read the response and set up the page accordingly */
152
 
            if (ignore_response != true)
153
 
                handle_response(path, response, true);
154
 
        }
155
 
    /* Call the server and perform the action. This mutates the server. */
156
 
    ajax_call(callback, service_app, path, args, "POST", content_type);
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
 
 */
167
 
function navigate(path)
168
 
{
169
 
    callback = function(response)
170
 
        {
171
 
            /* Read the response and set up the page accordingly */
172
 
            handle_response(path, response, false, url.args);
173
 
        }
174
 
    /* Get any query strings */
175
 
    url = parse_url(window.location.href);
176
 
    
177
 
    /* Call the server and request the listing. */
178
 
    ajax_call(callback, service_app, path, url.args, "GET");
179
 
}
180
 
 
181
 
/* Refreshes the current view.
182
 
 * Calls navigate on the current path.
183
 
 */
184
 
function refresh()
185
 
{
186
 
    if (maybe_save('All changes since the last save will be lost!'))
187
 
        navigate(current_path);
188
 
}
189
 
 
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
 
 
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.
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.
222
 
 * \param url_args Arguments dict, for the arguments passed to the URL
223
 
 * in the browser's address bar (will be forwarded along).
224
 
 */
225
 
function handle_response(path, response, is_action, url_args)
226
 
{
227
 
    /* TODO: Set location bar to "path" */
228
 
    current_path = path;
229
 
 
230
 
    /* Clear away the existing page contents */
231
 
    clearpage();
232
 
 
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
 
 
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
 
 
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
 
    {
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
 
        }
295
 
        return;
296
 
    }
297
 
    /* Get "." out, it's special */
298
 
    current_file = file_listing["."];     /* Global */
299
 
    delete file_listing["."];
300
 
 
301
 
    if ('revision' in listing)
302
 
    {
303
 
        current_revision = listing.revision;
304
 
    }
305
 
 
306
 
    /* Check if this is a directory listing or file contents */
307
 
    var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
308
 
    if (isdir)
309
 
    {
310
 
        setup_for_listing();
311
 
        home_listing(listing, subjects, path);
312
 
    }
313
 
    else
314
 
    {
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");
330
 
    }
331
 
    update_actions(isdir);
332
 
}
333
 
 
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
 
 
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)
379
 
    {
380
 
        document.getElementsByName('data')[0].value = '';
381
 
        refresh();
382
 
    }
383
 
}
384
 
 
385
 
/** Deletes all "dynamic" content on the page.
386
 
 * This returns the page back to the state it is in when the HTML arrives to
387
 
 * the browser, ready for another handler to populate it.
388
 
 */
389
 
function clearpage()
390
 
{
391
 
    dom_removechildren(document.getElementById("filesbody"));
392
 
}
393
 
 
394
 
/* Checks if a file needs to be saved. If it does, the user will be asked
395
 
 * if they want to continue anyway. The caller must specify a warning
396
 
 * sentence which indicates the consequences of continuing.
397
 
 * Returns true if we should continue, and false if we should not.
398
 
 */
399
 
function maybe_save(warning)
400
 
{
401
 
    if (warning == null) warning = '';
402
 
    if (current_file.isdir) return true;
403
 
    if (document.getElementById("save_button").disabled) return true;
404
 
    return confirm("This file has unsaved changes. " + warning +
405
 
                   "\nAre you sure you wish to continue?");
406
 
}
407
 
 
408
 
/** Deletes all "dynamic" content on the page necessary to navigate from
409
 
 * one directory listing to another (does not clear as much as clearpage
410
 
 * does).
411
 
 * This is the equivalent of calling clearpage() then
412
 
 * setup_for_dir_listing(), assuming the page is already on a dir listing.
413
 
 */
414
 
function clearpage_dir()
415
 
{
416
 
    dom_removechildren(document.getElementById("path"));
417
 
    dom_removechildren(document.getElementById("files"));
418
 
    dom_removechildren(document.getElementById("sidepanel"));
419
 
}
420
 
 
421
 
/*** HANDLERS for different types of responses (such as dir listing, file,
422
 
 * etc). */
423
 
 
424
 
/* handle_error.
425
 
 * message may either be a string, or a DOM node, which will be placed inside
426
 
 * a div.
427
 
 */
428
 
function handle_error(message)
429
 
{
430
 
    var files = document.getElementById("filesbody");
431
 
    var txt_elem;
432
 
    if (typeof(message) == "string")
433
 
    {
434
 
        txt_elem = dom_make_text_elem("div", "Error: "
435
 
                   + message.toString() + ".")
436
 
    }
437
 
    else
438
 
    {
439
 
        /* Assume message is a DOM node */
440
 
        txt_elem = document.createElement("div");
441
 
        txt_elem.appendChild(message);
442
 
    }
443
 
    txt_elem.setAttribute("class", "padding error");
444
 
    files.appendChild(txt_elem);
445
 
}
446
 
 
447
 
/** Given a path, filename and optional revision, returns a URL to open that
448
 
 *  revision of that file.
449
 
 */
450
 
function build_revision_url(path, filename, revision)
451
 
{
452
 
    bits = {'path': app_path(this_app, path, filename)};
453
 
    if (current_revision)
454
 
    {
455
 
        bits['query_string'] = 'r=' + revision;
456
 
    }
457
 
    return build_url(bits);
458
 
}
459
 
 
460
 
/** Given a mime type, returns the path to the icon.
461
 
 * \param type String, Mime type.
462
 
 * \param sizelarge Boolean, optional.
463
 
 * \return Path to the icon. Has applied make_path, so it is relative to site
464
 
 * root.
465
 
 */
466
 
function mime_type_to_icon(type, sizelarge)
467
 
{
468
 
    var filename;
469
 
    if (type in type_icons)
470
 
        filename = type_icons[type];
471
 
    else
472
 
        filename = default_type_icon;
473
 
    if (sizelarge)
474
 
        return make_path(path_join(type_icons_path_large, filename));
475
 
    else
476
 
        return make_path(path_join(type_icons_path, filename));
477
 
}
478
 
 
479
 
/** Given an svnstatus, returns the path to the icon.
480
 
 * \param type String, svn status.
481
 
 * \return Path to the icon. Has applied make_path, so it is relative to site
482
 
 * root. May return null to indicate no SVN icon.
483
 
 */
484
 
function svnstatus_to_icon(svnstatus)
485
 
{
486
 
    var filename;
487
 
    if (svnstatus in svn_icons)
488
 
        filename = svn_icons[svnstatus];
489
 
    else
490
 
        filename = default_svn_icon;
491
 
    if (filename == null) return null;
492
 
    return make_path(path_join(svn_icons_path, filename));
493
 
}
494
 
 
495
 
/** Given an svnstatus, returns the "nice" string.
496
 
 */
497
 
function svnstatus_to_string(svnstatus)
498
 
{
499
 
    if (svnstatus in svn_nice)
500
 
        return svn_nice[svnstatus];
501
 
    else
502
 
        return default_svn_nice;
503
 
}
504
 
 
505
 
/** Displays a download link to the binary file.
506
 
 */
507
 
function handle_binary(path)
508
 
{
509
 
    var files = document.getElementById("filesbody");
510
 
    var div = document.createElement("div");
511
 
    files.appendChild(div);
512
 
    div.setAttribute("class", "padding");
513
 
    var download_link = app_path(download_app, path);
514
 
    var par1 = dom_make_text_elem("p",
515
 
        "The file " + path + " is a binary file. To download this file, " +
516
 
        "click the following link:");
517
 
    var par2 = dom_make_link_elem("p",
518
 
        "Download " + path, "Download " + path, download_link);
519
 
    div.appendChild(par1);
520
 
    div.appendChild(par2);
521
 
}
522
 
 
523
 
/* Enable or disable actions1 moreactions actions. Takes either a single
524
 
 * name, or an array of them.*/
525
 
function set_action_state(names, which, allow_on_revision)
526
 
{
527
 
    if (!(names instanceof Array)) names = Array(names);
528
 
 
529
 
    for (var i=0; i < names.length; i++)
530
 
    {
531
 
        element = document.getElementById('act_' + names[i]);
532
 
        if (which &&
533
 
            !(current_file.svnstatus == 'revision' && !allow_on_revision))
534
 
        {
535
 
            /* Enabling */
536
 
            element.setAttribute("class", "choice");
537
 
            element.removeAttribute("disabled");
538
 
        }
539
 
        else
540
 
        {
541
 
            /* Disabling */
542
 
            element.setAttribute("class", "disabled");
543
 
            element.setAttribute("disabled", "disabled");
544
 
        }
545
 
    }
546
 
}
547
 
 
548
 
/* Updates the list of available actions based on files selected */
549
 
function update_actions()
550
 
{
551
 
    var file;
552
 
    var numsel = selected_files.length;
553
 
    var svn_selection = false;
554
 
    
555
 
    if (numsel > 0)
556
 
    {
557
 
        svn_selection = true;
558
 
        for (var i = 0; i < selected_files.length; i++){
559
 
            if (file_listing[selected_files[i]]["svnstatus"] == "unversioned")
560
 
            {
561
 
                svn_selection = false;        
562
 
            }
563
 
        }
564
 
    }
565
 
    
566
 
    if (numsel <= 1)
567
 
    {
568
 
        if (numsel == 0)
569
 
        {
570
 
            /* Display information about the current directory instead */
571
 
            filename = path_basename(current_path);
572
 
            file = current_file;
573
 
        }
574
 
        else if (numsel == 1)
575
 
        {
576
 
            filename = selected_files[0];
577
 
            file = file_listing[filename];
578
 
        }
579
 
 
580
 
        /* Update each action node in the topbar.
581
 
         * This includes enabling/disabling actions as appropriate, and
582
 
         * setting href/onclick attributes. */
583
 
    }
584
 
 
585
 
    /* Open */
586
 
    /* Available if exactly one file is selected */
587
 
    var open = document.getElementById("act_open");
588
 
    if (numsel == 1)
589
 
    {
590
 
        open.setAttribute("class", "choice");
591
 
        if (file.isdir)
592
 
            open.setAttribute("title",
593
 
                "Navigate to this directory in the file browser");
594
 
        else
595
 
            open.setAttribute("title",
596
 
                "Edit or view this file");
597
 
        open.setAttribute("href", build_revision_url(current_path, filename,
598
 
                                                     current_revision));
599
 
    }
600
 
    else
601
 
    {
602
 
        open.setAttribute("class", "disabled");
603
 
        open.removeAttribute("title");
604
 
        open.removeAttribute("href");
605
 
    }
606
 
 
607
 
    /* Serve */
608
 
    /* Available if zero or one files are selected,
609
 
     * and only if this is a file, not a directory */
610
 
    var serve = document.getElementById("act_serve");
611
 
    if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
612
 
    {
613
 
        serve.setAttribute("class", "choice");
614
 
        serve.setAttribute("onclick",
615
 
              "return maybe_save('The last saved version will be served.')");
616
 
        if (numsel == 0)
617
 
            serve.setAttribute("href",
618
 
                app_path(serve_app, current_path));
619
 
        else
620
 
            serve.setAttribute("href",
621
 
                app_path(serve_app, current_path, filename));
622
 
    }
623
 
    else
624
 
    {
625
 
        serve.setAttribute("class", "disabled");
626
 
        serve.removeAttribute("href");
627
 
        serve.removeAttribute("onclick");
628
 
    }
629
 
 
630
 
    /* Run */
631
 
    /* Available if exactly one file is selected,
632
 
     * and it is a Python file.
633
 
     */
634
 
    var run = document.getElementById("act_run");
635
 
     
636
 
    if (numsel <= 1 && !file.isdir && file.type == "text/x-python" 
637
 
            && current_file.svnstatus != 'revision')
638
 
    {
639
 
        if (numsel == 0)
640
 
        {
641
 
            // In the edit window
642
 
            var localpath = path_join('/home', current_path);
643
 
        }
644
 
        else
645
 
        {
646
 
            // In the browser window
647
 
            var localpath = path_join('/home', current_path, filename);
648
 
        }
649
 
        run.setAttribute("class", "choice");
650
 
        run.setAttribute("onclick", "runfile('" + localpath + "')");
651
 
    }
652
 
    else
653
 
    {
654
 
        run.setAttribute("class", "disabled");
655
 
        run.removeAttribute("onclick");
656
 
    }
657
 
 
658
 
    /* Download */
659
 
    /* Always available for current files.
660
 
     * If 0 files selected, download the current file or directory as a ZIP.
661
 
     * If 1 directory selected, download it as a ZIP.
662
 
     * If 1 non-directory selected, download it.
663
 
     * If >1 files selected, download them all as a ZIP.
664
 
     */
665
 
    var download = document.getElementById("act_download");
666
 
    if (current_file.svnstatus == 'revision')
667
 
    {
668
 
        download.setAttribute("class", "disabled");
669
 
        download.removeAttribute("onclick");
670
 
    }
671
 
    else if (numsel <= 1)
672
 
    {
673
 
        download.setAttribute("class", "choice")
674
 
        if (numsel == 0)
675
 
        {
676
 
            download.setAttribute("href",
677
 
                app_path(download_app, current_path));
678
 
            if (file.isdir)
679
 
                download.setAttribute("title",
680
 
                    "Download the current directory as a ZIP file");
681
 
            else
682
 
                download.setAttribute("title",
683
 
                    "Download the current file");
684
 
        }
685
 
        else
686
 
        {
687
 
            download.setAttribute("href",
688
 
                app_path(download_app, current_path, filename));
689
 
            if (file.isdir)
690
 
                download.setAttribute("title",
691
 
                    "Download the selected directory as a ZIP file");
692
 
            else
693
 
                download.setAttribute("title",
694
 
                    "Download the selected file");
695
 
        }
696
 
    }
697
 
    else
698
 
    {
699
 
        /* Make a query string with all the files to download */
700
 
        var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
701
 
        for (var i=0; i<numsel; i++)
702
 
            dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
703
 
        dlpath = dlpath.substr(0, dlpath.length-1);
704
 
        download.setAttribute("class", "choice")
705
 
        download.setAttribute("href", dlpath);
706
 
        download.setAttribute("title",
707
 
            "Download the selected files as a ZIP file");
708
 
    }
709
 
 
710
 
    /* Refresh - No changes required */
711
 
 
712
 
    /* Publish and Submit */
713
 
    /* If this directory is under subversion and selected/unselected file is a
714
 
     * directory. */
715
 
    var publish = document.getElementById("act_publish");
716
 
    var submit = document.getElementById("act_submit");
717
 
    var pubcond = numsel <= 1 && file.isdir;
718
 
    if (pubcond)
719
 
    {
720
 
        /* If this dir is already published, call it "Unpublish" */
721
 
        if (file.published)
722
 
        {
723
 
            publish.setAttribute("value", "unpublish");
724
 
            publish.setAttribute("title" ,"Make it so this directory "
725
 
                + "can not be seen by anyone on the web");
726
 
            publish.textContent = "Unpublish";
727
 
        } else {
728
 
            publish.setAttribute("value", "publish");
729
 
            publish.setAttribute("title","Make it so this directory "
730
 
                + "can be seen by anyone on the web");
731
 
            publish.textContent = "Publish";
732
 
        }
733
 
    }
734
 
    set_action_state(["publish", "submit"], pubcond);
735
 
 
736
 
    /* Share */
737
 
    /* If exactly 1 non-directory file is selected, and its parent
738
 
     * directory is published.
739
 
     */
740
 
    set_action_state("share", numsel == 1 && !file.isdir &&
741
 
                     current_file.published);
742
 
 
743
 
    /* Rename */
744
 
    /* If exactly 1 file is selected */
745
 
    set_action_state("rename", numsel == 1);
746
 
 
747
 
    /* Delete, cut, copy */
748
 
    /* If >= 1 file is selected */
749
 
    set_action_state(["delete", "cut", "copy"], numsel >= 1);
750
 
 
751
 
    /* Paste, new file, new directory, upload */
752
 
    /* Disable if the current file is not a directory */
753
 
    set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
754
 
 
755
 
    /* Subversion actions */
756
 
    /* These are only useful if we are in a versioned directory and have some
757
 
     * files selected. */
758
 
    set_action_state(["svnadd",], numsel >= 1 && current_file.svnstatus);
759
 
    /* And these are only usefull is ALL the selected files are versioned */
760
 
    set_action_state(["svnremove", "svnrevert", "svncommit", "svncopy", 
761
 
            "svncut"], numsel >= 1 && current_file.svnstatus && svn_selection);
762
 
    
763
 
    /* Diff, log and update only support one path at the moment, so we must
764
 
     * have 0 or 1 versioned files selected. If 0, the directory must be
765
 
     * versioned. */
766
 
    single_versioned_path = (
767
 
         (
768
 
          (numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
769
 
          (numsel == 0 && (svnst = current_file.svnstatus))
770
 
         ) && svnst != "unversioned");
771
 
    set_action_state(["svndiff", "svnupdate"], single_versioned_path);
772
 
 
773
 
    /* We can resolve if we have a file selected and it is conflicted. */
774
 
    set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
775
 
 
776
 
    /* Log should be available for revisions as well. */
777
 
    set_action_state("svnlog", single_versioned_path, true);
778
 
 
779
 
    /* There is currently nothing on the More Actions menu of use
780
 
     * when the current file is not a directory. Hence, just remove
781
 
     * it entirely.
782
 
     * (This makes some of the above decisions somewhat redundant).
783
 
     * We also take this opportunity to show the appropriate actions2
784
 
     * bar for this path. It should either be a save or upload widget.
785
 
     */
786
 
    if (current_file.isdir)
787
 
    {
788
 
        var actions2_directory = document.getElementById("actions2_directory");
789
 
        actions2_directory.setAttribute("style", "display: inline;");
790
 
        var moreactions = document.getElementById("moreactions_area");
791
 
        moreactions.setAttribute("style", "display: inline;");
792
 
    }
793
 
    else
794
 
    {
795
 
        var actions2_file = document.getElementById("actions2_file");
796
 
        actions2_file.setAttribute("style", "display: inline;");
797
 
    }
798
 
 
799
 
    return;
800
 
}
801
 
 
802
 
/** Event handler for when an item of the "More actions..." dropdown box is
803
 
 * selected. Performs the selected action. */
804
 
function handle_moreactions()
805
 
{
806
 
    var moreactions = document.getElementById("moreactions");
807
 
    if (moreactions.value == "top")
808
 
        return;
809
 
    var selectedaction = moreactions.value;
810
 
    /* Reset to "More actions..." */
811
 
    moreactions.selectedIndex = 0;
812
 
 
813
 
    /* If 0 files selected, filename is the name of the current dir.
814
 
     * If 1 file selected, filename is that file.
815
 
     */
816
 
    if (selected_files.length == 0)
817
 
        filename = path_basename(current_path);
818
 
    else if (selected_files.length == 1)
819
 
        filename = selected_files[0];
820
 
    else
821
 
        filename = null;
822
 
 
823
 
    /* Now handle the selected action */
824
 
    switch(selectedaction)
825
 
    {
826
 
    case "publish":
827
 
        action_publish(selected_files);
828
 
        break;
829
 
    case "unpublish":
830
 
        action_unpublish(selected_files);
831
 
        break;
832
 
    case "share":
833
 
        //alert("Not yet implemented: Sharing files");
834
 
        window.open(public_app_path(serve_app, current_path, filename), 'share')
835
 
        break;
836
 
    case "submit":
837
 
        // TODO
838
 
        alert("Not yet implemented: Submit");
839
 
        break;
840
 
    case "rename":
841
 
        action_rename(filename);
842
 
        break;
843
 
    case "delete":
844
 
        action_delete(selected_files);
845
 
        break;
846
 
    case "copy":
847
 
        action_copy(selected_files);
848
 
        break;
849
 
    case "cut":
850
 
        action_cut(selected_files);
851
 
        break;
852
 
    case "paste":
853
 
        action_paste();
854
 
        break;
855
 
    case "newfile":
856
 
        action_newfile();
857
 
        break;
858
 
    case "mkdir":
859
 
        action_mkdir();
860
 
        break;
861
 
    case "upload":
862
 
        show_uploadpanel(true);
863
 
        break;
864
 
    case "svnadd":
865
 
        action_add(selected_files);
866
 
        break;
867
 
    case "svnremove":
868
 
        action_remove(selected_files);
869
 
        break;
870
 
    case "svnrevert":
871
 
        action_revert(selected_files);
872
 
        break;
873
 
    case "svndiff":
874
 
        window.location = path_join(app_path('diff'), current_path, selected_files[0] || '');
875
 
        break;
876
 
    case "svnupdate":
877
 
        action_update(selected_files);
878
 
        break;
879
 
    case "svnresolved":
880
 
        action_resolved(selected_files);
881
 
        break;
882
 
    case "svncommit":
883
 
        action_commit(selected_files);
884
 
        break;
885
 
    case "svnlog":
886
 
        window.location = path_join(app_path('svnlog'), current_path, selected_files[0] || '');
887
 
        break;
888
 
    case "svncopy":
889
 
        action_svncopy(selected_files);
890
 
        break;
891
 
    case "svncut":
892
 
        action_svncut(selected_files);
893
 
        break;
894
 
    }
895
 
}
896
 
 
897
 
/** User clicks "Run" button.
898
 
 * Do an Ajax call and print the test output.
899
 
 */
900
 
function runfile(localpath)
901
 
{
902
 
    if (!maybe_save('The last saved version will be run.')) return false;
903
 
 
904
 
    /* Dump the entire file to the console */
905
 
    var callback = function()
906
 
    {
907
 
        console_enter_line("execfile('" + localpath + "')", "block");
908
 
    }
909
 
    start_server(callback)
910
 
    return;
911
 
}
912
 
 
913
 
/** Called when the page loads initially.
914
 
 */
915
 
function browser_init()
916
 
{
917
 
    /* Navigate (internally) to the path in the URL bar.
918
 
     * This causes the page to be populated with whatever is at that address,
919
 
     * whether it be a directory or a file.
920
 
     */
921
 
    var path = parse_url(window.location.href).path;
922
 
    /* Strip out root_dir + "/files" from the front of the path */
923
 
    var strip = make_path(this_app);
924
 
    if (path.substr(0, strip.length) == strip)
925
 
        path = path.substr(strip.length+1);
926
 
    else
927
 
    {
928
 
        /* See if this is an edit path */
929
 
        strip = make_path(edit_app);
930
 
        if (path.substr(0, strip.length) == strip)
931
 
        {
932
 
            path = path.substr(strip.length+1);
933
 
        }
934
 
    }
935
 
 
936
 
    if (path.length == 0)
937
 
    {
938
 
        /* Navigate to the user's home directory by default */
939
 
        /* TEMP? */
940
 
        path = username;
941
 
    }
942
 
 
943
 
    navigate(path);
944
 
}