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

« back to all changes in this revision

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

  • Committer: mattgiuca
  • Date: 2008-02-06 02:48:34 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:427
setup.py: Fix (put conf.py in wrong list in listmake)
doc/setup and dependencies: Added "make" as a dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 * "text" : When navigating to a text file, the text editor is opened.
32
32
 * "image" : When navigating to an image, the image is displayed (rather than
33
33
 *              going to the text editor).
34
 
 * "video" : When navigation to a video file, a "play" button is presented.
35
34
 * "audio" : When navigating to an audio file, a "play" button is presented.
36
35
 * "binary" : When navigating to a binary file, offer it as a download through
37
36
 *              "serve".
45
44
    "application/javascript" : "text",
46
45
    "application/json" : "text",
47
46
    "application/xml" : "text",
48
 
    "application/ogg" : "audio",
49
 
    "image/svg+xml": "object"
50
47
};
51
48
 
52
49
/* Mapping MIME types to icons, just the file's basename */
53
50
type_icons = {
54
51
    "text/directory": "dir.png",
55
 
    "text/x-python": "py.png"
 
52
    "text/x-python": "py.png",
56
53
};
57
54
 
58
55
default_type_icon = "txt.png";
59
56
 
60
57
/* Relative to IVLE root */
61
 
type_icons_path = "+media/ivle.webapp.core/images/mime";
62
 
type_icons_path_large = "+media/ivle.webapp.core/images/mime/large";
 
58
type_icons_path = "media/images/mime";
 
59
type_icons_path_large = "media/images/mime/large";
63
60
 
64
61
/* Mapping SVN status to icons, just the file's basename */
65
62
svn_icons = {
66
 
    "unversioned": "unversioned.png",
67
 
    "ignored": null,                    /* Supposed to be innocuous */
 
63
    "unversioned": null,
68
64
    "normal": "normal.png",
69
65
    "added": "added.png",
70
66
    "missing": "missing.png",
71
67
    "deleted": "deleted.png",
72
 
    "replaced": "replaced.png",
73
68
    "modified": "modified.png",
74
 
    "conflicted": "conflicted.png",
75
 
    "revision": "revision.png"
76
69
};
77
70
 
78
71
/* Mapping SVN status to "nice" strings */
79
72
svn_nice = {
80
73
    "unversioned": "Temporary file",
81
 
    "ignored": "Temporary file (ignored)",
82
74
    "normal": "Permanent file",
83
75
    "added": "Temporary file (scheduled to be added)",
84
76
    "missing": "Permanent file (missing)",
87
79
    "modified": "Permanent file (modified)",
88
80
    "merged": "Permanent file (merged)",
89
81
    "conflicted": "Permanent file (conflicted)",
90
 
    "revision": "Past Permanent file (revision)"
91
82
};
92
83
 
93
84
default_svn_icon = null;
94
85
default_svn_nice = "Unknown status";
95
86
 
96
 
svn_icons_path = "+media/ivle.webapp.core/images/svn";
 
87
svn_icons_path = "media/images/svn";
97
88
 
98
 
published_icon = "+media/ivle.webapp.core/images/interface/published.png";
 
89
published_icon = "media/images/interface/published.png";
99
90
 
100
91
/* List of MIME types considered "executable" by the system.
101
92
 * Executable files offer a "run" link, implying that the "serve"
102
93
 * application can interpret them.
103
94
 */
104
95
types_exec = [
105
 
    "text/x-python"
 
96
    "text/x-python",
106
97
];
107
98
 
108
99
 
109
100
/* Global variables */
110
101
 
111
 
/** The listing object returned by the server as JSON */
112
 
file_listing = null;
113
 
current_file = null;
114
 
current_revision = null;
115
102
current_path = "";
116
103
 
117
 
/** Filenames of all files selected
118
 
 * (Only used by dir listings, but still needs to be [] for files, so that
119
 
 * update_actions knows that nothing is selected).
120
 
 */
121
 
selected_files = [];
122
 
 
123
 
upload_callback_count = 0;      /* See upload_callback */
124
 
 
125
104
/** Calls the server using Ajax, performing an action on the server side.
126
105
 * Receives the response from the server and performs a refresh of the page
127
106
 * contents, updating it to display the returned data (such as a directory
140
119
 *      May be "application/x-www-form-urlencoded" or "multipart/form-data".
141
120
 *      Defaults to "application/x-www-form-urlencoded".
142
121
 *      "multipart/form-data" is recommended for large uploads.
143
 
 * \param callback, optional.
144
 
 *      A callback function for after the action has been handled.
145
122
 */
146
 
function do_action(action, path, args, content_type, callback)
 
123
function do_action(action, path, args, content_type, ignore_response)
147
124
{
148
125
    args.action = action;
149
 
    /* Callback action, when the server returns */
150
 
    var callback_inner = function(response)
151
 
        {
152
 
            /* Check for action errors reported by the server, and report them
153
 
             * to the user */
154
 
            var error = response.getResponseHeader("X-IVLE-Action-Error");
155
 
            if (error != null && error != "")
156
 
                /* Note: This header (in particular) comes URI-encoded, to
157
 
                 * allow multi-line error messages. Decode */
158
 
                alert("Error: " + decodeURIComponent(error.toString()) + ".");
159
 
            /* Now read the response and set up the page accordingly */
160
 
            if (callback != null)
161
 
                callback(path, response);
162
 
        }
163
126
    /* Call the server and perform the action. This mutates the server. */
164
 
    ajax_call(callback_inner, service_app, path, args, "POST", content_type);
 
127
    response = ajax_call(service_app, path, args, "POST", content_type);
 
128
    /* Check for action errors reported by the server, and report them to the
 
129
     * user */
 
130
    error = response.getResponseHeader("X-IVLE-Action-Error");
 
131
    if (error != null)
 
132
        alert("Error: " + error.toString() + ".");
 
133
    /* Now read the response and set up the page accordingly */
 
134
    if (ignore_response != true)
 
135
        handle_response(path, response);
165
136
}
166
137
 
