~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 12:57:30 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:197
dispatch.html, ivle.css: Main IVLE header is now an h1/h2 pair, styled to the
    left with absolute positioning (one over the top of the other).

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";
56
53
 
57
54
/* Relative to IVLE root */
58
 
type_icons_path = "+media/ivle.webapp.core/images/mime";
59
 
type_icons_path_large = "+media/ivle.webapp.core/images/mime/large";
 
55
type_icons_path = "media/images/mime";
 
56
type_icons_path_large = "media/images/mime/large";
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
 
    "conflicted": "conflicted.png",
70
 
    "revision": "revision.png"
71
 
};
72
 
 
73
 
/* Mapping SVN status to "nice" strings */
74
 
svn_nice = {
75
 
    "unversioned": "Temporary file",
76
 
    "normal": "Permanent file",
77
 
    "added": "Temporary file (scheduled to be added)",
78
 
    "missing": "Permanent file (missing)",
79
 
    "deleted": "Permanent file (scheduled for deletion)",
80
 
    "replaced": "Permanent file (replaced)",
81
 
    "modified": "Permanent file (modified)",
82
 
    "merged": "Permanent file (merged)",
83
 
    "conflicted": "Permanent file (conflicted)",
84
 
    "revision": "Past Permanent file (revision)"
85
 
};
86
 
 
87
 
default_svn_icon = null;
88
 
default_svn_nice = "Unknown status";
89
 
 
90
 
svn_icons_path = "+media/ivle.webapp.core/images/svn";
91
 
 
92
 
published_icon = "+media/ivle.webapp.core/images/interface/published.png";
 
63
};
 
64
 
 
65
default_svn_icon = "normal.png";
 
66
 
 
67
svn_icons_path = "media/images/svn";
93
68
 
94
69
/* List of MIME types considered "executable" by the system.
95
70
 * Executable files offer a "run" link, implying that the "serve"
96
71
 * application can interpret them.
97
72
 */
98
73
types_exec = [
99
 
    "text/x-python"
 
74
    "text/x-python",
100
75
];
101
76
 
102
 
 
103
 
/* Global variables */
104
 
 
105
 
/** The listing object returned by the server as JSON */
106
 
file_listing = null;
107
 
current_file = null;
108
 
current_revision = null;
109
 
current_path = "";
110
 
 
111
 
/** Filenames of all files selected
112
 
 * (Only used by dir listings, but still needs to be [] for files, so that
113
 
 * update_actions knows that nothing is selected).
114
 
 */
115
 
selected_files = [];
116
 
 
117
 
upload_callback_count = 0;      /* See upload_callback */
118
 
 
119
77
/** Calls the server using Ajax, performing an action on the server side.
120
78
 * Receives the response from the server and performs a refresh of the page
121
79
 * contents, updating it to display the returned data (such as a directory
135
93
 *      Defaults to "application/x-www-form-urlencoded".
136
94
 *      "multipart/form-data" is recommended for large uploads.
137
95
 */
138
 
function do_action(action, path, args, content_type, ignore_response)
 
96
function do_action(action, path, args, content_type)
139
97
{
140
98
    args.action = action;
141
 
    /* Callback action, when the server returns */
142
 
    var callback = function(response)
143
 
        {
144
 
            /* Check for action errors reported by the server, and report them
145
 
             * to the user */
146
 
            var error = response.getResponseHeader("X-IVLE-Action-Error");
147
 
            if (error != null && error != "")
148
 
                /* Note: This header (in particular) comes URI-encoded, to
149
 
                 * allow multi-line error messages. Decode */
150
 
                alert("Error: " + decodeURIComponent(error.toString()) + ".");
151
 
            /* Now read the response and set up the page accordingly */
152
 
            if (ignore_response != true)
153
 
                handle_response(path, response, true);
154
 
        }
155
99
    /* Call the server and perform the action. This mutates the server. */
156
 
    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);
157
108
}
158
109
 
159
110
/** Calls the server using Ajax, requesting a directory listing. This should
166
117
 */
167
118
function navigate(path)
168
119
{
169
 
    callback = function(response)
170
 
        {
171
 
            /* Read the response and set up the page accordingly */
172
 
            handle_response(path, response, false, url.args);
173
 
        }
174
 
    /* Get any query strings */
175
 
    url = parse_url(window.location.href);
176
 
    
177
 
    /* Call the server and request the listing. */
178
 
    ajax_call(callback, service_app, path, url.args, "GET");
179
 
}
180
 
 
181
 
