~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-01 01:39:24 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:362
console: Automatically focus input box when starting console app, or when
maximizing console pane. Fixed bug where JS errors occur when there is no
console present (now ignores).

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"
70
69
};
71
70
 
72
71
/* Mapping SVN status to "nice" strings */
80
79
    "modified": "Permanent file (modified)",
81
80
    "merged": "Permanent file (merged)",
82
81
    "conflicted": "Permanent file (conflicted)",
83
 
    "revision": "Past Permanent file (revision)"
84
82
};
85
83
 
86
84
default_svn_icon = null;
95
93
 * application can interpret them.
96
94
 */
97
95
types_exec = [
98
 
    "text/x-python"
 
96
    "text/x-python",
99
97
];
100
98
 
101
99
 
102
100
/* Global variables */
103
101
 
104
 
/** The listing object returned by the server as JSON */
105
 
file_listing = null;
106
 
current_file = null;
107
102
current_path = "";
108
103
 
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
 
 
117
104
/** Calls the server using Ajax, performing an action on the server side.
118
105
 * Receives the response from the server and performs a refresh of the page
119
106
 * contents, updating it to display the returned data (such as a directory
133
120
 *      Defaults to "application/x-www-form-urlencoded".
134
121
 *      "multipart/form-data" is recommended for large uploads.
135
122
 */
136
 
function do_action(action, path, args, content_type, ignore_response)
 
123
function do_action(action, path, args, content_type)
137
124
{
138
125
    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
 
                /* Note: This header (in particular) comes URI-encoded, to
147
 
                 * allow multi-line error messages. Decode */
148
 
                alert("Error: " + decodeURIComponent(error.toString()) + ".");
149
 
            /* Now read the response and set up the page accordingly */
150
 
            if (ignore_response != true)
151
 
                handle_response(path, response, true);
152
 
        }
153
126
    /* Call the server and perform the action. This mutates the server. */
154
 
    ajax_call(callback, service_app, path, args, "POST", content_type);
 
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
    handle_response(path, response);
155
135
}
156
136
 
157
137
/** Calls the server using Ajax, requesting a directory listing. This should
161
141
 * Called "navigate", can also be used for a simple refresh.
162
142
 * Always makes a GET request.
163
143
 * No return value.
164
 
 */
165
 
function navigate(path)
166
 
{
167
 
    callback = function(response)
168
 
        {
169
 
            /* Read the response and set up the page accordingly */
170
 
            handle_response(path, response, false, url.args);
171
 
        }
172
 
    /* Get any query strings */
173
 
    url = parse_url(window.location.href);
174
 
    
175
 
    /* Call the server and request the listing. */
176
 
    ajax_call(callback, service_app, path, url.args, "GET");
177
 
}
178
 
 
179
 
/* Refreshes the current view.
180
 
 * Calls navigate on the current path.
181
 
 */
182
 
function refresh()
183
 
