~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-27 10:15:01 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:597
Major refactor of actions in File browser.

listing.js: Calls update_actions when selecting a file. This is the new method
in browser.js which updates the actions along the top.
(Currently Listing still produces actions on the side, because the top actions
don't work properly yet and I don't want to break the build).

browser.js: 
    Added update_actions which applies properties to the actions buttons on
    the top. (Enables/disables them, etc).
    Added handle_moreactions - event handler for selecting an item in
    moreactions. Currently sets things up properly but doesn't execute any
    actions.
    Added Refresh function (new user action).

browser/__init__.py: Misc changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
103
103
 
104
104
current_path = "";
105
105
 
 
106
/** Filenames of all files selected
 
107
 * (Only used by dir listings, but still needs to be [] for files, so that
 
108
 * update_actions knows that nothing is selected).
 
109
 */
 
110
selected_files = [];
 
111
 
106
112
/** Calls the server using Ajax, performing an action on the server side.
107
113
 * Receives the response from the server and performs a refresh of the page
108
114
 * contents, updating it to display the returned data (such as a directory
165
171
    ajax_call(callback, service_app, path, url.args, "GET");
166
172
}
167
173
 
 
174
/* Refreshes the current view.
 
175
 * Calls navigate on the current path.
 
176
 */
 
177
function refresh()
 
178
{
 
179
    navigate(current_path, false);
 
180
}
 
181
 
168
182
/** Determines the "handler type" from a MIME type.
169
183
 * The handler type is a string, either "text", "image", "audio" or "binary".
170
184
 */
270
284
            break;
271
285
        }
272
286
    }
 
287
    update_actions(isdir);
273
288
}
274
289
 
275
290
/** Deletes all "dynamic" content on the page.
409
424
    div.appendChild(par2);
410
425
}
411
426
 
 
427
function update_actions()
 
428
{
 
429
    var file;
 
430
    var numsel = selected_files.length;
 
431
    if (numsel <= 1)
 
432
    {
 
433
        if (numsel == 0)
 
434
        {
 
435
            /* Display information about the current directory instead */
 
436
            filename = path_basename(current_path);
 
437
            file = thisdir;
 
438
        }
 
439
        else if (numsel == 1)
 
440
        {
 
441
            filename = selected_files[0];
 
442
            file = file_listing[filename];
 
443
        }
 
444
 
 
445
        /* Update each action node in the topbar.
 
446
         * This includes enabling/disabling actions as appropriate, and
 
447
         * setting href/onclick attributes. */
 
448
    }
 
449
 
 
450
    /* Open */
 
451
    /* Available if exactly one file is selected */
 
452
    var open = document.getElementById("act_open");
 
453
    if (numsel == 1)
 
454
    {
 
455
        open.setAttribute("class", "choice");
 
456
        if (file.isdir)
 
457
            open.setAttribute("title",
 
458
                "Navigate to this directory in the file browser");
 
459
        else
 
460
            open.setAttribute("title",
 
461
                "Edit or view this file");
 
462
        open.setAttribute("href", app_path(this_app, current_path, filename));
 
463
    }
 
464
    else
 
465
    {
 
466
        open.setAttribute("class", "disabled");
 
467
        open.removeAttribute("title");
 
468
        open.removeAttribute("href");
 
469
    }
 
470
 
 
471
    /* Serve */
 
472
    /* Available if exactly one file is selected,
 
473
     * and only if this is a file, not a directory */
 
474
    var serve = document.getElementById("act_serve");
 
475
    if (numsel == 1 && !file.isdir)
 
476
    {
 
477
        serve.setAttribute("class", "choice");
 
478
        serve.setAttribute("href",
 
479
            app_path(serve_app, current_path, filename));
 
480
    }
 
481
    else
 
482
    {
 
483
        serve.setAttribute("class", "disabled");
 
484
        serve.removeAttribute("href");
 
485
    }
 
486
 
 
487
    /* Run */
 
488
    /* Available if exactly one file is selected,
 
489
     * and it is a Python file.
 
490
     */
 
491
    /* TODO */
 
492
 
 
493
    /* Download */
 
