~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 00:40:57 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:617
Added upload panel to the topbar instead of being on the side.
browser/listing.js: Removed all upload panel generation code.
browser/__init__.py: Statically generates upload panel as part of the top bar.
browser/browser.css, js: minor.

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;
241
231
        return;
242
232
    }
243
233
 
244
 
    var subjects = null;
245
 
    var top_level_dir = path==username;
246
 
    if (top_level_dir)
247
 
    {
248
 
        var req = ajax_call(null, "userservice", "get_enrolments", null, "GET")
249
 
        subjects = decode_response(req);
250
 
    }
251
 
 
252
 
 
253
234
    /* This will always return a listing, whether it is a dir or a file.
254
235
     */
255
236
    var listing = response.responseText;
261
242
    }
262
243
    catch (e)
263
244
    {
264
 
        if (is_action)
265
 
        {
266
 
            var err = document.createElement("div");
267
 
            var p = dom_make_text_elem("p", "Error: "
268
 
                    + "There was an unexpected server error processing "
269
 
                    + "the selected command.");
270
 
            err.appendChild(p);
271
 
            p = dom_make_text_elem("p", "If the problem persists, please "
272
 
                    + "contact the system administrator.")
273
 
            err.appendChild(p);
274
 
            p = document.createElement("p");
275
 
            var refresh = document.createElement("input");
276
 
            refresh.setAttribute("type", "button");
277
 
            refresh.setAttribute("value", "Back to file view");
278
 
            refresh.setAttribute("onclick", "refresh()");
279
 
            p.appendChild(refresh);
280
 
            err.appendChild(p);
281
 
            handle_error(err);
282
 
        }
283
 
        else
284
 
        {
285
 
            var err = document.createElement("div");
286
 
            var p = dom_make_text_elem("p", "Error: "
287
 
                    + "There was an unexpected server error retrieving "
288
 
                    + "the requested file or directory.");
289
 
            err.appendChild(p);
290
 
            p = dom_make_text_elem("p", "If the problem persists, please "
291
 
                    + "contact the system administrator.")
292
 
            err.appendChild(p);
293
 
            handle_error(err);
294
 
        }
 
245
        handle_error("The server returned an invalid directory listing");
295
246
        return;
296
247
    }
297
248
    /* Get "." out, it's special */
298
249
    current_file = file_listing["."];     /* Global */
299
250
    delete file_listing["."];
300
251
 
301
 
    if ('revision' in listing)
302
 
    {
303
 
        current_revision = listing.revision;
304
 
    }
305
 
 
306
252
    /* Check if this is a directory listing or file contents */
307
253
    var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
308
254
    if (isdir)
309
255
    {
310
 
        setup_for_listing();
311
 
        home_listing(listing, subjects, path);
 
256
        handle_dir_listing(path, listing);
312
257
    }
313
258
    else
314
259
    {
359
304
    }
360
305
}
361
306
 
362
 
/* Called when a form upload comes back (from an iframe).
363
 
 * Refreshes the page.
364
 
 */
365
 
function upload_callback()
366
 
{
367
 
    /* This has a pretty nasty hack, which happens to work.
368
 
     * upload_callback is set as the "onload" callback for the iframe which
369
 
     * receives the response from the server for uploading a file.
370
 
     * This means it gets called twice. Once when initialising the iframe, and
371
 
     * a second time when the actual response comes back.
372
 
     * All we want to do is call navigate to refresh the page. But we CAN'T do
373
 
     * that on the first load or it will just go into an infinite cycle of
374
 
     * refreshing. We need to refresh the page ONLY on the second refresh.
375
 
     * upload_callback_count is reset to 0 just before the iframe is created.
376
 
     */
377
 
    upload_callback_count++;
378
 
    if (upload_callback_count >= 2)
379
 
    {
380
 
        document.getElementsByName('data')[0].value = '';
381
 
        refresh();
382
 
    }
383
 
}
384
 
 
385
307
/** Deletes all "dynamic" content on the page.
386
308
 * This returns the page back to the state it is in when the HTML arrives to
387
309
 * the browser, ready for another handler to populate it.
391
313
    dom_removechildren(document.getElementById("filesbody"));
392
314
}
393
315
 
394
 
/* Checks if a file needs to be saved. If it does, the user will be asked
395
 
 * if they want to continue anyway. The caller must specify a warning
396
 
 * sentence which indicates the consequences of continuing.
397
 
 * Returns true if we should continue, and false if we should not.
398
 
 */
399
 
function maybe_save(warning)
400
 
