~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 12:31:27 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:780
dispatch/login.py: When displaying the login page, returns a 200, not a 403
    Forbidden.
    This is because "Forbidden" has the wrong semantic - it means "you will
    never get in here even if you auth", whereas we want to present the
    opportunity to auth.
    Some user agents interpret a 403 as defeat, and don't even present the
    page body.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
    "application/x-javascript" : "text",
44
44
    "application/javascript" : "text",
45
45
    "application/json" : "text",
46
 
    "application/xml" : "text",
 
46
    "application/xml" : "text"
47
47
};
48
48
 
49
49
/* Mapping MIME types to icons, just the file's basename */
50
50
type_icons = {
51
51
    "text/directory": "dir.png",
52
 
    "text/x-python": "py.png",
 
52
    "text/x-python": "py.png"
53
53
};
54
54
 
55
55
default_type_icon = "txt.png";
66
66
    "missing": "missing.png",
67
67
    "deleted": "deleted.png",
68
68
    "modified": "modified.png",
 
69
    "revision": "revision.png"
69
70
};
70
71
 
71
72
/* Mapping SVN status to "nice" strings */
79
80
    "modified": "Permanent file (modified)",
80
81
    "merged": "Permanent file (merged)",
81
82
    "conflicted": "Permanent file (conflicted)",
 
83
    "revision": "Past Permanent file (revision)"
82
84
};
83
85
 
84
86
default_svn_icon = null;
93
95
 * application can interpret them.
94
96
 */
95
97
types_exec = [
96
 
    "text/x-python",
 
98
    "text/x-python"
97
99
];
98
100
 
99
101
 
100
102
/* Global variables */
101
103
 
 
104
/** The listing object returned by the server as JSON */
 
105
file_listing = null;
 
106
current_file = null;
102
107
current_path = "";
103
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
 
104
117
/** Calls the server using Ajax, performing an action on the server side.
105
118
 * Receives the response from the server and performs a refresh of the page
106
119
 * contents, updating it to display the returned data (such as a directory
123
136
function do_action(action, path, args, content_type, ignore_response)
124
137
{
125
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
        }
126
151
    /* Call the server and perform the action. This mutates the server. */
127
 
    response = ajax_call(service_app, path, args, "POST", content_type);
128
 
    /* Check for action errors reported by the server, and report them to the
129
 
     * user */
130
 
    error = response.getResponseHeader("X-IVLE-Action-Error");
131
 
    if (error != null)
132
 
        alert("Error: " + error.toString() + ".");
133
 
    /* Now read the response and set up the page accordingly */
134
 
    if (ignore_response != true)
135
 
        handle_response(path, response);
 
152
    ajax_call(callback, service_app, path, args, "POST", content_type);
136
153
}
137
154
 
138
155
/** Calls the server using Ajax, requesting a directory listing. This should
142
159
 * Called "navigate", can also be used for a simple refresh.
143
160
 * Always makes a GET request.
144
161
 * No return value.
145
 
 * \param editmode Optional boolean. If true, then the user navigated here
146
 
 * with an "edit" URL so we should favour using the editor.
147
 
 */
148
 
function navigate(path, editmode)
149
 
{
150
 
    /* Call the server and request the listing. This mutates the server. */
151
 
    response = ajax_call(service_app, path, null, "GET");
152
 
    /* Now read the response and set up the page accordingly */
153
 
    handle_response(path, response, editmode);
 
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);
154
183
}
155
184
 
156
185
/** Determines the "handler type" from a MIME type.
182
211
 * things) be used to update the URL in the location bar.
183
212
 * \param response XMLHttpRequest object returned by the server. Should
184
213
 * contain all the response data.
185
 
 * \param editmode Optional boolean. If true, then the user navigated here
186
 
 * with an "edit" URL so we should favour using the editor.
 
214
 * \param url_args Arguments dict, for the arguments passed to the URL
 
215
 * in the browser's address bar (will be forwarded along).
187
216
 */
188
 
function handle_response(path, response, editmode)
 
