prefs refactoring: partial implementation (wip)
[brisk.git] / web / prefs.js
1 /*
2  *  brisk - prefs.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_prefsn = { 'btn_close' : { 'it' : 'Chiudi.',
25                                      'en' : 'Close.' },
26                      'tit_list'  : { '0'  : { 'it' : '',
27                                               'en' : '' },
28                                      '1'  : { 'it' : '(solo aut.)',
29                                               'en' : '(only aut.)' },
30                                      '2'  : { 'it' : '(isolam.to)',
31                                               'en' : '(isolation)' } }
32                    };
33
34 var l_list_all  = 0x00;
35 var l_list_auth = 0x01;
36 var l_list_isol = 0x02;
37 var l_prefs_list_idx = new Array( 0x00, 0x01, 0x02 );
38 var l_prefs_list_id  = new Array( "all", "auth", "isol" );
39 var l_comps_name     = new Array('s_fg_r', 's_fg_g', 's_fg_b', 's_bg_r',  's_bg_g',  's_bg_b');
40
41 /* FIXME: move to html page with dynamic valorization */
42 var is_supp_custom = true;
43
44 function Prefsn(obj)
45 {
46     var fields = { listen: { type: 'radio', cb: Prefsn.listen_change_cb } };
47     if (is_supp_custom) {
48         fields.fg = {
49             type: 'fields', fields: { r: { type: 'value', cb: Prefsn.supp_change_cb },
50                                       g: { type: 'value', cb: Prefsn.supp_change_cb },
51                                       b: { type: 'value', cb: Prefsn.supp_change_cb }
52                                     }
53         };
54         fields.bg = {
55             type: 'fields', fields: { r: { type: 'value', cb: Prefsn.supp_change_cb },
56                                       g: { type: 'value', cb: Prefsn.supp_change_cb },
57                                       b: { type: 'value', cb: Prefsn.supp_change_cb }
58                                     }
59         };
60     }
61
62     this.fieldify = new Fieldify([obj], fields);
63     this.obj = obj;
64     this.data = null;
65 }
66
67 Prefsn.listen_change_cb = function(field)
68 {
69     var i;
70     var prefs_new;
71     var relo = false;
72
73     console.log("listen_change_cb:" + field.value);
74     console.log("xx" + g_prefsn.data);
75
76     /* listen management */
77     if (g_prefsn.data == null || g_prefsn.data.listen != field.value) {
78         $('list_info').innerHTML = mlang_prefsn['tit_list'][field.value][g_lang];
79         console.log('update listen');
80         for (i = g_tables_auth_n ; i < g_tables_n ; i++) {
81             if (i % 4 == 0) {
82                 $('tr_noauth'+i).style.display = (field.value == l_list_isol ? 'none' : '');
83             }
84
85             $('td_noauth'+i).style.display = (field.value == l_list_isol ? 'none' : '');
86         }
87         if (field.value == l_list_isol) {
88             tra.hide_noauth();
89         }
90         else {
91             tra.show_noauth();
92         }
93     }
94 }
95
96 Prefsn.supp_change_cb = function(field)
97 {
98     var i;
99     var prefs_new;
100     var relo = false;
101     console.log("prefs_update("+field+")");
102     return;
103     if (typeof(g_prefsn) == 'undefined')
104         return false;
105
106     for (i = 0 ; i < 6 ; i++) {
107         if (parseInt($(l_comps_name[i]).value) < 0 || parseInt($(l_comps_name[i]).value) > 255 ||
108             isNaN(parseInt($(l_comps_name[i]).value))) {
109             break;
110         }
111     }
112
113     if (i == 6) {
114         prefs_new.supp_comp = "";
115         for (i = 0 ; i < 6 ; i++) {
116             prefs_new.supp_comp += dec2hex(parseInt($(l_comps_name[i]).value), 2);
117         }
118     }
119
120     // console.log("prefs_update:: i break "+i+" ["+prefs_new.supp_comp+"]");
121
122     for (i = 0 ; i < 6 ; i++) {
123         $(l_comps_name[i]).value = parseInt(prefs_new.supp_comp.substr(i*2, 2), 16);
124     }
125
126     /* from form to struct */
127     // prefs_apply(prefs_new, true, true);
128 };
129
130
131
132 Prefsn.prototype = {
133     obj: null,
134     fieldify: null,
135     data: null,
136
137     load: function(data) {
138         var in_data = JSON.parse(data);
139         this.fieldify.json2dom(in_data);
140         this.data = in_data;
141     },
142
143     reset: function() {
144         console.log('here');
145         Fieldify.reset(this.obj);
146     },
147
148     show: function() {
149         this.fieldify.visible(true);
150     }
151 }
152
153 function prefsn_show(username)
154 {
155     var prefsn_in_in = server_request('index_wr.php', sess, 'mesg', 'chatt|/prefsn');
156     var prefsn_in = JSON.parse(prefsn_in_in);
157     var prefsn = null;
158
159     if (prefsn_in.ret == 0) {
160         prefsn = prefsn_fld($('prefsn'));
161         prefsn.json2dom(prefsn_in);
162         prefsn.visible(true);
163         }
164     else {
165         var noti = new notify(gst, prefsn_in.mesg, 0, mlang_prefsn['btn_close'][g_lang], 400, 150);
166     }
167 }
168
169 function prefsn_reset()
170 {
171     Fieldify.reset($('prefsn'));
172 }
173
174
175
176
177
178
179
180 function client_prefs(old)
181 {
182     if (old != null) {
183         this.listen    = old.listen;
184         this.supp_comp = old.supp_comp;
185     }
186 }
187
188 client_prefs.prototype = {
189     listen:    -1,
190     supp_comp: 'ff00ffff00'
191 }
192
193 function prefs_assign(content)
194 {
195     var prefs_new;
196     var s;
197
198     s = "prefs_new = " + content;
199     eval(s);
200
201     return (prefs_new);
202 }
203
204 function prefs_apply(prefs_new, is_update, is_volat)
205 {
206     var i;
207     var relo = false;
208
209     if (typeof(g_prefs) == 'undefined')
210         return false;
211     /* listen management */
212     if (g_prefs.listen != prefs_new.listen || is_update) {
213         for (i = 0 ; i < l_prefs_list_idx.length ; i++) {
214             set_checked_value($('ra_listen_'+l_prefs_list_id[i]), prefs_new.listen);
215             if (prefs_new.listen == l_prefs_list_idx[i]) {
216                 if (!is_volat)
217                     $('list_'+l_prefs_list_id[i]).style.color = 'red';
218                 $('list_info').innerHTML = mlang_prefsn['tit_list'][i][g_lang];
219             }
220             else {
221                 if (!is_volat)
222                     $('list_'+l_prefs_list_id[i]).style.color = 'black';
223             }
224         }
225
226         relo = true;
227     }
228
229     // supporter component management
230     if (g_prefs.supp_comp != prefs_new.supp_comp || is_update) {
231
232         for (i = 0 ; i < 6 ; i++) {
233             $(l_comps_name[i]).value = parseInt(prefs_new.supp_comp.substr(i*2,2), 16);
234         }
235         $('s_img').src = 'suprend.php?comp='+prefs_new.supp_comp;
236     }
237
238     if (relo || !is_update) {
239         for (i = g_tables_auth_n ; i < g_tables_n ; i++) {
240             if (i % 4 == 0) {
241                 $('tr_noauth'+i).style.display = (prefs_new.listen == l_list_isol ? 'none' : '');
242             }
243
244             $('td_noauth'+i).style.display = (prefs_new.listen == l_list_isol ? 'none' : '');
245         }
246         if (prefs_new.listen == l_list_isol) {
247             tra.hide_noauth();
248         }
249         else {
250             tra.show_noauth();
251         }
252
253         if (false) {
254             // ricalculation of standup area
255             if (standup_data_old != null) {
256                 standup_data = standup_data_old;
257                 standup_data_old = null;
258                 j_stand_cont(standup_data);
259             }
260         }
261     }
262
263     g_prefs.listen    = prefs_new.listen;
264     g_prefs.supp_comp = prefs_new.supp_comp;
265 }
266
267 function prefs_load(content, is_update, is_volat)
268 {
269     var prefs_new;
270
271     // console.log('prefs_load('+content+')');
272
273     if ((prefs_new = prefs_assign(content)) == null)
274         return false;
275
276     return prefs_apply(prefs_new, is_update, is_volat);
277 }
278
279 function prefs_save()
280 {
281     var ret;
282
283     if (typeof(g_prefs) == 'undefined')
284         return false;
285
286     ret = server_request('index_wr.php', sess, 'mesg', 'prefs|save','__POST__', 'prefs', JSON.stringify(g_prefs));
287
288     if (ret == 1) {
289         $('preferences').style.visibility = 'hidden';
290     }
291     else {
292         alert(ret);
293     }
294 }
295
296 function prefs_reset()
297 {
298     var ret;
299
300     ret = server_request('index_wr.php', sess, 'mesg', 'prefs|reset');
301 }
302
303 function prefs_update(field)
304 {
305     var i;
306     var prefs_new;
307     var relo = false;
308     // console.log("prefs_update("+field+")");
309
310     if (typeof(g_prefs) == 'undefined')
311         return false;
312
313     prefs_new = new client_prefs(g_prefs);
314
315     if (field == 'listen') {
316         /* listen management */
317         for (i = 0 ; i < l_prefs_list_idx.length ; i++) {
318             prefs_new.listen = get_checked_value($('ra_listen_'+l_prefs_list_id[i]));
319             if (prefs_new.listen != '')
320                 break;
321         }
322     }
323     else if (field == 'supp') {
324         for (i = 0 ; i < 6 ; i++) {
325             if (parseInt($(l_comps_name[i]).value) < 0 || parseInt($(l_comps_name[i]).value) > 255 ||
326                 isNaN(parseInt($(l_comps_name[i]).value))) {
327                 break;
328             }
329         }
330
331         if (i == 6) {
332             prefs_new.supp_comp = "";
333             for (i = 0 ; i < 6 ; i++) {
334                 prefs_new.supp_comp += dec2hex(parseInt($(l_comps_name[i]).value), 2);
335             }
336         }
337
338         // console.log("prefs_update:: i break "+i+" ["+prefs_new.supp_comp+"]");
339
340         for (i = 0 ; i < 6 ; i++) {
341             $(l_comps_name[i]).value = parseInt(prefs_new.supp_comp.substr(i*2, 2), 16);
342         }
343     }
344
345     /* from form to struct */
346     prefs_apply(prefs_new, true, true);
347 }
348
349 function list_set(what, is_update, info)
350 {
351     var i;
352     var relo = false;
353     var old_st = readCookie("CO_list");
354
355     if (what == 'auth') {
356         $('list_auth').style.color = 'red';
357         $('list_isol').style.color = 'black';
358         $('list_all').style.color = 'black';
359         if (old_st == 'isolation')
360             relo = true;
361         g_listen = l_list_auth;
362     }
363     else if (what == 'isolation') {
364         $('list_auth').style.color = 'black';
365         $('list_isol').style.color = 'red';
366         $('list_all').style.color = 'black';
367         if (old_st != 'isolation')
368             relo = true;
369         g_listen = l_list_isol;
370     }
371     else {
372         $('list_auth').style.color = 'black';
373         $('list_isol').style.color = 'black';
374         $('list_all').style.color = 'red';
375         if (old_st == 'isolation')
376             relo = true;
377         g_listen = l_list_all;
378     }
379
380     set_checked_value($('ra_listen_auth'), what);
381     set_checked_value($('ra_listen_isol'), what);
382     set_checked_value($('ra_listen_all'),  what);
383
384     $('list_info').innerHTML = info;
385     if (is_update) {
386         createCookie("CO_list", what, 24*365, cookiepath);
387     }
388
389
390     if (relo || !is_update) {
391         for (i = g_tables_auth_n ; i < g_tables_n ; i++) {
392
393             if (i % 4 == 0) {
394                 $('tr_noauth'+i).style.display = (what == 'isolation' ? 'none' : '');
395             }
396
397             $('td_noauth'+i).style.display = (what == 'isolation' ? 'none' : '');
398         }
399         if (what == 'isolation') {
400             tra.hide_noauth();
401         }
402         else {
403             tra.show_noauth();
404         }
405
406         if (false) {
407             // ricalculation of standup area
408             if (standup_data_old != null) {
409                 standup_data = standup_data_old;
410                 standup_data_old = null;
411                 j_stand_cont(standup_data);
412             }
413         }
414     }
415 }