{
401
 
    if (warning == null) warning = '';
402
 
    if (current_file.isdir) return true;
403
 
    if (document.getElementById("save_button").disabled) return true;
404
 
    return confirm("This file has unsaved changes. " + warning +
405
 
                   "\nAre you sure you wish to continue?");
406
 
}
407
 
 
408
316
/** Deletes all "dynamic" content on the page necessary to navigate from
409
317
 * one directory listing to another (does not clear as much as clearpage
410
318
 * does).
421
329
/*** HANDLERS for different types of responses (such as dir listing, file,
422
330
 * etc). */
423
331
 
424
 
/* handle_error.
425
 
 * message may either be a string, or a DOM node, which will be placed inside
426
 
 * a div.
427
 
 */
428
332
function handle_error(message)
429
333
{
430
334
    var files = document.getElementById("filesbody");
431
 
    var txt_elem;
432
 
    if (typeof(message) == "string")
433
 
    {
434
 
        txt_elem = dom_make_text_elem("div", "Error: "
435
 
                   + message.toString() + ".")
436
 
    }
437
 
    else
438
 
    {
439
 
        /* Assume message is a DOM node */
440
 
        txt_elem = document.createElement("div");
441
 
        txt_elem.appendChild(message);
442
 
    }
 
335
    var txt_elem = dom_make_text_elem("div", "Error: "
 
336
        + message.toString() + ".")
443
337
    txt_elem.setAttribute("class", "padding error");
444
338
    files.appendChild(txt_elem);
445
339
}
446
340
 
447
 
/** Given a path, filename and optional revision, returns a URL to open that
448
 
 *  revision of that file.
449
 
 */
450
 
function build_revision_url(path, filename, revision)
451
 
{
452
 
    bits = {'path': app_path(this_app, path, filename)};
453
 
    if (current_revision)
454
 
    {
455
 
        bits['query_string'] = 'r=' + revision;
456
 
    }
457
 
    return build_url(bits);
458
 
}
459
 
 
460
341
/** Given a mime type, returns the path to the icon.
461
342
 * \param type String, Mime type.
462
343
 * \param sizelarge Boolean, optional.
520
401
    div.appendChild(par2);
521
402
}
522
403
 
523
 
/* Enable or disable actions1 moreactions actions. Takes either a single
524
 
 * name, or an array of them.*/
525
 
function set_action_state(names, which, allow_on_revision)
526
 
{
527
 
    if (!(names instanceof Array)) names = Array(names);
528
 
 
529
 
    for (var i=0; i < names.length; i++)
530
 
    {
531
 
        element = document.getElementById('act_' + names[i]);
532
 
        if (which &&
533
 
            !(current_file.svnstatus == 'revision' && !allow_on_revision))
534
 
        {
535
 
            /* Enabling */
536
 
            element.setAttribute("class", "choice");
537
 
            element.removeAttribute("disabled");
538
 
        }
539
 
        else
540
 
        {
541
 
            /* Disabling */
542
 
            element.setAttribute("class", "disabled");
543
 
            element.setAttribute("disabled", "disabled");
544
 
        }
545
 
    }
546
 
}
547
 
 
548
 