/* Refreshes the current view.
182
 
 * Calls navigate on the current path.
183
 
 */
184
 
function refresh()
185
 
{
186
 
    if (maybe_save('All changes since the last save will be lost!'))
187
 
        navigate(current_path);
188
 
}
189
 
 
190
 
/** Determines the "handler type" from a MIME type.
191
 
 * The handler type is a string, either "text", "image", "audio" or "binary".
192
 
 */
193
 
function get_handler_type(content_type)
194
 
{
195
 
    if (!content_type)
196
 
        return null;
197
 
    if (content_type in type_handlers)
198
 
        return type_handlers[content_type];
199
 
    else
200
 
    {   /* Based on the first part of the MIME type */
201
 
        var handler_type = content_type.split('/')[0];
202
 
        if (handler_type != "text" && handler_type != "image" &&
203
 
            handler_type != "audio")
204
 
            handler_type = "binary";
205
 
        return handler_type;
206
 
    }
 
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);
207
124
}
208
125
 
209
126
/** Given an HTTP response object, cleans up and rebuilds the contents of the
216
133
 * things) be used to update the URL in the location bar.
217
134
 * \param response XMLHttpRequest object returned by the server. Should
218
135
 * contain all the response data.
219
 
 * \param is_action Boolean. True if this is the response to an action, false
220
 
 * if this is the response to a simple listing. This is used in handling the
221
 
 * error.
222
 
 * \param url_args Arguments dict, for the arguments passed to the URL
223
 
 * in the browser's address bar (will be forwarded along).
224
136
 */
225
 
function handle_response(path, response, is_action, url_args)
 
137
function handle_response(path, response)
226
138
{
227
139
    /* TODO: Set location bar to "path" */
228
 
    current_path = path;
229
140
 
230
141
    /* Clear away the existing page contents */
231
142
    clearpage();
 
143
    /* Display the path at the top, for navigation */
 
144
    presentpath(path);
232
145
 
233
146
    /* Check the status, and if not 200, read the error and handle this as an
234
147
     * error. */
241
154
        return;
242
155
    }
243
156
 
244
 
    var subjects = null;
245
 
    var top_level_dir = path==username;
246
 
    if (top_level_dir)
247
 
    {
248
 
        var req = ajax_call(null, "userservice", "get_enrolments", null, "GET")
249
 
        subjects = decode_response(req);
250
 
    }
251
 
 
252
 
 
253
 
    /* This will always return a listing, whether it is a dir or a file.
254
 
     */
255
 
    var listing = response.responseText;
256
 
    /* The listing SHOULD be valid JSON text. Parse it into an object. */
257
 
    try
258
 
    {
259
 
        listing = JSON.parse(listing);
260
 
        file_listing = listing.listing;     /* Global */
261
 
    }
262
 
    catch (e)
263
 
    {
264
 
        if (is_action)
265
 
        {
266
 
            var err = document.createElement("div");
267
 
            var p = dom_make_text_elem("p", "Error: "
268
 
                    + "There was an unexpected server error processing "
269
 
                    + "the selected command.");
270
 
            err.appendChild(p);
271
 
            p = dom_make_text_elem("p", "If the problem persists, please "
272
 
                    + "contact the system administrator.")
273
 
            err.appendChild(p);
274
 
            p = document.createElement("p");
275
 
            var refresh = document.createElement("input");
276
 
            refresh.setAttribute("type", "button");
277
 
            refresh.setAttribute("value", "Back to file view");
278
 
            refresh.setAttribute("onclick", "refresh()");
279
 
            p.appendChild(refresh);
280
 
            err.appendChild(p);
281
 
            handle_error(err);
282
 
        }
283
 
        else
284
 
        {
285
 
            var err = document.createElement("div");
286
 
            var p = dom_make_text_elem("p", "Error: "
287
 
                    + "There was an unexpected server error retrieving "
288
 
                    + "the requested file or directory.");
289
 
            err.appendChild(p);
290
 
            p = dom_make_text_elem("p", "If the problem persists, please "
291
 
                    + "contact the system administrator.")
292
 
            err.appendChild(p);
293
 
            handle_error(err);
294
 
        }
295
 
        return;
296
 
    }
297
 
    /* Get "." out, it's special */
298
 
    current_file = file_listing["."];     /* Global */
299
 
    delete file_listing["."];
300
 
 
301
 
    if ('revision' in listing)
302
 
    {
303
 
        current_revision = listing.revision;
304
 
    }
305
 
 
306
157
    /* Check if this is a directory listing or file contents */
307
 
    var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