{
184
 
    navigate(current_path);
 
144
 * \param editmode Optional boolean. If true, then the user navigated here
 
145
 * with an "edit" URL so we should favour using the editor.
 
146
 */
 
147
function navigate(path, editmode)
 
148
{
 
149
    /* Call the server and request the listing. This mutates the server. */
 
150
    response = ajax_call(service_app, path, null, "GET");
 
151
    /* Now read the response and set up the page accordingly */
 
152
    handle_response(path, response, editmode);
185
153
}
186
154
 
187
155
/** Determines the "handler type" from a MIME type.
213
181
 * things) be used to update the URL in the location bar.
214
182
 * \param response XMLHttpRequest object returned by the server. Should
215
183
 * contain all the response data.
216
 
 * \param is_action Boolean. True if this is the response to an action, false
217
 
 * if this is the response to a simple listing. This is used in handling the
218
 
 * error.
219
 
 * \param url_args Arguments dict, for the arguments passed to the URL
220
 
 * in the browser's address bar (will be forwarded along).
 
184
 * \param editmode Optional boolean. If true, then the user navigated here
 
185
 * with an "edit" URL so we should favour using the editor.
221
186
 */
222
 
function handle_response(path, response, is_action, url_args)
 
187
function handle_response(path, response, editmode)
223
188
{
224
189
    /* TODO: Set location bar to "path" */
225
190
    current_path = path;
226
191
 
227
192
    /* Clear away the existing page contents */
228
193
    clearpage();
 
194
    /* Display the path at the top, for navigation */
 
195
    presentpath(path);
229
196
 
230
197
    /* Check the status, and if not 200, read the error and handle this as an
231
198
     * error. */
238
205
        return;
239
206
    }
240
207
 
241
 
    /* This will always return a listing, whether it is a dir or a file.
242
 
     */
243
 
    var listing = response.responseText;
244
 
    /* The listing SHOULD be valid JSON text. Parse it into an object. */
245
 
    try
246
 
    {
247
 
        listing = JSON.parse(listing);
248
 
        file_listing = listing.listing;     /* Global */
249
 
    }
250
 
    catch (e)
251
 
    {
252
 
        if (is_action)
253
 
        {
254
 
            var err = document.createElement("div");
255
 
            var p = dom_make_text_elem("p", "Error: "
256
 
                    + "There was an unexpected server error processing "
257
 
                    + "the selected command.");
258
 
            err.appendChild(p);
259
 
            p = dom_make_text_elem("p", "If the problem persists, please "
260
 
                    + "contact the system administrator.")
261
 
            err.appendChild(p);
262
 
            p = document.createElement("p");
263
 
            var refresh = document.createElement("input");
264
 
            refresh.setAttribute("type", "button");
265
 
            refresh.setAttribute("value", "Back to file view");
266
 
            refresh.setAttribute("onclick", "refresh()");
267
 
            p.appendChild(refresh);
268
 
            err.appendChild(p);
269
 
            handle_error(err);
270
 
        }
271
 
        else
272
 
        {
273
 
            var err = document.createElement("div");
274
 
            var p = dom_make_text_elem("p", "Error: "
275
 
                    + "There was an unexpected server error retrieving "
276
 
                    + "the requested file or directory.");
277
 
            err.appendChild(p);
278
 
            p = dom_make_text_elem("p", "If the problem persists, please "
279
 
                    + "contact the system administrator.")
280
 
            err.appendChild(p);
281
 
            handle_error(err);
282
 
        }
283
 
        return;
284
 
    }
285
 
    /* Get "." out, it's special */
286
 
    current_file = file_listing["."];     /* Global */
287
 
    delete file_listing["."];
288
 
 
289
208
    /* Check if this is a directory listing or file contents */
290
209
    var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
291
 
    if (isdir)
 
210
    if (!editmode && isdir)
292
211
    {
 
212
        var listing = response.responseText;
 
213
        /* The listing SHOULD be valid JSON text. Parse it into an object. */
 
214
        try
 
215
        {
 
216
            listing = JSON.parse(listing);
 
217
        }
 
218
        catch (e)
 
219
        {
 
220
            handle_error("The server returned an invalid directory listing");
 
221
            return;
 
222
        }
293
223
        handle_dir_listing(path, listing);
294
224
    }
295
225
    else
296
226
    {
297
 
        /* Need to make a 2nd ajax call, this time get the actual file
298
 
         * contents */
299
 
        callback = function(response)
300
 
            {
301
 
                /* Read the response and set up the page accordingly */
302
 
                handle_contents_response(path, response);
303
 
            }
304
 
        /* Call the server and request the listing. */
305
 
        if (url_args)
306
 
            args = shallow_clone_object(url_args);
307
 
        else
308
 
            args = {};
309
 
        /* This time, get the contents of the file, not its metadata */
310
 
        args['return'] = "contents";
311
 
        ajax_call(callback, service_app, path, args, "GET");
312
 
    }
313
 
    update_actions(isdir);
314
 
}
315
 
 
316
 
