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

« back to all changes in this revision

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

  • Committer: mattgiuca
  • Date: 2008-06-16 06:44:28 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:774
scripts/diffservice, diff.css: HTML refactor (simpler layout - it's not valid
    to put pre inside a dt).
    Renamed CSS classes to have hyphens instead of underscores (underscores
    are illegal).

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
    "revision": "revision.png"
 
70
};
 
71
 
 
72
/* Mapping SVN status to "nice" strings */
 
73
svn_nice = {
 
74
    "unversioned": "Temporary file",
 
75
    "normal": "Permanent file",
 
76
    "added": "Temporary file (scheduled to be added)",
 
77
    "missing": "Permanent file (missing)",
 
78
    "deleted": "Permanent file (scheduled for deletion)",
 
79
    "replaced": "Permanent file (replaced)",
 
80
    "modified": "Permanent file (modified)",
 
81
    "merged": "Permanent file (merged)",
 
82
    "conflicted": "Permanent file (conflicted)",
 
83
    "revision": "Past Permanent file (revision)"
 
84
};
 
85
 
 
86
default_svn_icon = null;
 
87
default_svn_nice = "Unknown status";
 
88
 
 
89
svn_icons_path = "media/images/svn";
 
90
 
 
91
published_icon = "media/images/interface/published.png";
 
92
 
 
93
/* List of MIME types considered "executable" by the system.
 
94
 * Executable files offer a "run" link, implying that the "serve"
 
95
 * application can interpret them.
 
96
 */
 
97
types_exec = [
 
98
    "text/x-python"
 
99
];
 
100
 
 
101
 
 
102
/* Global variables */
 
103
 
 
104
/** The listing object returned by the server as JSON */
 
105
file_listing = null;
 
106
current_file = null;
 
107
current_path = "";
 
108
 
 
109
/** Filenames of all files selected
 
110
 * (Only used by dir listings, but still needs to be [] for files, so that
 
111
 * update_actions knows that nothing is selected).
 
112
 */
 
113
selected_files = [];
 
114
 
 
115
upload_callback_count = 0;      /* See upload_callback */
 
116
 
 
117
/** Calls the server using Ajax, performing an action on the server side.
 
118
 * Receives the response from the server and performs a refresh of the page
 
119
 * contents, updating it to display the returned data (such as a directory
 
120
 * listing, file preview, or editor pane).
 
121
 * Always makes a POST request.
 
122
 * No return value.
 
123
 *
 
124
 * \param action String. Name of the action to perform, as defined in the
 
125
 *     fileservice API.
 
126
 * \param path URL path to make the request to, within the application.
 
127
 * \param args Argument object, as described in util.parse_url and friends.
 
128
 *      This should contain the arguments to the action, but NOT the action
 
129
 *      itself. (Also a minor side-effect; the "args" object will be mutated
 
130
 *      to include the action attribute).
 
131
 * \param content_type String, optional.
 
132
 *      May be "application/x-www-form-urlencoded" or "multipart/form-data".
 
133
 *      Defaults to "application/x-www-form-urlencoded".
 
134
 *      "multipart/form-data" is recommended for large uploads.
 
135
 */
 
136
function do_action(action, path, args, content_type, ignore_response)
 
137
{
 
138
    args.action = action;
 
139
    /* Callback action, when the server returns */
 
140
    var callback = function(response)
 
141
        {
 
142
            /* Check for action errors reported by the server, and report them
 
143
             * to the user */
 
144
            var error = response.getResponseHeader("X-IVLE-Action-Error");
 
145
            if (error != null)
 
146
                alert("Error: " + error.toString() + ".");
 
147
            /* Now read the response and set up the page accordingly */
 
148
            if (ignore_response != true)
 
149
                handle_response(path, response);
 
150
        }
 
151
    /* Call the server and perform the action. This mutates the server. */
 
152
    ajax_call(callback, service_app, path, args, "POST", content_type);
 
153
}
 