167
138
/** Calls the server using Ajax, requesting a directory listing. This should
171
142
 * Called "navigate", can also be used for a simple refresh.
172
143
 * Always makes a GET request.
173
144
 * No return value.
174
 
 */
175
 
function navigate(path)
176
 
{
177
 
    callback = function(response)
178
 
        {
179
 
            /* Read the response and set up the page accordingly */
180
 
            handle_response(path, response, false, url.args);
181
 
        }
182
 
    /* Get any query strings */
183
 
    url = parse_url(window.location.href);
184
 
    
185
 
    /* Call the server and request the listing. */
186
 
    ajax_call(callback, service_app, path, url.args, "GET");
187
 
}
188
 
 
189
 
/* Refreshes the current view.
190
 
 * Calls navigate on the current path.
191
 
 */
192
 
function refresh()
193
 
{
194
 
    if (maybe_save('All changes since the last save will be lost!'))
195
 
        navigate(current_path);
 
145
 * \param editmode Optional boolean. If true, then the user navigated here
 
146
 * with an "edit" URL so we should favour using the editor.
 
147
 */
 
148
function navigate(path, editmode)
 
149
{
 
150
    /* Call the server and request the listing. This mutates the server. */
 
151
    response = ajax_call(service_app, path, null, "GET");
 
152
    /* Now read the response and set up the page accordingly */
 
153
    handle_response(path, response, editmode);
196
154
}
197
155
 
198
156
/** Determines the "handler type" from a MIME type.
199
 
 * The handler type is a string, either "text", "image", "video", "audio", 
200
 
 * "object" or "binary".
 
157
 * The handler type is a string, either "text", "image", "audio" or "binary".
201
158
 */
202
159
function get_handler_type(content_type)
203
160
{
209
166
    {   /* Based on the first part of the MIME type */
210
167
        var handler_type = content_type.split('/')[0];
211
168
        if (handler_type != "text" && handler_type != "image" &&
212
 
            handler_type != "video" && handler_type != "audio")
 
169
            handler_type != "audio")
213
170
            handler_type = "binary";
214
171
        return handler_type;
215
172
    }
225
182
 * things) be used to update the URL in the location bar.
226
183
 * \param response XMLHttpRequest object returned by the server. Should
227
184
 * contain all the response data.
228
 
 * \param is_action Boolean. True if this is the response to an action, false
229
 
 * if this is the response to a simple listing. This is used in handling the
230
 
 * error.
231
 
 * \param url_args Arguments dict, for the arguments passed to the URL
232
 
 * in the browser's address bar (will be forwarded along).
 
185
 * \param editmode Optional boolean. If true, then the user navigated here
 
186
 * with an "edit" URL so we should favour using the editor.
233
187
 */
234
 
function handle_response(path, response, is_action, url_args)
 
188
function handle_response(path, response, editmode)
235
189
{
236
190
    /* TODO: Set location bar to "path" */
237
191
    current_path = path;
238
192
 
239
193
    /* Clear away the existing page contents */
240
194
    clearpage();
 
195
    /* Display the path at the top, for navigation */
 
196
    presentpath(path);
241
197
 
242
198
    /* Check the status, and if not 200, read the error and handle this as an
243
199
     * error. */
250
206
        return;
251
207
    }
252
208
 
253
 
    var subjects = null;
254
 
    /* Remove trailing slash (or path==username won't compare properly) */
255
 
    if (path[path.length-1] == "/")
256
 
        path = path.substr(0, path.length-1);
257
 
    var top_level_dir = path==username;
258
 
    if (top_level_dir)
259
 
    {
260
 
        var req = ajax_call(null, "userservice", "get_enrolments", null, "GET")
261
 
        subjects = decode_response(req);
262
 
    }
263
 
 
264
 
 
265
 
    /* This will always return a listing, whether it is a dir or a file.
266
 
     */
267
 
    var listing = response.responseText;
268
 
    /* The listing SHOULD be valid JSON text. Parse it into an object. */
269
 
    try
270
 
    {
271
 
        listing = JSON.parse(listing);
272
 
        file_listing = listing.listing;     /* Global */
273
 
    }
274
 
    catch (e)
275
 
    {
276
 
        if (is_action)
277
 
        {
278
 
            var err = document.createElement("div");
279
 
            var p = dom_make_text_elem("p", "Error: "
280
 
                    + "There was an unexpected server error processing "
281
 
                    + "the selected command.");
282
 
            err.appendChild(p);
283
 
            p = dom_make_text_elem("p", "If the problem persists, please "
284
 
                    + "contact the system administrator.")
285
 
            err.appendChild(p);
286
 
            p = document.createElement("p");
287
 
            var refresh = document.createElement("input");
288
 
            refresh.setAttribute("type", "button");
289
 
            refresh.setAttribute("value", "Back to file view");
290
 
            refresh.setAttribute("onclick", "refresh()");
291
 
            p.appendChild(refresh);
292
 
            err.appendChild(p);
293
 
            handle_error(err);
294
 
        }
295
 
        else
296
 
        {
297
 
            var err = document.createElement("div");
298
 
            var p = dom_make_text_elem("p", "Error: "
299
 
                    + "There was an unexpected server error retrieving "
300
 
                    + "the requested file or directory.");
301
 
            err.appendChild(p);
302
 
            p = dom_make_text_elem("p", "If the problem persists, please "
303
 
                    + "contact the system administrator.")
304
 
            err.appendChild(p);
305
 
            handle_error(err);
306
 
        }
307
 
        return;
308
 
    }
309
 
    /* Get "." out, it's special */
310
 
    current_file = file_listing["."];     /* Global */
311
 
    delete file_listing["."];