function handle_contents_response(path, response)
317
 
{
318
 
    /* Treat this as an ordinary file. Get the file type. */
319
 
    var content_type = response.getResponseHeader("Content-Type");
320
 
    var handler_type = get_handler_type(content_type);
321
 
    would_be_handler_type = handler_type;
322
 
    /* handler_type should now be set to either
323
 
     * "text", "image", "audio" or "binary". */
324
 
    switch (handler_type)
325
 
    {
326
 
    case "text":
327
 
        handle_text(path, response.responseText,
328
 
            would_be_handler_type);
329
 
        break;
330
 
    case "image":
331
 
        /* TODO: Custom image handler */
332
 
        handle_binary(path, response.responseText);
333
 
        break;
334
 
    case "audio":
335
 
        /* TODO: Custom audio handler */
336
 
        handle_binary(path, response.responseText);
337
 
        break;
338
 
    case "binary":
339
 
        handle_binary(path);
340
 
        break;
341
 
    }
342
 
}
343
 
 
344
 
/* Called when a form upload comes back (from an iframe).
345
 
 * Refreshes the page.
346
 
 */
347
 
function upload_callback()
348
 
{
349
 
    /* This has a pretty nasty hack, which happens to work.
350
 
     * upload_callback is set as the "onload" callback for the iframe which
351
 
     * receives the response from the server for uploading a file.
352
 
     * This means it gets called twice. Once when initialising the iframe, and
353
 
     * a second time when the actual response comes back.
354
 
     * All we want to do is call navigate to refresh the page. But we CAN'T do
355
 
     * that on the first load or it will just go into an infinite cycle of
356
 
     * refreshing. We need to refresh the page ONLY on the second refresh.
357
 
     * upload_callback_count is reset to 0 just before the iframe is created.
358
 
     */
359
 
    upload_callback_count++;
360
 
    if (upload_callback_count >= 2)
361
 
        refresh();
 
227
        /* Treat this as an ordinary file. Get the file type. */
 
228
        var content_type = response.getResponseHeader("Content-Type");
 
229
        var handler_type = get_handler_type(content_type);
 
230
        /* If we're in "edit mode", always treat this file as text */
 
231
        would_be_handler_type = handler_type;
 
232
        if (editmode) handler_type = "text";
 
233
        /* handler_type should now be set to either
 
234
         * "text", "image", "audio" or "binary". */
 
235
        switch (handler_type)
 
236
        {
 
237
        case "text":
 
238
            if (isdir)
 
239
            {
 
240
                handle_text(path_join(path, "untitled"), "",
 
241
                    would_be_handler_type);
 
242
            }
 
243
            else
 
244
            {
 
245
                handle_text(path, response.responseText,
 
246
                    would_be_handler_type);
 
247
            }
 
248
            break;
 
249
        case "image":
 
250
            /* TODO: Custom image handler */
 
251
            handle_binary(path, response.responseText);
 
252
            break;
 
253
        case "audio":
 
254
            /* TODO: Custom audio handler */
 
255
            handle_binary(path, response.responseText);
 
256
            break;
 
257
        case "binary":
 
258
            handle_binary(path);
 
259
            break;
 
260
        }
 
261
    }
362
262
}
363
263
 
364
264
/** Deletes all "dynamic" content on the page.
367
267
 */
368
268
function clearpage()
369
269
{
 
270
    dom_removechildren(document.getElementById("path"));
370
271
    dom_removechildren(document.getElementById("filesbody"));
371
272
}
372
273
 
383
284
    dom_removechildren(document.getElementById("sidepanel"));
384
285
}
385
286
 
 
287
/** Sets the mode to either "file browser" or "text editor" mode.
 
288
 * This modifies the window icon, and selected tab.
 
289
 * \param editmode If True, editor mode. Else, file browser mode.
 
290
 */
 
