~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-01-13 11:03:44 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:209
browser.js: Split out dir-listing code into listing.js (it's going to get
    bigger).
listing.js: Added code to handle selection. Clicking checkboxes causes it to
    highlight selected files. Clicking a file row will select just that file.
    Also maintains a list of selected files.

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";
60
60
 
61
61
/* Mapping SVN status to icons, just the file's basename */
62
62
svn_icons = {
63
 
    "unversioned": null,
 
63
    "unversioned": "unversioned.png",
64
64
    "normal": "normal.png",
65
 
    "added": "added.png",
66
 
    "missing": "missing.png",
67
 
    "deleted": "deleted.png",
68
65
    "modified": "modified.png",
69
 
    "revision": "revision.png"
70
 
};
71
 
 
72
 
/* Mapping SVN status to "nice" strings */
73
 
svn_nice = {
74
 
    "unversioned": "Temporary file",
75
 
    "normal": "Permanent file",
76
 
    "added": "Temporary file (scheduled to be added)",
77
 
    "missing": "Permanent file (missing)",
78
 
    "deleted": "Permanent file (scheduled for deletion)",
79
 
    "replaced": "Permanent file (replaced)",
80
 
    "modified": "Permanent file (modified)",
81
 
    "merged": "Permanent file (merged)",
82
 
    "conflicted": "Permanent file (conflicted)",
83
 
    "revision": "Past Permanent file (revision)"
84
 
};
85
 
 
86
 
default_svn_icon = null;
87
 
default_svn_nice = "Unknown status";
 
66
};
 
67
 
 
68
default_svn_icon = "normal.png";
88
69
 
89
70
svn_icons_path = "media/images/svn";
90
71
 
91
 
published_icon = "media/images/interface/published.png";
92
 
 
93
72
/* List of MIME types considered "executable" by the system.
94
73
 * Executable files offer a "run" link, implying that the "serve"
95
74
 * application can interpret them.
96
75
 */
97
76
types_exec = [
98
 
    "text/x-python"
 
77
    "text/x-python",
99
78
];
100
79
 
101
 
 
102
 
/* Global variables */
103
 
 
104
 
/** The listing object returned by the server as JSON */
105
 
file_listing = null;
106
 
current_file = null;
107
 
current_path = "";
108
 
 
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
80
/** Calls the server using Ajax, performing an action on the server side.
118
81
 * Receives the response from the server and performs a refresh of the page
119
82
 * contents, updating it to display the returned data (such as a directory
133
96
 *      Defaults to "application/x-www-form-urlencoded".
134
97
 *      "multipart/form-data" is recommended for large uploads.
135
98
 */
136
 
function do_action(action, path, args, content_type, ignore_response)
 
99
function do_action(action, path, args, content_type)
137
100
{
138
101
    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
 
                alert("Error: " + error.toString() + ".");
147
 
            /* Now read the response and set up the page accordingly */
148
 
            if (ignore_response != true)
149
 
                handle_response(path, response);
150
 
        }
151
102
    /* Call the server and perform the action. This mutates the server. */
152
 
    ajax_call(callback, service_app, path, args, "POST", content_type);
 
103
    response = ajax_call(service_app, path, args, "POST", content_type);
 
104
    /* Check for action errors reported by the server, and report them to the
 
105
     * user */
 
106
    error = response.getResponseHeader("X-IVLE-Action-Error");
 
107
    if (error != null)
 
108
        alert("Error: " + error.toString() + ".");
 
109
    /* Now read the response and set up the page accordingly */
 
110
    handle_response(path, response);
153
111
}
154
112
 
155
113
/** Calls the server using Ajax, requesting a directory listing. This should
159
117
 * Called "navigate", can also be used for a simple refresh.
160
118
 * Always makes a GET request.
161
119
 * No return value.
162
 
 */
163
 
function navigate(path)
164
 