154
 
 
155
/** Calls the server using Ajax, requesting a directory listing. This should
 
156
 * not modify the server in any way. Receives the response from the server and
 
157
 * performs a refresh of the page contents, updating it to display the
 
158
 * returned data (such as a directory listing, file preview, or editor pane).
 
159
 * Called "navigate", can also be used for a simple refresh.
 
160
 * Always makes a GET request.
 
161
 * No return value.
 
162
 */
 
163
function navigate(path)
 
164
{
 
165
    callback = function(response)
 
166
        {
 
167
            /* Read the response and set up the page accordingly */
 
168
            handle_response(path, response, url.args);
 
169
        }
 
170
    /* Get any query strings */
 
171
    url = parse_url(window.location.href);
 
172
    
 
173
    /* Call the server and request the listing. */
 
174
    ajax_call(callback, service_app, path, url.args, "GET");
 
175
}
 
176
 
 
177
/* Refreshes the current view.
 
178
 * Calls navigate on the current path.
 
179
 */
 
180
function refresh()
 
181
{
 
182
    navigate(current_path);
 
183
}
 
184
 
 
185
/** Determines the "handler type" from a MIME type.
 
186
 * The handler type is a string, either "text", "image", "audio" or "binary".
 
187
 */
 
188
function get_handler_type(content_type)
 
189
{
 
190
    if (!content_type)
 
191
        return null;
 
192
    if (content_type in type_handlers)
 
193
        return type_handlers[content_type];
 
194
    else
 
195
    {   /* Based on the first part of the MIME type */
 
196
        var handler_type = content_type.split('/')[0];
 
197
        if (handler_type != "text" && handler_type != "image" &&
 
198
            handler_type != "audio")
 
199
            handler_type = "binary";
 
200
        return handler_type;
 
201
    }
 
202
}
 
203
 
 
204
/** Given an HTTP response object, cleans up and rebuilds the contents of the
 
205
 * page using the response data. This does not navigate away from the page, it
 
206
 * merely rebuilds most of the data.
 
207
 * Note that depending on the type of data returned, this could result in a
 
208
 * directory listing, an image preview, an editor pane, etc.
 
209
 * Figures out the type and calls the appropriate function.
 
210
 * \param path URL path which the request was made for. This can (among other
 
211
 * things) be used to update the URL in the location bar.
 
212
 * \param response XMLHttpRequest object returned by the server. Should
 
213
 * contain all the response data.
 
214
 * \param url_args Arguments dict, for the arguments passed to the URL
 
215
 * in the browser's address bar (will be forwarded along).
 
216
 */
 
217
function handle_response(path, response, url_args)
 
218
{
 
219
    /* TODO: Set location bar to "path" */
 
220
    current_path = path;
 
221
 
 
222
    /* Clear away the existing page contents */
 
223
    clearpage();
 
224
 
 
225
    /* Check the status, and if not 200, read the error and handle this as an
 
226
     * error. */
 
227
    if (response.status != 200)
 
228
    {
 
229
        var error = response.getResponseHeader("X-IVLE-Return-Error");
 
230
        if (error == null)
 
231
            error = response.statusText;
 
232
        handle_error(error);
 
233
        return;
 
234
    }
 
235
 
 
236
    /* This will always return a listing, whether it is a dir or a file.
 
237
     */
 
238
    var listing = response.responseText;
 
239
    /* The listing SHOULD be valid JSON text. Parse it into an object. */
 
240
    try
 
241
    {
 
242
        listing = JSON.parse(listing);
 
243
        file_listing = listing.listing;     /* Global */
 
244
    }
 
245
    catch (e)
 
246
    {
 
247
        handle_error("The server returned an invalid directory listing");
 
248
        return;
 
249
    }
 
250
    /* Get "." out, it's special */
 
251
    current_file = file_listing["."];     /* Global */
 
252
    delete file_listing["."];
 
253
 
 
254
    /* Check if this is a directory listing or file contents */
 
255
    var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
 
256
    if (isdir)
 
257
    {
 
258
        handle_dir_listing(path, listing);
 
259
    }
 
260
    else
 
261
    {
 
262
        /* Need to make a 2nd ajax call, this time get the actual file
 
263
         * contents */
 
264
        callback = function(response)
 
265
            {
 
266
                /* Read the response and set up the page accordingly */
 
267
                handle_contents_response(path, response);
 
268
            }
 
269
        /* Call the server and request the listing. */
 
270
        if (url_args)
 
271
            args = shallow_clone_object(url_args);
 
272
        else
 
273
            args = {};
 
274
        /* This time, get the contents of the file, not its metadata */
 
275
        args['return'] = "contents";
 
276
        ajax_call(callback, service_app, path, args, "GET");
 
277
    }
 
278
    update_actions(isdir);
 
279
}
 