217
function handle_response(path, response, url_args)
189
218
{
190
219
    /* TODO: Set location bar to "path" */
191
220
    current_path = path;
192
221
 
193
222
    /* Clear away the existing page contents */
194
223
    clearpage();
195
 
    /* Display the path at the top, for navigation */
196
 
    presentpath(path);
197
224
 
198
225
    /* Check the status, and if not 200, read the error and handle this as an
199
226
     * error. */
206
233
        return;
207
234
    }
208
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
 
209
254
    /* Check if this is a directory listing or file contents */
210
255
    var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
211
 
    if (!editmode && isdir)
 
256
    if (isdir)
212
257
    {
213
 
        var listing = response.responseText;
214
 
        /* The listing SHOULD be valid JSON text. Parse it into an object. */
215
 
        try
216
 
        {
217
 
            listing = JSON.parse(listing);
218
 
        }
219
 
        catch (e)
220
 
        {
221
 
            handle_error("The server returned an invalid directory listing");
222
 
            return;
223
 
        }
224
258
        handle_dir_listing(path, listing);
225
259
    }
226
260
    else
227
261
    {
228
 
        /* Treat this as an ordinary file. Get the file type. */
229
 
        var content_type = response.getResponseHeader("Content-Type");
230
 
        var handler_type = get_handler_type(content_type);
231
 
        /* If we're in "edit mode", always treat this file as text */
232
 
        would_be_handler_type = handler_type;
233
 
        if (editmode) handler_type = "text";
234
 
        /* handler_type should now be set to either
235
 
         * "text", "image", "audio" or "binary". */
236
 
        switch (handler_type)
237
 
        {
238
 
        case "text":
239
 
            if (isdir)
240
 
            {
241
 
                handle_text(path_join(path, "untitled"), "",
242
 
                    would_be_handler_type);
243
 
            }
244
 
            else
245
 
            {
246
 
                handle_text(path, response.responseText,
247
 
                    would_be_handler_type);
248
 
            }
249
 
            break;
250
 
        case "image":
251
 
            /* TODO: Custom image handler */
252
 
            handle_binary(path, response.responseText);
253
 
            break;
254
 
        case "audio":
255
 
            /* TODO: Custom audio handler */
256
 
            handle_binary(path, response.responseText);
257
 
            break;
258
 
        case "binary":
259
 
            handle_binary(path);
260
 
            break;
261
 
        }
262
 
    }
 
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();
263
327
}
264
328
 
265
329
/** Deletes all "dynamic" content on the page.
268
332
 */
269
333
function clearpage()
270
334
{
271
 
    dom_removechildren(document.getElementById("path"));
272
335
    dom_removechildren(document.getElementById("filesbody"));
273
336
}
274
337
 
285
348
    dom_removechildren(document.getElementById("sidepanel"));
286
349
}
287
350
 
288
 
/** Sets the mode to either "file browser" or "text editor" mode.
289
 
 * This modifies the window icon, and selected tab.
290
 
 * \param editmode If True, editor mode. Else, file browser mode.
291
 
 */
292
 
function setmode(editmode)
293
 
{
294
 
    /* Find the DOM elements for the file browser and editor tabs */
295
 
    var tabs = document.getElementById("apptabs");
296
 
    var tab_files = null;
297
 
    var tab_edit = null;
298
 
    var a;
299
 
    var href;
300
 
    for (var i=0; i<tabs.childNodes.length; i++)
301
 
    {
302
 
        /* Find the href of the link within */
303
 
        if (!tabs.childNodes[i].getElementsByTagName) continue;
304
 
        a = tabs.childNodes[i].getElementsByTagName("a");
305
 
        if (a.length == 0) continue;
306
 
        href = a[0].getAttribute("href");
307
 
        if (href == null) continue;
308
 
        if (endswith(href, this_app))
309
 
            tab_files = tabs.childNodes[i];
310
 
        else if (endswith(href, edit_app))
311
 
            tab_edit = tabs.childNodes[i];
312
 
    }
313
 
 
314
 
    if (editmode)
315
 
    {
316
 
        tab_files.removeAttribute("class");
317
 
        tab_edit.setAttribute("class", "thisapp");
318
 
    }
319
 
    else
320
 
    {
321
 
        tab_edit.removeAttribute("class");
322
 
        tab_files.setAttribute("class", "thisapp");
323
 
    }
324
 
}
325
 
 
326
351
/*** HANDLERS for different types of responses (such as dir listing, file,
327
352
 * etc). */
