summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/hosting/server/htdocs/account/index.html
blob: 47c0ea0e9c92a84921f4e59e9815790764b9255d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<!DOCTYPE html>
<!--
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
-->
<div id="bodydiv" class="body">

<div id="viewform" class="viewform">

<form id="userForm">
<table style="width: 100%;">
<tr><tr><td class="label">Username:</td></tr>
<tr><td><input type="text" id="userName" class="readentry" size="30" readonly="readonly" placeholder="Your username" style="width: 300px;"/></td></tr>
<tr><tr><td class="label">Email:</td></tr>
<tr><td><input type="text" id="userEmail" class="readentry" size="30" readonly="readonly" placeholder="Your email address" style="width: 300px;"/></td></tr>
<tr><tr><td class="label">Picture:</td></tr>
<tr><td><img id="userPicture" style="width: 50px; height: 50px; vertical-align: top;"/><input id="uploadPicture" type="button" class="lightbutton" value="Upload"/><input id="uploadFile" type="file" accept="image/*" style="display:none;"/><span id="refreshingPicture" class="refreshing" style="display:none;"/></td></tr>
<tr><tr><td class="label">Name:</td></tr>
<tr><td><input type="text" id="userFullname" class="flatentry" size="30" placeholder="Your name" style="width: 300px;"/></td></tr>
<tr><tr><td class="label">Bio:</td></tr>
<tr><td><textarea id="userDescription" class="flatentry" cols="40" rows="3" placeholder="About yourself, in fewer than 120 characters" style="width: 300px;"></textarea></td></tr>
</table>
<br/>

<!--
TODO Disabled for now
<table style="width: 100%;">
<tr>
<th class="thl thr" style="padding-top: 4px; padding-bottom: 4px; padding-left: 2px; padding-right: 2px; ">Calendar</th>
</tr>
</table>

<table>
<tr><td style="padding-right: 2px;"><input type="text" id="sched1" class="flatentry" size="10" placeholder="Schedule" style="width: 80px;"/></td><td><input type="text" id="service1" class="flatentry" size="2048" placeholder="Service URL" style="width: 200px;"/></td></tr>
<tr><td style="padding-right: 2px;"><input type="text" id="sched2" class="flatentry" size="10" placeholder="Schedule" style="width: 80px;"/></td><td><input type="text" id="service2" class="flatentry" size="2048" placeholder="Service URL" style="width: 200px;"/></td></tr>
<tr><td style="padding-right: 2px;"><input type="text" id="sched3" class="flatentry" size="10" placeholder="Schedule" style="width: 80px;"/></td><td><input type="text" id="service3" class="flatentry" size="2048" placeholder="Service URL" style="width: 200px;"/></td></tr>
<tr><td style="padding-right: 2px;"><input type="text" id="sched4" class="flatentry" size="10" placeholder="Schedule" style="width: 80px;"/></td><td><input type="text" id="service4" class="flatentry" size="2048" placeholder="Service URL" style="width: 200px;"/></td></tr>
<tr><td style="padding-right: 2px;"><input type="text" id="sched5" class="flatentry" size="10" placeholder="Schedule" style="width: 80px;"/></td><td><input type="text" id="service5" class="flatentry" size="2048" placeholder="Service URL" style="width: 200px;"/></td></tr>
</table>
<br/>

<table style="width: 100%;">
<tr>
<th class="thl thr" style="padding-top: 4px; padding-bottom: 4px; padding-left: 2px; padding-right: 2px; ">Key chain</th>
</tr>
</table>

