~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-02-29 01:18:22 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:621
Added 2 new apps: home and subjects. Both fairly incomplete, just a basic
    skeleton.
    "home" is the new default app, replacing "files".
    (It has a link to files).

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
    "missing": "missing.png",
67
67
    "deleted": "deleted.png",
68
68
    "modified": "modified.png",
69
 
    "conflicted": "conflicted.png",
70
69
    "revision": "revision.png"
71
70
};
72
71
 
105
104
/** The listing object returned by the server as JSON */
106
105
file_listing = null;
107
106
current_file = null;
108
 
current_revision = null;
109
107
current_path = "";
110
108
 
111
109
/** Filenames of all files selected
114
112
 */
115
113
selected_files = [];
116
114
 
117
 
upload_callback_count = 0;      /* See upload_callback */
118
 
 
119
115
/** Calls the server using Ajax, performing an action on the server side.
120
116
 * Receives the response from the server and performs a refresh of the page
121
117
 * contents, updating it to display the returned data (such as a directory
145
141
             * to the user */
146
142
            var error = response.getResponseHeader("X-IVLE-Action-Error");
147
143
            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()) + ".");
 
144
                alert("Error: " + error.toString() + ".");
151
145
            /* Now read the response and set up the page accordingly */
152
146
            if (ignore_response != true)
153
 
                handle_response(path, response, true);
 
147
                handle_response(path, response);
154
148
        }
155
149
    /* Call the server and perform the action. This mutates the server. */
156
150
    ajax_call(callback, service_app, path, args, "POST", content_type);
169
163
    callback = function(response)
170
164
        {
171
165
            /* Read the response and set up the page accordingly */
172
 
            handle_response(path, response, false, url.args);
 
166
            handle_response(path, response, url.args);
173
167
        }
174
168
    /* Get any query strings */
175
169
    url = parse_url(window.location.href);
183
177
 */
184
178
function refresh()
185
179
{
186
 
    if (maybe_save('All changes since the last save will be lost!'))
187
 
        navigate(current_path);
 
180
    navigate(current_path);
188
181
}
189
182
 
190
183
/** Determines the "handler type" from a MIME type.
216
209
 * things) be used to update the URL in the location bar.
217
210
 * \param response XMLHttpRequest object returned by the server. Should
218
211
 * 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
212
 * \param url_args Arguments dict, for the arguments passed to the URL
223
213
 * in the browser's address bar (will be forwarded along).
224
214
 */
225
 
function handle_response(path, response, is_action, url_args)
 
215
function handle_response(path, response, url_args)
226
216
{
227
217
    /* TODO: Set location bar to "path" */
228
218
    current_path = path;
252
242
    }
253
243
    catch (e)
254
244
    {
255
 
        if (is_action)
256
 
        {
257
 
            var err = document.createElement("div");
258
 
            var p = dom_make_text_elem("p", "Error: "
259
 
                    + "There was an unexpected server error processing "
260
 
                    + "the selected command.");
261
 
            err.appendChild(p);
262
 
            p = dom_make_text_elem("p", "If the problem persists, please "
263
 
                    + "contact the system administrator.")
264
 
            err.appendChild(p);
265
 
            p = document.createElement("p");
266
 
            var refresh = document.createElement("input");
267
 
            refresh.setAttribute("type", "button");
268
 
            refresh.setAttribute("value", "Back to file view");
269
 
            refresh.setAttribute("onclick", "refresh()");
270
 
            p.appendChild(refresh);
271
 
            err.appendChild(p);
272
 
            handle_error(err);
273
 
        }
274
 
        else
275
 
        {
276
 
            var err = document.createElement("div");
277
 
            var p = dom_make_text_elem("p", "Error: "
278
 
                    + "There was an unexpected server error retrieving "
279
 
                    + "the requested file or directory.");
280
 
            err.appendChild(p);
281
 
            p = dom_make_text_elem("p", "If the problem persists, please "
282
 
                    + "contact the system administrator.")
283
 
            err.appendChild(p);
284
 
            handle_error(err);
285
 
        }
 
245
        handle_error("The server returned an invalid directory listing");
286
246
        return;
287
247
    }
288
248
    /* Get "." out, it's special */
289
249
    current_file = file_listing["."];     /* Global */
290
250
    delete file_listing["."];
291
251
 
292
 
    if ('revision' in listing)
293
 
    {
294
 
        current_revision = listing.revision;
295
 
    }
296
 
 
297
252
    /* Check if this is a directory listing or file contents */
298
253
    var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
299
254
    if (isdir)