{
165
 
    callback = function(response)
166
 
        {
167
 
            /* Read the response and set up the page accordingly */
168
 
            handle_response(path, response, url.args);
169
 
        }
170
 
    /* Get any query strings */
171
 
    url = parse_url(window.location.href);
172
 
    
173
 
    /* Call the server and request the listing. */
174
 
    ajax_call(callback, service_app, path, url.args, "GET");
175
 
}
176
 
 
177
 
/* Refreshes the current view.
178
 
 * Calls navigate on the current path.
179
 
 */
180
 
function refresh()
181
 
{
182
 
    navigate(current_path);
183
 
}
184
 
 
185
 
/** Determines the "handler type" from a MIME type.
186
 
 * The handler type is a string, either "text", "image", "audio" or "binary".
187
 
 */
188
 
function get_handler_type(content_type)
189
 
{
190
 
    if (!content_type)
191
 
        return null;
192
 
    if (content_type in type_handlers)
193
 
        return type_handlers[content_type];
194
 
    else
195
 
    {   /* Based on the first part of the MIME type */
196
 
        var handler_type = content_type.split('/')[0];
197
 
        if (handler_type != "text" && handler_type != "image" &&
198
 
            handler_type != "audio")
199
 
            handler_type = "binary";
200
 
        return handler_type;
201
 
    }
 
120
 * \param editmode Optional boolean. If true, then the user navigated here
 
121
 * with an "edit" URL so we should favour using the editor.
 
122
 */
 
123
function navigate(path, editmode)
 
124
{
 
125
    /* Call the server and request the listing. This mutates the server. */
 
126
    response = ajax_call(service_app, path, null, "GET");
 
127
    /* Now read the response and set up the page accordingly */
 
128
    handle_response(path, response, editmode);
202
129
}
203
130
 
204
131
/** Given an HTTP response object, cleans up and rebuilds the contents of the
211
138
 * things) be used to update the URL in the location bar.
212
139
 * \param response XMLHttpRequest object returned by the server. Should
213
140
 * contain all the response data.
214
 
 * \param url_args Arguments dict, for the arguments passed to the URL
215
 
 * in the browser's address bar (will be forwarded along).
 
141
 * \param editmode Optional boolean. If true, then the user navigated here
 
142
 * with an "edit" URL so we should favour using the editor.
216
143
 */
217
 
function handle_response(path, response, url_args)
 
144
function handle_response(path, response, editmode)
218
145
{
219
146
    /* TODO: Set location bar to "path" */
220
 
    current_path = path;
 
147
    settitle(path);
221
148
 
222
149
    /* Clear away the existing page contents */
223
150
    clearpage();
 
151
    /* Display the path at the top, for navigation */
 
152
    presentpath(path);
224
153
 
225
154
    /* Check the status, and if not 200, read the error and handle this as an
226
155
     * error. */
233
162
        return;
234
163
    }
235
164
 
236
 
    /* This will always return a listing, whether it is a dir or a file.
237
 
     */
238
 
    var listing = response.responseText;
239
 
    /* The listing SHOULD be valid JSON text. Parse it into an object. */
240
 
    try
241
 
    {
242
 
        listing = JSON.parse(listing);
243
 
        file_listing = listing.listing;     /* Global */
244
 
    }
245
 
    catch (e)
246
 
    {
247
 
        handle_error("The server returned an invalid directory listing");
248
 
        return;
249
 
    }
250
 
    /* Get "." out, it's special */
251
 
    current_file = file_listing["."];     /* Global */
252
 
    delete file_listing["."];
253
 
 
254
165
    /* Check if this is a directory listing or file contents */
255
 
    var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
256
 
    if (isdir)
 
166
    if (response.getResponseHeader("X-IVLE-Return") == "Dir")
257
167
    {
 
168
        var listing = response.responseText;
 
169
        /* The listing SHOULD be valid JSON text. Parse it into an object. */
 
170
        try
 
171
        {
 
172
            listing = JSON.parse(listing);
 
173
        }
 
174
        catch (e)
 
175
        {
 
176
            handle_error("The server returned an invalid directory listing");
 
177
            return;
 
178
        }
258
179
        handle_dir_listing(path, listing);
259
180
    }
260
181
    else
261
182
    {
262
 
        /* Need to make a 2nd ajax call, this time get the actual file
263
 
         * contents */
264
 
        callback = function(response)
265
 
            {
266
 
                /* Read the response and set up the page accordingly */
267
 
                handle_contents_response(path, response);
268
 
            }
269
 
        /* Call the server and request the listing. */
270
 
        if (url_args)
271
 
            args = shallow_clone_object(url_args);
 
183
        /* Treat this as an ordinary file. Get the file type. */
 
184
        var content_type = response.getResponseHeader("Content-Type");
 
185
        var handler_type;
 
186
        if (content_type in type_handlers)
 
187
            handler_type = type_handlers[content_type];
272
188
        else
273
 
            args = {};
274
 
        /* This time, get the contents of the file, not its metadata */
275
 
        args['return'] = "contents";
276
 
        ajax_call(callback, service_app, path, args, "GET");
277
 
    }
278
 
    update_actions(isdir);
279
 
}
280
 
 
281
 