312
 
 
313
 
    if ('revision' in listing)
314
 
    {
315
 
        current_revision = listing.revision;
316
 
    }
317
 
 
318
209
    /* Check if this is a directory listing or file contents */
319
210
    var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
320
 
    if (isdir)
 
211
    if (!editmode && isdir)
321
212
    {
322
 
        setup_for_listing();
323
 
        if (top_level_dir)
324
 
        {
325
 
            /* Top-level dir, with subjects */
326
 
            special_home_listing(listing, subjects, path);
327
 
        }
328
 
        else
329
 
        {
330
 
            /* Not the top-level dir. Do a normal dir listing. */
331
 
            handle_dir_listing(path, listing.listing);
332
 
        }
 
213
        var listing = response.responseText;
 
214
        /* The listing SHOULD be valid JSON text. Parse it into an object. */
 
215
        try
 
216
        {
 
217
            listing = JSON.parse(listing);
 
218
        }
 
219
        catch (e)
 
220
        {
 
221
            handle_error("The server returned an invalid directory listing");
 
222
            return;
 
223
        }
 
224
        handle_dir_listing(path, listing);
333
225
    }
334
226
    else
335
227
    {
336
 
        /* Read the response and set up the page accordingly */
337
 
        var content_type = current_file.type;
338
 
        handle_contents_response(path, content_type, url_args);
339
 
 
340
 
    }
341
 
    update_actions(isdir);
342
 
}
343
 
 
344
 
function handle_contents_response(path, content_type)
345
 
{
346
 
    /* Treat this as an ordinary file. Get the file type. */
347
 
    //var content_type = response.getResponseHeader("Content-Type");
348
 
    var handler_type = get_handler_type(content_type);
349
 
    /* handler_type should now be set to either
350
 
     * "text", "image", "video", "audio" or "binary". */
351
 
    switch (handler_type)
352
 
    {
353
 
    case "text":
354
 
        handle_text(path, content_type);
355
 
        break;
356
 
    case "image":
357
 
        handle_image(path);
358
 
        break;
359
 
    case "video":
360
 
        handle_html5_media(path, content_type, "video");
361
 
        break;
362
 
    case "audio":
363
 
        handle_html5_media(path, content_type, "audio");
364
 
        break;
365
 
    case "object":
366
 
        handle_object(path, content_type);
367
 
        break;
368
 
    case "binary":
369
 
        handle_binary(path);
370
 
        break;
371
 
    }
372
 
}
373
 
 
374
 
/* Called when a form upload comes back (from an iframe).
375
 
 * Refreshes the page.
376
 
 */
377
 
function upload_callback()
378
 
{
379
 
    /* This has a pretty nasty hack, which happens to work.
380
 
     * upload_callback is set as the "onload" callback for the iframe which
381
 
     * receives the response from the server for uploading a file.
382
 
     * This means it gets called twice. Once when initialising the iframe, and
383
 
     * a second time when the actual response comes back.
384
 
     * All we want to do is call navigate to refresh the page. But we CAN'T do
385
 
     * that on the first load or it will just go into an infinite cycle of
386
 
     * refreshing. We need to refresh the page ONLY on the second refresh.
387
 
     * upload_callback_count is reset to 0 just before the iframe is created.
388
 
     */
389
 
    upload_callback_count++;
390
 
    if (upload_callback_count >= 2)
391
 
    {
392
 
        myFrame = frames['upload_iframe'].document;
393
 
        /* Browsers will turn the raw returned JSON into an HTML document. We
394
 
         * need to get the <pre> from inside the <body>, and look at its text.
395
 
         */
396
 
        var pre = myFrame.firstChild.getElementsByTagName(
397
 
            'body')[0].firstChild;
398
 
        var data = pre.innerText || pre.textContent;
399
 
        data = JSON.parse(data);
400
 
        if ('Error' in data)
401
 
            alert("Error: " + decodeURIComponent(data['Error']));
402
 
        document.getElementsByName('data')[0].value = '';
403
 
        refresh();
 
228
        /* Treat this as an ordinary file. Get the file type. */
 
229
        var content_type = response.getResponseHeader("Content-Type");
 
230
        var handler_type = get_handler_type(content_type);
 
231
        /* If we're in "edit mode", always treat this file as text */
 
232
        would_be_handler_type = handler_type;
 
233
        if (editmode) handler_type = "text";
 
234
        /* handler_type should now be set to either
 
235
         * "text", "image", "audio" or "binary". */
 
236
        switch (handler_type)
 
237
        {
 
238
        case "text":
 
239
            if (isdir)
 
240
            {
 
241
                handle_text(path_join(path, "untitled"), "",
 
242
                    would_be_handler_type);
 
243
            }
 
244
            else
 
245
            {
 
246
                handle_text(path, response.responseText,
 
247
                    would_be_handler_type);
 
248
            }
 
249
            break;
 
250
        case "image":
 
251
            /* TODO: Custom image handler */
 
252
            handle_binary(path, response.responseText);
 
253
            break;
 
254
        case "audio":
 
255
            /* TODO: Custom audio handler */
 
256
            handle_binary(path, response.responseText);
 
257
            break;
 
258
        case "binary":
 
259
            handle_binary(path);
 
260
            break;
 
261
        }
404
262
    }
405
263
}
406
264
 
410
268
 */
411
269
function clearpage()
412
270
{
 
271
    dom_removechildren(document.getElementById("path"));
413
272
    dom_removechildren(document.getElementById("filesbody"));
414
273
}
415
274
 
416
 
/* Checks if a file needs to be saved. If it does, the user will be asked
417
 
 * if they want to continue anyway. The caller must specify a warning
418
 
 * sentence which indicates the consequences of continuing.
419
 
 * Returns true if we should continue, and false if we should not.
420
 
 */
421
 
function maybe_save(warning)
422
 
