~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-12 15:35:53 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:201
Added "test" directory.
Added make_date_test.py, a short script I wrote to test the date format
algorithm committed in the previous revision.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
/* Url names for apps */
24
24
this_app = "files";
25
 
edit_app = "edit";
26
25
service_app = "fileservice";
27
 
serve_app = "serve";
28
 
download_app = "download";
29
26
 
30
27
/* Mapping MIME types onto handlers.
31
28
 * "text" : When navigating to a text file, the text editor is opened.
43
40
    "application/x-javascript" : "text",
44
41
    "application/javascript" : "text",
45
42
    "application/json" : "text",
46
 
    "application/xml" : "text"
 
43
    "application/xml" : "text",
47
44
};
48
45
 
49
46
/* Mapping MIME types to icons, just the file's basename */
50
47
type_icons = {
51
48
    "text/directory": "dir.png",
52
 
    "text/x-python": "py.png"
 
49
    "text/x-python": "py.png",
53
50
};
54
51
 
55
52
default_type_icon = "txt.png";
60
57
 
61
58
/* Mapping SVN status to icons, just the file's basename */
62
59
svn_icons = {
63
 
    "unversioned": null,
 
60
    "unversioned": "unversioned.png",
64
61
    "normal": "normal.png",
65
 
    "added": "added.png",
66
 
    "missing": "missing.png",
67
 
    "deleted": "deleted.png",
68
62
    "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";
 
63
};
 
64
 
 
65
default_svn_icon = "normal.png";
88
66
 
89
67
svn_icons_path = "media/images/svn";
90
68
 
91
 
published_icon = "media/images/interface/published.png";
92
 
 
93
69
/* List of MIME types considered "executable" by the system.
94
70
 * Executable files offer a "run" link, implying that the "serve"
95
71
 * application can interpret them.
96
72
 */
97
73
types_exec = [
98
 
    "text/x-python"
 
74
    "text/x-python",
99
75
];
100
76
 
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
77
/** Calls the server using Ajax, performing an action on the server side.
118
78
 * Receives the response from the server and performs a refresh of the page
119
79
 * contents, updating it to display the returned data (such as a directory
133
93
 *      Defaults to "application/x-www-form-urlencoded".
134
94
 *      "multipart/form-data" is recommended for large uploads.
135
95
 */
136
 
function do_action(action, path, args, content_type, ignore_response)
 
96
function do_action(action, path, args, content_type)
137
97
{
138
98
    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
99
    /* Call the server and perform the action. This mutates the server. */
152
 
    ajax_call(callback, service_app, path, args, "POST", content_type);
 
100
    response = ajax_call(service_app, path, args, "POST", content_type);
 
101
    /* Check for action errors reported by the server, and report them to the
 
102
     * user */
 
103
    error = response.getResponseHeader("X-IVLE-Action-Error");
 
104
    if (error != null)
 
105
        alert("Error: " + error.toString() + ".");
 
106
    /* Now read the response and set up the page accordingly */
 
107
    handle_response(path, response);
153
108
}
154
109
 
155
110
/** Calls the server using Ajax, requesting a directory listing. This should
162
117
 */
163
118
function navigate(path)
164
119
{
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
    /* Call the server and request the listing. This mutates the server. */
 
121
    response = ajax_call(service_app, path, null, "GET");
 
122
    /* Now read the response and set up the page accordingly */
 
123
    handle_response(path, response);
202
124
}
203
125
 
204
126
/** Given an HTTP response object, cleans up and rebuilds the contents of the
211
133
 * things) be used to update the URL in the location bar.
212
134
 * \param response XMLHttpRequest object returned by the server. Should
213
135
 * 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).
216
136
 */
217
 
function handle_response(path, response, url_args)
 
137
function handle_response(path, response)
218
138
{
219
139
    /* TODO: Set location bar to "path" */
220
 
    current_path = path;
221
140
 
222
141
    /* Clear away the existing page contents */
223
142
    clearpage();
 
143
    /* Display the path at the top, for navigation */
 
144
    presentpath(path);
224
145
 
225
146
    /* Check the status, and if not 200, read the error and handle this as an
226
147
     * error. */
233
154
        return;
234
155
    }