function handle_contents_response(path, response)
282
 
{
283
 
    /* Treat this as an ordinary file. Get the file type. */
284
 
    var content_type = response.getResponseHeader("Content-Type");
285
 
    var handler_type = get_handler_type(content_type);
286
 
    would_be_handler_type = handler_type;
287
 
    /* handler_type should now be set to either
288
 
     * "text", "image", "audio" or "binary". */
289
 
    switch (handler_type)
290
 
    {
291
 
    case "text":
292
 
        handle_text(path, response.responseText,
293
 
            would_be_handler_type);
294
 
        break;
295
 
    case "image":
296
 
        /* TODO: Custom image handler */
297
 
        handle_binary(path, response.responseText);
298
 
        break;
299
 
    case "audio":
300
 
        /* TODO: Custom audio handler */
301
 
        handle_binary(path, response.responseText);
302
 
        break;
303
 
    case "binary":
304
 
        handle_binary(path);
305
 
        break;
306
 
    }
307
 
}
308
 
 
309
 
/* Called when a form upload comes back (from an iframe).
310
 
 * Refreshes the page.
311
 
 */
312
 
function upload_callback()
313
 
{
314
 
    /* This has a pretty nasty hack, which happens to work.
315
 
     * upload_callback is set as the "onload" callback for the iframe which
316
 
     * receives the response from the server for uploading a file.
317
 
     * This means it gets called twice. Once when initialising the iframe, and
318
 
     * a second time when the actual response comes back.
319
 
     * All we want to do is call navigate to refresh the page. But we CAN'T do
320
 
     * that on the first load or it will just go into an infinite cycle of
321
 
     * refreshing. We need to refresh the page ONLY on the second refresh.
322
 
     * upload_callback_count is reset to 0 just before the iframe is created.
323
 
     */
324
 
    upload_callback_count++;
325
 
    if (upload_callback_count >= 2)
326
 
        refresh();
 
189
        {   /* Based on the first part of the MIME type */
 
190
            handler_type = content_type.split('/')[0];
 
191
            if (handler_type != "text" && handler_type != "image" &&
 
192
                handler_type != "audio")
 
193
                handler_type = "binary";
 
194
        }
 
195
        /* If we're in "edit mode", always treat this file as text */
 
196
        would_be_handler_type = handler_type;
 
197
        if (editmode) handler_type = "text";
 
198
        /* handler_type should now be set to either
 
199
         * "text", "image", "audio" or "binary". */
 
200
        switch (handler_type)
 
201
        {
 
202
        case "text":
 
203
            handle_text(path, response.responseText, would_be_handler_type);
 
204
            break;
 
205
        case "image":
 
206
            /* TODO: Custom image handler */
 
207
            handle_binary(path, response.responseText);
 
208
            break;
 
209
        case "audio":
 
210
            /* TODO: Custom audio handler */
 
211
            handle_binary(path, response.responseText);
 
212
            break;
 
213
        case "binary":
 
214
            handle_binary(path);
 
215
            break;
 
216
        }
 
217
    }