291
function setmode(editmode)
 
292
{
 
293
    /* Find the DOM elements for the file browser and editor tabs */
 
294
    var tabs = document.getElementById("apptabs");
 
295
    var tab_files = null;
 
296
    var tab_edit = null;
 
297
    var a;
 
298
    var href;
 
299
    for (var i=0; i<tabs.childNodes.length; i++)
 
300
    {
 
301
        /* Find the href of the link within */
 
302
        if (!tabs.childNodes[i].getElementsByTagName) continue;
 
303
        a = tabs.childNodes[i].getElementsByTagName("a");
 
304
        if (a.length == 0) continue;
 
305
        href = a[0].getAttribute("href");
 
306
        if (href == null) continue;
 
307
        if (endswith(href, this_app))
 
308
            tab_files = tabs.childNodes[i];
 
309
        else if (endswith(href, edit_app))
 
310
            tab_edit = tabs.childNodes[i];
 
311
    }
 
312
 
 
313
    if (editmode)
 
314
    {
 
315
        tab_files.removeAttribute("class");
 
316
        tab_edit.setAttribute("class", "thisapp");
 
317
    }
 
318
    else
 
319
    {
 
320
        tab_edit.removeAttribute("class");
 
321
        tab_files.setAttribute("class", "thisapp");
 
322
    }
 
323
}
 
324
 
386
325
/*** HANDLERS for different types of responses (such as dir listing, file,
387
326
 * etc). */
388
327
 
389
 
/* handle_error.
390
 
 * message may either be a string, or a DOM node, which will be placed inside
391
 
 * a div.
392
 
 */
393
328
function handle_error(message)
394
329
{
 
330
    setmode(false);
395
331
    var files = document.getElementById("filesbody");
396
 
    var txt_elem;
397
 
    if (typeof(message) == "string")
398
 
    {
399
 
        txt_elem = dom_make_text_elem("div", "Error: "
400
 
                   + message.toString() + ".")
401
 
    }
402
 
    else
403
 
    {
404
 
        /* Assume message is a DOM node */
405
 
        txt_elem = document.createElement("div");
406
 
        txt_elem.appendChild(message);
407
 
    }
 
332
    var txt_elem = dom_make_text_elem("div", "Error: "
 
333
        + message.toString() + ".")
408
334
    txt_elem.setAttribute("class", "padding error");
409
335
    files.appendChild(txt_elem);
410
336
}
411
337
 
 
338
/** Presents a path list (address bar inside the page) for clicking.
 
339
 */
 
340
function presentpath(path)
 
341
{
 
342
    var dom_path = document.getElementById("path");
 
343
    var href_path = make_path(this_app);
 
344
    var nav_path = "";
 
345
    var dir;
 
346
 
 
347
    /* Also set the document title */
 
348
    document.title = path_basename(path) + " - IVLE";
 
349
    /* Create all of the paths */
 
350
    var pathlist = path.split("/");
 
351
    for (var i=0; i<pathlist.length; i++)
 
352
    {
 
353
        dir = pathlist[i];
 
354
        if (dir == "") continue;
 
355
        /* Make an 'a' element */
 
356
        href_path = path_join(href_path, dir);
 
357
        nav_path = path_join(nav_path, dir);
 
358
        var link = dom_make_link_elem("a", dir, "Navigate to " + nav_path,
 
359
                href_path/*, "navigate(" + repr(href_path) + ")"*/);
 
360
        dom_path.appendChild(link);
 
361
        dom_path.appendChild(document.createTextNode("/"));
 
362
    }
 
363
    dom_path.removeChild(dom_path.lastChild);
 
364
}
 
365
 
412
366
/** Given a mime type, returns the path to the icon.
413
367
 * \param type String, Mime type.
414
368
 * \param sizelarge Boolean, optional.
458
412
 */