328
353
 
329
354
function handle_error(message)
330
355
{
331
 
    setmode(false);
332
356
    var files = document.getElementById("filesbody");
333
357
    var txt_elem = dom_make_text_elem("div", "Error: "
334
358
        + message.toString() + ".")
336
360
    files.appendChild(txt_elem);
337
361
}
338
362
 
339
 
/** Presents a path list (address bar inside the page) for clicking.
340
 
 */
341
 
function presentpath(path)
342
 
{
343
 
    var dom_path = document.getElementById("path");
344
 
    var href_path = make_path(this_app);
345
 
    var nav_path = "";
346
 
    var dir;
347
 
 
348
 
    /* Also set the document title */
349
 
    document.title = path_basename(path) + " - IVLE";
350
 
    /* Create all of the paths */
351
 
    var pathlist = path.split("/");
352
 
    for (var i=0; i<pathlist.length; i++)
353
 
    {
354
 
        dir = pathlist[i];
355
 
        if (dir == "") continue;
356
 
        /* Make an 'a' element */
357
 
        href_path = path_join(href_path, dir);
358
 
        nav_path = path_join(nav_path, dir);
359
 
        var link = dom_make_link_elem("a", dir, "Navigate to " + nav_path,
360
 
                href_path/*, "navigate(" + repr(href_path) + ")"*/);
361
 
        dom_path.appendChild(link);
362
 
        dom_path.appendChild(document.createTextNode("/"));
363
 
    }
364
 
    dom_path.removeChild(dom_path.lastChild);
365
 
}
366
 
 
367
363
/** Given a mime type, returns the path to the icon.
368
364
 * \param type String, Mime type.
369
365
 * \param sizelarge Boolean, optional.
413
409
 */
414
410
function handle_binary(path)
415
411
{
416
 
    setmode(false);
417
412
    var files = document.getElementById("filesbody");
418
413
    var div = document.createElement("div");
419
414
    files.appendChild(div);
428
423
    div.appendChild(par2);
429
424
}
430
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 zero or one files are 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
        if (numsel == 0)
 
478
            serve.setAttribute("href",
 
479
                app_path(serve_app, current_path));
 
480
        else
 
481
            serve.setAttribute("href",
 
482
                app_path(serve_app, current_path, filename));
 
483
    }
 
484
    else
 
485
    {
 
486
        serve.setAttribute("class", "disabled");
 
487
        serve.removeAttribute("href");
 
488
    }
 
489
 
 
490
    /* Run */
 
491
    /* Available if exactly one file is selected,
 
492
     * and it is a Python file.
 
493
     */
 
494
    var run = document.getElementById("act_run");
 
495
     
 
496
    if (numsel == 0 && !file.isdir && file.type == "text/x-python")
 
497
    {
 
498
        // In the edit window
 
499
        run.setAttribute("class", "choice");
 
500
        localpath = app_path('home',current_path);
 
501
        run.setAttribute("onclick", "runfile('" + localpath + "')");
 
502
    }
 
503
    else if (numsel == 1 && !file.isdir && file.type == "text/x-python")
 
504
    {
 
505
        // In the browser window
 
506
        run.setAttribute("class", "choice");
 
507
        localpath = app_path('home',current_path,filename);
 
508
        run.setAttribute("onclick", "runfile('" + localpath + "')");
 
509
    }
 
510
    else
 
511
    {
 
512
        run.setAttribute("class", "disabled");
 
513
        run.removeAttribute("onclick");
 
514
    }
 
515
 
 
516
    /* Download */
 