308
 
    if (isdir)
 
158
    if (response.getResponseHeader("X-IVLE-Return") == "Dir")
309
159
    {
310
 
        setup_for_listing();
311
 
        home_listing(listing, subjects, path);
 
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
        }
 
171
        handle_dir_listing(path, listing);
312
172
    }
313
173
    else
314
174
    {
315
 
        /* Need to make a 2nd ajax call, this time get the actual file
316
 
         * contents */
317
 
        callback = function(response)
318
 
            {
319
 
                /* Read the response and set up the page accordingly */
320
 
                handle_contents_response(path, response);
321
 
            }
322
 
        /* Call the server and request the listing. */
323
 
        if (url_args)
324
 
            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];
325
180
        else
326
 
            args = {};
327
 
        /* This time, get the contents of the file, not its metadata */
328
 
        args['return'] = "contents";
329
 
        ajax_call(callback, service_app, path, args, "GET");
330
 
    }
331
 
    update_actions(isdir);
332
 
}
333
 
 
334
 
function handle_contents_response(path, response)
335
 
{
336
 
    /* Treat this as an ordinary file. Get the file type. */
337
 
    var content_type = response.getResponseHeader("Content-Type");
338
 
    var handler_type = get_handler_type(content_type);
339
 
    would_be_handler_type = handler_type;
340
 
    /* handler_type should now be set to either
341
 
     * "text", "image", "audio" or "binary". */
342
 
    switch (handler_type)
343
 
    {
344
 
    case "text":
345
 
        handle_text(path, response.responseText,
346
 
            would_be_handler_type);
347
 
        break;
348
 
    case "image":
349
 
        /* TODO: Custom image handler */
350
 
        handle_binary(path, response.responseText);
351
 
        break;
352
 
    case "audio":
353
 
        /* TODO: Custom audio handler */
354
 
        handle_binary(path, response.responseText);
355
 
        break;
356
 
    case "binary":
357
 
        handle_binary(path);
358
 
        break;
359
 
    }
360
 
}
361
 
 
362
 
/* Called when a form upload comes back (from an iframe).
363
 
 * Refreshes the page.
364
 
 */
365
 
function upload_callback()
366
 
{
367
 
    /* This has a pretty nasty hack, which happens to work.
368
 
     * upload_callback is set as the "onload" callback for the iframe which
369
 
     * receives the response from the server for uploading a file.
370
 
     * This means it gets called twice. Once when initialising the iframe, and
371
 
     * a second time when the actual response comes back.
372
 
     * All we want to do is call navigate to refresh the page. But we CAN'T do
373
 
     * that on the first load or it will just go into an infinite cycle of
374
 
     * refreshing. We need to refresh the page ONLY on the second refresh.
375
 
     * upload_callback_count is reset to 0 just before the iframe is created.
376
 
     */
377
 
    upload_callback_count++;
378
 
    if (upload_callback_count >= 2)
379
 
    {
380
 
        myFrame = frames['upload_iframe'].document;
381
 
        data = myFrame.firstChild.childNodes[1].firstChild.firstChild.nodeValue;
382
 
        data = JSON.parse(data);
383
 
        if ('Error' in data)
384
 
            alert("Error: " + decodeURIComponent(data['Error']));
385
 
        document.getElementsByName('data')[0].value = '';
386
 
        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
        }
387
206
    }
388
207
}
389
208
 
393
212
 */
394
213
function clearpage()
395
214
{
396
 
    dom_removechildren(document.getElementById("filesbody"));
397
 
}
398
 
 
399
 
/* Checks if a file needs to be saved. If it does, the user will be asked
400
 
 * if they want to continue anyway. The caller must specify a warning
401
 
 * sentence which indicates the consequences of continuing.
402
 
 * Returns true if we should continue, and false if we should not.
403
 
 */
404
 
function maybe_save(warning)
405
 
{
406
 
    if (warning == null) warning = '';
407
 
    if (current_file.isdir) return true;
408
 
    if (document.getElementById("save_button").disabled) return true;
409
 
    return confirm("This file has unsaved changes. " + warning +
410
 
                   "\nAre you sure you wish to continue?");
411
 
}
412
 
 
413
 
/** Deletes all "dynamic" content on the page necessary to navigate from
414
 
 * one directory listing to another (does not clear as much as clearpage
415
 
 * does).
416
 
 * This is the equivalent of calling clearpage() then
417
 
 * setup_for_dir_listing(), assuming the page is already on a dir listing.
418
 
 */
419
 
function clearpage_dir()
420
 