459
413
function handle_binary(path)
460
414
{
 
415
    setmode(false);
461
416
    var files = document.getElementById("filesbody");
462
417
    var div = document.createElement("div");
463
418
    files.appendChild(div);
472
427
    div.appendChild(par2);
473
428
}
474
429
 
475
 
function update_actions()
476
 
{
477
 
    var file;
478
 
    var numsel = selected_files.length;
479
 
    if (numsel <= 1)
480
 
    {
481
 
        if (numsel == 0)
482
 
        {
483
 
            /* Display information about the current directory instead */
484
 
            filename = path_basename(current_path);
485
 
            file = current_file;
486
 
        }
487
 
        else if (numsel == 1)
488
 
        {
489
 
            filename = selected_files[0];
490
 
            file = file_listing[filename];
491
 
        }
492
 
 
493
 
        /* Update each action node in the topbar.
494
 
         * This includes enabling/disabling actions as appropriate, and
495
 
         * setting href/onclick attributes. */
496
 
    }
497
 
 
498
 
    /* Open */
499
 
    /* Available if exactly one file is selected */
500
 
    var open = document.getElementById("act_open");
501
 
    if (numsel == 1)
502
 
    {
503
 
        open.setAttribute("class", "choice");
504
 
        if (file.isdir)
505
 
            open.setAttribute("title",
506
 
                "Navigate to this directory in the file browser");
507
 
        else
508
 
            open.setAttribute("title",
509
 
                "Edit or view this file");
510
 
        open.setAttribute("href", app_path(this_app, current_path, filename));
511
 
    }
512
 
    else
513
 
    {
514
 
        open.setAttribute("class", "disabled");
515
 
        open.removeAttribute("title");
516
 
        open.removeAttribute("href");
517
 
    }
518
 
 
519
 
    /* Serve */
520
 
    /* Available if zero or one files are selected,
521
 
     * and only if this is a file, not a directory */
522
 
    var serve = document.getElementById("act_serve");
523
 
    if (numsel <= 1 && !file.isdir)
524
 
    {
525
 
        serve.setAttribute("class", "choice");
526
 
        if (numsel == 0)
527
 
            serve.setAttribute("href",
528
 
                app_path(serve_app, current_path));
529
 
        else
530
 
            serve.setAttribute("href",
531
 
                app_path(serve_app, current_path, filename));
532
 
    }
533
 
    else
534
 
    {
535
 
        serve.setAttribute("class", "disabled");
536
 
        serve.removeAttribute("href");
537
 
    }
538
 
 
539
 
    /* Run */
540
 
    /* Available if exactly one file is selected,
541
 
     * and it is a Python file.
542
 
     */
543
 
    var run = document.getElementById("act_run");
544
 
     
545
 
    if (numsel == 0 && !file.isdir && file.type == "text/x-python")
546
 
    {
547
 
        // In the edit window
548
 
        run.setAttribute("class", "choice");
549
 
        localpath = app_path('home',current_path);
550
 
        run.setAttribute("onclick", "runfile('" + localpath + "')");
551
 
    }
552
 
    else if (numsel == 1 && !file.isdir && file.type == "text/x-python")
553
 
    {
554
 
        // In the browser window
555
 
        run.setAttribute("class", "choice");
556
 
        localpath = app_path('home',current_path,filename);
557
 
        run.setAttribute("onclick", "runfile('" + localpath + "')");
558
 
    }
559
 
    else
560
 
    {
561
 
        run.setAttribute("class", "disabled");
562
 
        run.removeAttribute("onclick");
563
 
    }
564
 
 
565
 
    /* Download */
566
 
    /* Always available.
567
 
     * If 0 files selected, download the current file or directory as a ZIP.
568
 
     * If 1 directory selected, download it as a ZIP.
569
 
     * If 1 non-directory selected, download it.
570
 
     * If >1 files selected, download them all as a ZIP.
571
 
     */
572
 
    var download = document.getElementById("act_download");
573
 
    if (numsel <= 1)
574
 
    {
575
 
        if (numsel == 0)
576
 
        {
577
 
            download.setAttribute("href",
578
 
                app_path(download_app, current_path));
579
 
            if (file.isdir)
580
 
                download.setAttribute("title",
581
 
                    "Download the current directory as a ZIP file");
582
 
            else
583
 
                download.setAttribute("title",
584
 
                    "Download the current file");
585
 
        }
586
 
        else
587
 
        {
588
 
            download.setAttribute("href",
589
 
                app_path(download_app, current_path, filename));
590
 
            if (file.isdir)
591
 
                download.setAttribute("title",
592
 
                    "Download the selected directory as a ZIP file");
593
 
            else
594
 
                download.setAttribute("title",
595
 
                    "Download the selected file");
596
 
        }
597
 
    }
598
 
    else
599
 
    {
600
 
        /* Make a query string with all the files to download */
601
 
        var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
602
 
        for (var i=0; i<numsel; i++)
603
 
            dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
604
 
        dlpath = dlpath.substr(0, dlpath.length-1);
605
 
        download.setAttribute("href", dlpath);
606
 
        download.setAttribute("title",
607
 
            "Download the selected files as a ZIP file");
608
 
    }
609
 
 
610
 
    /* Refresh - No changes required */
611
 
 
612
 
    /* Publish and Submit */
613
 
    /* If this directory is under subversion and selected/unselected file is a
614
 
     * directory. */
615
 
    var publish = document.getElementById("act_publish");
616
 
    var submit = document.getElementById("act_submit");
617
 
    if (numsel <= 1 && file.isdir)
618
 
    {
619
 
        /* TODO: Work out of file is svn'd */
620
 
        publish.setAttribute("class", "choice");
621
 
        publish.removeAttribute("disabled");
622
 
        /* If this dir is already published, call it "Unpublish" */
623
 
        if (file.published)
624
 
        {
625
 
            publish.setAttribute("value", "unpublish");
626
 
            publish.setAttribute("title" ,"Make it so this directory "
627
 
                + "can not be seen by anyone on the web");
628
 
            publish.textContent = "Unpublish";
629
 
        } else {
630
 
            publish.setAttribute("value", "publish");
631
 
            publish.setAttribute("title","Make it so this directory "
632
 
                + "can be seen by anyone on the web");
633
 
            publish.textContent = "Publish";
634
 
        }
635
 
        submit.setAttribute("class", "choice");
636
 
        submit.removeAttribute("disabled");
637
 
    }
638
 
    else
639
 
    {
640
 
        publish.setAttribute("class", "disabled");
641
 
        publish.setAttribute("disabled", "disabled");
642
 
        submit.setAttribute("class", "disabled");
643
 
        submit.setAttribute("disabled", "disabled");
644
 
    }
645
 
 
646
 
    /* Share */
647
 
    /* If exactly 1 non-directory file is selected/opened, and its parent
648
 
     * directory is published.
649
 
     */
650
 
    var share = document.getElementById("act_share");
651
 
    if (numsel <= 1 && !file.isdir)
652
 
    {
653
 
        /* Work out if parent dir is published */
654
 
        parentdir = current_file;
655
 
        if (parentdir.published)
656
 
        {
657
 
            share.setAttribute("class", "choice");
658
 
            share.removeAttribute("disabled");
659
 
        } else {
660
 
            share.setAttribute("class", "disabled");
661
 
            share.setAttribute("disabled", "disabled");
662
 
        }
663
 
    }
664
 
    else
665
 
    {
666
 
        share.setAttribute("class", "disabled");
667
 
        share.setAttribute("disabled", "disabled");
668
 
    }
669
 
 
670
 
    /* Rename */
671
 
    /* If exactly 1 file is selected */
672
 
    var rename = document.getElementById("act_rename");
673
 
    if (numsel == 1)
674
 
    {
675
 
        rename.setAttribute("class", "choice");
676
 
        rename.removeAttribute("disabled");
677
 
    }
678
 
    else
679
 
    {
680
 
        rename.setAttribute("class", "disabled");
681
 
        rename.setAttribute("disabled", "disabled");
682
 
    }
683
 
 
684
 
    /* Delete, cut, copy */
685
 
    /* If >= 1 file is selected */
686
 
    var act_delete = document.getElementById("act_delete");
687
 
    var cut = document.getElementById("act_cut");
688
 
    var copy = document.getElementById("act_copy");
689
 
    if (numsel >= 1)
690
 
    {
691
 
        act_delete.setAttribute("class", "choice");
692
 
        act_delete.removeAttribute("disabled");
693
 
        cut.setAttribute("class", "choice");
694
 
        cut.removeAttribute("disabled");
695
 
        copy.setAttribute("class", "choice");
696
 
        copy.removeAttribute("disabled");
697
 
    }
698
 
    else
699
 
    {
700
 
        act_delete.setAttribute("class", "disabled");
701
 
        act_delete.setAttribute("disabled", "disabled");
702
 
        cut.setAttribute("class", "disabled");
703
 
        cut.setAttribute("disabled", "disabled");
704
 
        copy.setAttribute("class", "disabled");
705
 
        copy.setAttribute("disabled", "disabled");
706
 
    }
707
 
 
708
 
    /* Paste, new file, new directory, upload */
709
 
    /* Disable if the current file is not a directory */
710
 
    if (!current_file.isdir)
711
 
    {
712
 
        var paste = document.getElementById("act_paste");
713
 
        var newfile = document.getElementById("act_newfile");
714
 
        var mkdir = document.getElementById("act_mkdir");
715
 
        var upload = document.getElementById("act_upload");
716
 
        paste.setAttribute("class", "disabled");
717
 
        paste.setAttribute("disabled", "disabled");
718
 
        newfile.setAttribute("class", "disabled");
719
 
        newfile.setAttribute("disabled", "disabled");
720
 
        mkdir.setAttribute("class", "disabled");
721
 
        mkdir.setAttribute("disabled", "disabled");
722
 
        upload.setAttribute("class", "disabled");
723
 
        upload.setAttribute("disabled", "disabled");
724
 
    }
725
 
 
726
 
    /* Subversion actions */
727
 
    /* TODO: Work out when these are appropriate */
728
 
    var svnadd = document.getElementById("act_svnadd");
729
 
    var svnrevert = document.getElementById("act_svnrevert");
730
 
    var svncommit = document.getElementById("act_svncommit");
731
 
    if (true)
732
 
    {
733
 
        svnadd.setAttribute("class", "choice");
734
 
        svnadd.removeAttribute("disabled");
735
 
        svnrevert.setAttribute("class", "choice");
736
 
        svnrevert.removeAttribute("disabled");
737
 
        svncommit.setAttribute("class", "choice");
738
 
        svncommit.removeAttribute("disabled");
739
 
    }
740
 
    var svncheckout = document.getElementById("act_svncheckout");
741
 
    /* current_path == username: We are at the top level */
742
 
    if (current_path == username)
743
 
    {
744
 
        svncheckout.setAttribute("class", "choice");
745
 
        svncheckout.removeAttribute("disabled");
746
 
    }
747
 
    else
748
 
    {
749
 
        svncheckout.setAttribute("class", "disabled");
750
 
        svncheckout.setAttribute("disabled", "disabled");
751
 
    }
752
 
 
753
 
    /* There is currently nothing on the More Actions menu of use
754
 
     * when the current file is not a directory. Hence, just remove
755
 
     * it entirely.
756
 
     * (This makes some of the above decisions somewhat redundant).
757
 
     */
758
 
    if (!(current_file.isdir))
759
 
    {
760
 
        var moreactions = document.getElementById("moreactions_area");
761
 
        moreactions.setAttribute("style", "display: none;");
762
 
    }
763
 
 
764
 
    return;
765
 
}
766
 
 
767
 