517
    /* Always available.
 
518
     * If 0 files selected, download the current file or directory as a ZIP.
 
519
     * If 1 directory selected, download it as a ZIP.
 
520
     * If 1 non-directory selected, download it.
 
521
     * If >1 files selected, download them all as a ZIP.
 
522
     */
 
523
    var download = document.getElementById("act_download");
 
524
    if (numsel <= 1)
 
525
    {
 
526
        if (numsel == 0)
 
527
        {
 
528
            download.setAttribute("href",
 
529
                app_path(download_app, current_path));
 
530
            if (file.isdir)
 
531
                download.setAttribute("title",
 
532
                    "Download the current directory as a ZIP file");
 
533
            else
 
534
                download.setAttribute("title",
 
535
                    "Download the current file");
 
536
        }
 
537
        else
 
538
        {
 
539
            download.setAttribute("href",
 
540
                app_path(download_app, current_path, filename));
 
541
            if (file.isdir)
 
542
                download.setAttribute("title",
 
543
                    "Download the selected directory as a ZIP file");
 
544
            else
 
545
                download.setAttribute("title",
 
546
                    "Download the selected file");
 
547
        }
 
548
    }
 
549
    else
 
550
    {
 
551
        /* Make a query string with all the files to download */
 
552
        var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
 
553
        for (var i=0; i<numsel; i++)
 
554
            dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
 
555
        dlpath = dlpath.substr(0, dlpath.length-1);
 
556
        download.setAttribute("href", dlpath);
 
557
        download.setAttribute("title",
 
558
            "Download the selected files as a ZIP file");
 
559
    }
 
560
 
 
561
    /* Refresh - No changes required */
 
562
 
 
563
    /* Publish and Submit */
 
564
    /* If this directory is under subversion and selected/unselected file is a
 
565
     * directory. */
 
566
    var publish = document.getElementById("act_publish");
 
567
    var submit = document.getElementById("act_submit");
 
568
    if (numsel <= 1 && file.isdir)
 
569
    {
 
570
        /* TODO: Work out of file is svn'd */
 
571
        publish.setAttribute("class", "choice");
 
572
        publish.removeAttribute("disabled");
 
573
        /* If this dir is already published, call it "Unpublish" */
 
574
        if (file.published)
 
575
        {
 
576
            publish.setAttribute("value", "unpublish");
 
577
            publish.setAttribute("title" ,"Make it so this directory "
 
578
                + "can not be seen by anyone on the web");
 
579
            publish.textContent = "Unpublish";
 
580
        } else {
 
581
            publish.setAttribute("value", "publish");
 
582
            publish.setAttribute("title","Make it so this directory "
 
583
                + "can be seen by anyone on the web");
 
584
            publish.textContent = "Publish";
 
585
        }
 
586
        submit.setAttribute("class", "choice");
 
587
        submit.removeAttribute("disabled");
 
588
    }
 
589
    else
 
590
    {
 
591
        publish.setAttribute("class", "disabled");
 
592
        publish.setAttribute("disabled", "disabled");
 
593
        submit.setAttribute("class", "disabled");
 
594
        submit.setAttribute("disabled", "disabled");
 
595
    }
 
596
 
 
597
    /* Share */
 
598
    /* If exactly 1 non-directory file is selected/opened, and its parent
 
599
     * directory is published.
 
600
     */
 
601
    var share = document.getElementById("act_share");
 
602
    if (numsel <= 1 && !file.isdir)
 
603
    {
 
604
        /* Work out if parent dir is published */
 
605
        parentdir = current_file;
 
606
        if (parentdir.published)
 
607
        {
 
608
            share.setAttribute("class", "choice");
 
609
            share.removeAttribute("disabled");
 
610
        } else {
 
611
            share.setAttribute("class", "disabled");
 
612
            share.setAttribute("disabled", "disabled");
 
613
        }
 
614
    }
 
615
    else
 
616
    {
 
617
        share.setAttribute("class", "disabled");
 
618
        share.setAttribute("disabled", "disabled");
 
619
    }
 
620
 
 
621
    /* Rename */
 
