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

« back to all changes in this revision

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

  • Committer: stevenbird
  • Date: 2008-02-01 03:51:56 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:368
First version of a DTD for XML problem files

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";
25
26
service_app = "fileservice";
 
27
serve_app = "serve";
 
28
download_app = "download";
26
29
 
27
30
/* Mapping MIME types onto handlers.
28
31
 * "text" : When navigating to a text file, the text editor is opened.
43
46
    "application/xml" : "text",
44
47
};
45
48
 
 
49
/* Mapping MIME types to icons, just the file's basename */
 
50
type_icons = {
 
51
    "text/directory": "dir.png",
 
52
    "text/x-python": "py.png",
 
53
};
 
54
 
 
55
default_type_icon = "txt.png";
 
56
 
 
57
/* Relative to IVLE root */
 
58
type_icons_path = "media/images/mime";
 
59
type_icons_path_large = "media/images/mime/large";
 
60
 
 
61
/* Mapping SVN status to icons, just the file's basename */
 
62
svn_icons = {
 
63
    "unversioned": null,
 
64
    "normal": "normal.png",
 
65
    "added": "added.png",
 
66
    "missing": "missing.png",
 
67
    "deleted": "deleted.png",
 
68
    "modified": "modified.png",
 
69
};
 
70
 
 
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";
 
86
 
 
87
svn_icons_path = "media/images/svn";
 
88
 
 
89
published_icon = "media/images/interface/published.png";
 
90
 
46
91
/* List of MIME types considered "executable" by the system.
47
92
 * Executable files offer a "run" link, implying that the "serve"
48
93
 * application can interpret them.
51
96
    "text/x-python",
52
97
];
53
98
 
 
99
 
 
100
/* Global variables */
 
101
 
 
102
current_path = "";
 
103
 
54
104
/** Calls the server using Ajax, performing an action on the server side.
55
105
 * Receives the response from the server and performs a refresh of the page
56
106
 * contents, updating it to display the returned data (such as a directory
91
141
 * Called "navigate", can also be used for a simple refresh.
92
142
 * Always makes a GET request.
93
143
 * 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.
94
146
 */
95
 
function navigate(path)
 
147
function navigate(path, editmode)
96
148
{
97
149
    /* Call the server and request the listing. This mutates the server. */
98
150
    response = ajax_call(service_app, path, null, "GET");
99
151
    /* Now read the response and set up the page accordingly */
100
 
    handle_response(path, response);
 
152
    handle_response(path, response, editmode);
 
153
}
 
154
 
 
155
/** Determines the "handler type" from a MIME type.
 
156
 * The handler type is a string, either "text", "image", "audio" or "binary".
 
157
 */
 
158
function get_handler_type(content_type)
 
159
{
 
160
    if (!content_type)
 
161
        return null;
 
162
    if (content_type in type_handlers)
 
163
        return type_handlers[content_type];
 
164
    else
 
165
    {   /* Based on the first part of the MIME type */
 
166
        var handler_type = content_type.split('/')[0];
 
167
        if (handler_type != "text" && handler_type != "image" &&
 
168
            handler_type != "audio")
 
169
            handler_type = "binary";
 
170
        return handler_type;
 
171
    }
101
172
}
102
173
 
103
174
/** Given an HTTP response object, cleans up and rebuilds the contents of the
110
181
 * things) be used to update the URL in the location bar.
111
182
 * \param response XMLHttpRequest object returned by the server. Should
112
183
 * 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.
113
186
 */
114
 
function handle_response(path, response)
 
187
function handle_response(path, response, editmode)
115
188
{
116
189
    /* TODO: Set location bar to "path" */
 
190
    current_path = path;
 
191
 
 
192
    /* Clear away the existing page contents */
 
193
    clearpage();
 
194
    /* Display the path at the top, for navigation */
 
195
    presentpath(path);
 
196
 
117
197
    /* Check the status, and if not 200, read the error and handle this as an
118
198
     * error. */
119
199
    if (response.status != 200)
126
206
    }
