~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:33:26 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:194
Added svn:ignore to the new apps' directories.

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.
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
63
};
70
64
 
71
 
/* Mapping SVN status to "nice" strings */
72
 
svn_nice = {
73
 
    "unversioned": "Temporary file",
74
 
    "normal": "Permanent file",
75
 
    "added": "Temporary file (scheduled to be added)",
76
 
    "missing": "Permanent file (missing)",
77
 
    "deleted": "Permanent file (scheduled for deletion)",
78
 
    "replaced": "Permanent file (replaced)",
79
 
    "modified": "Permanent file (modified)",
80
 
    "merged": "Permanent file (merged)",
81
 
    "conflicted": "Permanent file (conflicted)",
82
 
};
83
 
 
84
 
default_svn_icon = null;
85
 
default_svn_nice = "Unknown status";
 
65
default_svn_icon = "normal.png";
86
66
 
87
67
svn_icons_path = "media/images/svn";
88
68
 
89
 
published_icon = "media/images/interface/published.png";
90
 
 
91
69
/* List of MIME types considered "executable" by the system.
92
70
 * Executable files offer a "run" link, implying that the "serve"
93
71
 * application can interpret them.
96
74
    "text/x-python",
97
75
];
98
76
 
99
 
 
100
 
/* Global variables */
101
 
 
102
 
current_path = "";
103
 
 
104
77
/** Calls the server using Ajax, performing an action on the server side.
105
78
 * Receives the response from the server and performs a refresh of the page
106
79
 * contents, updating it to display the returned data (such as a directory
120
93
 *      Defaults to "application/x-www-form-urlencoded".
121
94
 *      "multipart/form-data" is recommended for large uploads.
122
95
 */
123
 
function do_action(action, path, args, content_type, ignore_response)
 
96
function do_action(action, path, args, content_type)
124
97
{
125
98
    args.action = action;
126
99
    /* Call the server and perform the action. This mutates the server. */
131
104
    if (error != null)
132
105
        alert("Error: " + error.toString() + ".");
133
106
    /* Now read the response and set up the page accordingly */
134
 
    if (ignore_response != true)
135
 
        handle_response(path, response);
 
107
    handle_response(path, response);
136
108
}
137
109
 
138
110
/** Calls the server using Ajax, requesting a directory listing. This should
142
114
 * Called "navigate", can also be used for a simple refresh.
143
115
 * Always makes a GET request.
144
116
 * No return value.
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
117
 */
148
 
function navigate(path, editmode)
 
118
function navigate(path)
149
119
{
150
120
    /* Call the server and request the listing. This mutates the server. */
151
121
    response = ajax_call(service_app, path, null, "GET");
152
122
    /* Now read the response and set up the page accordingly */
153
 
    handle_response(path, response, editmode);
154
 
}
155
 
 
156
 
/** Determines the "handler type" from a MIME type.
157
 
 * The handler type is a string, either "text", "image", "audio" or "binary".
158
 
 */
159
 
function get_handler_type(content_type)
160
 
{
161
 
    if (!content_type)
162
 
        return null;
163
 
    if (content_type in type_handlers)
164
 
        return type_handlers[content_type];
165
 
    else
166
 
    {   /* Based on the first part of the MIME type */
167
 
        var handler_type = content_type.split('/')[0];
168
 
        if (handler_type != "text" && handler_type != "image" &&
169
 
            handler_type != "audio")
170
 
            handler_type = "binary";
171
 
        return handler_type;
172
 
    }
 
123
    handle_response(path, response);
173
124
}
174
125
 
175
126
/** Given an HTTP response object, cleans up and rebuilds the contents of the
182
133
 * things) be used to update the URL in the location bar.
183
134
 * \param response XMLHttpRequest object returned by the server. Should
184
135
 * contain all the response data.
185
 
 * \param editmode Optional boolean. If true, then the user navigated here
186
 
 * with an "edit" URL so we should favour using the editor.
187
136
 */
188
 
function handle_response(path, response, editmode)
 
137
function handle_response(path, response)
189
138
{
190
139
    /* TODO: Set location bar to "path" */
191
 
    current_path = path;
192
140
 
193
141
    /* Clear away the existing page contents */
194
142
    clearpage();
207
155
    }
208
156
 
209
157
    /* Check if this is a directory listing or file contents */
210
 
    var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
211
 
    if (!editmode && isdir)
 
158
    if (response.getResponseHeader("X-IVLE-Return") == "Dir")
