info: error handling refactored
[brisk.git] / web / info.js
1 /*
2  *  brisk - info.js
3  *
4  *  Copyright (C) 2015      Matteo Nastasi
5  *                          mailto: nastasi@alternativeoutput.it
6  *                                  matteo.nastasi@milug.org
7  *                          web: http://www.alternativeoutput.it
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details. You should have received a
18  * copy of the GNU General Public License along with this program; if
19  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
20  * Suite 330, Boston, MA 02111-1307, USA.
21  *
22  */
23
24 var mlang_info = { 'btn_close' : { 'it' : 'Chiudi.',
25                                    'en' : 'Close.' }
26                  };
27
28 function info_fld(dobj)
29 {
30     var fields = { login: { type: 'value' },
31                    state: { type: 'value', perms: 'ro' },
32                    guar: { type: 'value', perms: 'ro' },
33                    match: { type: 'value', perms: 'ro' },
34                    game: { type: 'value', perms: 'ro' },
35                    friend: { type: 'radio' },
36                    skill: { type: 'radio' },
37                    trust: { type: 'radio' }
38                  };
39
40     return (new Fieldify(dobj, fields));
41  }
42
43 function info_show(username)
44 {
45     var info_in_in = server_request('mesg', 'chatt|/info ' +
46                                             encodeURIComponent(username));
47     var info_in = JSON.parse(info_in_in);
48     var info = null;
49
50     if (info_in.ret == 0) {
51         info = info_fld($('info'));
52         info.json2dom(info_in);
53         info.visible(true);
54         }
55     else {
56         var noti = new notify(gst, info_in.mesg, 0, mlang_info['btn_close'][g_lang], 400, 150);
57     }
58 }
59
60 var g__info_show_target = "";
61 function info_show_cb(e)
62 {
63     if (g__info_show_target == e.target.innerHTML) {
64         g__info_show_target = "";
65         info_show(e.target.innerHTML);
66     }
67     else {
68         g__info_show_target = e.target.innerHTML;
69     }
70 }
71
72 function info_reset()
73 {
74     var ret, target;
75
76     target = $('info').getElementsByClassName('login_id')[0].innerHTML;
77     return info_show(target);
78 }
79
80 function info_save()
81 {
82     var info, jinfo, ret;
83
84     info = info_fld($('info'));
85     jinfo = info.dom2json();
86
87     ret = server_request('mesg', 'info|save','__POST__', 'info', JSON.stringify(jinfo));
88
89     if (ret == 1) {
90         $('info').style.visibility = 'hidden';
91     }
92     else {
93         alert(ret);
94     }
95 }