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

209 by mattgiuca
browser.js: Split out dir-listing code into listing.js (it's going to get
1
/* IVLE - Informatics Virtual Learning Environment
2
 * Copyright (C) 2007-2008 The University of Melbourne
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 2 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
 *
18
 * Module: Listing (File Browser, client)
19
 * Author: Matt Giuca
20
 * Date: 13/1/2008
21
 *
22
 * Handles directory listings on the client side.
23
 */
24
25
/* DOM nodeType constants */
26
ELEMENT_NODE = 1;
27
28
/** Filenames of all files selected */
29
selected_files = [];
30
210 by mattgiuca
File browser: listing.js - The status bar is now updated whenever the
31
/** The listing object returned by the server as JSON */
32
file_listing = null;
33
209 by mattgiuca
browser.js: Split out dir-listing code into listing.js (it's going to get
34
/** Updates the side-panel and status bar to reflect the current set of
35
 * selected files. This is done by inspecting the states of the check boxes.
36
 * Also changes the styling to highlight selected files.
37
 */
38
function update_selection()
39
{
40
    /* First get a list of all files that are selected, and
41
     * reset the styling on each file's row. */
42
    var files_children = document.getElementById("files").childNodes;
43
    var tr;
44
    var checkbox;
45
    var row_toggle = 1;
46
    selected_files = [];  /* Clear global selected_files */
210 by mattgiuca
File browser: listing.js - The status bar is now updated whenever the
47
48
    var total_files = 0;
49
    var total_file_size = 0;    /* In bytes */
50
    var total_file_size_sel = 0;
51
209 by mattgiuca
browser.js: Split out dir-listing code into listing.js (it's going to get
52
    /* Children are trs */
210 by mattgiuca
File browser: listing.js - The status bar is now updated whenever the
53
    var i = 0;
54
    var file;
55
    if (file_listing == null) return;
56
    for (var filename in file_listing)
209 by mattgiuca
browser.js: Split out dir-listing code into listing.js (it's going to get
57
    {
210 by mattgiuca
File browser: listing.js - The status bar is now updated whenever the
58
        /* Count total files and size so we can write to the status bar later
59
         */
60
        file = file_listing[filename];
61
        total_files++;
62
        if ("size" in file)
63
            total_file_size += file.size;
64
209 by mattgiuca
browser.js: Split out dir-listing code into listing.js (it's going to get
65
        tr = files_children[i];
66
        checked = tr.firstChild.firstChild.checked;
67
        /* Set the class for every row based on both the checked state,
68
         * and whether it is odd or even */
69
        tr.setAttribute("class", "row" + row_toggle.toString() +
70
            (checked ? "sel" : ""))
71
        row_toggle = row_toggle == 1 ? 2 : 1;
72
        if (checked)
73
        {
74
            /* Add the filename (column 3) to the selected_files list */
75
            selected_files[selected_files.length] = filename;
210 by mattgiuca
File browser: listing.js - The status bar is now updated whenever the
76
            if ("size" in file)
77
                total_file_size_sel += file.size;
209 by mattgiuca
browser.js: Split out dir-listing code into listing.js (it's going to get
78
        }
210 by mattgiuca
File browser: listing.js - The status bar is now updated whenever the
79
        i++;
80
    }
81
82
    /* Write to the status bar */
83
    var statusbar = document.getElementById("statusbar");
84
    var statusmsg;
85
    var file_plural;
86
    if (selected_files.length > 0)
87
    {
88
        statusmsg = selected_files.length.toString() + " file"
89
            + (selected_files.length == 1 ? "" : "s") + " selected, "
90
            + nice_filesize(total_file_size_sel);
91
    }
92
    else
93
    {
94
        statusmsg = total_files.toString() + " file"
95
            + (total_files == 1 ? "" : "s") + ", "
96
            + nice_filesize(total_file_size);
97
    }
98
    dom_removechildren(statusbar);
99
    statusbar.appendChild(document.createTextNode(statusmsg));
209 by mattgiuca
browser.js: Split out dir-listing code into listing.js (it's going to get
100
}
101
102
/** Clears all selected files and causes the single file specified to become
103
 * selected.
104
 * \param fileid The index of the file in the list to select.
105
 */
106
function select_file(fileid)
107
{
108
    var files_children = document.getElementById("files").childNodes;
109
    var checkbox;
110
    for (var i=0; i<files_children.length; i++)
111
    {
112
        tr = files_children[i];
113
        checkbox = tr.firstChild.firstChild;
114
        checkbox.checked = i == fileid;
115
    }
116
    update_selection();
117
}
118
119
/** Initialises the DOM elements required to present a dir listing,
120
 * assuming that clear_page has just been called or the page just
121
 * loaded for the first time.
122
 */
123
function setup_for_dir_listing()
124
{
125
    var filesbody = document.getElementById("filesbody");
126
127
    /* Using a table-based layout, for reasons of sanity */
128
    /* One row, 2 columns */
129
    var middle = document.createElement("table");
130
    filesbody.appendChild(middle);
131
    middle.setAttribute("id", "middle");
132
    var middle_tbody = document.createElement("tbody");
133
    middle.appendChild(middle_tbody);
134
    var middle_tr = document.createElement("tr");
135
    middle_tbody.appendChild(middle_tr);
136
137
    /* Column 1: File table */
138
    var filetable = document.createElement("td");
139
    middle_tr.appendChild(filetable);
140
    filetable.setAttribute("id", "filetable");
141
    var filetablediv = document.createElement("div");
142
    filetable.appendChild(filetablediv);
143
    filetablediv.setAttribute("id", "filetablediv");
144
    /* A nested table within this div - the actual files listing */
145
    var filetabletable = document.createElement("table");
146
    filetablediv.appendChild(filetabletable);
147
    filetabletable.setAttribute("width", "100%");
148
    var filetablethead = document.createElement("thead");
149
    filetabletable.appendChild(filetablethead);
150
    var filetablethead_tr = document.createElement("tr");
151
    filetablethead.appendChild(filetablethead_tr);
152
    filetablethead_tr.setAttribute("class", "rowhead");
153
    /* Row headers */
154
    var filetablethead_th = document.createElement("th");
155
    filetablethead_tr.appendChild(filetablethead_th);
156
    filetablethead_th.setAttribute("class", "col-check");
157
    filetablethead_th = dom_make_link_elem("th", "Filename",
158
        "Sort by filename", "")
159
    filetablethead_tr.appendChild(filetablethead_th);
160
    filetablethead_th.setAttribute("class", "col-filename");
161
    filetablethead_th.setAttribute("colspan", 3);
162
    filetablethead_th = dom_make_link_elem("th", "Size",
163
        "Sort by file size", "")
164
    filetablethead_tr.appendChild(filetablethead_th);
165
    filetablethead_th.setAttribute("class", "col-size");
166
    filetablethead_th = dom_make_link_elem("th", "Modified",
167
        "Sort by date modified", "")
168
    filetablethead_tr.appendChild(filetablethead_th);
169
    filetablethead_th.setAttribute("class", "col-date");
170
    /* Empty body */
171
    var filetabletbody = document.createElement("tbody");
172
    filetabletable.appendChild(filetabletbody);
173
    filetabletbody.setAttribute("id", "files");
174
175
    /* Column 2: Side-panel */
176
    var sidepanel = document.createElement("td");
177
    middle_tr.appendChild(sidepanel);
178
    sidepanel.setAttribute("id", "sidepanel");
179
180
181
    /* Now after the table "middle", there is a status bar */
182
    var statusbar = document.createElement("div");
183
    filesbody.appendChild(statusbar);
184
    statusbar.setAttribute("id", "statusbar");
185
}
186
187
/** Presents the directory listing.
188
 */
189
function handle_dir_listing(path, listing)
190
{
191
    setmode(false);
192
    setup_for_dir_listing();
193
    var row_toggle = 1;
194
    var i;
195
    /* Nav through the top-level of the JSON to the actual listing object. */
196
    var listing = listing.listing;
210 by mattgiuca
File browser: listing.js - The status bar is now updated whenever the
197
    file_listing = listing;     /* Global */
209 by mattgiuca
browser.js: Split out dir-listing code into listing.js (it's going to get
198
199
    /* Get "." out, it's special */
200
    var thisdir = listing["."];
201
    delete listing["."];
202
    /* Is this dir under svn? */
203
    var under_subversion = "svnstatus" in thisdir;
204
205
    var files = document.getElementById("files");
206
    var file;
207
    var row;
208
    var td;
209
    var checkbox;
210
211
    var selection_string;
212
213
    /* Create all of the files */
214
    i = 0;
215
    for (var filename in listing)
216
    {
217
        selection_string = "select_file(" + i.toString() + ")";
218
        file = listing[filename];
219
        /* Make a 'tr' element */
220
        row = document.createElement("tr");
221
        /* Column 1: Selection checkbox */
222
        row.setAttribute("class", "row" + row_toggle.toString())
223
        row_toggle = row_toggle == 1 ? 2 : 1;
224
        td = document.createElement("td");
225
        checkbox = document.createElement("input");
226
        checkbox.setAttribute("type", "checkbox");
227
        checkbox.setAttribute("title", "Select this file");
228
        checkbox.setAttribute("onchange", "update_selection()");
229
        td.appendChild(checkbox);
230
        row.appendChild(td);
231
        if (file.isdir)
232
        {
233
            /* Column 2: Filetype and subversion icons. */
234
            td = document.createElement("td");
235
            td.setAttribute("class", "thincol");
236
            td.setAttribute("onclick", selection_string);
237
            td.appendChild(dom_make_img(mime_type_to_icon("text/directory"),
238
                22, 22, file.type));
239
            row.appendChild(td);
240
            td = document.createElement("td");
241
            td.setAttribute("class", "thincol");
242
            if (under_subversion)
243
                td.appendChild(dom_make_img(svnstatus_to_icon(file.svnstatus),
244
                    22, 22, file.svnstatus));
245
            row.appendChild(td);
246
            /* Column 3: Filename */
247
            td = dom_make_link_elem("td", filename,
248
                "Navigate to " + path_join(path, filename),
249
                make_path(path_join(this_app, path, filename)),
250
                "navigate(" + path_join(path, filename) + ")");
251
            td.setAttribute("onclick", selection_string);
252
            row.appendChild(td);
253
        }
254
        else
255
        {
256
            /* Column 2: Filetype and subversion icons. */
257
            td = document.createElement("td");
258
            td.setAttribute("class", "thincol");
259
            td.appendChild(dom_make_img(mime_type_to_icon(file.type),
260
                22, 22, file.type));
261
            row.appendChild(td);
262
            td = document.createElement("td");
263
            td.setAttribute("class", "thincol");
264
            if (under_subversion)
265
                td.appendChild(dom_make_img(svnstatus_to_icon(file.svnstatus),
266
                    22, 22, file.svnstatus));
267
            row.appendChild(td);
268
            /* Column 3: Filename */
269
            td = dom_make_text_elem("td", filename);
270
            td.setAttribute("onclick", selection_string);
271
            row.appendChild(td);
272
        }
273
        /* Column 4: Size */
274
        td = dom_make_text_elem("td", nice_filesize(file.size));
275
        td.setAttribute("onclick", selection_string);
276
        row.appendChild(td);
277
        /* Column 4: Date */
278
        td = dom_make_text_elem("td", file.mtime_short, file.mtime_nice);
279
        td.setAttribute("onclick", selection_string);
280
        row.appendChild(td);
281
        files.appendChild(row);
282
        i++;
283
    }
284
210 by mattgiuca
File browser: listing.js - The status bar is now updated whenever the
285
    /* Do a selection update (create initial elements for side panel and
286
     * status bar). */
287
    update_selection();
209 by mattgiuca
browser.js: Split out dir-listing code into listing.js (it's going to get
288
}
289