280
 
 
281
function handle_contents_response(path, response)
 
282
{
 
283
    /* Treat this as an ordinary file. Get the file type. */
 
284
    var content_type = response.getResponseHeader("Content-Type");
 
285
    var handler_type = get_handler_type(content_type);
 
286
    would_be_handler_type = handler_type;
 
287
    /* handler_type should now be set to either
 
288
     * "text", "image", "audio" or "binary". */
 
289
    switch (handler_type)
 
290
    {
 
291
    case "text":
 
292
        handle_text(path, response.responseText,
 
293
            would_be_handler_type);
 
294
        break;
 
295
    case "image":
 
296
        /* TODO: Custom image handler */
 
297
        handle_binary(path, response.responseText);
 
298
        break;
 
299
    case "audio":
 
300
        /* TODO: Custom audio handler */
 
301
        handle_binary(path, response.responseText);
 
302
        break;
 
303
    case "binary":
 
304
        handle_binary(path);
 
305
        break;
 
306
    }
 
307
}
 
308
 
 
309
/* Called when a form upload comes back (from an iframe).
 
310
 * Refreshes the page.
 
311
 */
 
312
function upload_callback()
 
313
{
 
314
    /* This has a pretty nasty hack, which happens to work.
 
315
     * upload_callback is set as the "onload" callback for the iframe which
 
316
     * receives the response from the server for uploading a file.
 
317
     * This means it gets called twice. Once when initialising the iframe, and
 
318
     * a second time when the actual response comes back.
 
319
     * All we want to do is call navigate to refresh the page. But we CAN'T do
 
320
     * that on the first load or it will just go into an infinite cycle of
 
321
     * refreshing. We need to refresh the page ONLY on the second refresh.
 
322
     * upload_callback_count is reset to 0 just before the iframe is created.
 
323
     */
 
324
    upload_callback_count++;
 
325
    if (upload_callback_count >= 2)
 
326
        refresh();
 
327
}
 
328
 
 
329
/** Deletes all "dynamic" content on the page.
 
330
 * This returns the page back to the state it is in when the HTML arrives to
 
331
 * the browser, ready for another handler to populate it.
 
332
 */
 
333
function clearpage()
 
334
{
 
335
    dom_removechildren(document.getElementById("filesbody"));
 
336
}
 
337
 
 
338
/** Deletes all "dynamic" content on the page necessary to navigate from
 
339
 * one directory listing to another (does not clear as much as clearpage
 
340
 * does).
 
341
 * This is the equivalent of calling clearpage() then
 
342
 * setup_for_dir_listing(), assuming the page is already on a dir listing.
 
343
 */
 
344
function clearpage_dir()
 
345
{
 
346
    dom_removechildren(document.getElementById("path"));
 
347
    dom_removechildren(document.getElementById("files"));
 
348
    dom_removechildren(document.getElementById("sidepanel"));
 
349
}
 
350
 
 
351
/*** HANDLERS for different types of responses (such as dir listing, file,
 
352
 * etc). */
 
353
 
 
354
function handle_error(message)
 
355
{
 
356
    var files = document.getElementById("filesbody");
 
357
    var txt_elem = dom_make_text_elem("div", "Error: "
 
358
        + message.toString() + ".")
 
359
    txt_elem.setAttribute("class", "padding error");
 
360
    files.appendChild(txt_elem);
 
361
}
 
362
 
 
363
/** Given a mime type, returns the path to the icon.
 
364
 * \param type String, Mime type.
 
365
 * \param sizelarge Boolean, optional.
 
366
 * \return Path to the icon. Has applied make_path, so it is relative to site
 
367
 * root.
 
368
 */
 
