~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-07-21 02:21:56 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:915
Moved projectid from all tables using assessedid into the assessed table
itself.
    Rationale: "assessed" now describes the submitter of a project along with
    the project itself (as these two things are always used together).

Show diffs side-by-side

added added

removed removed

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