494
    /* Always available.
 
495
     * If 0 files selected, download the current file or directory as a ZIP.
 
496
     * If 1 directory selected, download it as a ZIP.
 
497
     * If 1 non-directory selected, download it.
 
498
     * If >1 files selected, download them all as a ZIP.
 
499
     */
 
500
    var download = document.getElementById("act_download");
 
501
    if (numsel <= 1)
 
502
    {
 
503
        if (numsel == 0)
 
504
        {
 
505
            download.setAttribute("href",
 
506
                app_path(download_app, current_path));
 
507
            if (file.isdir)
 
508
                download.setAttribute("title",
 
509
                    "Download the current directory as a ZIP file");
 
510
            else
 
511
                download.setAttribute("title",
 
512
                    "Download the current file");
 
513
        }
 
514
        else
 
515
        {
 
516
            download.setAttribute("href",
 
517
                app_path(download_app, current_path, filename));
 
518
            if (file.isdir)
 
519
                download.setAttribute("title",
 
520
                    "Download the selected directory as a ZIP file");
 
521
            else
 
522
                download.setAttribute("title",
 
523
                    "Download the selected file");
 
524
        }
 
525
    }
 
526
    else
 
527
    {
 
528
        /* Make a query string with all the files to download */
 
529
        var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
 
530
        for (var i=0; i<numsel; i++)
 
531
            dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
 
532
        dlpath = dlpath.substr(0, dlpath.length-1);
 
533
        download.setAttribute("href", dlpath);
 
534
        download.setAttribute("title",
 
535
            "Download the selected files as a ZIP file");
 
536
    }
 
537
 
 
538
    /* Refresh - No changes required */
 
539
 
 
540
    /* Publish and Submit */
 
541
    /* If this directory is under subversion and selected/unselected file is a
 
542
     * directory. */
 
543
    var publish = document.getElementById("act_publish");
 
544
    var submit = document.getElementById("act_submit");
 
545
    if (numsel <= 1 && file.isdir)
 
546
    {
 
547
        /* TODO: Work out of file is svn'd */
 
548
        /* TODO: If this dir is already published, call it "Unpublish" */
 
549
        publish.setAttribute("class", "choice");
 
550
        publish.removeAttribute("disabled");
 
551
        submit.setAttribute("class", "choice");
 
552
        submit.removeAttribute("disabled");
 
553
    }
 
554
    else
 
555
    {
 
556
        publish.setAttribute("class", "disabled");
 
557
        publish.setAttribute("disabled", "disabled");
 
558
        submit.setAttribute("class", "disabled");
 
559
        submit.setAttribute("disabled", "disabled");
 
560
    }
 
561
 
 
562
    /* Share */
 
563
    /* If exactly 1 non-directory file is selected/opened, and its parent
 
564
     * directory is published.
 
565
     */
 
566
    var share = document.getElementById("act_share");
 
567
    if (numsel <= 1 && !file.isdir)
 
568
    {
 
569
        /* TODO: Work out if parent dir is published */
 
570
        share.setAttribute("class", "choice");
 
571
        share.removeAttribute("disabled");
 
572
    }
 
573
    else
 
574
    {
 
575
        share.setAttribute("class", "disabled");
 
576
        share.setAttribute("disabled", "disabled");
 
577
    }
 
578
 
 
579
    /* Rename */
 
580
    /* If exactly 1 file is selected */
 
581
    var rename = document.getElementById("act_rename");
 
582
    if (numsel == 1)
 
583
    {
 
584
        rename.setAttribute("class", "choice");
 
585
        rename.removeAttribute("disabled");
 
586
    }
 
587
    else
 
588
    {
 
589
        rename.setAttribute("class", "disabled");
 
590
        rename.setAttribute("disabled", "disabled");
 
591
    }
 
592
 
 
593
    /* Delete, cut, copy */
 
594
    /* If >= 1 file is selected */
 
595
    var act_delete = document.getElementById("act_delete");
 
596
    var cut = document.getElementById("act_cut");
 
597
    var copy = document.getElementById("act_copy");
 
598
    if (numsel >= 1)
 