/** Event handler for when an item of the "More actions..." dropdown box is
768
 
 * selected. Performs the selected action. */
769
 
function handle_moreactions()
770
 
{
771
 
    var moreactions = document.getElementById("moreactions");
772
 
    if (moreactions.value == "top")
773
 
        return;
774
 
    var selectedaction = moreactions.value;
775
 
    /* Reset to "More actions..." */
776
 
    moreactions.selectedIndex = 0;
777
 
 
778
 
    /* If 0 files selected, filename is the name of the current dir.
779
 
     * If 1 file selected, filename is that file.
780
 
     */
781
 
    if (selected_files.length == 0)
782
 
        filename = path_basename(current_path);
783
 
    else if (selected_files.length == 1)
784
 
        filename = selected_files[0];
785
 
    else
786
 
        filename = null;
787
 
 
788
 
    /* Now handle the selected action */
789
 
    switch(selectedaction)
790
 
    {
791
 
    case "publish":
792
 
        action_publish(selected_files);
793
 
        break;
794
 
    case "unpublish":
795
 
        action_unpublish(selected_files);
796
 
        break;
797
 
    case "share":
798
 
        //alert("Not yet implemented: Sharing files");
799
 
        window.open(public_app_path(serve_app, current_path, filename), 'share')
800
 
        break;
801
 
    case "submit":
802
 
        // TODO
803
 
        alert("Not yet implemented: Submit");
804
 
        break;
805
 
    case "rename":
806
 
        action_rename(filename);
807
 
        break;
808
 
    case "delete":
809
 
        action_remove(selected_files);
810
 
        break;
811
 
    case "copy":
812
 
        action_copy(selected_files);
813
 
        break;
814
 
    case "cut":
815
 
        action_cut(selected_files);
816
 
        break;
817
 
    case "paste":
818
 
        action_paste();
819
 
        break;
820
 
    case "newfile":
821
 
        action_newfile();
822
 
        break;
823
 
    case "mkdir":
824
 
        action_mkdir();
825
 
        break;
826
 
    case "upload":
827
 
        show_uploadpanel(true);
828
 
        break;
829
 
    case "svnadd":
830
 
        action_add(selected_files);
831
 
        break;
832
 
    case "svnrevert":
833
 
        action_revert(selected_files);
834
 
        break;
835
 
    case "svncommit":
836
 
        action_commit(selected_files);
837
 
        break;
838
 
    case "svncheckout":
839
 
        action_checkout();
840
 
        break;
841
 
    }
842
 
}
843
 
 
844
 
