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

944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
1
/* IVLE
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
19
/* Module: SpecialHome (File Browser, client)
20
 * Author: David Coles
21
 * Date: 23/7/2008
22
 */
23
24
/* This module implements the functionality to lay out a special home directory 
25
 * view on top of the normal listing
26
 */
27
28
/* The name of the personal file directory */
945 by wagrant
specialhome: Rebrand personal to mywork (by majority), refactor a bit,
29
PERSONALDIR="mywork"
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
30
31
/* LAYOUT FUNCTIONS */
32
1670 by Matt Giuca
specialhome.js: Refactor so home_listing (now special_home_listing) is ONLY called if the special home is used, rather than all the time. browser.js: Now calls special_home_listing conditionally.
33
/** Present special home directory. This is only called if in the top level.
34
 * Subjects must not be null.
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
35
 */
1670 by Matt Giuca
specialhome.js: Refactor so home_listing (now special_home_listing) is ONLY called if the special home is used, rather than all the time. browser.js: Now calls special_home_listing conditionally.
36
function special_home_listing(listing, subjects, path)
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
37
{
980 by dcoles
Files: "Junk" is now only shown when other (non-subject/stuff) files are in the
38
    /* Nav through the top-level of the JSON to the actual listing object. */
39
    var listing = listing.listing;
40
    
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
41
    var filetablediv = document.getElementById("filetablediv");
955 by mattgiuca
browser: Restyled "special home" page. Now much more in-line with the existing
42
    var specialhomediv;
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
43
    var h2;
955 by mattgiuca
browser: Restyled "special home" page. Now much more in-line with the existing
44
    var h3;
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
45
    var ul;
46
    var li;
980 by dcoles
Files: "Junk" is now only shown when other (non-subject/stuff) files are in the
47
    var div;
48
1670 by Matt Giuca
specialhome.js: Refactor so home_listing (now special_home_listing) is ONLY called if the special home is used, rather than all the time. browser.js: Now calls special_home_listing conditionally.
49
    /* Wrap all this "special" stuff in a div, for styling purposes */
50
    specialhomediv = document.createElement("div");
51
    specialhomediv.setAttribute("id", "specialhome");
52
    filetablediv.appendChild(specialhomediv);
53
54
    /* SUBJECTS Section
55
    /* Create the header row */
56
    if (subjects.length > 0)
57
    {
58
        h2 = dom_make_text_elem("h2", "Subjects");
59
        specialhomediv.appendChild(h2);
60
    }
61
62
    /* Create the contents */
63
    for (var i=0; i<subjects.length; i++)
64
    {
65
        var subject = subjects[i];
66
        var subjpath = subject.subj_short_name;
67
        // Header, with link to offering home page.
1686 by Matt Giuca
Specialhome now shows the semester name for any non-current offerings. userservice/get_enrolments now returns the 'state' property of an offering (past, current, future, etc). This fixes Launchpad bug #526838.
68
        h3 = $('<h3><span class="subjname"></span><span style="font-weight: normal"><span class="semester"></span> &ndash; <a class="subjectaction">Subject home</a></span>');
69
        h3.find('.subjname').text(subject.subj_name);
70
        /* Non-current offerings need to show the semester, to avoid confusion
71
         * about which offering we are talking about */
72
        if (subject.state != "current")
73
        {
74
            h3.find('.semester').text(" (" + subject.year + ", semester "
75
                                      + subject.semester + ")");
76
        }
1670 by Matt Giuca
specialhome.js: Refactor so home_listing (now special_home_listing) is ONLY called if the special home is used, rather than all the time. browser.js: Now calls special_home_listing conditionally.
77
        h3.find('a').attr('href', subject.url);
78
        $(specialhomediv).append(h3);
79
    
80
        /* Print the file listing */
81
        ul = document.createElement("ul");
82
        // Stuff
83
        ul.appendChild(make_subject_item(subjpath,
84
            path_join("users", username, subjpath), PERSONALDIR,
85
            "Your own files in this subject"));
86
87
        // Groups
88
        var groups = subject.groups;
89
        for (var j=0; j<subject.groups.length; j++)
980 by dcoles
Files: "Junk" is now only shown when other (non-subject/stuff) files are in the
90
        {
1670 by Matt Giuca
specialhome.js: Refactor so home_listing (now special_home_listing) is ONLY called if the special home is used, rather than all the time. browser.js: Now calls special_home_listing conditionally.
91
            var group = subject.groups[j];
92
            ul.appendChild(make_subject_item(subjpath,
93
                path_join("groups", subject.subj_short_name + "_" +
94
                          subject.year + "_" + subject.semester + "_" +
95
                          group.name),
96
                group.name,
97
                "This group's files in this subject"));
980 by dcoles
Files: "Junk" is now only shown when other (non-subject/stuff) files are in the
98
        }
99
        
955 by mattgiuca
browser: Restyled "special home" page. Now much more in-line with the existing
100
        specialhomediv.appendChild(ul);
980 by dcoles
Files: "Junk" is now only shown when other (non-subject/stuff) files are in the
101
1670 by Matt Giuca
specialhome.js: Refactor so home_listing (now special_home_listing) is ONLY called if the special home is used, rather than all the time. browser.js: Now calls special_home_listing conditionally.
102
        /* Remove it from listing */
103
        if (subject.subj_short_name in listing)
104
            delete listing[subject.subj_short_name];
980 by dcoles
Files: "Junk" is now only shown when other (non-subject/stuff) files are in the
105
    }
1670 by Matt Giuca
specialhome.js: Refactor so home_listing (now special_home_listing) is ONLY called if the special home is used, rather than all the time. browser.js: Now calls special_home_listing conditionally.
106
107
    /* FIXME: Old Subjects? */
108
109
    /* STUFF Section -- For the stuff directory */
110
    /* Create the header */
111
    h2 = dom_make_text_elem("h2", "Stuff");
112
    specialhomediv.appendChild(h2);
113
    /* Create the contents */
114
    ul = document.createElement("ul");
115
    ul.appendChild(make_subject_item("",
116
          path_join("users", username, "stuff"), "stuff",
117
          "Your own files not related to a subject"));
118
    specialhomediv.appendChild(ul);
119
    /* Remove stuff from the listing */
120
    if ("stuff" in listing)
121
        delete listing["stuff"];
122
123
    /* JUNK Section -- All the rest */
124
    /* Create the header row */
125
    if (obj_length(listing) > 0)
980 by dcoles
Files: "Junk" is now only shown when other (non-subject/stuff) files are in the
126
    {
1670 by Matt Giuca
specialhome.js: Refactor so home_listing (now special_home_listing) is ONLY called if the special home is used, rather than all the time. browser.js: Now calls special_home_listing conditionally.
127
        h2 = dom_make_text_elem("h2", "Junk");
128
        specialhomediv.appendChild(h2);
980 by dcoles
Files: "Junk" is now only shown when other (non-subject/stuff) files are in the
129
        handle_dir_listing(path, listing);
130
    }
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
131
}
132
133
/* Does an series of AJAX requests to find out the properties of this folder 
134
 * and then updates the folder view
135
 */