327
218
}
328
219
 
329
220
/** Deletes all "dynamic" content on the page.
332
223
 */
333
224
function clearpage()
334
225
{
 
226
    dom_removechildren(document.getElementById("path"));
335
227
    dom_removechildren(document.getElementById("filesbody"));
336
228
}
337
229
 
348
240
    dom_removechildren(document.getElementById("sidepanel"));
349
241
}
350
242
 
 
243
/** Sets the mode to either "file browser" or "text editor" mode.
 
244
 * This modifies the window icon, and selected tab.
 
245
 * \param editmode If True, editor mode. Else, file browser mode.
 
246
 */
 
247
function setmode(editmode)
 
248
{
 
249
    /* Find the DOM elements for the file browser and editor tabs */
 
250
    var tabs = document.getElementById("apptabs");
 
251
    var tab_files = null;
 
252
    var tab_edit = null;
 
253
    var a;
 
254
    var href;
 
255
    for (var i=0; i<tabs.childNodes.length; i++)
 
256
    {
 
257
        /* Find the href of the link within */
 
258
        if (!tabs.childNodes[i].getElementsByTagName) continue;
 
259
        a = tabs.childNodes[i].getElementsByTagName("a");
 
260
        if (a.length == 0) continue;
 
261
        href = a[0].getAttribute("href");
 
262
        if (href == null) continue;
 
263
        if (endswith(href, this_app))
 
264
            tab_files = tabs.childNodes[i];
 
265
        else if (endswith(href, edit_app))
 
266
            tab_edit = tabs.childNodes[i];
 
267
    }
 
268
 
 
269
    if (editmode)
 
270
    {
 
271
        tab_files.removeAttribute("class");
 
272
        tab_edit.setAttribute("class", "thisapp");
 
273
    }
 
274
    else
 
275
    {
 
276
        tab_edit.removeAttribute("class");
 
277
        tab_files.setAttribute("class", "thisapp");
 
278
    }
 
279
}
 
280
 
 
281
function settitle(path)
 
282
{
 
283
    document.title = path_basename(path) + " - IVLE";
 
284
}
 
285
 
351
286
/*** HANDLERS for different types of responses (such as dir listing, file,
352
287
 * etc). */
353
288
 
354
289
function handle_error(message)
355
290
{
 
291
    setmode(false);
356
292
    var files = document.getElementById("filesbody");
357
293
    var txt_elem = dom_make_text_elem("div", "Error: "
358
294
        + message.toString() + ".")
360
296
    files.appendChild(txt_elem);
361
297
}
362
298
 
 
299
/** Presents a path list (address bar inside the page) for clicking.
 
300
 */
 
301
function presentpath(path)
 
302
{
 
303
    var dom_path = document.getElementById("path");
 
304
    var href_path = make_path(this_app);
 
305
    var nav_path = "";
 
306
 
 
307
    /* Create all of the paths */
 
308
    for each (var dir in path.split("/"))
 
309
    {
 
310
        if (dir == "") continue;
 
311
        /* Make an 'a' element */
 
312
        href_path = path_join(href_path, dir);
 
313
        nav_path = path_join(nav_path, dir);
 
314
        var link = dom_make_link_elem("a", dir, "Navigate to " + nav_path,
 
315
                href_path, "navigate(" + href_path + ")");
 
316
        dom_path.appendChild(link);
 
317
        dom_path.appendChild(document.createTextNode("/"));
 
318
    }
 
319
    dom_path.removeChild(dom_path.lastChild);
 
320
}
 
321
 
363
322
/** Given a mime type, returns the path to the icon.
364
323
 * \param type String, Mime type.
365
324
 * \param sizelarge Boolean, optional.
382
341
/** Given an svnstatus, returns the path to the icon.
383
342
 * \param type String, svn status.
384
343
 * \return Path to the icon. Has applied make_path, so it is relative to site
385
 
 * root. May return null to indicate no SVN icon.
 
344
 * root.
386
345
 */