599
    {
 
600
        act_delete.setAttribute("class", "choice");
 
601
        act_delete.removeAttribute("disabled");
 
602
        cut.setAttribute("class", "choice");
 
603
        cut.removeAttribute("disabled");
 
604
        copy.setAttribute("class", "choice");
 
605
        copy.removeAttribute("disabled");
 
606
    }
 
607
    else
 
608
    {
 
609
        act_delete.setAttribute("class", "disabled");
 
610
        act_delete.setAttribute("disabled", "disabled");
 
611
        cut.setAttribute("class", "disabled");
 
612
        cut.setAttribute("disabled", "disabled");
 
613
        copy.setAttribute("class", "disabled");
 
614
        copy.setAttribute("disabled", "disabled");
 
615
    }
 
616
 
 
617
    /* Paste, new file, new directory, upload */
 
618
    /* Always enabled (assuming this is a directory) */
 
619
 
 
620
    /* Subversion actions */
 
621
    /* TODO: Work out when these are appropriate */
 
622
    var svnadd = document.getElementById("act_svnadd");
 
623
    var svnrevert = document.getElementById("act_svnrevert");
 
624
    var svncommit = document.getElementById("act_svncommit");
 
625
    if (true)
 
626
    {
 
627
        svnadd.setAttribute("class", "choice");
 
628
        svnadd.removeAttribute("disabled");
 
629
        svnrevert.setAttribute("class", "choice");
 
630
        svnrevert.removeAttribute("disabled");
 
631
        svncommit.setAttribute("class", "choice");
 
632
        svncommit.removeAttribute("disabled");
 
633
    }
 
634
 
 
635
    return;
 
636
 
 
637
 
 
638
    /* Unrefactored Code */
 
639
 
 
640
 
 
641
 
 
642
    if (1)
 
643
    {
 
644
        if (file.isdir)
 
645
        {
 
646
            /* Publish/unpublish */
 
647
            if (selected_files.length == 0)
 
648
                path = ".";
 
649
            else
 
650
                path = filename;
 
651
            if ("published" in file && file.published)
 
652
            {
 
653
                p = dom_make_link_elem("p", "Unpublish",
 
654
                    "Make it so this directory cannot be seen by anyone but you",
 
655
                    null,
 
656
                    "return action_unpublish(" + repr(path) + ")");
 
657
                sidepanel.appendChild(p);
 
658
            }
 
659
            else
 
660
            {
 
661
                p = dom_make_link_elem("p", "Publish",
 
662
                    "Make it so this directory can be seen by anyone on the web",
 
663
                    null,
 
664
                    "return action_publish(" + repr(path) + ")");
 
665
                sidepanel.appendChild(p);
 
666
            }
 
667
        }
 
668
 
 
669
        var handler_type = null;
 
670
        if ("type" in file)
 
671
            handler_type = get_handler_type(file.type);
 
672
        /* Action: Use the "files" / "edit" app */
 
673
        var path;
 
674
        if (selected_files.length == 1)
 
675
        {
 
676
            /* Don't have "Browse" if this is the current dir */
 
677
            if (file.isdir)
 
678
                p = dom_make_link_elem("p", "Browse",
 
679
                    "Navigate to this directory in the file browser",
 
680
                    app_path(this_app, current_path, filename));
 
681
            else if (handler_type == "text")
 
682
                p = dom_make_link_elem("p", "Edit", "Edit this file",
 
683
                    app_path(edit_app, current_path, filename));
 
684
            else
 
685
                p = dom_make_link_elem("p", "Browse",
 
686
                    "View this file in the file browser",
 
687
                    app_path(this_app, current_path, filename));
 
688
            sidepanel.appendChild(p);
 
689
        }
 
690
 
 
691
        /* Action: Use the "serve" app */
 
692
        /* TODO: Figure out if this file is executable,
 
693
         * and change the link to "Run" */
 
694
        p = null;
 
695
        if (file.isdir || handler_type == "binary") {}
 
696
        else
 
697
            p = dom_make_link_elem("p", "View",
 
698
                "View this file",
 
699
                app_path(serve_app, current_path, filename));
 
700
        if (p)
 
701
            sidepanel.appendChild(p);
 
702
 
 
703
        /* Action: Use the "download" app */
 
704
        p = null;
 
705
        if (selected_files.length == 0)
 
706
            path = app_path(download_app, current_path);
 
707
        else
 
708
            path = app_path(download_app, current_path, filename);
 
709
        if (file.isdir)
 
710
            p = dom_make_link_elem("p", "Download as zip",
 
711
                "Download this directory as a ZIP file", path);
 
712
        else
 
713
            p = dom_make_link_elem("p", "Download",
 
714
                "Download this file to your computer", path);
 
715
        if (p)
 
716
            sidepanel.appendChild(p);
 
717
 
 
718
        if (selected_files.length > 0)
 
719
        {   /* Can't rename if we're in it */
 
720
            p = dom_make_link_elem("p", "Rename",
 
721
                "Change the name of this file", null,
 
722
                "return action_rename(" + repr(filename) + ")");
 
723
            sidepanel.appendChild(p);
 
724
        }
 
725
    }
 