{
 
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. */
421
220
    dom_removechildren(document.getElementById("path"));
422
221
    dom_removechildren(document.getElementById("files"));
423
222
    dom_removechildren(document.getElementById("sidepanel"));
426
225
/*** HANDLERS for different types of responses (such as dir listing, file,
427
226
 * etc). */
428
227
 
429
 
/* handle_error.
430
 
 * message may either be a string, or a DOM node, which will be placed inside
431
 
 * a div.
432
 
 */
433
228
function handle_error(message)
434
229
{
435
 
    var files = document.getElementById("filesbody");
436
 
    var txt_elem;
437
 
    if (typeof(message) == "string")
438
 
    {
439
 
        txt_elem = dom_make_text_elem("div", "Error: "
440
 
                   + message.toString() + ".")
441
 
    }
442
 
    else
443
 
    {
444
 
        /* Assume message is a DOM node */
445
 
        txt_elem = document.createElement("div");
446
 
        txt_elem.appendChild(message);
447
 
    }
448
 
    txt_elem.setAttribute("class", "padding error");
449
 
    files.appendChild(txt_elem);
 
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: "
 
240
        + message.toString() + ".")
 
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);
450
248
}
451
249
 
452
 
/** Given a path, filename and optional revision, returns a URL to open that
453
 
 *  revision of that file.
 
250
/** Presents a path list (address bar inside the page) for clicking.
454
251
 */
455
 
function build_revision_url(path, filename, revision)
 
252
function presentpath(path)
456
253
{
457
 
    bits = {'path': app_path(this_app, path, filename)};
458
 
    if (current_revision)
 
254
    var dom_path = document.getElementById("path");
 
255
    var navlist = [];
 
256
    var href_path = make_path(this_app);
 
257
 
 
258
    /* Create all of the paths */
 
259
    for each (dir in path.split("/"))
459
260
    {
460
 
        bits['query_string'] = 'r=' + revision;
 
261
        if (dir == "") continue;
 
262
        navlist.push(dir);
 
263
        /* Make an 'a' element */
 
264
        href_path = path_join(href_path, dir);
 
265
        var link = dom_make_link_elem("a", dir, href_path,
 
266
                "navigate(" + href_path + ")");
 
267
        dom_path.appendChild(link);
 
268
        dom_path.appendChild(document.createTextNode("/"));
461
269
    }
462
 
    return build_url(bits);
463
270
}
464
271
 
465
272
/** Given a mime type, returns the path to the icon.
484
291
/** Given an svnstatus, returns the path to the icon.
485
292
 * \param type String, svn status.
486
293
 * \return Path to the icon. Has applied make_path, so it is relative to site
487
 
 * root. May return null to indicate no SVN icon.
 
294
 * root.
488
295
 */
489
296
function svnstatus_to_icon(svnstatus)
490
297
{
493
300
        filename = svn_icons[svnstatus];
494
301
    else
495
302
        filename = default_svn_icon;
496
 
    if (filename == null) return null;
497
303
    return make_path(path_join(svn_icons_path, filename));
498
304
}
499
305
 
500
 
/** Given an svnstatus, returns the "nice" string.
501
 
 */
502
 
function svnstatus_to_string(svnstatus)
503
 
{
504
 
    if (svnstatus in svn_nice)
505
 
        return svn_nice[svnstatus];
506
 
    else
507
 
        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
                make_path(path_join(this_app, path, filename)),
 
358
                "navigate(" + path_join(path, filename) + ")"));
 
359
        }
 
360
        else
 
361
        {
 
362
            /* Column 2: Filetype and subversion icons. */
 
363
            td = document.createElement("td");
 
364
            td.setAttribute("class", "thincol");
 
365
            td.appendChild(dom_make_img(mime_type_to_icon(file.type),
 
366
                22, 22, file.type));
 
367
            row.appendChild(td);
 
368
            td = document.createElement("td");
 
369
            td.setAttribute("class", "thincol");
 
370
            if (under_subversion)
 
371
                td.appendChild(dom_make_img(svnstatus_to_icon(file.svnstatus),
 
372
                    22, 22, file.svnstatus));
 
373
            row.appendChild(td);
 
374
            /* Column 3: Filename */
 
375
            row.appendChild(dom_make_text_elem("td", filename));
 
376
        }
 
377
        /* Column 4: Size */
 
378
        row.appendChild(dom_make_text_elem("td", nice_filesize(file.size)));
 
379
        /* Column 4: Date */
 