387
346
function svnstatus_to_icon(svnstatus)
388
347
{
391
350
        filename = svn_icons[svnstatus];
392
351
    else
393
352
        filename = default_svn_icon;
394
 
    if (filename == null) return null;
395
353
    return make_path(path_join(svn_icons_path, filename));
396
354
}
397
355
 
398
 
/** Given an svnstatus, returns the "nice" string.
 
356
/** Presents the text editor.
399
357
 */
400
 
function svnstatus_to_string(svnstatus)
 
358
function handle_text(path, text, handler_type)
401
359
{
402
 
    if (svnstatus in svn_nice)
403
 
        return svn_nice[svnstatus];
404
 
    else
405
 
        return default_svn_nice;
 
360
    /* Create a textarea with the text in it
 
361
     * (The makings of a primitive editor).
 
362
     */
 
363
 
 
364
    setmode(true);
 
365
    var files = document.getElementById("filesbody");
 
366
    var div = document.createElement("div");
 
367
    files.appendChild(div);
 
368
    div.setAttribute("class", "padding");
 
369
    /* First, print a warning message if this is not actually a text file.
 
370
     */
 
371
    if (handler_type != "text")
 
372
    {
 
373
        var warn = dom_make_text_elem("p",
 
374
            "Warning: You are editing a binary " +
 
375
            "file, which explains any strange characters you may see. If " +
 
376
            "you save this file, you could corrupt it.");
 
377
        div.appendChild(warn);
 
378
    }
 
379
    var txt_elem = dom_make_text_elem("textarea",
 
380
        text.toString())
 
381
    div.appendChild(txt_elem);
 
382
    txt_elem.setAttribute("id", "editbox");
 
383
    /* TODO: Make CSS height: 100% work */
 
384
    txt_elem.setAttribute("rows", "20");
406
385
}
407
386
 
408
387
/** Displays a download link to the binary file.
409
388
 */
410
389
function handle_binary(path)
411
390
{
 
391
    setmode(false);
412
392
    var files = document.getElementById("filesbody");
413
393
    var div = document.createElement("div");
414
394
    files.appendChild(div);
415
395
    div.setAttribute("class", "padding");
416
 
    var download_link = app_path(download_app, path);
 
396
    var download_link = make_path(path_join(download_app, path));
417
397
    var par1 = dom_make_text_elem("p",
418
398
        "The file " + path + " is a binary file. To download this file, " +
419
399
        "click the following link:");
423
403
    div.appendChild(par2);
424
404
}
425
405
 
426
 
function update_actions()
427
 