369
function mime_type_to_icon(type, sizelarge)
 
370
{
 
371
    var filename;
 
372
    if (type in type_icons)
 
373
        filename = type_icons[type];
 
374
    else
 
375
        filename = default_type_icon;
 
376
    if (sizelarge)
 
377
        return make_path(path_join(type_icons_path_large, filename));
 
378
    else
 
379
        return make_path(path_join(type_icons_path, filename));
 
380
}
 
381
 
 
382
/** Given an svnstatus, returns the path to the icon.
 
383
 * \param type String, svn status.
 
384
 * \return Path to the icon. Has applied make_path, so it is relative to site
 
385
 * root. May return null to indicate no SVN icon.
 
386
 */
 
387
function svnstatus_to_icon(svnstatus)
 
388
{
 
389
    var filename;
 
390
    if (svnstatus in svn_icons)
 
391
        filename = svn_icons[svnstatus];
 
392
    else
 
393
        filename = default_svn_icon;
 
394
    if (filename == null) return null;
 
395
    return make_path(path_join(svn_icons_path, filename));
 
396
}
 
397
 
 
398
/** Given an svnstatus, returns the "nice" string.
 
399
 */
 
400
function svnstatus_to_string(svnstatus)
 
401
{
 
402
    if (svnstatus in svn_nice)
 
403
        return svn_nice[svnstatus];
 
404
    else
 
405
        return default_svn_nice;
 
406
}
 
407
 
 
408
/** Displays a download link to the binary file.
 
409
 */
 
410
function handle_binary(path)
 
411
{
 
412
    var files = document.getElementById("filesbody");
 
413
    var div = document.createElement("div");
 
414
    files.appendChild(div);
 
415
    div.setAttribute("class", "padding");
 
416
    var download_link = app_path(download_app, path);
 
417
    var par1 = dom_make_text_elem("p",
 
418
        "The file " + path + " is a binary file. To download this file, " +
 
419
        "click the following link:");
 
420
    var par2 = dom_make_link_elem("p",
 
421
        "Download " + path, "Download " + path, download_link);
 
422
    div.appendChild(par1);
 
423
    div.appendChild(par2);
 
424
}
 
425
 
 
426
function update_actions()
 
