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

« back to all changes in this revision

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

  • Committer: mattgiuca
  • Date: 2008-02-03 11:38:23 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:387
Implemented file uploads.
fileservice: Added "putfiles" action (slightly different to putfile action;
    see comments at top which discuss the difference).
browser/listing: Added "upload" button to side panel, which opens an upload
    panel with a file selection dialog. This uses a little trick (documented
    in the code) to get the upload to work without refreshing the page.

Show diffs side-by-side

added added

removed removed

Lines of Context:
113
113
    return false;
114
114
}
115
115
 
 
116
/* Shows or hides the "upload panel" in the side panel.
 
117
 * toshow is true for showing, false for hiding.
 
118
 */
 
119
function show_uploadpanel(toshow)
 
120
{
 
121
    document.getElementById("uploadpanel").setAttribute("style",
 
122
        "display: " + (toshow ? "auto" : "none") + ";");
 
123
    return false;
 
124
}
 
125
 
 
126
/* Called when a form upload comes back (from an iframe).
 
127
 * Refreshes the page.
 
128
 */
 
129
function upload_callback()
 
130
{
 
131
    /* This has a pretty nasty hack, which happens to work.
 
132
     * upload_callback is set as the "onload" callback for the iframe which
 
133
     * receives the response from the server for uploading a file.
 
134
     * This means it gets called twice. Once when initialising the iframe, and
 
135
     * a second time when the actual response comes back.
 
136
     * All we want to do is call navigate to refresh the page. But we CAN'T do
 
137
     * that on the first load or it will just go into an infinite cycle of
 
138
     * refreshing. We need to refresh the page ONLY on the second refresh.
 
139
     * upload_callback_count is reset to 0 just before the iframe is created.
 
140
     */
 
141
    upload_callback_count++;
 
142
    if (upload_callback_count == 2)
 
143
    {
 
144
        navigate(current_path);
 
145
        /* Keep upload panel open */
 
146
        show_uploadpanel(true);
 
147
    }
 
148
}
 
149
 
116
150
/** END ACTIONS **/
117
151
 
118
152
/** Updates the side-panel. Expects selected_files reflects the current
124
158
    var filename;
125
159
    var file;
126
160
    var p;
 
161
    var div;
127
162
    /* Is this dir under svn? */
128
163
    var under_subversion = "svnstatus" in thisdir;
129
164
    dom_removechildren(sidepanel);
337
372
        "Paste the copied or cut files to the current directory", null,
338
373
        "return action_paste()");
339
374
    sidepanel.appendChild(p);
 
375
    p = dom_make_link_elem("p", "Upload",
 
376
        "Upload a file to the current directory", null,
 
377
        "return show_uploadpanel(true)");
 
378
    sidepanel.appendChild(p);
 
379
    /* The "Upload" button expands the following panel with upload tools */
 
380
    /* This panel has a form for submitting the file to, and an iframe to load
 
381
     * the target page in (this avoids the entire page being refreshed) */
 
382
    div = document.createElement("div");
 
383
    div.setAttribute("id", "uploadpanel");
 
384
    /* This deliberately hides the upload panel whenever the selection
 
385
     * changes. It can be re-shown by clicking "upload". */
 
386
    div.setAttribute("style", "display: none;");
 
387
    sidepanel.appendChild(div);
 
388
    p = dom_make_text_elem("h3", "Upload File");
 
389
    div.appendChild(p);
 
390
    var form = document.createElement("form");
 
391
    form.setAttribute("method", "POST");
 
392
    form.setAttribute("enctype", "multipart/form-data");
 
393
    form.setAttribute("action", app_path("fileservice", current_path));
 
394
    form.setAttribute("target", "upload_iframe");
 
395
    div.appendChild(form);
 
396
    var input;
 
397
    input = document.createElement("input");
 
398
    input.setAttribute("type", "hidden");
 
399
    input.setAttribute("name", "action");
 
400
    input.setAttribute("value", "putfiles");
 
401
    form.appendChild(input);
 
402
 
 
403
    input = document.createElement("input");
 
404
    input.setAttribute("type", "hidden");
 
405
    input.setAttribute("name", "path");
 
406
    input.setAttribute("value", "");
 
407
    form.appendChild(input);
 
408
 
 
409
    p = document.createElement("p");
 
410
    form.appendChild(p);
 
411
    input = document.createElement("input");
 
412
    input.setAttribute("type", "file");
 
413
    input.setAttribute("name", "data");
 
414
    p.appendChild(input);
 
415
 
 
416
    p = document.createElement("p");
 
417
    form.appendChild(p);
 
418
    input = document.createElement("input");
 
419
    input.setAttribute("type", "button");
 
420
    input.setAttribute("value", "Hide");
 
421
    input.setAttribute("onclick", "show_uploadpanel(false)");
 
422
    p.appendChild(input);
 
423
    p.appendChild(document.createTextNode(" "));
 
424
    input = document.createElement("input");
 
425
    input.setAttribute("type", "submit");
 
426
    input.setAttribute("value", "Send");
 
427
    p.appendChild(input);
 
428
 
 
429
    /* Now we create an invisible iframe which will receive the upload.
 
430
     * The form submits to fileservice, loading the result into this iframe
 
431
     * instead of the whole browser window (this is an alternative to Ajax,
 
432
     * since Ajax doesn't allow reading the file from the user's disk).
 
433
     * Note this iframe's id is the same as the form's target.
 
434
     */
 
435
    var upload_iframe = document.createElement("iframe");
 
436
    upload_iframe.setAttribute("id", "upload_iframe");
 
437
    upload_iframe.setAttribute("name", "upload_iframe");
 
438
    upload_iframe.setAttribute("style", "display: none;");
 
439
    /* When we get a callback, simply cause a nav to the current path, so we
 
440
     * update the directory listing. */
 
441
    upload_callback_count = 0;      /* See upload_callback */
 
442
    upload_iframe.setAttribute("onload", "upload_callback()");
 
443
    div.appendChild(upload_iframe);
 
444
    /* END Upload panel */
340
445
 
341
446
    if (under_subversion)
342
447
    {