{
423
 
    if (warning == null) warning = '';
424
 
    if (current_file == null || current_file.isdir) return true;
425
 
    if (document.getElementById("save_button").disabled) return true;
426
 
    return confirm("This file has unsaved changes. " + warning +
427
 
                   "\nAre you sure you wish to continue?");
428
 
}
429
 
 
430
275
/** Deletes all "dynamic" content on the page necessary to navigate from
431
276
 * one directory listing to another (does not clear as much as clearpage
432
277
 * does).
440
285
    dom_removechildren(document.getElementById("sidepanel"));
441
286
}
442
287
 
 
288
/** Sets the mode to either "file browser" or "text editor" mode.
 
289
 * This modifies the window icon, and selected tab.
 
290
 * \param editmode If True, editor mode. Else, file browser mode.
 
291
 */
 
292
function setmode(editmode)
 
293
{
 
294
    /* Find the DOM elements for the file browser and editor tabs */
 
295
    var tabs = document.getElementById("apptabs");
 
296
    var tab_files = null;
 
297
    var tab_edit = null;
 
298
    var a;
 
299
    var href;
 
300
    for (var i=0; i<tabs.childNodes.length; i++)
 
301
    {
 
302
        /* Find the href of the link within */
 
303
        if (!tabs.childNodes[i].getElementsByTagName) continue;
 
304
        a = tabs.childNodes[i].getElementsByTagName("a");
 
305
        if (a.length == 0) continue;
 
306
        href = a[0].getAttribute("href");
 
307
        if (href == null) continue;
 
308
        if (endswith(href, this_app))
 
309
            tab_files = tabs.childNodes[i];
 
310
        else if (endswith(href, edit_app))
 
311
            tab_edit = tabs.childNodes[i];
 
312
    }
 
313
 
 
314
    if (editmode)
 
315
    {
 
316
        tab_files.removeAttribute("class");
 
317
        tab_edit.setAttribute("class", "thisapp");
 
318
    }
 
319
    else
 
320
    {
 
321
        tab_edit.removeAttribute("class");
 
322
        tab_files.setAttribute("class", "thisapp");
 
323
    }
 
324
}
 
325
 
443
326
/*** HANDLERS for different types of responses (such as dir listing, file,
444
327
 * etc). */
445
328
 
446
 
/* handle_error.
447
 
 * message may either be a string, or a DOM node, which will be placed inside
448
 
 * a div.
449
 
 */
450
329
function handle_error(message)
451
330
{
 
331
    setmode(false);
452
332
    var files = document.getElementById("filesbody");
453
 
    var txt_elem;
454
 
    if (typeof(message) == "string")
455
 
    {
456
 
        txt_elem = dom_make_text_elem("div", "Error: "
457
 
                   + message.toString() + ".")
458
 
    }
459
 
    else
460
 
    {
461
 
        /* Assume message is a DOM node */
462
 
        txt_elem = document.createElement("div");
463
 
        txt_elem.appendChild(message);
464
 
    }
 
333
    var txt_elem = dom_make_text_elem("div", "Error: "
 
334
        + message.toString() + ".")
465
335
    txt_elem.setAttribute("class", "padding error");
466
336
    files.appendChild(txt_elem);
467
337
}
468
338
 
469
 
/** Given a path, filename and optional revision, returns a URL to open that
470
 
 *  revision of that file.
 
339
/** Presents a path list (address bar inside the page) for clicking.
471
340
 */
472
 
function build_revision_url(path, filename, revision)
 
341
function presentpath(path)
473
342
{
474
 
    bits = {'path': app_path(this_app, path, filename)};
475
 
    if (current_revision)
 
343
    var dom_path = document.getElementById("path");
 
344
    var href_path = make_path(this_app);
 
345
    var nav_path = "";
 
346
    var dir;
 
347
 
 
348
    /* Also set the document title */
 
349
    document.title = path_basename(path) + " - IVLE";
 
350
    /* Create all of the paths */
 
351
    var pathlist = path.split("/");
 
352
    for (var i=0; i<pathlist.length; i++)
476
353
    {
477
 
        bits['query_string'] = 'r=' + revision;
 
354
        dir = pathlist[i];
 
355
        if (dir == "") continue;
 
356
        /* Make an 'a' element */
 
357
        href_path = path_join(href_path, dir);
 
358
        nav_path = path_join(nav_path, dir);
 
359
        var link = dom_make_link_elem("a", dir, "Navigate to " + nav_path,
 
360
                href_path/*, "navigate(" + repr(href_path) + ")"*/);
 
361
        dom_path.appendChild(link);
 
362
        dom_path.appendChild(document.createTextNode("/"));
478
363
    }
479
 
    return build_url(bits);
 
364
    dom_path.removeChild(dom_path.lastChild);
480
365
}
481
366
 
482
367
/** Given a mime type, returns the path to the icon.
524
409
        return default_svn_nice;
525
410
}
526
411
 
527
 
/** Returns true if a file is versioned (not unversioned or ignored).
528
 
 */
529
 
function svnstatus_versioned(svnstatus)
530
 
{
531
 
    return svnstatus != "unversioned" && svnstatus != "ignored";
532
 
}
533
 
 
534
412
/** Displays a download link to the binary file.
535
413
 */
536
414
function handle_binary(path)
537
415
{
538
 
    // Disable save button
539
 
    using_codepress = false;
540
 
    disable_save();
541
 
 
542
 
    // Show download link
 
416
    setmode(false);
543
417
    var files = document.getElementById("filesbody");
544
418
    var div = document.createElement("div");
545
419
    files.appendChild(div);
546
420
    div.setAttribute("class", "padding");
547
 
    var download_link = app_url(download_app, path);
 
421
    var download_link = app_path(download_app, path);
548
422
    var par1 = dom_make_text_elem("p",
549
423
        "The file " + path + " is a binary file. To download this file, " +
550
424
        "click the following link:");
554
428
    div.appendChild(par2);
555
429
}
556
430
 