235
156
 
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
157
    /* Check if this is a directory listing or file contents */
255
 
    var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
256
 
    if (isdir)
 
158
    if (response.getResponseHeader("X-IVLE-Return") == "Dir")
257
159
    {
 
160
        var listing = response.responseText;
 
161
        /* The listing SHOULD be valid JSON text. Parse it into an object. */
 
162
        try
 
163
        {
 
164
            listing = JSON.parse(listing);
 
165
        }
 
166
        catch (e)
 
167
        {
 
168
            handle_error("The server returned an invalid directory listing");
 
169
            return;
 
170
        }
258
171
        handle_dir_listing(path, listing);
259
172
    }
260
173
    else
261
174
    {
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);
 
175
        /* Treat this as an ordinary file. Get the file type. */
 
176
        var content_type = response.getResponseHeader("Content-Type");
 
177
        var handler_type;
 
178
        if (content_type in type_handlers)
 
179
            handler_type = type_handlers[content_type];
272
180
        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();
 
181
        {   /* Based on the first part of the MIME type */
 
182
            handler_type = content_type.split('/')[0];
 
183
            if (handler_type != "text" && handler_type != "image" &&
 
184
                handler_type != "audio")
 
185
                handler_type = "binary";
 
186
        }
 
187
        /* handler_type should now be set to either
 
188
         * "text", "image", "audio" or "binary". */
 
189
        switch (handler_type)
 
190
        {
 
191
        case "text":
 
192
            handle_text(path, response.responseText);
 
193
            break;
 
194
        case "image":
 
195
            /* TODO: Custom image handler */
 
196
            handle_binary(path, response.responseText);
 
197
            break;
 
198
        case "audio":
 
199
            /* TODO: Custom audio handler */
 
200
            handle_binary(path, response.responseText);
 
201
            break;
 
202
        case "binary":
 
203
            handle_binary(path);
 
204
            break;
 
205
        }
 
206
    }
327
207
}
328
208
 
329
209
/** Deletes all "dynamic" content on the page.
332
212
 */
333
213
function clearpage()
334
214
{
335
 
    dom_removechildren(document.getElementById("filesbody"));
336
 
}
337
 
 
338
 
/** Deletes all "dynamic" content on the page necessary to navigate from
339
 
 * one directory listing to another (does not clear as much as clearpage
340
 
 * does).
341
 
 * This is the equivalent of calling clearpage() then
342
 
 * setup_for_dir_listing(), assuming the page is already on a dir listing.
343
 
 */
344
 
function clearpage_dir()
345
 