622
    /* If exactly 1 file is selected */
 
623
    var rename = document.getElementById("act_rename");
 
624
    if (numsel == 1)
 
625
    {
 
626
        rename.setAttribute("class", "choice");
 
627
        rename.removeAttribute("disabled");
 
628
    }
 
629
    else
 
630
    {
 
631
        rename.setAttribute("class", "disabled");
 
632
        rename.setAttribute("disabled", "disabled");
 
633
    }
 
634
 
 
635
    /* Delete, cut, copy */
 
636
    /* If >= 1 file is selected */
 
637
    var act_delete = document.getElementById("act_delete");
 
638
    var cut = document.getElementById("act_cut");
 
639
    var copy = document.getElementById("act_copy");
 
640
    if (numsel >= 1)
 
641
    {
 
642
        act_delete.setAttribute("class", "choice");
 
643
        act_delete.removeAttribute("disabled");
 
644
        cut.setAttribute("class", "choice");
 
645
        cut.removeAttribute("disabled");
 
646
        copy.setAttribute("class", "choice");
 
647
        copy.removeAttribute("disabled");
 
648
    }
 
649
    else
 
650
    {
 
651
        act_delete.setAttribute("class", "disabled");
 
652
        act_delete.setAttribute("disabled", "disabled");
 
653
        cut.setAttribute("class", "disabled");
 
654
        cut.setAttribute("disabled", "disabled");
 
655
        copy.setAttribute("class", "disabled");
 
656
        copy.setAttribute("disabled", "disabled");
 
657
    }
 
658
 
 
659
    /* Paste, new file, new directory, upload */
 
660
    /* Disable if the current file is not a directory */
 
661
    if (!current_file.isdir)
 
662
    {
 
663
        var paste = document.getElementById("act_paste");
 
664
        var newfile = document.getElementById("act_newfile");
 
665
        var mkdir = document.getElementById("act_mkdir");
 
666
        var upload = document.getElementById("act_upload");
 
667
        paste.setAttribute("class", "disabled");
 
668
        paste.setAttribute("disabled", "disabled");
 
669
        newfile.setAttribute("class", "disabled");
 
670
        newfile.setAttribute("disabled", "disabled");
 
671
        mkdir.setAttribute("class", "disabled");
 
672
        mkdir.setAttribute("disabled", "disabled");
 
673
        upload.setAttribute("class", "disabled");
 
674
        upload.setAttribute("disabled", "disabled");
 
675
    }
 
676
 
 
677
    /* Subversion actions */
 
678
    /* TODO: Work out when these are appropriate */
 
679
    var svnadd = document.getElementById("act_svnadd");
 
680
    var svnrevert = document.getElementById("act_svnrevert");
 
681
    var svncommit = document.getElementById("act_svncommit");
 
682
    if (true)
 
683
    {
 
684
        svnadd.setAttribute("class", "choice");
 
685
        svnadd.removeAttribute("disabled");
 
686
        svnrevert.setAttribute("class", "choice");
 
687
        svnrevert.removeAttribute("disabled");
 
688
        svncommit.setAttribute("class", "choice");
 
689
        svncommit.removeAttribute("disabled");
 
690
    }
 
691
    var svncheckout = document.getElementById("act_svncheckout");
 
692
    /* current_path == username: We are at the top level */
 
693
    if (current_path == username)
 
694
    {
 
695
        svncheckout.setAttribute("class", "choice");
 
696
        svncheckout.removeAttribute("disabled");
 
697
    }
 
698
    else
 
699
    {
 
700
        svncheckout.setAttribute("class", "disabled");
 
701
        svncheckout.setAttribute("disabled", "disabled");
 
702
    }
 
703
 
 
704
    /* There is currently nothing on the More Actions menu of use
 
705
     * when the current file is not a directory. Hence, just remove
 
706
     * it entirely.
 
707
     * (This makes some of the above decisions somewhat redundant).
 
708
     */
 
709
    if (!(current_file.isdir))
 