427
{
 
428
    var file;
 
429
    var numsel = selected_files.length;
 
430
    if (numsel <= 1)
 
431
    {
 
432
        if (numsel == 0)
 
433
        {
 
434
            /* Display information about the current directory instead */
 
435
            filename = path_basename(current_path);
 
436
            file = current_file;
 
437
        }
 
438
        else if (numsel == 1)
 
439
        {
 
440
            filename = selected_files[0];
 
441
            file = file_listing[filename];
 
442
        }
 
443
 
 
444
        /* Update each action node in the topbar.
 
445
         * This includes enabling/disabling actions as appropriate, and
 
446
         * setting href/onclick attributes. */
 
447
    }
 
448
 
 
449
    /* Open */
 
450
    /* Available if exactly one file is selected */
 
451
    var open = document.getElementById("act_open");
 
452
    if (numsel == 1)
 
453
    {
 
454
        open.setAttribute("class", "choice");
 
455
        if (file.isdir)
 
456
            open.setAttribute("title",
 
457
                "Navigate to this directory in the file browser");
 
458
        else
 
459
            open.setAttribute("title",
 
460
                "Edit or view this file");
 
461
        open.setAttribute("href", app_path(this_app, current_path, filename));
 
462
    }
 
463
    else
 
464
    {
 
465
        open.setAttribute("class", "disabled");
 
466
        open.removeAttribute("title");
 
467
        open.removeAttribute("href");
 
468
    }
 
469
 
 
470
    /* Serve */
 
471
    /* Available if exactly one file is selected,
 
472
     * and only if this is a file, not a directory */
 
473
    var serve = document.getElementById("act_serve");
 
474
    if (numsel == 1 && !file.isdir)
 
475
    {
 
476
        serve.setAttribute("class", "choice");
 
477
        serve.setAttribute("href",
 
478
            app_path(serve_app, current_path, filename));
 
479
    }
 
480
    else
 
481
    {
 
482
        serve.setAttribute("class", "disabled");
 
483
        serve.removeAttribute("href");
 
484
    }
 
485
 
 
486
    /* Run */
 
487
    /* Available if exactly one file is selected,
 
488
     * and it is a Python file.
 
489
     */
 
490
    var run = document.getElementById("act_run");
 
491
     
 
492
    if (numsel == 0 && !file.isdir && file.type == "text/x-python")
 
493
    {
 
494
        // In the edit window
 
495
        run.setAttribute("class", "choice");
 
496
        localpath = app_path('home',current_path);
 
497
        run.setAttribute("onclick", "runfile('" + localpath + "')");
 
498
    }
 
499
    else if (numsel == 1 && !file.isdir && file.type == "text/x-python")
 
500
    {
 
501
        // In the browser window
 
502
        run.setAttribute("class", "choice");
 
503
        localpath = app_path('home',current_path,filename);
 
504
        run.setAttribute("onclick", "runfile('" + localpath + "')");
 
505
    }
 
506
    else
 
507
    {
 
508
        run.setAttribute("class", "disabled");
 
509
        run.removeAttribute("onclick");
 
510
    }
 
511
 
 
512
    /* Download */
 
513
    /* Always available.
 
514
     * If 0 files selected, download the current file or directory as a ZIP.
 
515
     * If 1 directory selected, download it as a ZIP.
 
516
     * If 1 non-directory selected, download it.
 
517
     * If >1 files selected, download them all as a ZIP.
 
518
     */
 
519
    var download = document.getElementById("act_download");
 
520
    if (numsel <= 1)
 
521
    {
 
522
        if (numsel == 0)
 
523
        {
 
524
            download.setAttribute("href",
 
525
                app_path(download_app, current_path));
 
526
            if (file.isdir)
 
527
                download.setAttribute("title",
 
528
                    "Download the current directory as a ZIP file");
 
529
            else
 
530
                download.setAttribute("title",
 
531
                    "Download the current file");
 
532
        }
 
533
        else
 
534
        {
 
535
            download.setAttribute("href",
 
536
                app_path(download_app, current_path, filename));
 
537
            if (file.isdir)
 
538
                download.setAttribute("title",
 
539
                    "Download the selected directory as a ZIP file");
 
540
            else
 
541
                download.setAttribute("title",
 
542
                    "Download the selected file");
 
543
        }
 
544
    }
 
545
    else
 
546
    {
 
547
        /* Make a query string with all the files to download */
 
548
        var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
 
549
        for (var i=0; i<numsel; i++)
 
550
            dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
 
551
        dlpath = dlpath.substr(0, dlpath.length-1);
 
552
        download.setAttribute("href", dlpath);
 
553
        download.setAttribute("title",
 
554
            "Download the selected files as a ZIP file");
 
555
    }
 
556
 
 
557
    /* Refresh - No changes required */
 
558
 
 
559
    /* Publish and Submit */
 
560
    /* If this directory is under subversion and selected/unselected file is a
 
561
     * directory. */
 
562
    var publish = document.getElementById("act_publish");
 
563
    var submit = document.getElementById("act_submit");
 
564
    if (numsel <= 1 && file.isdir)
 
565
    {
 
566
        /* TODO: Work out of file is svn'd */
 
567
        publish.setAttribute("class", "choice");
 
568
        publish.removeAttribute("disabled");
 
569
        /* If this dir is already published, call it "Unpublish" */
 
570
        if (file.published)
 
571
        {
 
572
            publish.setAttribute("value", "unpublish");
 
573
            publish.setAttribute("title" ,"Make it so this directory "
 
574
                + "can not be seen by anyone on the web");
 
575
            publish.textContent = "Unpublish";
 
576
        } else {
 
577
            publish.setAttribute("value", "publish");
 
578
            publish.setAttribute("title","Make it so this directory "
 
579
                + "can be seen by anyone on the web");
 
580
            publish.textContent = "Publish";
 
581
        }
 
582
        submit.setAttribute("class", "choice");
 
583
        submit.removeAttribute("disabled");
 
584
    }
 
585
    else
 
586
    {
 
587
        publish.setAttribute("class", "disabled");
 
588
        publish.setAttribute("disabled", "disabled");
 
589
        submit.setAttribute("class", "disabled");
 
590
        submit.setAttribute("disabled", "disabled");
 
591
    }
 
592
 
 
593
    /* Share */
 
594
    /* If exactly 1 non-directory file is selected/opened, and its parent
 
595
     * directory is published.
 
596
     */
 
597
    var share = document.getElementById("act_share");
 
598
    if (numsel <= 1 && !file.isdir)
 
599
    {
 
600
        /* Work out if parent dir is published */
 
601
        parentdir = current_file;
 
602
        if (parentdir.published)
 
603
        {
 
604
            share.setAttribute("class", "choice");
 
605
            share.removeAttribute("disabled");
 
606
        } else {
 
607
            share.setAttribute("class", "disabled");
 
608
            share.setAttribute("disabled", "disabled");
 
609
        }
 
610
    }
 
611
    else
 
612
    {
 
613
        share.setAttribute("class", "disabled");
 
614
        share.setAttribute("disabled", "disabled");
 
615
    }
 
616
 
 
617
    /* Rename */
 
618
    /* If exactly 1 file is selected */
 
619
    var rename = document.getElementById("act_rename");
 
620
    if (numsel == 1)
 
621
    {
 
622
        rename.setAttribute("class", "choice");
 
623
        rename.removeAttribute("disabled");
 
624
    }
 
625
    else
 
626
    {
 
627
        rename.setAttribute("class", "disabled");
 
628
        rename.setAttribute("disabled", "disabled");
 
629
    }
 
630
 
 
631
    /* Delete, cut, copy */
 
632
    /* If >= 1 file is selected */
 
633
    var act_delete = document.getElementById("act_delete");
 
634
    var cut = document.getElementById("act_cut");
 
635
    var copy = document.getElementById("act_copy");
 
636
    if (numsel >= 1)
 
637
    {
 
638
        act_delete.setAttribute("class", "choice");
 
639
        act_delete.removeAttribute("disabled");
 
640
        cut.setAttribute("class", "choice");
 
641
        cut.removeAttribute("disabled");
 
642
        copy.setAttribute("class", "choice");
 
643
        copy.removeAttribute("disabled");
 
644
    }
 
645
    else
 
646
    {
 
647
        act_delete.setAttribute("class", "disabled");
 
648
        act_delete.setAttribute("disabled", "disabled");
 
649
        cut.setAttribute("class", "disabled");
 
650
        cut.setAttribute("disabled", "disabled");
 
651
        copy.setAttribute("class", "disabled");
 
652
        copy.setAttribute("disabled", "disabled");
 
653
    }
 
654
 
 
655
    /* Paste, new file, new directory, upload */
 
656
    /* Always enabled (assuming this is a directory) */
 
657
 
 
658
    /* Subversion actions */
 
659
    /* TODO: Work out when these are appropriate */
 
660
    var svnadd = document.getElementById("act_svnadd");
 
661
    var svnrevert = document.getElementById("act_svnrevert");
 
662
    var svncommit = document.getElementById("act_svncommit");
 
663
    if (true)
 
664
    {
 
665
        svnadd.setAttribute("class", "choice");
 
666
        svnadd.removeAttribute("disabled");
 
667
        svnrevert.setAttribute("class", "choice");
 
668
        svnrevert.removeAttribute("disabled");
 
669
        svncommit.setAttribute("class", "choice");
 
670
        svncommit.removeAttribute("disabled");
 
671
    }
 
672
    var svncheckout = document.getElementById("act_svncheckout");
 
673
    /* current_path == username: We are at the top level */
 
674
    if (current_path == username)
 
675
    {
 
676
        svncheckout.setAttribute("class", "choice");
 
677
        svncheckout.removeAttribute("disabled");
 
678
    }
 
679
    else
 
680
    {
 
681
        svncheckout.setAttribute("class", "disabled");
 
682
        svncheckout.setAttribute("disabled", "disabled");
 
683
    }
 
684
 
 
685
    return;
 
686
}
 