380
        row.appendChild(dom_make_text_elem("td", file.mtime_nice));
 
381
        files.appendChild(row);
 
382
    }
 
383
 
 
384
}
 
385
 
 
386
/** Presents the text editor.
 
387
 */
 
388
function handle_text(path, text)
 
389
{
 
390
    /* TODO */
 
391
    alert(text);
508
392
}
509
393
 
510
394
/** Displays a download link to the binary file.
511
395
 */
512
396
function handle_binary(path)
513
397
{
514
 
    var files = document.getElementById("filesbody");
515
 
    var div = document.createElement("div");
516
 
    files.appendChild(div);
517
 
    div.setAttribute("class", "padding");
518
 
    var download_link = app_path(download_app, path);
519
 
    var par1 = dom_make_text_elem("p",
520
 
        "The file " + path + " is a binary file. To download this file, " +
521
 
        "click the following link:");
522
 
    var par2 = dom_make_link_elem("p",
523
 
        "Download " + path, "Download " + path, download_link);
524
 
    div.appendChild(par1);
525
 
    div.appendChild(par2);
526
 
}
527
 
 
528
 
/* Enable or disable actions1 moreactions actions. Takes either a single
529
 
 * name, or an array of them.*/
530
 
function set_action_state(names, which, allow_on_revision)
531
 
{
532
 
    if (!(names instanceof Array)) names = Array(names);
533
 
 
534
 
    for (var i=0; i < names.length; i++)
535
 
    {
536
 
        element = document.getElementById('act_' + names[i]);
537
 
        if (which &&
538
 
            !(current_file.svnstatus == 'revision' && !allow_on_revision))
539
 
        {
540
 
            /* Enabling */
541
 
            element.setAttribute("class", "choice");
542
 
            element.removeAttribute("disabled");
543
 
        }
544
 
        else
545
 
        {
546
 
            /* Disabling */
547
 
            element.setAttribute("class", "disabled");
548
 
            element.setAttribute("disabled", "disabled");
549
 
        }
550
 
    }
551
 
}
552
 
 
553
 
/* Updates the list of available actions based on files selected */
554
 
function update_actions()
555
 
