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

« back to all changes in this revision

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

  • Committer: mattgiuca
  • Date: 2008-02-25 07:06:16 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:580
settings: Now able to handle the save button. This updates the user's details.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 * Date: 25/2/2008
21
21
 */
22
22
 
 
23
var user_data;
 
24
 
23
25
function onload()
24
26
{
25
27
    revert_settings();
26
28
}
27
29
 
28
 
/* Fetch the user's details from the server, and populate the page */
 
30
/* Fetch the user's details from the server, and populate the page.
 
31
 * Returns false. */
29
32
function revert_settings()
30
33
{
31
34
    var callback = function(xhr)
35
38
        }
36
39
    /* Just get details for the logged in user */
37
40
    ajax_call(callback, "userservice", "get_user", {}, "GET");
 
41
    return false;
38
42
}
39
43
 
40
44
/* Populate the page with the given user's account details */
41
45
function populate(user)
42
46
{
 
47
    user_data = user;
43
48
    /* Plain text elements (non-editable) */
44
49
    var login = document.getElementById("login");
45
50
    var role = document.getElementById("role");
172
177
        }
173
178
    }
174
179
}
 
180
 
 
181
/* Sets the "result" text.
 
182
 * iserror (bool) determines the styling.
 
183
 */
 
184
function set_result(text, iserror)
 
185
{
 
186
    var p = document.getElementById("result");
 
187
    dom_removechildren(p);
 
188
    p.appendChild(document.createTextNode(text));
 
189
    if (iserror)
 
190
        p.setAttribute("class", "error");
 
191
    else
 
192
        p.removeAttribute("class");
 
193
}
 
194
 
 
195
/* Writes the settings to the server.
 
196
 * Returns false. */
 
197
function save_settings()
 
198
{
 
199
    /* Button (input) elements */
 
200
    var save = document.getElementById("save");
 
201
    /* Textbox (input) elements */
 
202
    try
 
203
    {
 
204
        var newpass = document.getElementById("newpass");
 
205
        var repeatpass = document.getElementById("repeatpass");
 
206
    }
 
207
    catch (e)
 
208
    {
 
209
        var newpass = null;
 
210
        var repeatpass = null;
 
211
    }
 
212
    var nick = document.getElementById("nick");
 
213
    var email = document.getElementById("email");
 
214
 
 
215
    /* Check */
 
216
    newpassval = newpass == null ? null : newpass.value;
 
217
    repeatpassval = repeatpass == null ? null : repeatpass.value;
 
218
    nickval = nick.value;
 
219
    emailval = email.value;
 
220
 
 
221
    /* Clear the password boxes, even if there are errors later */
 
222
    try
 
223
    {
 
224
        newpass.value = "";
 
225
        repeatpass.value = "";
 
226
    }
 
227
    catch (e)
 
228
    {
 
229
    }
 
230
 
 
231
    if (nickval == "")
 
232
    {
 
233
        set_result("Display name is empty.", true);
 
234
        return false;
 
235
    }
 
236
    if (newpassval != repeatpassval)
 
237
    {
 
238
        set_result("Passwords do not match.", true);
 
239
        return false;
 
240
    }
 
241
 
 
242
    /* Disable the heavy-duty supercolliding super button */
 
243
    save.setAttribute("disabled", "disabled");
 
244
    save.setAttribute("value", "Saving...");
 
245
    var callback = function(xhr)
 
246
    {
 
247
        save.removeAttribute("disabled");
 
248
        save.setAttribute("value", "Save");
 
249
 
 
250
        if (xhr.status == 200)
 
251
        {
 
252
            set_result("Successfully updated details.");
 
253
            user_data.nick = nickval;
 
254
            user_data.email = emailval;
 
255
        }
 
256
        else
 
257
        {
 
258
            set_result("There was a problem updating the details."
 
259
                + " Your changes have not been saved.");
 
260
        }
 
261
    }
 
262
    data = {
 
263
        "login": user_data.login,
 
264
        "nick": nickval,
 
265
        "email": emailval,
 
266
    }
 
267
    if (newpassval != null && newpassval != "")
 
268
        data['password'] = newpassval;
 
269
    ajax_call(callback, "userservice", "update_user", data, "POST");
 
270
    return false;
 
271
}