687
 
 
688
/** Event handler for when an item of the "More actions..." dropdown box is
 
689
 * selected. Performs the selected action. */
 
690
function handle_moreactions()
 
691
{
 
692
    var moreactions = document.getElementById("moreactions");
 
693
    if (moreactions.value == "top")
 
694
        return;
 
695
    var selectedaction = moreactions.value;
 
696
    /* Reset to "More actions..." */
 
697
    moreactions.selectedIndex = 0;
 
698
 
 
699
    /* If 0 files selected, filename is the name of the current dir.
 
700
     * If 1 file selected, filename is that file.
 
701
     */
 
702
    if (selected_files.length == 0)
 
703
        filename = path_basename(current_path);
 
704
    else if (selected_files.length == 1)
 
705
        filename = selected_files[0];
 
706
    else
 
707
        filename = null;
 
708
 
 
709
    /* Now handle the selected action */
 
710
    switch(selectedaction)
 
711
    {
 
712
    case "publish":
 
713
        action_publish(selected_files);
 
714
        break;
 
715
    case "unpublish":
 
716
        action_unpublish(selected_files);
 
717
        break;
 
718
    case "share":
 
719
        //alert("Not yet implemented: Sharing files");
 
720
        window.open(public_app_path(serve_app, current_path, filename), 'share')
 
721
        break;
 
722
    case "submit":
 
723
        // TODO
 
724
        alert("Not yet implemented: Submit");
 
725
        break;
 
726
    case "rename":
 
727
        action_rename(filename);
 
728
        break;
 
729
    case "delete":
 
730
        action_remove(selected_files);
 
731
        break;
 
732
    case "copy":
 
733
        action_copy(selected_files);
 
734
        break;
 
735
    case "cut":
 
736
        action_cut(selected_files);
 
737
        break;
 
738
    case "paste":
 
739
        action_paste();
 
740
        break;
 
741
    case "newfile":
 
742
        action_newfile();
 
743
        break;
 
744
    case "mkdir":
 
745
        action_mkdir();
 
746
        break;
 
747
    case "upload":
 
748
        show_uploadpanel(true);
 
749
        break;
 
750
    case "svnadd":
 
751
        action_add(selected_files);
 
752
        break;
 
753
    case "svnrevert":
 
754
        action_revert(selected_files);
 
755
        break;
 
756
    case "svncommit":
 
757
        action_commit(selected_files);
 
758
        break;
 
759
    case "svncheckout":
 
760
        action_checkout();
 
761
        break;
 
762
    }
 
763
}
 