557
 
/** Displays an image file.
558
 
 */
559
 
function handle_image(path)
560
 
{
561
 
    /* Disable save button */
562
 
    using_codepress = false;
563
 
    disable_save();
564
 
 
565
 
    /* URL */
566
 
    var url = app_url(service_app, path) + "?return=contents";
567
 
 
568
 
    /* Image Preview */
569
 
    var img = $("<img />");
570
 
    img.attr("alt", path);
571
 
    img.attr("src", url);
572
 
 
573
 
    /* Show Preview */
574
 
    var div = $('<div class="padding" />');
575
 
    div.append('<h1>Image Preview</h1>');
576
 
    div.append(img);
577
 
    $("#filesbody").append(div);
578
 
}
579
 
 
580
 
/* Displays a media file using an HTML5 <audio> or <video> tag.
581
 
 * Falls back to <object> if the format is unsupported.
582
 
 */
583
 
function handle_html5_media(path, type, tag_name)
584
 
{
585
 
    /* Disable save button and hide the save panel */
586
 
    using_codepress = false;
587
 
    disable_save();
588
 
 
589
 
    /* URL */
590
 
    var url = app_url(service_app, path) + "?return=contents";
591
 
    var download_url = app_url(download_app, path);
592
 
 
593
 
    /* Fallback download link */
594
 
    var link = $(
595
 
        '<p>Could not play ' + tag_name + ' file. ' +
596
 
        'Try <a>downloading it</a> instead.</p>');
597
 
    link.find('a').attr("href", download_url);
598
 
 
599
 
    /* HTML 5 media element */
600
 
    var html5_element = $(
601
 
        '<' + tag_name + ' controls="true" autoplay="true" />');
602
 
    html5_element.attr("src", url);
603
 
    var support = (html5_element[0].canPlayType &&
604
 
                   html5_element[0].canPlayType(type));
605
 
 
606
 
    /* If the browser thinks it might be able to play it, use the HTML5
607
 
     * element. Otherwise, fall back to an <object>, which might work.
608
 
     */
609
 
    if (support == "probably" || support == "maybe") {
610
 
        var element = html5_element;
611
 
    } else {
612
 
        var element = $('<object />');
613
 
        element.attr("type", type);
614
 
        element.attr("data", url);
615
 
    }
616
 
    element.append(link);
617
 
 
618
 
    /* Show Preview */
619
 
    var div = $('<div class="padding" />');
620
 
    div.append('<h1>File preview</h1>');
621
 
    div.append(element);
622
 
    $("#filesbody").append(div);
623
 
}
624
 
 
625
 
/** Display generic object content
626
 
 */
627
 
function handle_object(path, content_type)
628
 
{
629
 
    /* Disable save button and hide the save panel */
630
 
    using_codepress = false;
631
 
    disable_save();
632
 
 
633
 
    /* URL */
634
 
    var url = app_url(service_app, path) + "?return=contents";
635
 
    var download_url = app_url(download_app, path);
636
 
 
637
 
    /* Fallback Download Link */
638
 
    var link = $('<p><a /></p>');
639
 
    var a = link.find('a');
640
 
    a.attr("href", download_url);
641
 
    a.text("Download " + path);
642
 
 
643
 
    /* Object Tag */
644
 
    var obj = $('<object width="100%" height="500px" />');
645
 
    obj.attr("type", content_type);
646
 
    obj.attr("data", url);
647
 
    obj.append('Could not load object');
648
 
 
649
 
    /* Show Preview */
650
 
    var div = $('<div class="padding" />');
651
 
    div.append('<h1>Preview</h1>');
652
 
    div.append(obj);
653
 
    div.append(link);
654
 
    $("#filesbody").append(div);
655
 
}
656
 
 
657
 
/* Present an element for the given path.
658
 
 * Gives it a title and download link.
659
 
 */
660
 
function present_custom_handler(path, type, element)
661
 
{
662
 
    /* Disable save button and hide the save panel */
663
 
    using_codepress = false;
664
 
    disable_save();
665
 
 
666
 
    /* URL */
667
 
    var url = app_url(service_app, path) + "?return=contents";
668
 
    var download_url = app_url(download_app, path);
669
 
 
670
 
    /* Fallback download link */
671
 
    var link = $(
672
 
        '<p>Could not play ' + tag_name + ' file. ' +
673
 
        'Try <a>downloading it</a> instead.</p>');
674
 
    link.find('a').attr("href", download_url);
675
 
 
676
 
    /* HTML 5 media element */
677
 
    var html5_element = $(
678
 
        '<' + tag_name + ' controls="true" autoplay="true" />');
679
 
    html5_element.attr("src", url);
680
 
    var support = (html5_element[0].canPlayType &&
681
 
                   html5_element[0].canPlayType(type));
682
 
 
683
 
    /* If the browser thinks it might be able to play it, use the HTML5
684
 
     * element. Otherwise, fall back to an <object>, which might work.
685
 
     */
686
 
    if (support == "probably" || support == "maybe") {
687
 
        var element = html5_element;
688
 
    } else {
689
 
        var element = $('<object />');
690
 
        element.attr("type", type);
691
 
        element.attr("data", url);
692
 
    }
693
 
    element.append(link);
694
 
 
695
 
    /* Show Preview */
696
 
    var div = $('<div class="padding" />');
697
 
    div.append('<h1>File preview</h1>');
698
 
    div.append(element);
699
 
    $("#filesbody").append(div);
700
 
}
701
 
 
702
 
/* Enable or disable actions1 moreactions actions. Takes either a single
703
 
 * name, or an array of them.*/
704
 
function set_action_state(names, which, allow_on_revision)
705
 