710
    {
 
711
        var moreactions = document.getElementById("moreactions_area");
 
712
        moreactions.setAttribute("style", "display: none;");
 
713
    }
 
714
 
 
715
    return;
 
716
}
 
717
 
 
718
/** Event handler for when an item of the "More actions..." dropdown box is
 
719
 * selected. Performs the selected action. */
 
720
function handle_moreactions()
 
721
{
 
722
    var moreactions = document.getElementById("moreactions");
 
723
    if (moreactions.value == "top")
 
724
        return;
 
725
    var selectedaction = moreactions.value;
 
726
    /* Reset to "More actions..." */
 
727
    moreactions.selectedIndex = 0;
 
728
 
 
729
    /* If 0 files selected, filename is the name of the current dir.
 
730
     * If 1 file selected, filename is that file.
 
731
     */
 
732
    if (selected_files.length == 0)
 
733
        filename = path_basename(current_path);
 
734
    else if (selected_files.length == 1)
 
735
        filename = selected_files[0];
 
736
    else
 
737
        filename = null;
 
738
 
 
739
    /* Now handle the selected action */
 
740
    switch(selectedaction)
 
741
    {
 
742
    case "publish":
 
743
        action_publish(selected_files);
 
744
        break;
 
745
    case "unpublish":
 
746
        action_unpublish(selected_files);
 
747
        break;
 
748
    case "share":
 
749
        //alert("Not yet implemented: Sharing files");
 
750
        window.open(public_app_path(serve_app, current_path, filename), 'share')
 
751
        break;
 
752
    case "submit":
 
753
        // TODO
 
754
        alert("Not yet implemented: Submit");
 
755
        break;
 
756
    case "rename":
 
757
        action_rename(filename);
 
758
        break;
 
759
    case "delete":
 
760
        action_remove(selected_files);
 
761
        break;
 
762
    case "copy":
 
763
        action_copy(selected_files);
 
764
        break;
 
765
    case "cut":
 
766
        action_cut(selected_files);
 
767
        break;
 
768
    case "paste":
 
769
        action_paste();
 
770
        break;
 
771
    case "newfile":
 
772
        action_newfile();
 
773
        break;
 
774
    case "mkdir":
 
775
        action_mkdir();
 
776
        break;
 
777
    case "upload":
 
778
        show_uploadpanel(true);
 
779
        break;
 
780
    case "svnadd":
 
781
        action_add(selected_files);
 
782
        break;
 
783
    case "svnrevert":
 
784
        action_revert(selected_files);
 
785
        break;
 
786
    case "svncommit":
 
787
        action_commit(selected_files);
 
788
        break;
 
789
    case "svncheckout":
 
790
        action_checkout();
 
791
        break;
 
792
    }
 
793
}
 
794
 
 
795
/** User clicks "Run" button.
 
796
 * Do an Ajax call and print the test output.
 
797
 */
 
798
function runfile(localpath)
 
799
{
 
800
    /* Dump the entire file to the console */
 
801
    var callback = function()
 
802
    {
 
803
        console_enter_line("execfile('" + localpath + "')", "block");
 
804
    }
 
805
    start_server(callback)
 
806
    return;
 
807
}
 
808
 
431
809
/** Called when the page loads initially.
432
810
 */
433
811
window.onload = function()
439
817
    var path = parse_url(window.location.href).path;
440
818
    /* Strip out root_dir + "/files" from the front of the path */
441
819
    var strip = make_path(this_app);
442
 
    var editmode = false;
443
820
    if (path.substr(0, strip.length) == strip)
444
821
        path = path.substr(strip.length+1);
445
822
    else
449
826
        if (path.substr(0, strip.length) == strip)
450
827
        {
451
828
            path = path.substr(strip.length+1);
452
 
            editmode = true;
453
829
        }
454
830
    }
455
831
 
460
836
        path = username;
461
837
    }
462
838
 
463
 
    navigate(path, editmode);
 
839
    navigate(path);
 
840
 
 
841
    /* Set up the console plugin to display as a popup window */
 
842
    console_init(true);
464
843
}