<table>
<tr><td style="padding-right: 2px;"><input type="text" id="name1" class="flatentry" size="10" placeholder="Key name" style="width: 80px;"/></td><td><input type="text" id="value1" class="flatentry" size="2048" placeholder="Key value" style="width: 200px;"/></td></tr>
<tr><td style="padding-right: 2px;"><input type="text" id="name2" class="flatentry" size="10" placeholder="Key name" style="width: 80px;"/></td><td><input type="text" id="value2" class="flatentry" size="2048" placeholder="Key value" style="width: 200px;"/></td></tr>
<tr><td style="padding-right: 2px;"><input type="text" id="name3" class="flatentry" size="10" placeholder="Key name" style="width: 80px;"/></td><td><input type="text" id="value3" class="flatentry" size="2048" placeholder="Key value" style="width: 200px;"/></td></tr>
<tr><td style="padding-right: 2px;"><input type="text" id="name4" class="flatentry" size="10" placeholder="Key name" style="width: 80px;"/></td><td><input type="text" id="value4" class="flatentry" size="2048" placeholder="Key value" style="width: 200px;"/></td></tr>
<tr><td style="padding-right: 2px;"><input type="text" id="name5" class="flatentry" size="10" placeholder="Key name" style="width: 80px;"/></td><td><input type="text" id="value5" class="flatentry" size="2048" placeholder="Key value" style="width: 200px;"/></td></tr>
<tr><td style="padding-right: 2px;"><input type="text" id="name6" class="flatentry" size="10" placeholder="Key name" style="width: 80px;"/></td><td><input type="text" id="value6" class="flatentry" size="2048" placeholder="Key value" style="width: 200px;"/></td></tr>
<tr><td style="padding-right: 2px;"><input type="text" id="name7" class="flatentry" size="10" placeholder="Key name" style="width: 80px;"/></td><td><input type="text" id="value7" class="flatentry" size="2048" placeholder="Key value" style="width: 200px;"/></td></tr>
<tr><td style="padding-right: 2px;"><input type="text" id="name8" class="flatentry" size="10" placeholder="Key name" style="width: 80px;"/></td><td><input type="text" id="value8" class="flatentry" size="2048" placeholder="Key value" style="width: 200px;"/></td></tr>
<tr><td style="padding-right: 2px;"><input type="text" id="name9" class="flatentry" size="10" placeholder="Key name" style="width: 80px;"/></td><td><input type="text" id="value9" class="flatentry" size="2048" placeholder="Key value" style="width: 200px;"/></td></tr>
<tr><td style="padding-right: 2px;"><input type="text" id="name10" class="flatentry" size="10" placeholder="Key name" style="width: 80px;"/></td><td><input type="text" id="value10" class="flatentry" size="2048" placeholder="Key value" style="width: 200px;"/></td></tr>
</table>
</form>
<br/>
-->

</div>