997 by wagrant
specialhome: Support group workspaces! Yay! Involved a bit of poking to
136
function make_subject_item(path, repopath, name, description)
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
137
{
138
    // Create the temporary item
139
    var li = document.createElement("li");
140
    var span;
141
    li.setAttribute("class", "listing-loading");
951 by wagrant
specialhome: Make nonexistent directories a little nicer, with titles
142
    li.appendChild(dom_make_text_elem("span", name, description));
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
143
    span = dom_make_text_elem("loading...");
144
    span.setAttribute("class","status");
145
    li.appendChild(span);
146
147
    // Set the callback function to update when we get the real details
148
    var callback = function(response)
149
    {
150
        var sublisting = decode_response(response);
151
        dom_removechildren(li);
152
        li.setAttribute("class", "listing-dir");
153
154
        // If we could find the file status...
155
        if (sublisting != null)
156
        {
157
            var thisdir = sublisting.listing['.'];
158
            var versioned = ("svnstatus" in thisdir) && (thisdir.svnstatus != "unversioned");
159
            if (versioned)
160
            {
161
                // Default: Just offer a link to the directory
162
                li.appendChild(dom_make_link_elem("span", name, description,
163
                    app_path(this_app, username, path, name)));
164
            }
165
            else
166
            {
167
                // Blocked: Offer to rename the directory
952 by wagrant
specialhome: Also be nice if a directory is blocked.
168
                li.appendChild(dom_make_text_elem("span", name, description));
169
                span = dom_make_text_elem("span", " (blocked) ",
170
                      "Another file or directory is in the way of this directory.");
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
171
                span.setAttribute("class", "status");
172
                li.appendChild(span);
173
174
                var button = document.createElement("input");
1166 by William Grant
Replace all non-external addEventListeners with equivalent jQuery calls.
175
                $(button).click(function(event)
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
176
                {
177
                    action_rename(path_join(path, name));
1166 by William Grant
Replace all non-external addEventListeners with equivalent jQuery calls.
178
                });
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
179
                button.setAttribute("type", "button");
180
                button.setAttribute("value", "Rename");
181
                span.appendChild(button);
182
            }
183
        }
1626 by Matt Giuca
specialhome.js: Previously assumed 'missing' if a directory listing couldn't be retrieved. Now checks for 404 status. Any other status, prints 'error -- contact system administrator', because clicking Checkout won't help you.
184
        else if (response.status == 404)
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
185
        {
186
            // Missing: Try to check out or create the repository
951 by wagrant
specialhome: Make nonexistent directories a little nicer, with titles
187
            li.appendChild(dom_make_text_elem("span", name, description));
188
            span = dom_make_text_elem("span", " (missing) ",
189
                  "This directory does not yet exist.");
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
190
            span.setAttribute("class", "status");
191
            li.appendChild(span);
192
193
            var button = document.createElement("input");
1166 by William Grant
Replace all non-external addEventListeners with equivalent jQuery calls.
194
            $(button).click(function(event)
946 by wagrant
specialhome: Work with per-subject 'mywork' directories, although it
195
            {
947 by wagrant
specialhome: Display the classic AJAX loading animation while
196
                li.setAttribute("class", "listing-loading");
197
946 by wagrant
specialhome: Work with per-subject 'mywork' directories, although it
198
                var localpath = path_join(path, name);
947 by wagrant
specialhome: Display the classic AJAX loading animation while
199
946 by wagrant
specialhome: Work with per-subject 'mywork' directories, although it
200
                if (create_if_needed(repopath))
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
201
                {
202
                    // Try a checkout
1326.1.2 by David Coles
Small refactor of browser's JavaScript to allow do_action to be called from
203
                    do_act("svncheckout", {"path": [repopath, localpath]});
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
204
                }
947 by wagrant
specialhome: Display the classic AJAX loading animation while
205
                else
206
                {
207
                    li.setAttribute("class", "listing-dir");
208
                }
1166 by William Grant
Replace all non-external addEventListeners with equivalent jQuery calls.
209
            });
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
210
            button.setAttribute("type", "button");
211
            button.setAttribute("value", "Checkout");
212
            span.appendChild(button);
213
        }
1626 by Matt Giuca
specialhome.js: Previously assumed 'missing' if a directory listing couldn't be retrieved. Now checks for 404 status. Any other status, prints 'error -- contact system administrator', because clicking Checkout won't help you.
214
        else
215
        {
216
            // Error: The directory listing could not be retrieved
217
            // Make a link in case the user wants to investigate
218
            li.appendChild(dom_make_link_elem("span", name, description,
219
                app_path(this_app, username, path, name)));
220
            span = dom_make_text_elem("span",
221
                  " (error \u2013 contact system administrator)",
222
                  "There was an error retrieving information about this "
223
                  + "directory.");
224
            span.setAttribute("class", "status");
225
            li.appendChild(span);
226
        }
944 by dcoles
Special Home Directory: Work to create a special home directory that shows the
227
    }
228
229
    /* Are we working in a subdirectory or parent? */
230
    ajax_call(callback, service_app, path_join(username, path, name), {}, "GET");
231
    return li;
232
}
233
945 by wagrant
specialhome: Rebrand personal to mywork (by majority), refactor a bit,
234
function create_if_needed(path)
235
{
236
    response = ajax_call(null, service_app, current_path,
237
            {
238
                "action": "svnrepostat",
239
                "path": path
240
            }, "POST");
241
242
243
    if (response.status == 200)
244
    {
946 by wagrant
specialhome: Work with per-subject 'mywork' directories, although it
245
        return true;
945 by wagrant
specialhome: Rebrand personal to mywork (by majority), refactor a bit,
246
    }
247
    else if (response.status == 404)
248
    {
249
        // Try a mkdir
250
        r2 = ajax_call(null, service_app, current_path,
251
                {
252
                    "action": "svnrepomkdir",
253
                    "path": path,
946 by wagrant
specialhome: Work with per-subject 'mywork' directories, although it
254
                    "logmsg": "Automated creation of '" + path + "' work directory"
945 by wagrant
specialhome: Rebrand personal to mywork (by majority), refactor a bit,
255
                }, "POST");
256
257
        if (r2.status == 200)
258
        {
259
            return true;
260
        }
261
    }
262
    alert("Error: Could not create Subversion directory");
263
    return false;
264
}
980 by dcoles
Files: "Junk" is now only shown when other (non-subject/stuff) files are in the
265
266
/** Finds the length (number of user defined properties) of an object
267
 */
268
function obj_length(obj)
269
{
1168 by William Grant
Rename length to len in specialhome.js to please IE8.
270
    len = 0;
980 by dcoles
Files: "Junk" is now only shown when other (non-subject/stuff) files are in the
271
    for (prop in obj)
1168 by William Grant
Rename length to len in specialhome.js to please IE8.
272
        len++;
273
    return len;
980 by dcoles
Files: "Junk" is now only shown when other (non-subject/stuff) files are in the
274
}