{
556
 
    var file;
557
 
    var numsel = selected_files.length;
558
 
    var svn_selection = false;
559
 
    
560
 
    if (numsel > 0)
561
 
    {
562
 
        svn_selection = true;
563
 
        for (var i = 0; i < selected_files.length; i++){
564
 
            if (file_listing[selected_files[i]]["svnstatus"] == "unversioned")
565
 
            {
566
 
                svn_selection = false;        
567
 
            }
568
 
        }
569
 
    }
570
 
    
571
 
    if (numsel <= 1)
572
 
    {
573
 
        if (numsel == 0)
574
 
        {
575
 
            /* Display information about the current directory instead */
576
 
            filename = path_basename(current_path);
577
 
            file = current_file;
578
 
        }
579
 
        else if (numsel == 1)
580
 
        {
581
 
            filename = selected_files[0];
582
 
            file = file_listing[filename];
583
 
        }
584
 
 
585
 
        /* Update each action node in the topbar.
586
 
         * This includes enabling/disabling actions as appropriate, and
587
 
         * setting href/onclick attributes. */
588
 
    }
589
 
 
590
 
    /* Open */
591
 
    /* Available if exactly one file is selected */
592
 
    var open = document.getElementById("act_open");
593
 
    if (numsel == 1)
594
 
    {
595
 
        open.setAttribute("class", "choice");
596
 
        if (file.isdir)
597
 
            open.setAttribute("title",
598
 
                "Navigate to this directory in the file browser");
599
 
        else
600
 
            open.setAttribute("title",
601
 
                "Edit or view this file");
602
 
        open.setAttribute("href", build_revision_url(current_path, filename,
603
 
                                                     current_revision));
604
 
    }
605
 
    else
606
 
    {
607
 
        open.setAttribute("class", "disabled");
608
 
        open.removeAttribute("title");
609
 
        open.removeAttribute("href");
610
 
    }
611
 
 
612
 
    /* Serve */
613
 
    /* Available if zero or one files are selected,
614
 
     * and only if this is a file, not a directory */
615
 
    var serve = document.getElementById("act_serve");
616
 
    if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
617
 
    {
618
 
        serve.setAttribute("class", "choice");
619
 
        serve.setAttribute("onclick",
620
 
              "return maybe_save('The last saved version will be served.')");
621
 
        if (numsel == 0)
622
 
            serve.setAttribute("href",
623
 
                app_path(serve_app, current_path));
624
 
        else
625
 
            serve.setAttribute("href",
626
 
                app_path(serve_app, current_path, filename));
627
 
    }
628
 
    else
629
 
    {
630
 
        serve.setAttribute("class", "disabled");
631
 
        serve.removeAttribute("href");
632
 
        serve.removeAttribute("onclick");
633
 
    }
634
 
 
635
 
    /* Run */
636
 
    /* Available if exactly one file is selected,
637
 
     * and it is a Python file.
638
 
     */
639
 
    var run = document.getElementById("act_run");
640
 
     
641
 
    if (numsel <= 1 && !file.isdir && file.type == "text/x-python" 
642
 
            && current_file.svnstatus != 'revision')
643
 
    {
644
 
        if (numsel == 0)
645
 
        {
646
 
            // In the edit window
647
 
            var localpath = path_join('/home', current_path);
648
 
        }
649
 
        else
650
 
        {
651
 
            // In the browser window
652
 
            var localpath = path_join('/home', current_path, filename);
653
 
        }
654
 
        run.setAttribute("class", "choice");
655
 
        run.setAttribute("onclick", "runfile('" + localpath + "')");
656
 
    }
657
 
    else
658
 
    {
659
 
        run.setAttribute("class", "disabled");
660
 
        run.removeAttribute("onclick");
661
 
    }
662
 
 
663
 
    /* Download */
664
 
    /* Always available for current files.
665
 
     * If 0 files selected, download the current file or directory as a ZIP.
666
 
     * If 1 directory selected, download it as a ZIP.
667
 
     * If 1 non-directory selected, download it.
668
 
     * If >1 files selected, download them all as a ZIP.
669
 
     */
670
 
    var download = document.getElementById("act_download");
671
 
    if (current_file.svnstatus == 'revision')
672
 
    {
673
 
        download.setAttribute("class", "disabled");
674
 
        download.removeAttribute("onclick");
675
 
    }
676
 
    else if (numsel <= 1)
677
 
    {
678
 
        download.setAttribute("class", "choice")
679
 
        if (numsel == 0)
680
 
        {
681
 
            download.setAttribute("href",
682
 
                app_path(download_app, current_path));
683
 
            if (file.isdir)
684
 
                download.setAttribute("title",
685
 
                    "Download the current directory as a ZIP file");
686
 
            else
687
 
                download.setAttribute("title",
688
 
                    "Download the current file");
689
 
        }
690
 
        else
691
 
        {
692
 
            download.setAttribute("href",
693
 
                app_path(download_app, current_path, filename));
694
 
            if (file.isdir)
695
 
                download.setAttribute("title",
696
 
                    "Download the selected directory as a ZIP file");
697
 
            else
698
 
                download.setAttribute("title",
699
 
                    "Download the selected file");
700
 
        }
701
 
    }
702
 
    else
703
 
    {
704
 
        /* Make a query string with all the files to download */
705
 
        var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
706
 
        for (var i=0; i<numsel; i++)
707
 
            dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
708
 
        dlpath = dlpath.substr(0, dlpath.length-1);
709
 
        download.setAttribute("class", "choice")
710
 
        download.setAttribute("href", dlpath);
711
 
        download.setAttribute("title",
712
 
            "Download the selected files as a ZIP file");
713
 
    }
714
 
 
715
 
    /* Refresh - No changes required */
716
 
 
717
 
    /* Publish and Submit */
718
 
    /* If this directory is under subversion and selected/unselected file is a
719
 
     * directory. */
720
 
    var publish = document.getElementById("act_publish");
721
 
    var submit = document.getElementById("act_submit");
722
 
    var pubcond = numsel <= 1 && file.isdir;
723
 
    if (pubcond)
724
 
    {
725
 
        /* If this dir is already published, call it "Unpublish" */
726
 
        if (file.published)
727
 
        {
728
 
            publish.setAttribute("value", "unpublish");
729
 
            publish.setAttribute("title" ,"Make it so this directory "
730
 
                + "can not be seen by anyone on the web");
731
 
            publish.firstChild.nodeValue = "Unpublish";
732
 
        } else {
733
 
            publish.setAttribute("value", "publish");
734
 
            publish.setAttribute("title","Make it so this directory "
735
 
                + "can be seen by anyone on the web");
736
 
            publish.firstChild.nodeValue = "Publish";
737
 
        }
738
 
    }
739
 
    set_action_state(["publish", "submit"], pubcond);
740
 
 
741
 
    /* Share */
742
 
    /* If exactly 1 non-directory file is selected, and its parent
743
 
     * directory is published.
744
 
     */
745
 
    set_action_state("share", numsel == 1 && !file.isdir &&
746
 
                     current_file.published);
747
 
 
748
 
    /* Rename */
749
 
    /* If exactly 1 file is selected */
750
 
    set_action_state("rename", numsel == 1);
751
 
 
752
 
    /* Delete, cut, copy */
753
 
    /* If >= 1 file is selected */
754
 
    set_action_state(["delete", "cut", "copy"], numsel >= 1);
755
 
 
756
 
    /* Paste, new file, new directory, upload */
757
 
    /* Disable if the current file is not a directory */
758
 
    set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
759
 
 
760
 
    /* Subversion actions */
761
 
    /* These are only useful if we are in a versioned directory and have some
762
 
     * files selected. */
763
 
    set_action_state(["svnadd"], numsel >= 1 && current_file.svnstatus);
764
 
    /* And these are only usefull is ALL the selected files are versioned */
765
 
    set_action_state(["svnremove", "svnrevert", "svncommit", "svncopy", 
766
 
            "svncut"], numsel >= 1 && current_file.svnstatus && svn_selection);
767
 
    
768
 
    /* Diff, log and update only support one path at the moment, so we must
769
 
     * have 0 or 1 versioned files selected. If 0, the directory must be
770
 
     * versioned. */
771
 
    single_versioned_path = (
772
 
         (
773
 
          (numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
774
 
          (numsel == 0 && (svnst = current_file.svnstatus))
775
 
         ) && svnst != "unversioned");
776
 
    set_action_state(["svndiff", "svnupdate"], single_versioned_path);
777
 
 
778
 
    /* We can resolve if we have a file selected and it is conflicted. */
779
 
    set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
780
 
 
781
 
    /* Log should be available for revisions as well. */
782
 
    set_action_state("svnlog", single_versioned_path, true);
783
 
 
784
 
    single_ivle_versioned_path = (
785
 
         (
786
 
          (numsel == 1 && (stat = file_listing[selected_files[0]])) ||
787
 
          (numsel == 0 && (stat = current_file))
788
 
         ) && stat.svnstatus != "unversioned"
789
 
           && stat.svnurl
790
 
           && stat.svnurl.substr(0, svn_base.length) == svn_base);
791
 
    set_action_state(["submit"], single_ivle_versioned_path);
792
 
 
793
 
    /* There is currently nothing on the More Actions menu of use
794
 
     * when the current file is not a directory. Hence, just remove
795
 
     * it entirely.
796
 
     * (This makes some of the above decisions somewhat redundant).
797
 
     * We also take this opportunity to show the appropriate actions2
798
 
     * bar for this path. It should either be a save or upload widget.
799
 
     */
800
 
    if (current_file.isdir)
801
 
    {
802
 
        var actions2_directory = document.getElementById("actions2_directory");
803
 
        actions2_directory.setAttribute("style", "display: inline;");
804
 
        var moreactions = document.getElementById("moreactions_area");
805
 
        moreactions.setAttribute("style", "display: inline;");
806
 
    }
807
 
    else
808
 
    {
809
 
        var actions2_file = document.getElementById("actions2_file");
810
 
        actions2_file.setAttribute("style", "display: inline;");
811
 
    }
812
 
 
813
 
    return;
814
 
}
815
 
 
816
 
/** Event handler for when an item of the "More actions..." dropdown box is
817
 
 * selected. Performs the selected action. */
818
 
function handle_moreactions()
819
 
{
820
 
    var moreactions = document.getElementById("moreactions");
821
 
    if (moreactions.value == "top")
822
 
        return;
823
 
    var selectedaction = moreactions.value;
824
 
    /* Reset to "More actions..." */
825
 
    moreactions.selectedIndex = 0;
826
 
 
827
 
    /* If 0 files selected, filename is the name of the current dir.
828
 
     * If 1 file selected, filename is that file.
829
 
     */
830
 
    if (selected_files.length == 0)
831
 
        filename = path_basename(current_path);
832
 
    else if (selected_files.length == 1)
833
 
        filename = selected_files[0];
834
 
    else
835
 
        filename = null;
836
 
 
837
 
    /* Now handle the selected action */
838
 
    switch(selectedaction)
839
 
    {
840
 
    case "publish":
841
 
        action_publish(selected_files);
842
 
        break;
843
 
    case "unpublish":
844
 
        action_unpublish(selected_files);
845
 
        break;
846
 
    case "share":
847
 
        window.open(public_app_path("~" + current_path, filename), 'share')
848
 
        break;
849
 
    case "submit":
850
 
        if (selected_files.length == 1)
851
 
            stat = file_listing[selected_files[0]];
852
 
        else
853
 
            stat = current_file;
854
 
        path = stat.svnurl.substr(svn_base.length);
855
 
 
856
 
        /* The working copy might not have an up-to-date version of the
857
 
         * directory. While submitting like this could yield unexpected
858
 
         * results, we should really submit the latest revision to minimise
859
 
         * terrible mistakes - so we run off and ask fileservice for the
860
 
         * latest revision.*/
861
 
        $.post(app_path(service_app, current_path),
862
 
            {"action": "svnrepostat", "path": path},
863
 
            function(result)
864
 
            {
865
 
                window.location = path_join(app_path('+submit'), path) + '?revision=' + result.svnrevision;
866
 
            },
867
 
            "json");
868
 
 
869
 
        break;
870
 
    case "rename":
871
 
        action_rename(filename);
872
 
        break;
873
 
    case "delete":
874
 
        action_delete(selected_files);
875
 
        break;
876
 
    case "copy":
877
 
        action_copy(selected_files);
878
 
        break;
879
 
    case "cut":
880
 
        action_cut(selected_files);
881
 
        break;
882
 
    case "paste":
883
 
        action_paste();
884
 
        break;
885
 
    case "newfile":
886
 
        action_newfile();
887
 
        break;
888
 
    case "mkdir":
889
 
        action_mkdir();
890
 
        break;
891
 
    case "upload":
892
 
        show_uploadpanel(true);
893
 
        break;
894
 
    case "svnadd":
895
 
        action_add(selected_files);
896
 
        break;
897
 
    case "svnremove":
898
 
        action_remove(selected_files);
899
 
        break;
900
 
    case "svnrevert":
901
 
        action_revert(selected_files);
902
 
        break;
903
 
    case "svndiff":
904
 
        window.location = path_join(app_path('diff'), current_path, selected_files[0] || '');
905
 
        break;
906
 
    case "svnupdate":
907
 
        action_update(selected_files);
908
 
        break;
909
 
    case "svnresolved":
910
 
        action_resolved(selected_files);
911
 
        break;
912
 
    case "svncommit":
913
 
        action_commit(selected_files);
914
 
        break;
915
 
    case "svnlog":
916
 
        window.location = path_join(app_path('svnlog'), current_path, selected_files[0] || '');
917
 
        break;
918
 
    case "svncopy":
919
 
        action_svncopy(selected_files);
920
 
        break;
921
 
    case "svncut":
922
 
        action_svncut(selected_files);
923
 
        break;
924
 
    }
925
 
}
926
 
 
927
 
/** User clicks "Run" button.
928
 
 * Do an Ajax call and print the test output.
929
 
 */
930
 
function runfile(localpath)
931
 
{
932
 
    if (!maybe_save('The last saved version will be run.')) return false;
933
 
 
934
 
    /* Dump the entire file to the console */
935
 
    var callback = function()
936
 
    {
937
 
        console_enter_line("execfile('" + localpath + "')", "block");
938
 
    }
939
 
    start_server(callback)
940
 
    return;
 
398
    /* TODO */
 
399
    alert(path);
941
400
}
942
401
 
943
402
/** Called when the page loads initially.
944
403
 */
945
 
function browser_init()
 
404
window.onload = function()
946
405
{
947
406
    /* Navigate (internally) to the path in the URL bar.
948
407
     * This causes the page to be populated with whatever is at that address,
950
409
     */
951
410
    var path = parse_url(window.location.href).path;
952
411
    /* Strip out root_dir + "/files" from the front of the path */
953
 
    var strip = make_path(this_app);
954
 
    if (path.substr(0, strip.length) == strip)
955
 
        path = path.substr(strip.length+1);
956
 
    else
957
 
    {
958
 
        /* See if this is an edit path */
959
 
        strip = make_path(edit_app);
960
 
        if (path.substr(0, strip.length) == strip)
961
 
        {
962
 
            path = path.substr(strip.length+1);
963
 
        }
964
 
    }
965
 
 
966
 
    if (path.length == 0)
967
 
    {
968
 
        /* Navigate to the user's home directory by default */
969
 
        /* TEMP? */
970
 
        path = username;
971
 
    }
 
412
    strip_chars = make_path(this_app).length + 1;
 
413
    path = path.substr(strip_chars);
972
414
 
973
415
    navigate(path);
974
416
}