212
159
    {
213
160
        var listing = response.responseText;
214
161
        /* The listing SHOULD be valid JSON text. Parse it into an object. */
227
174
    {
228
175
        /* Treat this as an ordinary file. Get the file type. */
229
176
        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";
 
177
        var handler_type;
 
178
        if (content_type in type_handlers)
 
179
            handler_type = type_handlers[content_type];
 
180
        else
 
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
        }
234
187
        /* handler_type should now be set to either
235
188
         * "text", "image", "audio" or "binary". */
236
189
        switch (handler_type)
237
190
        {
238
191
        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
 
            }
 
192
            handle_text(path, response.responseText);
249
193
            break;
250
194
        case "image":
251
195
            /* TODO: Custom image handler */
268
212
 */
269
213
function clearpage()
270
214
{
271
 
    dom_removechildren(document.getElementById("path"));
272
 
    dom_removechildren(document.getElementById("filesbody"));
273
 
}
274
 
 
275
 
/** Deletes all "dynamic" content on the page necessary to navigate from
276
 
 * one directory listing to another (does not clear as much as clearpage
277
 
 * does).
278
 
 * This is the equivalent of calling clearpage() then
279
 
 * setup_for_dir_listing(), assuming the page is already on a dir listing.
280
 
 */
281
 
function clearpage_dir()
282
 
{
 
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. */
283
220
    dom_removechildren(document.getElementById("path"));
284
221
    dom_removechildren(document.getElementById("files"));
285
222
    dom_removechildren(document.getElementById("sidepanel"));
286
223
}
287
224
 
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
 
 
326
225
/*** HANDLERS for different types of responses (such as dir listing, file,
327
226
 * etc). */
328
227
 
329
228
function handle_error(message)
330
229
{
331
 
    setmode(false);
332
 
    var files = document.getElementById("filesbody");
333
 
    var txt_elem = dom_make_text_elem("div", "Error: "
 
230
    /* TODO: Find a better place to put this message. */
 
231
    var files = document.getElementById("files");
 
232
    var tr = document.createElement("tr");
 
233
    var td = document.createElement("td");
 
234
    tr.appendChild(td);
 
235
    var td = document.createElement("td");
 
236
    tr.appendChild(td);
 
237
    var td = document.createElement("td");
 
238
    tr.appendChild(td);
 
239
    var txt_elem = dom_make_text_elem("td", "Error: "
334
240
        + message.toString() + ".")
335
 
    txt_elem.setAttribute("class", "padding error");
336
 
    files.appendChild(txt_elem);
 
241
    txt_elem.setAttribute("class", "error");
 
242
    tr.appendChild(txt_elem);
 
243
    var td = document.createElement("td");
 
244
    tr.appendChild(td);
 
245
    var td = document.createElement("td");
 
246
    tr.appendChild(td);
 
247
    files.appendChild(tr);
337
248
}
338
249
 
339
250
/** Presents a path list (address bar inside the page) for clicking.
341
252
function presentpath(path)
342
253
{
343
254
    var dom_path = document.getElementById("path");
 
255
    var navlist = [];
344
256
    var href_path = make_path(this_app);
345
 
    var nav_path = "";
346
 
    var dir;
347
257
 
348
 
    /* Also set the document title */
349
 
    document.title = path_basename(path) + " - IVLE";
350
258
    /* Create all of the paths */
351
 
    var pathlist = path.split("/");
352
 
    for (var i=0; i<pathlist.length; i++)
 
259
    for each (dir in path.split("/"))
353
260
    {
354
 
        dir = pathlist[i];
355
261
        if (dir == "") continue;
 
262
        navlist.push(dir);
356
263
        /* Make an 'a' element */
357
264
        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) + ")"*/);
 
265
        var link = dom_make_link_elem("a", dir, href_path,
 
266
                "navigate(" + href_path + ")");
361
267
        dom_path.appendChild(link);
362
268
        dom_path.appendChild(document.createTextNode("/"));
363
269
    }
364
 
    dom_path.removeChild(dom_path.lastChild);
365
270
}
366
271
 
367
272
/** Given a mime type, returns the path to the icon.
386
291
/** Given an svnstatus, returns the path to the icon.
387
292
 * \param type String, svn status.
388
293
 * \return Path to the icon. Has applied make_path, so it is relative to site
389
 
 * root. May return null to indicate no SVN icon.
 
294
 * root.
390
295
 */
391
296
function svnstatus_to_icon(svnstatus)
392
297
{
395
300
        filename = svn_icons[svnstatus];
396
301
    else
397
302
        filename = default_svn_icon;
398
 
    if (filename == null) return null;
399
303
    return make_path(path_join(svn_icons_path, filename));
400
304
}
401
305
 
402
 
/** Given an svnstatus, returns the "nice" string.
403
 
 */
404
 
function svnstatus_to_string(svnstatus)
405
 
{
406
 
    if (svnstatus in svn_nice)
407
 
        return svn_nice[svnstatus];
408
 
    else
409
 
        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);
410
392
}
411
393
 
412
394
/** Displays a download link to the binary file.
413
395
 */
414
396
function handle_binary(path)
415
397
{
416
 
    setmode(false);
417
 
    var files = document.getElementById("filesbody");
418
 
    var div = document.createElement("div");
419
 
    files.appendChild(div);
420
 
    div.setAttribute("class", "padding");
421
 
    var download_link = app_path(download_app, path);
422
 
    var par1 = dom_make_text_elem("p",
423
 
        "The file " + path + " is a binary file. To download this file, " +
424
 
        "click the following link:");
425
 
    var par2 = dom_make_link_elem("p",
426
 
        "Download " + path, "Download " + path, download_link);
427
 
    div.appendChild(par1);
428
 
    div.appendChild(par2);
 
398
    /* TODO */
 
399
    alert(path);
429
400
}
430
401
 
431
402
/** Called when the page loads initially.
438
409
     */
439
410
    var path = parse_url(window.location.href).path;
440
411
    /* Strip out root_dir + "/files" from the front of the path */
441
 
    var strip = make_path(this_app);
442
 
    var editmode = false;
443
 
    if (path.substr(0, strip.length) == strip)
444
 
        path = path.substr(strip.length+1);
445
 
    else
446
 
    {
447
 
        /* See if this is an edit path */
448
 
        strip = make_path(edit_app);
449
 
        if (path.substr(0, strip.length) == strip)
450
 
        {
451
 
            path = path.substr(strip.length+1);
452
 
            editmode = true;
453
 
        }
454
 
    }
455
 
 
456
 
    if (path.length == 0)
457
 
    {
458
 
        /* Navigate to the user's home directory by default */
459
 
        /* TEMP? */
460
 
        path = username;
461
 
    }
462
 
 
463
 
    navigate(path, editmode);
 
412
    strip_chars = make_path(this_app).length + 1;
 
413
    path = path.substr(strip_chars);
 
414
 
 
415
    navigate(path);
464
416
}