349
304
    }
350
305
}
351
306
 
352
 
/* Called when a form upload comes back (from an iframe).
353
 
 * Refreshes the page.
354
 
 */
355
 
function upload_callback()
356
 
{
357
 
    /* This has a pretty nasty hack, which happens to work.
358
 
     * upload_callback is set as the "onload" callback for the iframe which
359
 
     * receives the response from the server for uploading a file.
360
 
     * This means it gets called twice. Once when initialising the iframe, and
361
 
     * a second time when the actual response comes back.
362
 
     * All we want to do is call navigate to refresh the page. But we CAN'T do
363
 
     * that on the first load or it will just go into an infinite cycle of
364
 
     * refreshing. We need to refresh the page ONLY on the second refresh.
365
 
     * upload_callback_count is reset to 0 just before the iframe is created.
366
 
     */
367
 
    upload_callback_count++;
368
 
    if (upload_callback_count >= 2)
369
 
    {
370
 
        document.getElementsByName('data')[0].value = '';
371
 
        refresh();
372
 
    }
373
 
}
374
 
 
375
307
/** Deletes all "dynamic" content on the page.
376
308
 * This returns the page back to the state it is in when the HTML arrives to
377
309
 * the browser, ready for another handler to populate it.
381
313
    dom_removechildren(document.getElementById("filesbody"));
382
314
}
383
315
 
384
 
/* Checks if a file needs to be saved. If it does, the user will be asked
385
 
 * if they want to continue anyway. The caller must specify a warning
386
 
 * sentence which indicates the consequences of continuing.
387
 
 * Returns true if we should continue, and false if we should not.
388
 
 */
389
 
function maybe_save(warning)
390
 
{
391
 
    if (warning == null) warning = '';
392
 
    if (current_file.isdir) return true;
393
 
    if (document.getElementById("save_button").disabled) return true;
394
 
    return confirm("This file has unsaved changes. " + warning +
395
 
                   "\nAre you sure you wish to continue?");
396
 
}
397
 
 
398
316
/** Deletes all "dynamic" content on the page necessary to navigate from
399
317
 * one directory listing to another (does not clear as much as clearpage
400
318
 * does).
411
329
/*** HANDLERS for different types of responses (such as dir listing, file,
412
330
 * etc). */
413
331
 
414
 
/* handle_error.
415
 
 * message may either be a string, or a DOM node, which will be placed inside
416
 
 * a div.
417
 
 */
418
332
function handle_error(message)
419
333
{
420
334
    var files = document.getElementById("filesbody");
421
 
    var txt_elem;
422
 
    if (typeof(message) == "string")
423
 
    {
424
 
        txt_elem = dom_make_text_elem("div", "Error: "
425
 
                   + message.toString() + ".")
426
 
    }
427
 
    else
428
 
    {
429
 
        /* Assume message is a DOM node */
430
 
        txt_elem = document.createElement("div");
431
 
        txt_elem.appendChild(message);
432
 
    }
 
335
    var txt_elem = dom_make_text_elem("div", "Error: "
 
336
        + message.toString() + ".")
433
337
    txt_elem.setAttribute("class", "padding error");
434
338
    files.appendChild(txt_elem);
435
339
}
436
340
 
437
 
/** Given a path, filename and optional revision, returns a URL to open that
438
 
 *  revision of that file.
439
 
 */
440
 
function build_revision_url(path, filename, revision)
441
 
{
442
 
    bits = {'path': app_path(this_app, path, filename)};
443
 
    if (current_revision)
444
 
    {
445
 
        bits['query_string'] = 'r=' + revision;
446
 
    }
447
 
    return build_url(bits);
448
 
}
449
 
 
450
341
/** Given a mime type, returns the path to the icon.
451
342
 * \param type String, Mime type.
452
343
 * \param sizelarge Boolean, optional.
510
401
    div.appendChild(par2);
511
402
}
512
403
 
513
 
/* Enable or disable actions1 moreactions actions. Takes either a single
514
 
 * name, or an array of them.*/
515
 
function set_action_state(names, which, allow_on_revision)
516
 