127
207
 
128
208
    /* Check if this is a directory listing or file contents */
129
 
    if (response.getResponseHeader("X-IVLE-Return") == "Dir")
 
209
    var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
 
210
    if (!editmode && isdir)
130
211
    {
131
212
        var listing = response.responseText;
132
213
        /* The listing SHOULD be valid JSON text. Parse it into an object. */
145
226
    {
146
227
        /* Treat this as an ordinary file. Get the file type. */
147
228
        var content_type = response.getResponseHeader("Content-Type");
148
 
        var handler_type;
149
 
        if (content_type in type_handlers)
150
 
            handler_type = type_handlers[content_type];
151
 
        else
152
 
        {   /* Based on the first part of the MIME type */
153
 
            handler_type = content_type.split('/')[0];
154
 
            if (handler_type != "text" && handler_type != "image" &&
155
 
                handler_type != "audio")
156
 
                handler_type = "binary";
157
 
        }
 
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";
158
233
        /* handler_type should now be set to either
159
234
         * "text", "image", "audio" or "binary". */
160
235
        switch (handler_type)
161
236
        {
162
237
        case "text":
163
 
            handle_text(path, response.responseText);
 
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
            }
164
248
            break;
165
249
        case "image":
166
250
            /* TODO: Custom image handler */
183
267
 */
184
268
function clearpage()
185
269
{
186
 
    /* Note: For now clear just enough to repopulate with a dir listing.
187
 
     * Later, will have to clear more to make way for other handlers.
188
 
     * Possibly have a "full clear" for all handlers, and special
189
 
     * less-violent clearers for each handler if the same handler is going to
190
 
     * be used that was used last time. */
 
270
    dom_removechildren(document.getElementById("path"));
 
271
    dom_removechildren(document.getElementById("filesbody"));
 
272
}
 
273
 
 
274
/** Deletes all "dynamic" content on the page necessary to navigate from
 
275
 * one directory listing to another (does not clear as much as clearpage
 
276
 * does).
 
277
 * This is the equivalent of calling clearpage() then
 
278
 * setup_for_dir_listing(), assuming the page is already on a dir listing.
 
279
 */
 
280
function clearpage_dir()
 
281
{
191
282
    dom_removechildren(document.getElementById("path"));
192
283
    dom_removechildren(document.getElementById("files"));
193
284
    dom_removechildren(document.getElementById("sidepanel"));
194
285
}
195
286
 
 
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
 
196
325
/*** HANDLERS for different types of responses (such as dir listing, file,
197
326
 * etc). */
198
327
 
199
328
function handle_error(message)
200
329
{
201
 
    /* TODO: Find a better place to put this message. */
202
 
    var msg_elem = document.getElementById("path");
203
 
    var txt_elem = dom_make_text_elem("span", "Error: "
 
330
    setmode(false);
 
331
    var files = document.getElementById("filesbody");
 
332
    var txt_elem = dom_make_text_elem("div", "Error: "
204
333
        + message.toString() + ".")
205
 
    txt_elem.setAttribute("class", "error");
206
 
    msg_elem.appendChild(txt_elem)
 
334
    txt_elem.setAttribute("class", "padding error");
 
335
    files.appendChild(txt_elem);
207
336
}
208
337
 
209
338
/** Presents a path list (address bar inside the page) for clicking.
210
 
 * (Dir listing).
211
339
 */
212
340
function presentpath(path)
213
341
{
214
342
    var dom_path = document.getElementById("path");
215
 
    var navlist = [];
216
343
    var href_path = make_path(this_app);
 
344
    var nav_path = "";
 
345
    var dir;
217
346
 
 
347
    /* Also set the document title */
 
348
    document.title = path_basename(path) + " - IVLE";
218
349
    /* Create all of the paths */
219
 
    for each (dir in path.split("/"))
 
350
    var pathlist = path.split("/");
 
351
    for (var i=0; i<pathlist.length; i++)
220
352
    {
 
353
        dir = pathlist[i];
221
354
        if (dir == "") continue;
222
 
        navlist.push(dir);
223
355
        /* Make an 'a' element */
224
356
        href_path = path_join(href_path, dir);
225
 
        var link = dom_make_link_elem("a", dir, href_path,
226
 
                "navigate(" + href_path + ")");
 
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) + ")"*/);
227
360
        dom_path.appendChild(link);
228
361
        dom_path.appendChild(document.createTextNode("/"));
229
362
    }
230
 
}
231
 
 
232
 
/** Presents the directory listing.
233
 
 */
234
 
function handle_dir_listing(path, listing)
235
 
{
236
 
    var row_toggle = 1;
237
 
    presentpath(path);
238
 
    /* Nav through the top-level of the JSON to the actual listing object. */
239
 
    var listing = listing.listing;
240
 
 
241
 
    /* Get "." out, it's special */
242
 
    var thisdir = listing["."];
243
 
    delete listing["."];
244
 
    /* Is this dir under svn? */
245
 
    var under_subversion = "svnstatus" in thisdir;
246
 
 
247
 
    var files = document.getElementById("files");
248
 
    var file;
249
 
    var row;
250
 
    var td;
251
 
    var checkbox;
252
 
 
253
 
    /* Create all of the files */
254
 
    for (var filename in listing)
255
 
    {
256
 
        file = listing[filename];
257
 
        /* Make a 'tr' element */
258
 
        row = document.createElement("tr");
259
 
        /* Column 1: Selection checkbox */
260
 
        row.setAttribute("class", "row" + row_toggle.toString())
261
 
        row_toggle = row_toggle == 1 ? 2 : 1;
262
 
        td = document.createElement("td");
263
 
        checkbox = document.createElement("input");
264
 
        checkbox.setAttribute("type", "checkbox");
265
 
        checkbox.setAttribute("title", "Select this file");
266
 
        td.appendChild(checkbox);
267
 
        row.appendChild(td);
268
 
        if (file.isdir)
269
 
        {
270
 
            /* Column 2: Filetype and subversion icons. */
271
 
            td = document.createElement("td");
272
 
            td.setAttribute("class", "thincol");
273
 
            td.appendChild(document.createTextNode("dir"));
274
 
            row.appendChild(td);
275
 
            td = document.createElement("td");
276
 
            td.setAttribute("class", "thincol");
277
 
            if (under_subversion)
278
 
                td.appendChild(document.createTextNode(file.svnstatus));
279
 
            row.appendChild(td);
280
 
            /* Column 3: Filename */
281
 
            row.appendChild(dom_make_link_elem("td", filename,
282
 
                make_path(path_join(this_app, path, filename)),
283
 
                "navigate(" + path_join(path, filename) + ")"));
284
 
        }
285
 
        else
286
 
        {
287
 
            /* Column 2: Filetype and subversion icons. */
288
 
            td = document.createElement("td");
289
 
            td.setAttribute("class", "thincol");
290
 
            td.appendChild(document.createTextNode(file.type));
291
 
            row.appendChild(td);
292
 
            td = document.createElement("td");
293
 
            td.setAttribute("class", "thincol");
294
 
            if (under_subversion)
295
 
                td.appendChild(document.createTextNode(file.svnstatus));
296
 
            row.appendChild(td);
297
 
            /* Column 3: Filename */
298
 
            row.appendChild(dom_make_text_elem("td", filename));
299
 
        }
300
 
        /* Column 4: Size */
301
 
        row.appendChild(dom_make_text_elem("td", nice_filesize(file.size)));
302
 
        /* Column 4: Date */
303
 
        row.appendChild(dom_make_text_elem("td", file.mtime_nice));
304
 
        files.appendChild(row);
305
 
    }
306
 
 
307
 
}
308
 
 
309
 
/** Presents the text editor.
310
 
 */
311
 
function handle_text(path, text)
312
 
{
313
 
    /* TODO */
314
 
    alert(text);
 
363
    dom_path.removeChild(dom_path.lastChild);
 
364
}
 
365
 
 
366
/** Given a mime type, returns the path to the icon.
 
367
 * \param type String, Mime type.
 
368
 * \param sizelarge Boolean, optional.
 
369
 * \return Path to the icon. Has applied make_path, so it is relative to site
 
370
 * root.
 
371
 */
 
372
function mime_type_to_icon(type, sizelarge)
 
373
{
 
374
    var filename;
 
375
    if (type in type_icons)
 
376
        filename = type_icons[type];
 
377
    else
 
378
        filename = default_type_icon;
 
379
    if (sizelarge)
 
380
        return make_path(path_join(type_icons_path_large, filename));
 
381
    else
 
382
        return make_path(path_join(type_icons_path, filename));
 
383
}
 
384
 
 
385
/** Given an svnstatus, returns the path to the icon.
 
386
 * \param type String, svn status.
 
387
 * \return Path to the icon. Has applied make_path, so it is relative to site
 
388
 * root. May return null to indicate no SVN icon.
 
389
 */
 
390
function svnstatus_to_icon(svnstatus)
 
391
{
 
392
    var filename;
 
393
    if (svnstatus in svn_icons)
 
394
        filename = svn_icons[svnstatus];
 
395
    else
 
396
        filename = default_svn_icon;
 
397
    if (filename == null) return null;
 
398
    return make_path(path_join(svn_icons_path, filename));
 
399
}
 
400
 
 
401
/** Given an svnstatus, returns the "nice" string.
 
402
 */
 
403
function svnstatus_to_string(svnstatus)
 
404
{
 
405
    if (svnstatus in svn_nice)
 
406
        return svn_nice[svnstatus];
 
407
    else
 
408
        return default_svn_nice;
315
409
}
316
410
 
317
411
/** Displays a download link to the binary file.
318
412
 */
319
413
function handle_binary(path)
320
414
{
321
 
    /* TODO */
322
 
    alert(path);
 
415
    setmode(false);
 
416
    var files = document.getElementById("filesbody");
 
417
    var div = document.createElement("div");
 
418
    files.appendChild(div);
 
419
    div.setAttribute("class", "padding");
 
420
    var download_link = app_path(download_app, path);
 
421
    var par1 = dom_make_text_elem("p",
 
422
        "The file " + path + " is a binary file. To download this file, " +
 
423
        "click the following link:");
 
424
    var par2 = dom_make_link_elem("p",
 
425
        "Download " + path, "Download " + path, download_link);
 
426
    div.appendChild(par1);
 
427
    div.appendChild(par2);
323
428
}
324
429
 
325
430
/** Called when the page loads initially.
332
437
     */
333
438
    var path = parse_url(window.location.href).path;
334
439
    /* Strip out root_dir + "/files" from the front of the path */
335
 
    strip_chars = make_path(this_app).length + 1;
336
 
    path = path.substr(strip_chars);
337
 
 
338
 
    navigate(path);
 
440
    var strip = make_path(this_app);
 
441
    var editmode = false;
 
442
    if (path.substr(0, strip.length) == strip)
 
443
        path = path.substr(strip.length+1);
 
444
    else
 
445
    {
 
446
        /* See if this is an edit path */
 
447
        strip = make_path(edit_app);
 
448
        if (path.substr(0, strip.length) == strip)
 
449
        {
 
450
            path = path.substr(strip.length+1);
 
451
            editmode = true;
 
452
        }
 
453
    }
 
454
 
 
455
    if (path.length == 0)
 
456
    {
 
457
        /* Navigate to the user's home directory by default */
 
458
        /* TEMP? */
 
459
        path = username;
 
460
    }
 
461
 
 
462
    navigate(path, editmode);
339
463
}