726
    else
 
727
    {
 
728
        path = urlencode_path(app_path(download_app, current_path)) + "?";
 
729
        for (var i=0; i<selected_files.length; i++)
 
730
            path += "path=" + encodeURIComponent(selected_files[i]) + "&";
 
731
        path = path.substr(0, path.length-1);
 
732
        /* Multiple files selected */
 
733
        p = dom_make_link_elem("p", "Download as zip",
 
734
            "Download the selected files as a ZIP file", path, null, true);
 
735
        sidepanel.appendChild(p);
 
736
    }
 
737
 
 
738
    /* Common actions */
 
739
    if (selected_files.length > 0)
 
740
    {
 
741
        p = dom_make_link_elem("p", "Delete",
 
742
            "Delete the selected files", null,
 
743
            "return action_remove(selected_files)");
 
744
        sidepanel.appendChild(p);
 
745
        p = dom_make_link_elem("p", "Cut",
 
746
            "Prepare to move the selected files to another directory", null,
 
747
            "return action_cut(selected_files)");
 
748
        sidepanel.appendChild(p);
 
749
        p = dom_make_link_elem("p", "Copy",
 
750
            "Prepare to copy the selected files to another directory", null,
 
751
            "return action_copy(selected_files)");
 
752
        sidepanel.appendChild(p);
 
753
    }
 
754
    p = dom_make_link_elem("p", "Paste",
 
755
        "Paste the copied or cut files to the current directory", null,
 
756
        "return action_paste()");
 
757
    sidepanel.appendChild(p);
 
758
    p = dom_make_link_elem("p", "Make Directory",
 
759
        "Make a new subdirectory in the current directory", null,
 
760
        "return action_mkdir()");
 
761
    sidepanel.appendChild(p);
 
762
    p = dom_make_link_elem("p", "Upload",
 
763
        "Upload a file to the current directory", null,
 
764
        "return show_uploadpanel()");
 
765
    sidepanel.appendChild(p);
 
766
    /* The "Upload" button expands the following panel with upload tools */
 
767
    /* This panel has a form for submitting the file to, and an iframe to load
 
768
     * the target page in (this avoids the entire page being refreshed) */
 
769
    div = document.createElement("div");
 
770
    div.setAttribute("id", "uploadpanel");
 
771
    /* This deliberately hides the upload panel whenever the selection
 
772
     * changes. It can be re-shown by clicking "upload". */
 
773
    div.setAttribute("style", "display: none;");
 
774
    sidepanel.appendChild(div);
 
775
    p = dom_make_text_elem("h3", "Upload File");
 
776
    div.appendChild(p);
 
777
    var form = document.createElement("form");
 
778
    form.setAttribute("method", "POST");
 
779
    form.setAttribute("enctype", "multipart/form-data");
 
780
    form.setAttribute("action", app_path("fileservice", current_path));
 
781
    form.setAttribute("target", "upload_iframe");
 
782
    div.appendChild(form);
 
783
    var input;
 
784
    input = document.createElement("input");
 
785
    input.setAttribute("type", "hidden");
 
786
    input.setAttribute("name", "action");
 
787
    input.setAttribute("value", "putfiles");
 
788
    form.appendChild(input);
 
789
 
 
790
    input = document.createElement("input");
 
791
    input.setAttribute("type", "hidden");
 
792
    input.setAttribute("name", "path");
 