{
706
 
    if (!(names instanceof Array)) names = Array(names);
707
 
 
708
 
    for (var i=0; i < names.length; i++)
709
 
    {
710
 
        element = document.getElementById('act_' + names[i]);
711
 
        if (which &&
712
 
            !(current_file.svnstatus == 'revision' && !allow_on_revision))
713
 
        {
714
 
            /* Enabling */
715
 
            element.setAttribute("class", "choice");
716
 
            element.removeAttribute("disabled");
717
 
        }
718
 
        else
719
 
        {
720
 
            /* Disabling */
721
 
            element.setAttribute("class", "disabled");
722
 
            element.setAttribute("disabled", "disabled");
723
 
        }
724
 
    }
725
 
}
726
 
 
727
 
/* Updates the list of available actions based on files selected */
728
 
function update_actions()
729
 
{
730
 
    var file;
731
 
    var numsel = selected_files.length;
732
 
    var svn_selection = false;
733
 
    
734
 
    if (numsel > 0)
735
 
    {
736
 
        svn_selection = true;
737
 
        for (var i = 0; i < selected_files.length; i++){
738
 
            if (!svnstatus_versioned(file_listing[selected_files[i]].svnstatus))
739
 
            {
740
 
                svn_selection = false;
741
 
            }
742
 
        }
743
 
    }
744
 
    
745
 
    if (numsel <= 1)
746
 
    {
747
 
        if (numsel == 0)
748
 
        {
749
 
            /* Display information about the current directory instead */
750
 
            filename = path_basename(current_path);
751
 
            file = current_file;
752
 
        }
753
 
        else if (numsel == 1)
754
 
        {
755
 
            filename = selected_files[0];
756
 
            file = file_listing[filename];
757
 
        }
758
 
 
759
 
        /* Update each action node in the topbar.
760
 
         * This includes enabling/disabling actions as appropriate, and
761
 
         * setting href/onclick attributes. */
762
 
    }
763
 
 
764
 
    /* Open */
765
 
    /* Available if exactly one file is selected */
766
 
    var open = document.getElementById("act_open");
767
 
    if (numsel == 1)
768
 
    {
769
 
        open.setAttribute("class", "choice");
770
 
        if (file.isdir)
771
 
            open.setAttribute("title",
772
 
                "Navigate to this directory in the file browser");
773
 
        else
774
 
            open.setAttribute("title",
775
 
                "Edit or view this file");
776
 
        open.setAttribute("href", build_revision_url(current_path, filename,
777
 
                                                     current_revision));
778
 
    }
779
 
    else
780
 
    {
781
 
        open.setAttribute("class", "disabled");
782
 
        open.removeAttribute("title");
783
 
        open.removeAttribute("href");
784
 
    }
785
 
 
786
 
    /* Serve */
787
 
    /* Available if zero or one files are selected,
788
 
     * and only if this is a file, not a directory */
789
 
    var serve = document.getElementById("act_serve");
790
 
    if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
791
 
    {
792
 
        serve.setAttribute("class", "choice");
793
 
        serve.setAttribute("onclick",
794
 
              "return maybe_save('The last saved version will be served.')");
795
 
        if (numsel == 0)
796
 
            serve.setAttribute("href",
797
 
                app_url(serve_app, current_path));
798
 
        else
799
 
            serve.setAttribute("href",
800
 
                app_url(serve_app, current_path, filename));
801
 
    }
802
 
    else
803
 
    {
804
 
        serve.setAttribute("class", "disabled");
805
 
        serve.removeAttribute("href");
806
 
        serve.removeAttribute("onclick");
807
 
    }
808
 
 
809
 
    /* Run */
810
 
    /* Available if exactly one file is selected,
811
 
     * and it is a Python file.
812
 
     */
813
 
    var run = document.getElementById("act_run");
814
 
     
815
 
    if (numsel <= 1 && !file.isdir && file.type == "text/x-python" 
816
 
            && current_file.svnstatus != 'revision')
817
 
    {
818
 
        if (numsel == 0)
819
 
        {
820
 
            // In the edit window
821
 
            var localpath = path_join('/home', current_path);
822
 
        }
823
 
        else
824
 
        {
825
 
            // In the browser window
826
 
            var localpath = path_join('/home', current_path, filename);
827
 
        }
828
 
        run.setAttribute("class", "choice");
829
 
        run.setAttribute("onclick", "runfile('" + localpath + "')");
830
 
    }
831
 
    else
832
 
    {
833
 
        run.setAttribute("class", "disabled");
834
 
        run.removeAttribute("onclick");
835
 
    }
836
 
 
837
 
    /* Download */
838
 
    /* Always available for current files.
839
 
     * If 0 files selected, download the current file or directory as a ZIP.
840
 
     * If 1 directory selected, download it as a ZIP.
841
 
     * If 1 non-directory selected, download it.
842
 
     * If >1 files selected, download them all as a ZIP.
843
 
     */
844
 
    var download = document.getElementById("act_download");
845
 
    if (current_file.svnstatus == 'revision')
846
 
    {
847
 
        download.setAttribute("class", "disabled");
848
 
        download.removeAttribute("onclick");
849
 
    }
850
 
    else if (numsel <= 1)
851
 
    {
852
 
        download.setAttribute("class", "choice")
853
 
        if (numsel == 0)
854
 
        {
855
 
            download.setAttribute("href",
856
 
                app_url(download_app, current_path));
857
 
            if (file.isdir)
858
 
                download.setAttribute("title",
859
 
                    "Download the current directory as a ZIP file");
860
 
            else
861
 
                download.setAttribute("title",
862
 
                    "Download the current file");
863
 
        }
864
 
        else
865
 
        {
866
 
            download.setAttribute("href",
867
 
                app_url(download_app, current_path, filename));
868
 
            if (file.isdir)
869
 
                download.setAttribute("title",
870
 
                    "Download the selected directory as a ZIP file");
871
 
            else
872
 
                download.setAttribute("title",
873
 
                    "Download the selected file");
874
 
        }
875
 
    }
876
 
    else
877
 
    {
878
 
        /* Make a query string with all the files to download */
879
 
        var dlpath = app_url(download_app, current_path) + "?";
880
 
        for (var i=0; i<numsel; i++)
881
 
            dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
882
 
        dlpath = dlpath.substr(0, dlpath.length-1);
883
 
        download.setAttribute("class", "choice")
884
 
        download.setAttribute("href", dlpath);
885
 
        download.setAttribute("title",
886
 
            "Download the selected files as a ZIP file");
887
 
    }
888
 
 
889
 
    /* Refresh - No changes required */
890
 
 
891
 
    /* Publish and Submit */
892
 
    /* If this directory is under subversion and selected/unselected file is a
893
 
     * directory. */
894
 
    var publish = document.getElementById("act_publish");
895
 
    var submit = document.getElementById("act_submit");
896
 
    var pubcond = numsel <= 1 && file.isdir;
897
 
    if (pubcond)
898
 
    {
899
 
        /* If this dir is already published, call it "Unpublish" */
900
 
        if (file.published)
901
 
        {
902
 
            publish.setAttribute("value", "unpublish");
903
 
            publish.setAttribute("title" ,"Make it so this directory "
904
 
                + "can not be seen by anyone on the web");
905
 
            publish.firstChild.nodeValue = "Unpublish";
906
 
        } else {
907
 
            publish.setAttribute("value", "publish");
908
 
            publish.setAttribute("title","Make it so this directory "
909
 
                + "can be seen by anyone on the web");
910
 
            publish.firstChild.nodeValue = "Publish";
911
 
        }
912
 
    }
913
 
    set_action_state(["publish", "submit"], pubcond);
914
 
 
915
 
    /* Share */
916
 
    /* If exactly 1 non-directory file is selected, and its parent
917
 
     * directory is published.
918
 
     */
919
 
    set_action_state("share", numsel == 1 && !file.isdir &&
920
 
                     current_file.published);
921
 
 
922
 
    /* Rename */
923
 
    /* If exactly 1 file is selected */
924
 
    set_action_state("rename", numsel == 1);
925
 
 
926
 
    /* Delete, cut, copy */
927
 
    /* If >= 1 file is selected */
928
 
    set_action_state(["delete", "cut", "copy"], numsel >= 1);
929
 
 
930
 
    /* Paste, new file, new directory, upload */
931
 
    /* Disable if the current file is not a directory */
932
 
    set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
933
 
 
934
 
    /* Subversion actions */
935
 
    /* These are only useful if we are in a versioned directory and have some
936
 
     * files selected. */
937
 
    set_action_state(["svnrename"], numsel == 1 && current_file.svnstatus);
938
 
    set_action_state(["svnadd"], numsel >= 1 && current_file.svnstatus);
939
 
    /* And these are only useful is ALL the selected files are versioned */
940
 
    set_action_state(["svnremove", "svnrevert", "svncopy", "svncut"],
941
 
            numsel >= 1 && current_file.svnstatus && svn_selection);
942
 
    /* Commit is useful if ALL selected files are versioned, or the current
943
 
     * directory is versioned */
944
 
    set_action_state(["svncommit"], current_file.svnstatus &&
945
 
            (numsel >= 1 && svn_selection || numsel == 0));
946
 
 
947
 
    /* Diff, log and update only support one path at the moment, so we must
948
 
     * have 0 or 1 versioned files selected. If 0, the directory must be
949
 
     * versioned. */
950
 
    single_versioned_path = (
951
 
         (
952
 
          (numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
953
 
          (numsel == 0 && (svnst = current_file.svnstatus))
954
 
         ) && svnstatus_versioned(svnst));
955
 
    set_action_state(["svndiff", "svnupdate"], single_versioned_path);
956
 
 
957
 
    /* We can resolve if we have a file selected and it is conflicted. */
958
 
    set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
959
 
 
960
 
    /* Log should be available for revisions as well. */
961
 
    set_action_state("svnlog", single_versioned_path, true);
962
 
 
963
 
    /* Cleanup should be available for revisions as well. */
964
 
    set_action_state("svncleanup", single_versioned_path, true);
965
 
 
966
 
    single_ivle_versioned_path = (
967
 
         (
968
 
          (numsel == 1 && (stat = file_listing[selected_files[0]])) ||
969
 
          (numsel == 0 && (stat = current_file))
970
 
         ) && svnstatus_versioned(stat.svnstatus)
971
 
           && stat.svnurl
972
 
           && stat.svnurl.substr(0, svn_base.length) == svn_base);
973
 
    set_action_state(["submit"], single_ivle_versioned_path);
974
 
 
975
 
    /* There is currently nothing on the More Actions menu of use
976
 
     * when the current file is not a directory. Hence, just remove
977
 
     * it entirely.
978
 
     * (This makes some of the above decisions somewhat redundant).
979
 
     * We also take this opportunity to show the appropriate actions2
980
 
     * bar for this path. It should either be a save or upload widget.
981
 
     */
982
 
    if (current_file.isdir)
983
 
    {
984
 
        var actions2_directory = document.getElementById("actions2_directory");
985
 
        actions2_directory.setAttribute("style", "display: inline;");
986
 
        var moreactions = document.getElementById("moreactions_area");
987
 
        moreactions.setAttribute("style", "display: inline;");
988
 
    }
989
 
 
990
 
    return;
991
 
}
992
 
 
993
 
/** Event handler for when an item of the "More actions..." dropdown box is
994
 
 * selected. Performs the selected action. */
995
 
function handle_moreactions()
996
 
{
997
 
    var moreactions = document.getElementById("moreactions");
998
 
    if (moreactions.value == "top")
999
 
        return;
1000
 
    var selectedaction = moreactions.value;
1001
 
    /* Reset to "More actions..." */
1002
 
    moreactions.selectedIndex = 0;
1003
 
 
1004
 
    /* If 0 files selected, filename is the name of the current dir.
1005
 
     * If 1 file selected, filename is that file.
1006
 
     */
1007
 
    if (selected_files.length == 0)
1008
 
        filename = path_basename(current_path);
1009
 
    else if (selected_files.length == 1)
1010
 
        filename = selected_files[0];
1011
 
    else
1012
 
        filename = null;
1013
 
 
1014
 
    /* Now handle the selected action */
1015
 
    switch(selectedaction)
1016
 
    {
1017
 
    case "publish":
1018
 
        action_publish(selected_files);
1019
 
        break;
1020
 
    case "unpublish":
1021
 
        action_unpublish(selected_files);
1022
 
        break;
1023
 
    case "share":
1024
 
        window.open(public_app_url("~" + current_path, filename), 'share')
1025
 
        break;
1026
 
    case "submit":
1027
 
        if (selected_files.length == 1)
1028
 
            stat = file_listing[selected_files[0]];
1029
 
        else
1030
 
            stat = current_file;
1031
 
        url = stat.svnurl.substr(svn_base.length);      // URL-encoded
1032
 
        path = decodeURIComponent(url);
1033
 
 
1034
 
        /* The working copy might not have an up-to-date version of the
1035
 
         * directory. While submitting like this could yield unexpected
1036
 
         * results, we should really submit the latest revision to minimise
1037
 
         * terrible mistakes - so we run off and ask fileservice for the
1038
 
         * latest revision.*/
1039
 
        $.post(app_path(service_app, current_path),
1040
 
            {"action": "svnrepostat", "path": path},
1041
 
            function(result)
1042
 
            {
1043
 
                window.location = path_join(app_path('+submit'), url) + '?revision=' + result.svnrevision;
1044
 
            },
1045
 
            "json");
1046
 
 
1047
 
        break;
1048
 
    case "rename":
1049
 
        action_rename(filename);
1050
 
        break;
1051
 
    case "delete":
1052
 
        action_delete(selected_files);
1053
 
        break;
1054
 
    case "copy":
1055
 
        action_copy(selected_files);
1056
 
        break;
1057
 
    case "cut":
1058
 
        action_cut(selected_files);
1059
 
        break;
1060
 
    case "paste":
1061
 
        action_paste();
1062
 
        break;
1063
 
    case "newfile":
1064
 
        action_newfile();
1065
 
        break;
1066
 
    case "mkdir":
1067
 
        action_mkdir();
1068
 
        break;
1069
 
    case "upload":
1070
 
        show_uploadpanel(true);
1071
 
        break;
1072
 
    case "svnadd":
1073
 
        action_add(selected_files);
1074
 
        break;
1075
 
    case "svnremove":
1076
 
        action_svnremove(selected_files);
1077
 
        break;
1078
 
    case "svnrename":
1079
 
        action_svnrename(selected_files);
1080
 
        break;
1081
 
    case "svnrevert":
1082
 
        action_revert(selected_files);
1083
 
        break;
1084
 
    case "svndiff":
1085
 
        window.location = path_join(app_url('diff'), current_path, selected_files[0] || '');
1086
 
        break;
1087
 
    case "svnupdate":
1088
 
        action_update(selected_files);
1089
 
        break;
1090
 
    case "svnresolved":
1091
 
        action_resolved(selected_files);
1092
 
        break;
1093
 
    case "svncommit":
1094
 
        action_commit(selected_files);
1095
 
        break;
1096
 
    case "svnlog":
1097
 
        window.location = path_join(app_url('svnlog'), current_path, selected_files[0] || '');
1098
 
        break;
1099
 
    case "svncopy":
1100
 
        action_svncopy(selected_files);
1101
 
        break;
1102
 
    case "svncut":
1103
 
        action_svncut(selected_files);
1104
 
        break;
1105
 
    case "svncleanup":
1106
 
        action_svncleanup(".");
1107
 
        break;
1108
 
    }
1109
 
}
1110
 
 
1111
 
/** User clicks "Run" button.
1112
 
 * Do an Ajax call and print the test output.
1113
 
 */
1114
 
function runfile(localpath)
1115
 
{
1116
 
    if (!maybe_save('The last saved version will be run.')) return false;
1117
 
 
1118
 
    /* Dump the entire file to the console */
1119
 
    var callback = function()
1120
 
    {
1121
 
        console_enter_line("execfile('" + localpath + "')", "block");
1122
 
    }
1123
 
    start_server(callback)
1124
 
    return;
1125
 
}
1126
 
 
1127
431
/** Called when the page loads initially.
1128
432
 */
1129
 
function browser_init()
 
433
window.onload = function()
1130
434
{
1131
435
    /* Navigate (internally) to the path in the URL bar.
1132
436
     * This causes the page to be populated with whatever is at that address,
1133
437
     * whether it be a directory or a file.
1134
438
     */
1135
 
    var path = get_path();
1136
 
    navigate(path);
1137
 
}
1138
 
 
1139
 
/** Gets the current path of the window */
1140
 
function get_path() {
1141
439
    var path = parse_url(window.location.href).path;
1142
440
    /* Strip out root_dir + "/files" from the front of the path */
1143
441
    var strip = make_path(this_app);
 
442
    var editmode = false;
1144
443
    if (path.substr(0, strip.length) == strip)
1145
444
        path = path.substr(strip.length+1);
1146
445
    else
1150
449
        if (path.substr(0, strip.length) == strip)
1151
450
        {
1152
451
            path = path.substr(strip.length+1);
 
452
            editmode = true;
1153
453
        }
1154
454
    }
1155
455
 
1160
460
        path = username;
1161
461
    }
1162
462
 
1163
 
    return path;
 
463
    navigate(path, editmode);
1164
464
}