update info form with new Fieldify, use on-change callback to manage friendship subfo...
[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_onlyifknown_isvisible_cb(item)
29 {
30     var vis = 'inherit';
31
32     if (item.value == 'black' || item.value == 'unknown') {
33         vis = 'hidden';
34     }
35
36     $('info').getElementsByClassName('onlyifknown_gid')[0].style.visibility = vis;
37     return true;
38 }
39
40 function info_fld(dobj)
41 {
42     var fields = { login: { type: 'value', perms: 'key' },
43                    state: { type: 'value', perms: 'ro' },
44                    guar: { type: 'value', perms: 'ro' },
45
46                    widefriend: { type: 'fields', fields: { skill: { type: 'value', perms: 'ro' },
47                                                            black: { type: 'value', perms: 'ro' },
48                                                            test: { type: 'value', perms: 'ro'},
49                                                            friend: { type: 'value', perms: 'ro'},
50                                                            bff: { type: 'value', perms: 'ro'}
51                                                          } },
52                    narrowfriend: { type: 'fields', fields: { skill: { type: 'value', perms: 'ro' },
53                                                              black: { type: 'value', perms: 'ro'},
54                                                              test: { type: 'value', perms: 'ro'},
55                                                              friend: { type: 'value', perms: 'ro'},
56                                                              bff: { type: 'value', perms: 'ro'}
57                                                          } },
58                    match: { type: 'value', perms: 'ro' },
59                    game: { type: 'value', perms: 'ro' },
60                    party: { type: 'value', perms: 'ro' },
61                    friend: { type: 'radio', cb: info_onlyifknown_isvisible_cb },
62                    skill: { type: 'radio' },
63                    trust: { type: 'radio' }
64                  };
65
66     var ret = new Fieldify([dobj], fields);
67     return (ret);
68  }
69
70 function info_show(username)
71 {
72     var info_in_in = server_request('index_wr.php', sess, 'mesg', 'chatt|/info ' +
73                                             encodeURIComponent(username));
74     var info_in = JSON.parse(info_in_in);
75     var info = null;
76
77     if (info_in.ret == 0) {
78         info = info_fld($('info'));
79         info.json2dom(info_in);
80         info.visible(true);
81         }
82     else {
83         var noti = new notify(gst, info_in.mesg, 0, mlang_info['btn_close'][g_lang], 400, 150);
84     }
85 }
86
87 var g__info_show_target = "";
88 function info_show_cb(e)
89 {
90     if (g__info_show_target == e.target.innerHTML) {
91         g__info_show_target = "";
92         info_show(e.target.innerHTML);
93     }
94     else {
95         g__info_show_target = e.target.innerHTML;
96     }
97 }
98
99 function info_reset()
100 {
101     var ret, target;
102
103     Fieldify.reset($('info'));
104 }
105
106 function info_save()
107 {
108     var info, jinfo, ret;
109
110     jinfo = Fieldify.dom2json($('info'));
111
112     ret = server_request('index_wr.php', sess, 'mesg', 'info|save','__POST__', 'info', JSON.stringify(jinfo));
113
114     if (ret == 1) {
115         Fieldify.actualize($('info'));
116     }
117     else {
118         alert(ret);
119     }
120 }