793
    input.setAttribute("value", "");
 
794
    form.appendChild(input);
 
795
 
 
796
    p = document.createElement("p");
 
797
    form.appendChild(p);
 
798
    input = document.createElement("input");
 
799
    input.setAttribute("type", "file");
 
800
    input.setAttribute("name", "data");
 
801
    p.appendChild(input);
 
802
 
 
803
    p = document.createElement("p");
 
804
    form.appendChild(p);
 
805
    input = document.createElement("input");
 
806
    input.setAttribute("type", "checkbox");
 
807
    input.setAttribute("name", "unpack");
 
808
    input.setAttribute("value", "true");
 
809
    input.setAttribute("checked", "on");
 
810
    p.appendChild(input);
 
811
    p.appendChild(document.createTextNode(" Unpack zip file"));
 
812
 
 
813
    p = document.createElement("p");
 
814
    form.appendChild(p);
 
815
    input = document.createElement("input");
 
816
    input.setAttribute("type", "button");
 
817
    input.setAttribute("value", "Hide");
 
818
    input.setAttribute("onclick", "show_uploadpanel(false)");
 
819
    p.appendChild(input);
 
820
    p.appendChild(document.createTextNode(" "));
 
821
    input = document.createElement("input");
 
822
    input.setAttribute("type", "submit");
 
823
    input.setAttribute("value", "Send");
 
824
    p.appendChild(input);
 
825
 
 
826
    /* Now we create an invisible iframe which will receive the upload.
 
827
     * The form submits to fileservice, loading the result into this iframe
 
828
     * instead of the whole browser window (this is an alternative to Ajax,
 
829
     * since Ajax doesn't allow reading the file from the user's disk).
 
830
     * Note this iframe's id is the same as the form's target.
 
831
     */
 
832
    var upload_iframe = document.createElement("iframe");
 
833
    upload_iframe.setAttribute("id", "upload_iframe");
 
834
    upload_iframe.setAttribute("name", "upload_iframe");
 
835
    upload_iframe.setAttribute("style", "display: none;");
 
836
    /* When we get a callback, simply cause a nav to the current path, so we
 
837
     * update the directory listing. */
 
838
    upload_callback_count = 0;      /* See upload_callback */
 
839
    upload_iframe.setAttribute("onload", "upload_callback()");
 
840
    div.appendChild(upload_iframe);
 
841
    /* END Upload panel */
 
842
 
 
843
    if (under_subversion)
 
844
    {
 
845
        /* TODO: Only show relevant links */
 
846
        p = dom_make_text_elem("h3", "Subversion");
 
847
        sidepanel.appendChild(p);
 
848
 
 
849
        /* TODO: if any selected files are unversioned */
 
850
        p = dom_make_link_elem("p", "Add",
 
851
            "Schedule the selected temporary files to be added permanently",
 
852
            null,
 
853
            "return action_add(selected_files)");
 
854
        sidepanel.appendChild(p);
 
855
        p = dom_make_link_elem("p", "Revert",
 
856
            "Restore the selected files back to their last committed state",
 
857
            null,
 
858
            "return action_revert(selected_files)");
 
859
        sidepanel.appendChild(p);
 
860
        /* TODO: Update */
 
861
        p = dom_make_link_elem("p", "Commit",
 
862
            "Commit any changes to the permanent repository",
 
863
            null,
 
864
            "return action_commit(selected_files)");
 
865
        sidepanel.appendChild(p);
 
866
    }
 
867
 
 
868
}
 
869
 
 
870
/** Event handler for when an item of the "More actions..." dropdown box is
 
871
 * selected. Performs the selected action. */
 
872
function handle_moreactions()
 
873
{
 
874
    var moreactions = document.getElementById("moreactions");
 
875
    if (moreactions.value == "top")
 
876
        return;
 
877
    var selectedaction = moreactions.value;
 
878
    /* Reset to "More actions..." */
 
879
    moreactions.selectedIndex = 0;
 
880
 
 
881
    /* Now handle the selected action */
 
882
    alert("Action: " + selectedaction);
 
883
    /* TODO */
 
884
}
 
885
 
412
886
/** Called when the page loads initially.
413
887
 */
414
888
window.onload = function()