{
428
 
    var file;
429
 
    var numsel = selected_files.length;
430
 
    if (numsel <= 1)
431
 
    {
432
 
        if (numsel == 0)
433
 
        {
434
 
            /* Display information about the current directory instead */
435
 
            filename = path_basename(current_path);
436
 
            file = current_file;
437
 
        }
438
 
        else if (numsel == 1)
439
 
        {
440
 
            filename = selected_files[0];
441
 
            file = file_listing[filename];
442
 
        }
443
 
 
444
 
        /* Update each action node in the topbar.
445
 
         * This includes enabling/disabling actions as appropriate, and
446
 
         * setting href/onclick attributes. */
447
 
    }
448
 
 
449
 
    /* Open */
450
 
    /* Available if exactly one file is selected */
451
 
    var open = document.getElementById("act_open");
452
 
    if (numsel == 1)
453
 
    {
454
 
        open.setAttribute("class", "choice");
455
 
        if (file.isdir)
456
 
            open.setAttribute("title",
457
 
                "Navigate to this directory in the file browser");
458
 
        else
459
 
            open.setAttribute("title",
460
 
                "Edit or view this file");
461
 
        open.setAttribute("href", app_path(this_app, current_path, filename));
462
 
    }
463
 
    else
464
 
    {
465
 
        open.setAttribute("class", "disabled");
466
 
        open.removeAttribute("title");
467
 
        open.removeAttribute("href");
468
 
    }
469
 
 
470
 
    /* Serve */
471
 
    /* Available if exactly one file is selected,
472
 
     * and only if this is a file, not a directory */
473
 
    var serve = document.getElementById("act_serve");
474
 
    if (numsel == 1 && !file.isdir)
475
 
    {
476
 
        serve.setAttribute("class", "choice");
477
 
        serve.setAttribute("href",
478
 
            app_path(serve_app, current_path, filename));
479
 
    }
480
 
    else
481
 
    {
482
 
        serve.setAttribute("class", "disabled");
483
 
        serve.removeAttribute("href");
484
 
    }
485
 
 
486
 
    /* Run */
487
 
    /* Available if exactly one file is selected,
488
 
     * and it is a Python file.
489
 
     */
490
 
    var run = document.getElementById("act_run");
491
 
     
492
 
    if (numsel == 0 && !file.isdir && file.type == "text/x-python")
493
 
    {
494
 
        // In the edit window
495
 
        run.setAttribute("class", "choice");
496
 
        localpath = app_path('home',current_path);
497
 
        run.setAttribute("onclick", "runfile('" + localpath + "')");
498
 
    }
499
 
    else if (numsel == 1 && !file.isdir && file.type == "text/x-python")
500
 
    {
501
 
        // In the browser window
502
 
        run.setAttribute("class", "choice");
503
 
        localpath = app_path('home',current_path,filename);
504
 
        run.setAttribute("onclick", "runfile('" + localpath + "')");
505
 
    }
506
 
    else
507
 
    {
508
 
        run.setAttribute("class", "disabled");
509
 
        run.removeAttribute("onclick");
510
 
    }
511
 
 
512
 
    /* Download */
513
 
    /* Always available.
514
 
     * If 0 files selected, download the current file or directory as a ZIP.
515
 
     * If 1 directory selected, download it as a ZIP.
516
 
     * If 1 non-directory selected, download it.
517
 
     * If >1 files selected, download them all as a ZIP.
518
 
     */
519
 
    var download = document.getElementById("act_download");
520
 
    if (numsel <= 1)
521
 
    {
522
 
        if (numsel == 0)
523
 
        {
524
 
            download.setAttribute("href",
525
 
                app_path(download_app, current_path));
526
 
            if (file.isdir)
527
 
                download.setAttribute("title",
528
 
                    "Download the current directory as a ZIP file");
529
 
            else
530
 
                download.setAttribute("title",
531
 
                    "Download the current file");
532
 
        }
533
 
        else
534
 
        {
535
 
            download.setAttribute("href",
536
 
                app_path(download_app, current_path, filename));
537
 
            if (file.isdir)
538
 
                download.setAttribute("title",
539
 
                    "Download the selected directory as a ZIP file");
540
 
            else
541
 
                download.setAttribute("title",
542
 
                    "Download the selected file");
543
 
        }
544
 
    }
545
 
    else
546
 
    {
547
 
        /* Make a query string with all the files to download */
548
 
        var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
549
 
        for (var i=0; i<numsel; i++)
550
 
            dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
551
 
        dlpath = dlpath.substr(0, dlpath.length-1);
552
 
        download.setAttribute("href", dlpath);
553
 
        download.setAttribute("title",
554
 
            "Download the selected files as a ZIP file");
555
 
    }
556
 
 
557
 
    /* Refresh - No changes required */
558
 
 
559
 
    /* Publish and Submit */
560
 
    /* If this directory is under subversion and selected/unselected file is a
561
 
     * directory. */
562
 
    var publish = document.getElementById("act_publish");
563
 
    var submit = document.getElementById("act_submit");
564
 
    if (numsel <= 1 && file.isdir)
565
 
    {
566
 
        /* TODO: Work out of file is svn'd */
567
 
        /* TODO: If this dir is already published, call it "Unpublish" */
568
 
        publish.setAttribute("class", "choice");
569
 
        publish.removeAttribute("disabled");
570
 
        submit.setAttribute("class", "choice");
571
 
        submit.removeAttribute("disabled");
572
 
    }
573
 
    else
574
 
    {
575
 
        publish.setAttribute("class", "disabled");
576
 
        publish.setAttribute("disabled", "disabled");
577
 
        submit.setAttribute("class", "disabled");
578
 
        submit.setAttribute("disabled", "disabled");
579
 
    }
580
 
 
581
 
    /* Share */
582
 
    /* If exactly 1 non-directory file is selected/opened, and its parent
583
 
     * directory is published.
584
 
     */
585
 
    var share = document.getElementById("act_share");
586
 
    if (numsel <= 1 && !file.isdir)
587
 
    {
588
 
        /* TODO: Work out if parent dir is published */
589
 
        share.setAttribute("class", "choice");
590
 
        share.removeAttribute("disabled");
591
 
    }
592
 
    else
593
 
    {
594
 
        share.setAttribute("class", "disabled");
595
 
        share.setAttribute("disabled", "disabled");
596
 
    }
597
 
 
598
 
    /* Rename */
599
 
    /* If exactly 1 file is selected */
600
 
    var rename = document.getElementById("act_rename");
601
 
    if (numsel == 1)
602
 
    {
603
 
        rename.setAttribute("class", "choice");
604
 
        rename.removeAttribute("disabled");
605
 
    }
606
 
    else
607
 
    {
608
 
        rename.setAttribute("class", "disabled");
609
 
        rename.setAttribute("disabled", "disabled");
610
 
    }
611
 
 
612
 
    /* Delete, cut, copy */
613
 
    /* If >= 1 file is selected */
614
 
    var act_delete = document.getElementById("act_delete");
615
 
    var cut = document.getElementById("act_cut");
616
 
    var copy = document.getElementById("act_copy");
617
 
    if (numsel >= 1)
618
 
    {
619
 
        act_delete.setAttribute("class", "choice");
620
 
        act_delete.removeAttribute("disabled");
621
 
        cut.setAttribute("class", "choice");
622
 
        cut.removeAttribute("disabled");
623
 
        copy.setAttribute("class", "choice");
624
 
        copy.removeAttribute("disabled");
625
 
    }
626
 
    else
627
 
    {
628
 
        act_delete.setAttribute("class", "disabled");
629
 
        act_delete.setAttribute("disabled", "disabled");
630
 
        cut.setAttribute("class", "disabled");
631
 
        cut.setAttribute("disabled", "disabled");
632
 
        copy.setAttribute("class", "disabled");
633
 
        copy.setAttribute("disabled", "disabled");
634
 
    }
635
 
 
636
 
    /* Paste, new file, new directory, upload */
637
 
    /* Always enabled (assuming this is a directory) */
638
 
 
639
 
    /* Subversion actions */
640
 
    /* TODO: Work out when these are appropriate */
641
 
    var svnadd = document.getElementById("act_svnadd");
642
 
    var svnrevert = document.getElementById("act_svnrevert");
643
 
    var svncommit = document.getElementById("act_svncommit");
644
 
    if (true)
645
 
    {
646
 
        svnadd.setAttribute("class", "choice");
647
 
        svnadd.removeAttribute("disabled");
648
 
        svnrevert.setAttribute("class", "choice");
649
 
        svnrevert.removeAttribute("disabled");
650
 
        svncommit.setAttribute("class", "choice");
651
 
        svncommit.removeAttribute("disabled");
652
 
    }
653
 
    var svncheckout = document.getElementById("act_svncheckout");
654
 
    /* current_path == username: We are at the top level */
655
 
    if (current_path == username)
656
 
    {
657
 
        svncheckout.setAttribute("class", "choice");
658
 
        svncheckout.removeAttribute("disabled");
659
 
    }
660
 
    else
661
 
    {
662
 
        svncheckout.setAttribute("class", "disabled");
663
 
        svncheckout.setAttribute("disabled", "disabled");
664
 
    }
665
 
 
666
 
    return;
667
 
}
668
 
 
669
 