/* Updates the list of available actions based on files selected */
549
404
function update_actions()
550
405
{
551
406
    var file;
552
407
    var numsel = selected_files.length;
553
 
    var svn_selection = false;
554
 
    
555
 
    if (numsel > 0)
556
 
    {
557
 
        svn_selection = true;
558
 
        for (var i = 0; i < selected_files.length; i++){
559
 
            if (file_listing[selected_files[i]]["svnstatus"] == "unversioned")
560
 
            {
561
 
                svn_selection = false;        
562
 
            }
563
 
        }
564
 
    }
565
 
    
566
408
    if (numsel <= 1)
567
409
    {
568
410
        if (numsel == 0)
594
436
        else
595
437
            open.setAttribute("title",
596
438
                "Edit or view this file");
597
 
        open.setAttribute("href", build_revision_url(current_path, filename,
598
 
                                                     current_revision));
 
439
        open.setAttribute("href", app_path(this_app, current_path, filename));
599
440
    }
600
441
    else
601
442
    {
605
446
    }
606
447
 
607
448
    /* Serve */
608
 
    /* Available if zero or one files are selected,
 
449
    /* Available if exactly one file is selected,
609
450
     * and only if this is a file, not a directory */
610
451
    var serve = document.getElementById("act_serve");
611
 
    if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
 
452
    if (numsel == 1 && !file.isdir)
612
453
    {
613
454
        serve.setAttribute("class", "choice");
614
 
        serve.setAttribute("onclick",
615
 
              "return maybe_save('The last saved version will be served.')");
616
 
        if (numsel == 0)
617
 
            serve.setAttribute("href",
618
 
                app_path(serve_app, current_path));
619
 
        else
620
 
            serve.setAttribute("href",
621
 
                app_path(serve_app, current_path, filename));
 
455
        serve.setAttribute("href",
 
456
            app_path(serve_app, current_path, filename));
622
457
    }
623
458
    else
624
459
    {
625
460
        serve.setAttribute("class", "disabled");
626
461
        serve.removeAttribute("href");
627
 
        serve.removeAttribute("onclick");
628
462
    }
629
463
 
630
464
    /* Run */
631
465
    /* Available if exactly one file is selected,
632
466
     * and it is a Python file.
633
467
     */
634
 
    var run = document.getElementById("act_run");
635
 
     
636
 
    if (numsel <= 1 && !file.isdir && file.type == "text/x-python" 
637
 
            && current_file.svnstatus != 'revision')
638
 
    {
639
 
        if (numsel == 0)
640
 
        {
641
 
            // In the edit window
642
 
            var localpath = path_join('/home', current_path);
643
 
        }
644
 
        else
645
 
        {
646
 
            // In the browser window
647
 
            var localpath = path_join('/home', current_path, filename);
648
 
        }
649
 
        run.setAttribute("class", "choice");
650
 
        run.setAttribute("onclick", "runfile('" + localpath + "')");
651
 
    }
652
 
    else
653
 
    {
654
 
        run.setAttribute("class", "disabled");
655
 
        run.removeAttribute("onclick");
656
 
    }
 
468
    /* TODO */
657
469
 
658
470
    /* Download */
659
 
    /* Always available for current files.
 
471
    /* Always available.
660
472
     * If 0 files selected, download the current file or directory as a ZIP.
661
473
     * If 1 directory selected, download it as a ZIP.
662
474
     * If 1 non-directory selected, download it.
663
475
     * If >1 files selected, download them all as a ZIP.
664
476
     */
665
477
    var download = document.getElementById("act_download");
666
 
    if (current_file.svnstatus == 'revision')
667
 
    {
668
 
        download.setAttribute("class", "disabled");
669
 
        download.removeAttribute("onclick");
670
 
    }
671
 
    else if (numsel <= 1)
672
 
    {
673
 
        download.setAttribute("class", "choice")
 
478
    if (numsel <= 1)
 
479
    {
674
480
        if (numsel == 0)
675
481
        {
676
482
            download.setAttribute("href",
701
507
        for (var i=0; i<numsel; i++)
702
508
            dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
703
509
        dlpath = dlpath.substr(0, dlpath.length-1);
704
 
        download.setAttribute("class", "choice")
705
510
        download.setAttribute("href", dlpath);
706
511
        download.setAttribute("title",
707
512
            "Download the selected files as a ZIP file");
714
519
     * directory. */
715
520
    var publish = document.getElementById("act_publish");
716
521
    var submit = document.getElementById("act_submit");
717
 
    var pubcond = numsel <= 1 && file.isdir;
718
 
    if (pubcond)
719
 
    {
720
 
        /* If this dir is already published, call it "Unpublish" */
721
 
        if (file.published)
722
 
        {
723
 
            publish.setAttribute("value", "unpublish");
724
 
            publish.setAttribute("title" ,"Make it so this directory "
725
 
                + "can not be seen by anyone on the web");
726
 
            publish.textContent = "Unpublish";
727
 
        } else {
728
 
            publish.setAttribute("value", "publish");
729
 
            publish.setAttribute("title","Make it so this directory "
730
 
                + "can be seen by anyone on the web");
731
 
            publish.textContent = "Publish";
732
 
        }
733
 
    }
734
 
    set_action_state(["publish", "submit"], pubcond);
 
522
    if (numsel <= 1 && file.isdir)
 
523
    {
 
524
        /* TODO: Work out of file is svn'd */
 
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
    }
735
538
 
736
539
    /* Share */
737
 
    /* If exactly 1 non-directory file is selected, and its parent
 
540
    /* If exactly 1 non-directory file is selected/opened, and its parent
738
541
     * directory is published.
739
542
     */
740
 
    set_action_state("share", numsel == 1 && !file.isdir &&
741
 
                     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
    }
742
555
 
743
556
    /* Rename */
744
557
    /* If exactly 1 file is selected */
745
 
    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
    }
746
569
 
747
570
    /* Delete, cut, copy */
748
571
    /* If >= 1 file is selected */
749
 
    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
    }
750
593
 
751
594
    /* Paste, new file, new directory, upload */
752
 
    /* Disable if the current file is not a directory */
753
 
    set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
 
595
    /* Always enabled (assuming this is a directory) */
754
596
 
755
597
    /* Subversion actions */
756
 
    /* These are only useful if we are in a versioned directory and have some
757
 
     * files selected. */
758
 
    set_action_state(["svnadd",], numsel >= 1 && current_file.svnstatus);
759
 
    /* And these are only usefull is ALL the selected files are versioned */
760
 
    set_action_state(["svnremove", "svnrevert", "svncommit", "svncopy", 
761
 
            "svncut"], numsel >= 1 && current_file.svnstatus && svn_selection);
762
 
    
763
 
    /* Diff, log and update only support one path at the moment, so we must
764
 
     * have 0 or 1 versioned files selected. If 0, the directory must be
765
 
     * versioned. */
766
 
    single_versioned_path = (
767
 
         (
768
 
          (numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
769
 
          (numsel == 0 && (svnst = current_file.svnstatus))
770
 
         ) && svnst != "unversioned");
771
 
    set_action_state(["svndiff", "svnupdate"], single_versioned_path);
772
 
 
773
 
    /* We can resolve if we have a file selected and it is conflicted. */
774
 
    set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
775
 
 
776
 
    /* Log should be available for revisions as well. */
777
 
    set_action_state("svnlog", single_versioned_path, true);
778
 
 
779
 
    /* There is currently nothing on the More Actions menu of use
780
 
     * when the current file is not a directory. Hence, just remove
781
 
     * it entirely.
782
 
     * (This makes some of the above decisions somewhat redundant).
783
 
     * We also take this opportunity to show the appropriate actions2
784
 
     * bar for this path. It should either be a save or upload widget.
785
 
     */
786
 
    if (current_file.isdir)
787
 
    {
788
 
        var actions2_directory = document.getElementById("actions2_directory");
789
 
        actions2_directory.setAttribute("style", "display: inline;");
790
 
        var moreactions = document.getElementById("moreactions_area");
791
 
        moreactions.setAttribute("style", "display: inline;");
792
 
    }
793
 
    else
794
 
    {
795
 
        var actions2_file = document.getElementById("actions2_file");
796
 
        actions2_file.setAttribute("style", "display: inline;");
 
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");
797
610
    }
798
611
 
799
612
    return;
830
643
        action_unpublish(selected_files);
831
644
        break;
832
645
    case "share":
833
 
        //alert("Not yet implemented: Sharing files");
834
 
        window.open(public_app_path(serve_app, current_path, filename), 'share')
 
646
        // TODO
 
647
        alert("Not yet implemented: Sharing files");
835
648
        break;
836
649
    case "submit":
837
650
        // TODO
841
654
        action_rename(filename);
842
655
        break;
843
656
    case "delete":
844
 
        action_delete(selected_files);
 
657
        action_remove(selected_files);
845
658
        break;
846
659
    case "copy":
847
660
        action_copy(selected_files);
864
677
    case "svnadd":
865
678
        action_add(selected_files);
866
679
        break;
867
 
    case "svnremove":
868
 
        action_remove(selected_files);
869
 
        break;
870
680
    case "svnrevert":
871
681
        action_revert(selected_files);
872
682
        break;
873
 
    case "svndiff":
874
 
        window.location = path_join(app_path('diff'), current_path, selected_files[0] || '');
875
 
        break;
876
 
    case "svnupdate":
877
 
        action_update(selected_files);
878
 
        break;
879
 
    case "svnresolved":
880
 
        action_resolved(selected_files);
881
 
        break;
882
683
    case "svncommit":
883
684
        action_commit(selected_files);
884
685
        break;
885
 
    case "svnlog":
886
 
        window.location = path_join(app_path('svnlog'), current_path, selected_files[0] || '');
887
 
        break;
888
 
    case "svncopy":
889
 
        action_svncopy(selected_files);
890
 
        break;
891
 
    case "svncut":
892
 
        action_svncut(selected_files);
893
 
        break;
894
 
    }
895
 
}
896
 
 
897
 
/** User clicks "Run" button.
898
 
 * Do an Ajax call and print the test output.
899
 
 */
900
 
function runfile(localpath)
901
 
{
902
 
    if (!maybe_save('The last saved version will be run.')) return false;
903
 
 
904
 
    /* Dump the entire file to the console */
905
 
    var callback = function()
906
 
    {
907
 
        console_enter_line("execfile('" + localpath + "')", "block");
908
 
    }
909
 
    start_server(callback)
910
 
    return;
 
686
    }
911
687
}
912
688
 
913
689
/** Called when the page loads initially.
914
690
 */
915
 
function browser_init()
 
691
window.onload = function()
916
692
{
917
693
    /* Navigate (internally) to the path in the URL bar.
918
694
     * This causes the page to be populated with whatever is at that address,