/** User clicks "Run" button.
845
 
 * Do an Ajax call and print the test output.
846
 
 */
847
 
function runfile(localpath)
848
 
{
849
 
    /* Dump the entire file to the console */
850
 
    var callback = function()
851
 
    {
852
 
        console_enter_line("execfile('" + localpath + "')", "block");
853
 
    }
854
 
    start_server(callback)
855
 
    return;
856
 
}
857
 
 
858
430
/** Called when the page loads initially.
859
431
 */
860
432
window.onload = function()
866
438
    var path = parse_url(window.location.href).path;
867
439
    /* Strip out root_dir + "/files" from the front of the path */
868
440
    var strip = make_path(this_app);
 
441
    var editmode = false;
869
442
    if (path.substr(0, strip.length) == strip)
870
443
        path = path.substr(strip.length+1);
871
444
    else
875
448
        if (path.substr(0, strip.length) == strip)
876
449
        {
877
450
            path = path.substr(strip.length+1);
 
451
            editmode = true;
878
452
        }
879
453
    }
880
454
 
885
459
        path = username;
886
460
    }
887
461
 
888
 
    navigate(path);
889
 
 
890
 
    /* Set up the console plugin to display as a popup window */
891
 
    console_init(true);
 
462
    navigate(path, editmode);
892
463
}