/** Event handler for when an item of the "More actions..." dropdown box is
670
 
 * selected. Performs the selected action. */
671
 
function handle_moreactions()
672
 
{
673
 
    var moreactions = document.getElementById("moreactions");
674
 
    if (moreactions.value == "top")
675
 
        return;
676
 
    var selectedaction = moreactions.value;
677
 
    /* Reset to "More actions..." */
678
 
    moreactions.selectedIndex = 0;
679
 
 
680
 
    /* If 0 files selected, filename is the name of the current dir.
681
 
     * If 1 file selected, filename is that file.
682
 
     */
683
 
    if (selected_files.length == 0)
684
 
        filename = path_basename(current_path);
685
 
    else if (selected_files.length == 1)
686
 
        filename = selected_files[0];
687
 
    else
688
 
        filename = null;
689
 
 
690
 
    /* Now handle the selected action */
691
 
    switch(selectedaction)
692
 
    {
693
 
    case "publish":
694
 
        action_publish(selected_files);
695
 
        break;
696
 
    case "unpublish":
697
 
        action_unpublish(selected_files);
698
 
        break;
699
 
    case "share":
700
 
        // TODO
701
 
        alert("Not yet implemented: Sharing files");
702
 
        break;
703
 
    case "submit":
704
 
        // TODO
705
 
        alert("Not yet implemented: Submit");
706
 
        break;
707
 
    case "rename":
708
 
        action_rename(filename);
709
 
        break;
710
 
    case "delete":
711
 
        action_remove(selected_files);
712
 
        break;
713
 
    case "copy":
714
 
        action_copy(selected_files);
715
 
        break;
716
 
    case "cut":
717
 
        action_cut(selected_files);
718
 
        break;
719
 
    case "paste":
720
 
        action_paste();
721
 
        break;
722
 
    case "newfile":
723
 
        action_newfile();
724
 
        break;
725
 
    case "mkdir":
726
 
        action_mkdir();
727
 
        break;
728
 
    case "upload":
729
 
        show_uploadpanel(true);
730
 
        break;
731
 
    case "svnadd":
732
 
        action_add(selected_files);
733
 
        break;
734
 
    case "svnrevert":
735
 
        action_revert(selected_files);
736
 
        break;
737
 
    case "svncommit":
738
 
        action_commit(selected_files);
739
 
        break;
740
 
    case "svncheckout":
741
 
        action_checkout();
742
 
        break;
743
 
    }
744
 
}
745
 
 
746
 