764
 
 
765
/** User clicks "Run" button.
 
766
 * Do an Ajax call and print the test output.
 
767
 */
 
768
function runfile(localpath)
 
769
{
 
770
    /* Dump the entire file to the console */
 
771
    var callback = function()
 
772
    {
 
773
        console_enter_line("execfile('" + localpath + "')", "block");
 
774
    }
 
775
    start_server(callback)
 
776
    return;
 
777
}
 
778
 
 
779
/** Called when the page loads initially.
 
780
 */
 
781
window.onload = function()
 
782
{
 
783
    /* Navigate (internally) to the path in the URL bar.
 
784
     * This causes the page to be populated with whatever is at that address,
 
785
     * whether it be a directory or a file.
 
786
     */
 
787
    var path = parse_url(window.location.href).path;
 
788
    /* Strip out root_dir + "/files" from the front of the path */
 
789
    var strip = make_path(this_app);
 
790
    if (path.substr(0, strip.length) == strip)
 
791
        path = path.substr(strip.length+1);
 
792
    else
 
793
    {
 
794
        /* See if this is an edit path */
 
795
        strip = make_path(edit_app);
 
796
        if (path.substr(0, strip.length) == strip)
 
797
        {
 
798
            path = path.substr(strip.length+1);
 
799
        }
 
800
    }
 
801
 
 
802
    if (path.length == 0)
 
803
    {
 
804
        /* Navigate to the user's home directory by default */
 
805
        /* TEMP? */
 
806
        path = username;
 
807
    }
 
808
 
 
809
    navigate(path);
 
810
 
 
811
    /* Set up the console plugin to display as a popup window */
 
812
    console_init(true);
 
813
}