{
 
215
    /* Note: For now clear just enough to repopulate with a dir listing.
 
216
     * Later, will have to clear more to make way for other handlers.
 
217
     * Possibly have a "full clear" for all handlers, and special
 
218
     * less-violent clearers for each handler if the same handler is going to
 
219
     * be used that was used last time. */
346
220
    dom_removechildren(document.getElementById("path"));
347
221
    dom_removechildren(document.getElementById("files"));
348
222
    dom_removechildren(document.getElementById("sidepanel"));
353
227
 
354
228
function handle_error(message)
355
229
{
356
 
    var files = document.getElementById("filesbody");
357
 
    var txt_elem = dom_make_text_elem("div", "Error: "
 
230
    /* TODO: Find a better place to put this message. */
 
231
    var files = document.getElementById("files");
 
232
    var tr = document.createElement("tr");
 
233
    var td = document.createElement("td");
 
234
    tr.appendChild(td);
 
235
    var td = document.createElement("td");
 
236
    tr.appendChild(td);
 
237
    var td = document.createElement("td");
 
238
    tr.appendChild(td);
 
239
    var txt_elem = dom_make_text_elem("td", "Error: "
358
240
        + message.toString() + ".")
359
 
    txt_elem.setAttribute("class", "padding error");
360
 
    files.appendChild(txt_elem);
 
241
    txt_elem.setAttribute("class", "error");
 
242
    tr.appendChild(txt_elem);
 
243
    var td = document.createElement("td");
 
244
    tr.appendChild(td);
 
245
    var td = document.createElement("td");
 
246
    tr.appendChild(td);
 
247
    files.appendChild(tr);
 
248
}
 
249
 
 
250
/** Presents a path list (address bar inside the page) for clicking.
 
251
 */
 
252
function presentpath(path)
 
253
{
 
254
    var dom_path = document.getElementById("path");
 
255
    var href_path = make_path(this_app);
 
256
    var nav_path = "";
 
257
 
 
258
    /* Create all of the paths */
 
259
    for each (dir in path.split("/"))
 
260
    {
 
261
        if (dir == "") continue;
 
262
        /* Make an 'a' element */
 
263
        href_path = path_join(href_path, dir);
 
264
        nav_path = path_join(nav_path, dir);
 
265
        var link = dom_make_link_elem("a", dir, "Navigate to " + nav_path,
 
266
                href_path, "navigate(" + href_path + ")");
 
267
        dom_path.appendChild(link);
 
268
        dom_path.appendChild(document.createTextNode("/"));
 
269
    }
361
270
}
362
271
 
363
272
/** Given a mime type, returns the path to the icon.
382
291
/** Given an svnstatus, returns the path to the icon.
383
292
 * \param type String, svn status.
384
293
 * \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.
 
294
 * root.
386
295
 */
387
296
function svnstatus_to_icon(svnstatus)
388
297
{
391
300
        filename = svn_icons[svnstatus];
392
301
    else
393
302
        filename = default_svn_icon;
394
 
    if (filename == null) return null;
395
303
    return make_path(path_join(svn_icons_path, filename));
396
304
}
397
305
 
398
 
/** Given an svnstatus, returns the "nice" string.
399
 
 */
400
 
function svnstatus_to_string(svnstatus)
401
 
{
402
 
    if (svnstatus in svn_nice)
403
 
        return svn_nice[svnstatus];
404
 
    else
405
 
        return default_svn_nice;
 
306
/** Presents the directory listing.
 
307
 */
 
308
function handle_dir_listing(path, listing)
 
309
{
 
310
    var row_toggle = 1;
 
311
    /* Nav through the top-level of the JSON to the actual listing object. */
 
312
    var listing = listing.listing;
 
313
 
 
314
    /* Get "." out, it's special */
 
315
    var thisdir = listing["."];
 
316
    delete listing["."];
 
317
    /* Is this dir under svn? */
 
318
    var under_subversion = "svnstatus" in thisdir;
 
319
 
 
320
    var files = document.getElementById("files");
 
321
    var file;
 
322
    var row;
 
323
    var td;
 
324
    var checkbox;
 
325
 
 
326
    /* Create all of the files */
 
327
    for (var filename in listing)
 
328
    {
 
329
        file = listing[filename];
 
330
        /* Make a 'tr' element */
 
331
        row = document.createElement("tr");
 
332
        /* Column 1: Selection checkbox */
 
333
        row.setAttribute("class", "row" + row_toggle.toString())
 
334
        row_toggle = row_toggle == 1 ? 2 : 1;
 
335
        td = document.createElement("td");
 
336
        checkbox = document.createElement("input");
 
337
        checkbox.setAttribute("type", "checkbox");
 
338
        checkbox.setAttribute("title", "Select this file");
 
339
        td.appendChild(checkbox);
 
340
        row.appendChild(td);
 
341
        if (file.isdir)
 
342
        {
 
343
            /* Column 2: Filetype and subversion icons. */
 
344
            td = document.createElement("td");
 
345
            td.setAttribute("class", "thincol");
 
346
            td.appendChild(dom_make_img(mime_type_to_icon("text/directory"),
 
347
                22, 22, file.type));
 
348
            row.appendChild(td);
 
349
            td = document.createElement("td");
 
350
            td.setAttribute("class", "thincol");
 
351
            if (under_subversion)
 
352
                td.appendChild(dom_make_img(svnstatus_to_icon(file.svnstatus),
 
353
                    22, 22, file.svnstatus));
 
354
            row.appendChild(td);
 
355
            /* Column 3: Filename */
 
356
            row.appendChild(dom_make_link_elem("td", filename,
 
357
                "Navigate to " + path_join(path, filename),
 
358
                make_path(path_join(this_app, path, filename)),
 
359
                "navigate(" + path_join(path, filename) + ")"));
 
360
        }
 
361
        else
 
362
        {
 
363
            /* Column 2: Filetype and subversion icons. */
 
364
            td = document.createElement("td");
 
365
            td.setAttribute("class", "thincol");
 
366
            td.appendChild(dom_make_img(mime_type_to_icon(file.type),
 
367
                22, 22, file.type));
 
368
            row.appendChild(td);
 
369
            td = document.createElement("td");
 
370
            td.setAttribute("class", "thincol");
 
371
            if (under_subversion)
 
372
                td.appendChild(dom_make_img(svnstatus_to_icon(file.svnstatus),
 
373
                    22, 22, file.svnstatus));
 
374
            row.appendChild(td);
 
375
            /* Column 3: Filename */
 
376
            row.appendChild(dom_make_text_elem("td", filename));
 
377
        }
 
378
        /* Column 4: Size */
 
379
        row.appendChild(dom_make_text_elem("td", nice_filesize(file.size)));
 
380
        /* Column 4: Date */
 
381
        row.appendChild(dom_make_text_elem("td", file.mtime_short,
 
382
            file.mtime_nice));
 
383
        files.appendChild(row);
 
384
    }
 
385
 
 
386
}
 
387
 
 
388
/** Presents the text editor.
 
389
 */
 
390
function handle_text(path, text)
 
391
{
 
392
    /* TODO */
 
393
    alert(text);
406
394
}
407
395
 
408
396
/** Displays a download link to the binary file.
409
397
 */
410
398
function handle_binary(path)
411
399
{
412
 
    var files = document.getElementById("filesbody");
413
 
    var div = document.createElement("div");
414
 
    files.appendChild(div);
415
 
    div.setAttribute("class", "padding");
416
 
    var download_link = app_path(download_app, path);
417
 
    var par1 = dom_make_text_elem("p",
418
 
        "The file " + path + " is a binary file. To download this file, " +
419
 
        "click the following link:");
420
 
    var par2 = dom_make_link_elem("p",
421
 
        "Download " + path, "Download " + path, download_link);
422
 
    div.appendChild(par1);
423
 
    div.appendChild(par2);
424
 
}
425
 
 
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
400
    /* TODO */
491
 
 
492
 
    /* Download */
493
 
    /* Always available.
494
 
     * If 0 files selected, download the current file or directory as a ZIP.
495
 
     * If 1 directory selected, download it as a ZIP.
496
 
     * If 1 non-directory selected, download it.
497
 
     * If >1 files selected, download them all as a ZIP.
498
 
     */
499
 
    var download = document.getElementById("act_download");
500
 
    if (numsel <= 1)
501
 
    {
502
 
        if (numsel == 0)
503
 
        {
504
 
            download.setAttribute("href",
505
 
                app_path(download_app, current_path));
506
 
            if (file.isdir)
507
 
                download.setAttribute("title",
508
 
                    "Download the current directory as a ZIP file");
509
 
            else
510
 
                download.setAttribute("title",
511
 
                    "Download the current file");
512
 
        }
513
 
        else
514
 
        {
515
 
            download.setAttribute("href",
516
 
                app_path(download_app, current_path, filename));
517
 
            if (file.isdir)
518
 
                download.setAttribute("title",
519
 
                    "Download the selected directory as a ZIP file");
520
 
            else
521
 
                download.setAttribute("title",
522
 
                    "Download the selected file");
523
 
        }
524
 
    }
525
 
    else
526
 
    {
527
 
        /* Make a query string with all the files to download */
528
 
        var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
529
 
        for (var i=0; i<numsel; i++)
530
 
            dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
531
 
        dlpath = dlpath.substr(0, dlpath.length-1);
532
 
        download.setAttribute("href", dlpath);
533
 
        download.setAttribute("title",
534
 
            "Download the selected files as a ZIP file");
535
 
    }
536
 
 
537
 
    /* Refresh - No changes required */
538
 
 
539
 
    /* Publish and Submit */
540
 
    /* If this directory is under subversion and selected/unselected file is a
541
 
     * directory. */
542
 
    var publish = document.getElementById("act_publish");
543
 
    var submit = document.getElementById("act_submit");
544
 
    if (numsel <= 1 && file.isdir)
545
 
    {
546
 
        /* TODO: Work out of file is svn'd */
547
 
        /* TODO: If this dir is already published, call it "Unpublish" */
548
 
        publish.setAttribute("class", "choice");
549
 
        publish.removeAttribute("disabled");
550
 
        submit.setAttribute("class", "choice");
551
 
        submit.removeAttribute("disabled");
552
 
    }
553
 
    else
554
 
    {
555
 
        publish.setAttribute("class", "disabled");
556
 
        publish.setAttribute("disabled", "disabled");
557
 
        submit.setAttribute("class", "disabled");
558
 
        submit.setAttribute("disabled", "disabled");
559
 
    }
560
 
 
561
 
    /* Share */
562
 
    /* If exactly 1 non-directory file is selected/opened, and its parent
563
 
     * directory is published.
564
 
     */
565
 
    var share = document.getElementById("act_share");
566
 
    if (numsel <= 1 && !file.isdir)
567
 
    {
568
 
        /* TODO: Work out if parent dir is published */
569
 
        share.setAttribute("class", "choice");
570
 
        share.removeAttribute("disabled");
571
 
    }
572
 
    else
573
 
    {
574
 
        share.setAttribute("class", "disabled");
575
 
        share.setAttribute("disabled", "disabled");
576
 
    }
577
 
 
578
 
    /* Rename */
579
 
    /* If exactly 1 file is selected */
580
 
    var rename = document.getElementById("act_rename");
581
 
    if (numsel == 1)
582
 
    {
583
 
        rename.setAttribute("class", "choice");
584
 
        rename.removeAttribute("disabled");
585
 
    }
586
 
    else
587
 
    {
588
 
        rename.setAttribute("class", "disabled");
589
 
        rename.setAttribute("disabled", "disabled");
590
 
    }
591
 
 
592
 
    /* Delete, cut, copy */
593
 
    /* If >= 1 file is selected */
594
 
    var act_delete = document.getElementById("act_delete");
595
 
    var cut = document.getElementById("act_cut");
596
 
    var copy = document.getElementById("act_copy");
597
 
    if (numsel >= 1)
598
 
    {
599
 
        act_delete.setAttribute("class", "choice");
600
 
        act_delete.removeAttribute("disabled");
601
 
        cut.setAttribute("class", "choice");
602
 
        cut.removeAttribute("disabled");
603
 
        copy.setAttribute("class", "choice");
604
 
        copy.removeAttribute("disabled");
605
 
    }
606
 
    else
607
 
    {
608
 
        act_delete.setAttribute("class", "disabled");
609
 
        act_delete.setAttribute("disabled", "disabled");
610
 
        cut.setAttribute("class", "disabled");
611
 
        cut.setAttribute("disabled", "disabled");
612
 
        copy.setAttribute("class", "disabled");
613
 
        copy.setAttribute("disabled", "disabled");
614
 
    }
615
 
 
616
 
    /* Paste, new file, new directory, upload */
617
 
    /* Always enabled (assuming this is a directory) */
618
 
 
619
 
    /* Subversion actions */
620
 
    /* TODO: Work out when these are appropriate */
621
 
    var svnadd = document.getElementById("act_svnadd");
622
 
    var svnrevert = document.getElementById("act_svnrevert");
623
 
    var svncommit = document.getElementById("act_svncommit");
624
 
    if (true)
625
 
    {
626
 
        svnadd.setAttribute("class", "choice");
627
 
        svnadd.removeAttribute("disabled");
628
 
        svnrevert.setAttribute("class", "choice");
629
 
        svnrevert.removeAttribute("disabled");
630
 
        svncommit.setAttribute("class", "choice");
631
 
        svncommit.removeAttribute("disabled");
632
 
    }
633
 
    var svncheckout = document.getElementById("act_svncheckout");
634
 
    /* current_path == username: We are at the top level */
635
 
    if (current_path == username)
636
 
    {
637
 
        svncheckout.setAttribute("class", "choice");
638
 
        svncheckout.removeAttribute("disabled");
639
 
    }
640
 
    else
641
 
    {
642
 
        svncheckout.setAttribute("class", "disabled");
643
 
        svncheckout.setAttribute("disabled", "disabled");
644
 
    }
645
 
 
646
 
    return;
647
 
}
648
 
 
649
 
/** Event handler for when an item of the "More actions..." dropdown box is
650
 
 * selected. Performs the selected action. */
651
 
function handle_moreactions()
652
 
{
653
 
    var moreactions = document.getElementById("moreactions");
654
 
    if (moreactions.value == "top")
655
 
        return;
656
 
    var selectedaction = moreactions.value;
657
 
    /* Reset to "More actions..." */
658
 
    moreactions.selectedIndex = 0;
659
 
 
660
 
    /* If 0 files selected, filename is the name of the current dir.
661
 
     * If 1 file selected, filename is that file.
662
 
     */
663
 
    if (selected_files.length == 0)
664
 
        filename = path_basename(current_path);
665
 
    else if (selected_files.length == 1)
666
 
        filename = selected_files[0];
667
 
    else
668
 
        filename = null;
669
 
 
670
 
    /* Now handle the selected action */
671
 
    switch(selectedaction)
672
 
    {
673
 
    case "publish":
674
 
        action_publish(selected_files);
675
 
        break;
676
 
    case "unpublish":
677
 
        action_unpublish(selected_files);
678
 
        break;
679
 
    case "share":
680
 
        // TODO
681
 
        alert("Not yet implemented: Sharing files");
682
 
        break;
683
 
    case "submit":
684
 
        // TODO
685
 
        alert("Not yet implemented: Submit");
686
 
        break;
687
 
    case "rename":
688
 
        action_rename(filename);
689
 
        break;
690
 
    case "delete":
691
 
        action_remove(selected_files);
692
 
        break;
693
 
    case "copy":
694
 
        action_copy(selected_files);
695
 
        break;
696
 
    case "cut":
697
 
        action_cut(selected_files);
698
 
        break;
699
 
    case "paste":
700
 
        action_paste();
701
 
        break;
702
 
    case "newfile":
703
 
        action_newfile();
704
 
        break;
705
 
    case "mkdir":
706
 
        action_mkdir();
707
 
        break;
708
 
    case "upload":
709
 
        show_uploadpanel(true);
710
 
        break;
711
 
    case "svnadd":
712
 
        action_add(selected_files);
713
 
        break;
714
 
    case "svnrevert":
715
 
        action_revert(selected_files);
716
 
        break;
717
 
    case "svncommit":
718
 
        action_commit(selected_files);
719
 
        break;
720
 
    case "svncheckout":
721
 
        action_checkout();
722
 
        break;
723
 
    }
 
401
    alert(path);
724
402
}
725
403
 
726
404
/** Called when the page loads initially.
733
411
     */
734
412
    var path = parse_url(window.location.href).path;
735
413
    /* Strip out root_dir + "/files" from the front of the path */
736
 
    var strip = make_path(this_app);
737
 
    if (path.substr(0, strip.length) == strip)
738
 
        path = path.substr(strip.length+1);
739
 
    else
740
 
    {
741
 
        /* See if this is an edit path */
742
 
        strip = make_path(edit_app);
743
 
        if (path.substr(0, strip.length) == strip)
744
 
        {
745
 
            path = path.substr(strip.length+1);
746
 
        }
747
 
    }
 
414
    strip_chars = make_path(this_app).length + 1;
 
415
    path = path.substr(strip_chars);
748
416
 
749
417
    if (path.length == 0)
750
418
    {