/** User clicks "Run" button.
747
 
 * Do an Ajax call and print the test output.
748
 
 */
749
 
function runfile(localpath)
750
 
{
751
 
    /* Dump the entire file to the console */
752
 
    var callback = function()
753
 
    {
754
 
        console_enter_line("execfile('" + localpath + "')", "block");
755
 
    }
756
 
    start_server(callback)
757
 
    return;
758
 
}
759
 
 
760
406
/** Called when the page loads initially.
761
407
 */
762
408
window.onload = function()
768
414
    var path = parse_url(window.location.href).path;
769
415
    /* Strip out root_dir + "/files" from the front of the path */
770
416
    var strip = make_path(this_app);
 
417
    var editmode = false;
771
418
    if (path.substr(0, strip.length) == strip)
772
419
        path = path.substr(strip.length+1);
773
420
    else
777
424
        if (path.substr(0, strip.length) == strip)
778
425
        {
779
426
            path = path.substr(strip.length+1);
 
427
            editmode = true;
780
428
        }
781
429
    }
782
430
 
787
435
        path = username;
788
436
    }
789
437
 
790
 
    navigate(path);
791
 
 
792
 
    /* Set up the console plugin to display as a popup window */
793
 
    console_init(true);
 
438
    navigate(path, editmode);
794
439
}