{
517
 
    if (!(names instanceof Array)) names = Array(names);
518
 
 
519
 
    for (var i=0; i < names.length; i++)
520
 
    {
521
 
        element = document.getElementById('act_' + names[i]);
522
 
        if (which &&
523
 
            !(current_file.svnstatus == 'revision' && !allow_on_revision))
524
 
        {
525
 
            /* Enabling */
526
 
            element.setAttribute("class", "choice");
527
 
            element.removeAttribute("disabled");
528
 
        }
529
 
        else
530
 
        {
531
 
            /* Disabling */
532
 
            element.setAttribute("class", "disabled");
533
 
            element.setAttribute("disabled", "disabled");
534
 
        }
535
 
    }
536
 
}
537
 
 
538
404
function update_actions()
539
405
{
540
406
    var file;
570
436
        else
571
437
            open.setAttribute("title",
572
438
                "Edit or view this file");
573
 
        open.setAttribute("href", build_revision_url(current_path, filename,
574
 
                                                     current_revision));
 
439
        open.setAttribute("href", app_path(this_app, current_path, filename));
575
440
    }
576
441
    else
577
442
    {
581
446
    }
582
447
 
583
448
    /* Serve */
584
 
    /* Available if zero or one files are selected,
 
449
    /* Available if exactly one file is selected,
585
450
     * and only if this is a file, not a directory */
586
451
    var serve = document.getElementById("act_serve");
587
 
    if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
 
452
    if (numsel == 1 && !file.isdir)
588
453
    {
589
454
        serve.setAttribute("class", "choice");
590
 
        serve.setAttribute("onclick",
591
 
              "return maybe_save('The last saved version will be served.')");
592
 
        if (numsel == 0)
593
 
            serve.setAttribute("href",
594
 
                app_path(serve_app, current_path));
595
 
        else
596
 
            serve.setAttribute("href",
597
 
                app_path(serve_app, current_path, filename));
 
455
        serve.setAttribute("href",
 
456
            app_path(serve_app, current_path, filename));
598
457
    }
599
458
    else
600
459
    {
601
460
        serve.setAttribute("class", "disabled");
602
461
        serve.removeAttribute("href");
603
 
        serve.removeAttribute("onclick");
604
462
    }
605
463
 
606
464
    /* Run */
607
465
    /* Available if exactly one file is selected,
608
466
     * and it is a Python file.
609
467
     */
610
 
    var run = document.getElementById("act_run");
611
 
     
612
 
    if (!file.isdir && file.type == "text/x-python" && numsel <= 1
613
 
        && current_file.svnstatus != 'revision')
614
 
    {
615
 
        if (numsel == 0)
616
 
        {
617
 
            // In the edit window
618
 
            var localpath = path_join('/home', current_path);
619
 
        }
620
 
        else
621
 
        {
622
 
            // In the browser window
623
 
            var localpath = path_join('/home', current_path, filename);
624
 
        }
625
 
        run.setAttribute("class", "choice");
626
 
        run.setAttribute("onclick", "runfile('" + localpath + "')");
627
 
    }
628
 
    else
629
 
    {
630
 
        run.setAttribute("class", "disabled");
631
 
        run.removeAttribute("onclick");
632
 
    }
 
468
    /* TODO */
633
469
 
634
470
    /* Download */
635
 
    /* Always available for current files.
 
471
    /* Always available.
636
472
     * If 0 files selected, download the current file or directory as a ZIP.
637
473
     * If 1 directory selected, download it as a ZIP.
638
474
     * If 1 non-directory selected, download it.
639
475
     * If >1 files selected, download them all as a ZIP.
640
476
     */
641
477
    var download = document.getElementById("act_download");
642
 
    if (current_file.svnstatus == 'revision')
643
 
    {
644
 
        download.setAttribute("class", "disabled");
645
 
        download.removeAttribute("onclick");
646
 
    }
647
 
    else if (numsel <= 1)