<script type="text/javascript">
(function accountbody() {

/**
 * Setup page layout.
 */
(function layout() {
    document.title = config.windowtitle() + ' - Account';
    $('viewhead').innerHTML = '<span class="cmenu">' + username + '</span>' +
        '<input type="button" class="redbutton plusminus" style="position: absolute; top: 4px; left: 5px;" id="deleteUser" value="-" title="Delete your account" disabled="true"/>';
    if (!ui.isMobile())
        $('viewform').className = 'viewform flatscrollbars';
    $('userName').value = username;
})();

/**
 * Set images.
 */
$('userPicture').src = ui.b64png(appcache.get('/public/user.b64'));

/**
 * Initialize service references.
 */
var editorComp = sca.component("Editor");
var user= sca.defun(sca.reference(editorComp, "user"));
var accounts = sca.reference(editorComp, "accounts");
var pictures = sca.reference(editorComp, "pictures");

/**
 * The current account entry and corresponding saved XML content.
 */
var savedacctxml = '';
var savedpicxml = '';

/**
 * Get and display the user's account.
 */
(function getacct() {
    workingstatus(true);
    showstatus('Loading');

    return accounts.get('', function(doc) {

        // Stop now if we didn't get an account
        if (doc == null) {
            errorstatus('Account info not available');
            workingstatus(false);
            return false;
        }

        var acctentry = car(elementsToValues(atom.readATOMEntry(mklist(doc))));
        $('userFullname').value = cadr(assoc("'title", acctentry));

        var acct = cadr(assoc("'content", acctentry));

        var email = assoc("'email", acct);
        $('userEmail').value = isNil(email) || isNil(cdr(email))? '' : cadr(email);

        var desc = assoc("'description", acct);
        $('userDescription').innerHTML = isNil(desc) || isNil(cdr(desc))? '' : cadr(desc);

        /* TODO disabled for now
        var cal = assoc("'calendar", acct);
        reduce(function(i, evt) {
                var sched = assoc("'@schedule", evt);
                var svc = assoc("'@service", evt);
                $('sched' + i).value = isNil(sched)? '' : cadr(sched);
                $('service' + i).value = isNil(svc)? '' : cadr(svc);
                return i + 1;
            }, 1, isNil(cal)? mklist() : cadr(cadr(cal)));

        var keys = assoc("'keys", acct);
        reduce(function(i, key) {
                var kn = assoc("'@name", key);
                var kv = assoc("'@value", key);
                $('name' + i).value = isNil(kn)? '' : cadr(kn);
                $('value' + i).value = isNil(kv)? '' : cadr(kv);
                return i + 1;
            }, 1, isNil(keys)? mklist() : cadr(cadr(keys)));
        */

        savedacctxml = car(atom.writeATOMEntry(valuesToElements(mklist(acctentry))));
        return true;
    });
    return true;
})();

/**
 * Get and display the user's picture.
 */
(function getpic() {
    workingstatus(true);
    showstatus('Loading');

    return pictures.get('', function(doc) {
        // Stop now if we didn't get a picture
        if (doc == null) {
            errorstatus('Picture not available');
            workingstatus(false);
            return false;
        }

        var picentry = car(elementsToValues(atom.readATOMEntry(mklist(doc))));
        savedpicxml = car(atom.writeATOMEntry(valuesToElements(mklist(picentry))));
        var content = assoc("'content", picentry);
        var picture = assoc("'picture", content);
        var img = assoc("'image", picture);
        if (!isNil(img))
            $('userPicture').src = cadr(img);

        onlinestatus();
        workingstatus(false);
        return true;
    });
    return true;
})();

/**
 * Refresh picture.
 */
var refreshingpic = false;
function refreshpic() {
    if (!refreshingpic)
        return false;
    $('refreshingPicture').style.display = 'inline-block';
    return pictures.get('', function(doc) {
        if (doc == null) {
            errorstatus('Picture not available');
            $('refreshingPicture').style.display = 'none';
            refreshingpic = false;
            return false;
        }

        var picentry = car(elementsToValues(atom.readATOMEntry(mklist(doc))));
        var content = assoc("'content", picentry);
        var picture = assoc("'picture", content);
        var token = assoc("'token", picture);

        // Update picture
        if (isNil(token)) {
            var entryxml = car(atom.writeATOMEntry(valuesToElements(mklist(picentry))));
            savedpicxml = entryxml;
            var img = assoc("'image", picture);
            if (!isNil(img))
                $('userPicture').src = cadr(img);
            $('refreshingPicture').style.display = 'none';
            refreshingpic = false;
            return true;
        }

        // Refresh in 2 secs
        return ui.delay(refreshpic, 2000);
    }, 'remote');
    return true;
}

/**
 * Save the user's account.
 */
function saveacct(entryxml) {
    if (isNil(username))
        return false;
    workingstatus(true);
    showstatus('Saving');

    savedacctxml = entryxml;
    accounts.put('', savedacctxml, function(e) {
        if (e) {
            showstatus('Local copy');
            workingstatus(false);
            return false;
        }

        showstatus('Saved');
        workingstatus(false);
        return true;
    });
    return true;
}

/**
 * Save the user's picture.
 */
function savepic(entryxml) {
    if (isNil(username))
        return false;
    workingstatus(true);
    showstatus('Uploading');

    savedpicxml = entryxml;
    pictures.put('', savedpicxml, function(e) {
        if (e) {
            showstatus('Local copy');
            workingstatus(false);
            return false;
        }

        showstatus('Uploaded');
        workingstatus(false);
        return true;
    });
    return true;
}

/**
 * Handle a change event
 */
function onaccountchange() {

    // Validate user input
    var title = $('userFullname').value;
    if (title.length == 0) {
        errorstatus('Name cannot be empty');
        return false;
    }
    if (title.length > 40) {
        errorstatus('Name cannot be longer than 40 characters');
        return false;
    }

    var email = $('userEmail').value;

    var description = $('userDescription').value;
    if (description.length > 120) {
        errorstatus('Bio cannot be longer than 120 characters');
        return false;
    }

    /* TODO disabled for now
    var cal = map(function(i) {
        var sched = $('sched' + i).value;
        var svc = $('service' + i).value;
        return mklist("'event", mklist("'@schedule", sched), mklist("'@service", svc));
    }, range(1, 6));
    var keys = map(function(i) {
        var kn = $('name' + i).value;
        var kv = $('value' + i).value;
        return mklist("'key", mklist("'@name", kn), mklist("'@value", kv));
    }, range(1, 11));

    var acctentry = mklist("'entry", mklist("'title", title != ''? title : username), mklist("'id", username),
                        mklist("'content", mklist("'account", mklist("'email", email), mklist("'description", description), cons("'keys", keys), cons("'calendar", cal))));
    */

    var acctentry = mklist("'entry", mklist("'title", title != ''? title : username), mklist("'id", username),
                        mklist("'content", mklist("'account", mklist("'email", email), mklist("'description", description))));
    var entryxml = car(atom.writeATOMEntry(valuesToElements(mklist(acctentry))));
    if (savedacctxml == entryxml)
        return false;

    showstatus('Modified');
    return saveacct(entryxml);
}

$('userFullname').onchange = onaccountchange;
$('userDescription').onchange = onaccountchange;

/* TODO disabled for now
map(function(i) {
    $('sched' + i).onchange = onaccountchange;
    $('service' + i).onchange = onaccountchange;
    return true;
}, range(1, 6));
map(function(i) {
    $('name' + i).onchange = onaccountchange;
    $('value' + i).onchange = onaccountchange;
    return true;
}, range(1, 11));
*/

/**
 * Handle a key event.
 */
var lastkeyup = null;
$('userFullname').onkeyup = $('userDescription').onkeyup = function() {
    var t = new Date().getTime();
    lastkeyup = t;
    ui.delay(function() {
            return t == lastkeyup? onaccountchange() : true;
        }, 2000);
};

/**
 * Handle a form submit event.
 */
$('userForm').onsubmit = function() {
    onaccountchange();
    return false;
};

/**
 * Read and upload icon file.
 */
function readpic(files) {
    if (!files || files.length == 0)
        return false;
    if (!files[0].type.match('image.*')) {
        errorstatus('Please select an image');
        return false;
    }
    workingstatus(true);
    showstatus('Loading');

    // Read the selected file into a 50x50 image
    return ui.readimage(files[0],
        function(e) {
            errorstatus('Couldn\'t read the file');
            workingstatus(false);
        },
        function(p) {
            showstatus('Loading ' + p + '%');
        },
        function(url) {
            // Update the user picture
            $('userPicture').src = url;
            showstatus('Loaded');

            // Now upload it
            ui.delay(function() {
                var picentry = mklist("'entry", mklist("'title", username), mklist("'id", username), mklist("'author", username), mklist("'content", mklist("'picture", mklist("'image", url))));
                var entryxml = car(atom.writeATOMEntry(valuesToElements(mklist(picentry))));
                if (savedpicxml == entryxml) {
                    onlinestatus();
                    workingstatus(false);
                    return false;
                }
                return savepic(entryxml);
            });
        }, 50, 50);
}

/**
 * Upload a picture in an email.
 */
function emailpicture() {

    // Generate and put a picture email upload token
    workingstatus(true);
    showstatus('Uploading');
    var token = uuid4();
    var picentry = mklist("'entry", mklist("'title", username), mklist("'id", username), mklist("'author", username), mklist("'content", mklist("'picture", mklist("'token", token))));
    var entryxml = car(atom.writeATOMEntry(valuesToElements(mklist(picentry))));
    pictures.put('', entryxml, function(e) {
        if (e) {
            showstatus('Local copy');
            workingstatus(false);
            return false;
        }
        workingstatus(false);

        // Open the email app
        var mailto = safeb64encode('p/' + username + '/' + token);
        ui.navigate('mailto:' + mailto + '@' + topdomainname(window.location.hostname) + '?subject=Email to upload&body=Paste picture here', '_self');

        // Refresh app icon
        refreshingpic = true;
        return ui.delay(refreshpic, 500);
    }, 'remote');
}

/**
 * Handle picture upload events.
 */
$('uploadPicture').onclick = function() {
    if (ui.isMobile())
        return emailpicture();
    return $('uploadFile').click();
};
$('uploadFile').onchange = function(e) {
    return readpic(e.target.files);
};
$('userPicture').ondrag = function(e) {
    e.stopPropagation();
    e.preventDefault();
    e.dataTransfer.dropEffect = 'copy';
};
$('userPicture').ondrop = function(e) {
    e.stopPropagation();
    e.preventDefault();
    return readpic(e.dataTransfer.files);
};

})();
</script>

</div>