648
 
    {
649
 
        download.setAttribute("class", "choice")
 
478
    if (numsel <= 1)
 
479
    {
650
480
        if (numsel == 0)
651
481
        {
652
482
            download.setAttribute("href",
677
507
        for (var i=0; i<numsel; i++)
678
508
            dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
679
509
        dlpath = dlpath.substr(0, dlpath.length-1);
680
 
        download.setAttribute("class", "choice")
681
510
        download.setAttribute("href", dlpath);
682
511
        download.setAttribute("title",
683
512
            "Download the selected files as a ZIP file");
690
519
     * directory. */
691
520
    var publish = document.getElementById("act_publish");
692
521
    var submit = document.getElementById("act_submit");
693
 
    var pubcond = numsel <= 1 && file.isdir;
694
 
    if (pubcond)
 
522
    if (numsel <= 1 && file.isdir)
695
523
    {
696
524
        /* TODO: Work out of file is svn'd */
697
 
        /* If this dir is already published, call it "Unpublish" */
698
 
        if (file.published)
699
 
        {
700
 
            publish.setAttribute("value", "unpublish");
701
 
            publish.setAttribute("title" ,"Make it so this directory "
702
 
                + "can not be seen by anyone on the web");
703
 
            publish.textContent = "Unpublish";
704
 
        } else {
705
 
            publish.setAttribute("value", "publish");
706
 
            publish.setAttribute("title","Make it so this directory "
707
 
                + "can be seen by anyone on the web");
708
 
            publish.textContent = "Publish";
709
 
        }
710
 
    }
711
 
    set_action_state(["publish", "submit"], pubcond);
 
525
        /* TODO: If this dir is already published, call it "Unpublish" */
 
526
        publish.setAttribute("class", "choice");
 
527
        publish.removeAttribute("disabled");
 
528
        submit.setAttribute("class", "choice");
 
529
        submit.removeAttribute("disabled");
 
530
    }
 
531
    else
 
532
    {
 
533
        publish.setAttribute("class", "disabled");
 
534
        publish.setAttribute("disabled", "disabled");
 
535
        submit.setAttribute("class", "disabled");
 
536
        submit.setAttribute("disabled", "disabled");
 
537
    }
712
538
 
713
539
    /* Share */
714
 
    /* If exactly 1 non-directory file is selected, and its parent
 
540
    /* If exactly 1 non-directory file is selected/opened, and its parent
715
541
     * directory is published.
716
542
     */
717
 
    set_action_state("share", numsel == 1 && !file.isdir &&
718
 
                     current_file.published);
 
543
    var share = document.getElementById("act_share");
 
544
    if (numsel <= 1 && !file.isdir)
 
545
    {
 
546
        /* TODO: Work out if parent dir is published */
 
547
        share.setAttribute("class", "choice");
 
548
        share.removeAttribute("disabled");
 
549
    }
 
550
    else
 
551
    {
 
552
        share.setAttribute("class", "disabled");
 
553
        share.setAttribute("disabled", "disabled");
 
554
    }
719
555
 
720
556
    /* Rename */
721
557
    /* If exactly 1 file is selected */
722
 
    set_action_state("rename", numsel == 1);
 
558
    var rename = document.getElementById("act_rename");
 
559
    if (numsel == 1)
 
560
    {
 
561
        rename.setAttribute("class", "choice");
 
562
        rename.removeAttribute("disabled");
 
563
    }
 
564
    else
 
565
    {
 
566
        rename.setAttribute("class", "disabled");
 
567
        rename.setAttribute("disabled", "disabled");
 
568
    }
723
569
 
724
570
    /* Delete, cut, copy */
725
571
    /* If >= 1 file is selected */
726
 
    set_action_state(["delete", "cut", "copy"], numsel >= 1);
 
572
    var act_delete = document.getElementById("act_delete");
 
573
    var cut = document.getElementById("act_cut");
 
574
    var copy = document.getElementById("act_copy");
 
575
    if (numsel >= 1)
 
576
    {
 
577
        act_delete.setAttribute("class", "choice");
 
578
        act_delete.removeAttribute("disabled");
 
579
        cut.setAttribute("class", "choice");
 
580
        cut.removeAttribute("disabled");
 
581
        copy.setAttribute("class", "choice");
 
582
        copy.removeAttribute("disabled");
 
583
    }
 
584
    else
 
585
    {
 
586
        act_delete.setAttribute("class", "disabled");
 
587
        act_delete.setAttribute("disabled", "disabled");
 
588
        cut.setAttribute("class", "disabled");
 
589
        cut.setAttribute("disabled", "disabled");
 
590
        copy.setAttribute("class", "disabled");
 
591
        copy.setAttribute("disabled", "disabled");
 
592
    }
727
593
 
728
594
    /* Paste, new file, new directory, upload */
729
 
    /* Disable if the current file is not a directory */
730
 
    set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
 
595
    /* Always enabled (assuming this is a directory) */
731
596
 
732
597
    /* Subversion actions */
733
 
    /* These are only useful if we are in a versioned directory and have some
734
 
     * files selected. */
735
 
    set_action_state(["svnadd", "svnremove", "svnrevert", "svncommit"], numsel >= 1 && current_file.svnstatus);
736
 
 
737
 
    /* Diff, log and update only support one path at the moment, so we must
738
 
     * have 0 or 1 versioned files selected. If 0, the directory must be
739
 
     * versioned. */
740
 
    single_versioned_path = (
741
 
         (
742
 
          (numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
743
 
          (numsel == 0 && (svnst = current_file.svnstatus))
744
 
         ) && svnst != "unversioned");
745
 
    set_action_state(["svndiff", "svnupdate"], single_versioned_path);
746
 
 
747
 
    /* We can resolve if we have a file selected and it is conflicted. */
748
 
    set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
749
 
 
750
 
    /* Log should be available for revisions as well. */
751
 
    set_action_state("svnlog", single_versioned_path, true);
752
 
 
753
 
    /* current_path == username: We are at the top level */
754
 
    set_action_state("svncheckout", current_path == username);
755
 
 
756
 
    /* There is currently nothing on the More Actions menu of use
757
 
     * when the current file is not a directory. Hence, just remove
758
 
     * it entirely.
759
 
     * (This makes some of the above decisions somewhat redundant).
760
 
     * We also take this opportunity to show the appropriate actions2
761
 
     * bar for this path. It should either be a save or upload widget.
762
 
     */
763
 
    if (current_file.isdir)
764
 
    {
765
 
        var actions2_directory = document.getElementById("actions2_directory");
766
 
        actions2_directory.setAttribute("style", "display: inline;");
767
 
        var moreactions = document.getElementById("moreactions_area");
768
 
        moreactions.setAttribute("style", "display: inline;");
769
 
    }
770
 
    else
771
 
    {
772
 
        var actions2_file = document.getElementById("actions2_file");
773
 
        actions2_file.setAttribute("style", "display: inline;");
 
598
    /* TODO: Work out when these are appropriate */
 
599
    var svnadd = document.getElementById("act_svnadd");
 
600
    var svnrevert = document.getElementById("act_svnrevert");
 
601
    var svncommit = document.getElementById("act_svncommit");
 
602
    if (true)
 
603
    {
 
604
        svnadd.setAttribute("class", "choice");
 
605
        svnadd.removeAttribute("disabled");
 
606
        svnrevert.setAttribute("class", "choice");
 
607
        svnrevert.removeAttribute("disabled");
 
608
        svncommit.setAttribute("class", "choice");
 
609
        svncommit.removeAttribute("disabled");
774
610
    }
775
611
 
776
612
    return;
807
643
        action_unpublish(selected_files);
808
644
        break;
809
645
    case "share":
810
 
        //alert("Not yet implemented: Sharing files");
811
 
        window.open(public_app_path(serve_app, current_path, filename), 'share')
 
646
        // TODO
 
647
        alert("Not yet implemented: Sharing files");
812
648
        break;
813
649
    case "submit":
814
650
        // TODO
818
654
        action_rename(filename);
819
655
        break;
820
656
    case "delete":
821
 
        action_delete(selected_files);
 
657
        action_remove(selected_files);
822
658
        break;
823
659
    case "copy":
824
660
        action_copy(selected_files);
841
677
    case "svnadd":
842
678
        action_add(selected_files);
843
679
        break;
844
 
    case "svnremove":
845
 
        action_remove(selected_files);
846
 
        break;
847
680
    case "svnrevert":
848
681
        action_revert(selected_files);
849
682
        break;
850
 
    case "svndiff":
851
 
        window.location = path_join(app_path('diff'), current_path, selected_files[0] || '');
852
 
        break;
853
 
    case "svnupdate":
854
 
        action_update(selected_files);
855
 
        break;
856
 
    case "svnresolved":
857
 
        action_resolved(selected_files);
858
 
        break;
859
683
    case "svncommit":
860
684
        action_commit(selected_files);
861
685
        break;
862
 
    case "svnlog":
863
 
        window.location = path_join(app_path('svnlog'), current_path, selected_files[0] || '');
864
 
        break;
865
 
    case "svncheckout":
866
 
        action_checkout();
867
 
        break;
868
 
    }
869
 
}
870
 
 
871
 
/** User clicks "Run" button.
872
 
 * Do an Ajax call and print the test output.
873
 
 */
874
 
function runfile(localpath)
875
 
{
876
 
    if (!maybe_save('The last saved version will be run.')) return false;
877
 
 
878
 
    /* Dump the entire file to the console */
879
 
    var callback = function()
880
 
    {
881
 
        console_enter_line("execfile('" + localpath + "')", "block");
882
 
    }
883
 
    start_server(callback)
884
 
    return;
 
686
    }
885
687
}
886
688
 
887
689
/** Called when the page loads initially.
888
690
 */
889
 
function browser_init()
 
691
window.onload = function()
890
692
{
891
693
    /* Navigate (internally) to the path in the URL bar.
892
694
     * This causes the page to be populated with whatever is at that address,