afddd0919662865ecd9527431e4273e096d309f5
[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 function obj_clone(obj) // begin = {} or [] is optional
97 {
98     var ret, ty, begin;
99
100     if (arguments.length > 1)
101         begin = arguments[1];
102     else
103         begin = {};
104
105     for (i in obj) {
106         ty = typeof(obj[i]);
107
108         if (ty == 'object') {
109             if (obj[i].constructor == Array) {
110                 ret[i] = obj_clone(obj[i], []);
111             }
112             else {
113                 ret[i] = obj_clone(obj[i], {});
114             }
115         }
116         else {
117             ret[i] = obj[i];
118         }
119     }
120     return ret;
121 }
122
123 function obj_update(cur, delta)
124 {
125     for (i in cur) {
126         if (i in delta) {
127             ty = typeof(cur[i]);
128             if (ty == 'object') {
129                 obj_update(cur[i], delta[i]);
130             }
131             else {
132                 cur[i] = delta[i];
133             }
134         }
135     }
136 }
137
138 Prefsn.supp_change_cb = function(field)
139 {
140     var i;
141     var supp_new;
142     var relo = false;
143
144     if (g_prefsn == null)
145         return false;
146
147     supp_new = Fieldify.item2tree([g_prefsn.obj], field);
148     supp_cur = {};
149     console.log(g_prefsn.data.fg);
150     supp_cur['fg'] = obj_clone(g_prefsn.data.fg);
151     supp_cur['bg'] = obj_clone(g_prefsn.data.bg);
152
153     for (i in ['fg', 'bg']) {
154         if (! i in supp_new) {
155             continue;
156         }
157         obj_update(supp_cur[i], supp_new[i]);
158     }
159
160     var col, ct = 0, s = "";
161     for (col in [ 'fg', 'bg' ]) {
162         for (cmp in [ 'r', 'g', 'b' ]) {
163             var val_s = supp_cur[col][cmp];
164             if (parseInt(val_s) < 0 || parseInt(val_s) > 255 || isNaN(parseInt(val_s)))
165                 return false;
166         }
167         s += dec2hex(parseInt(val_s), 2);
168     }
169     $('s_img').src = 'suprend.php?comp=' + s;
170 };
171
172
173
174 Prefsn.prototype = {
175     obj: null,
176     fieldify: null,
177     data: null,
178
179     load: function(data) {
180         var in_data = JSON.parse(data);
181         this.fieldify.json2dom(in_data);
182         this.data = in_data;
183     },
184
185     reset: function() {
186         console.log('here');
187         Fieldify.reset(this.obj);
188     },
189
190     show: function() {
191         this.fieldify.visible(true);
192     }
193 }
194
195 function prefsn_show(username)
196 {
197     var prefsn_in_in = server_request('index_wr.php', sess, 'mesg', 'chatt|/prefsn');
198     var prefsn_in = JSON.parse(prefsn_in_in);
199     var prefsn = null;
200
201     if (prefsn_in.ret == 0) {
202         prefsn = prefsn_fld($('prefsn'));
203         prefsn.json2dom(prefsn_in);
204         prefsn.visible(true);
205     }
206     else {
207         var noti = new notify(gst, prefsn_in.mesg, 0, mlang_prefsn['btn_close'][g_lang], 400, 150);
208     }
209 }
210
211 function prefsn_reset()
212 {
213     Fieldify.reset($('prefsn'));
214 }
215
216
217
218
219
220
221
222 function client_prefs(old)
223 {
224     if (old != null) {
225         this.listen    = old.listen;
226         this.supp_comp = old.supp_comp;
227     }
228 }
229
230 client_prefs.prototype = {
231     listen:    -1,
232     supp_comp: 'ff00ffff00'
233 }
234
235 function prefs_assign(content)
236 {
237     var prefs_new;
238     var s;
239
240     s = "prefs_new = " + content;
241     eval(s);
242
243     return (prefs_new);
244 }
245
246 function prefs_apply(prefs_new, is_update, is_volat)
247 {
248     var i;
249     var relo = false;
250
251     if (typeof(g_prefs) == 'undefined')
252         return false;
253     /* listen management */
254     if (g_prefs.listen != prefs_new.listen || is_update) {
255         for (i = 0 ; i < l_prefs_list_idx.length ; i++) {
256             set_checked_value($('ra_listen_'+l_prefs_list_id[i]), prefs_new.listen);
257             if (prefs_new.listen == l_prefs_list_idx[i]) {
258                 if (!is_volat)
259                     $('list_'+l_prefs_list_id[i]).style.color = 'red';
260                 $('list_info').innerHTML = mlang_prefsn['tit_list'][i][g_lang];
261             }
262             else {
263                 if (!is_volat)
264                     $('list_'+l_prefs_list_id[i]).style.color = 'black';
265             }
266         }
267
268         relo = true;
269     }
270
271     // supporter component management
272     if (g_prefs.supp_comp != prefs_new.supp_comp || is_update) {
273
274         for (i = 0 ; i < 6 ; i++) {
275             $(l_comps_name[i]).value = parseInt(prefs_new.supp_comp.substr(i*2,2), 16);
276         }
277         $('s_img').src = 'suprend.php?comp='+prefs_new.supp_comp;
278     }
279
280     if (relo || !is_update) {
281         for (i = g_tables_auth_n ; i < g_tables_n ; i++) {
282             if (i % 4 == 0) {
283                 $('tr_noauth'+i).style.display = (prefs_new.listen == l_list_isol ? 'none' : '');
284             }
285
286             $('td_noauth'+i).style.display = (prefs_new.listen == l_list_isol ? 'none' : '');
287         }
288         if (prefs_new.listen == l_list_isol) {
289             tra.hide_noauth();
290         }
291         else {
292             tra.show_noauth();
293         }
294
295         if (false) {
296             // ricalculation of standup area
297             if (standup_data_old != null) {
298                 standup_data = standup_data_old;
299                 standup_data_old = null;
300                 j_stand_cont(standup_data);
301             }
302         }
303     }
304
305     g_prefs.listen    = prefs_new.listen;
306     g_prefs.supp_comp = prefs_new.supp_comp;
307 }
308
309 function prefs_load(content, is_update, is_volat)
310 {
311     var prefs_new;
312
313     // console.log('prefs_load('+content+')');
314
315     if ((prefs_new = prefs_assign(content)) == null)
316         return false;
317
318     return prefs_apply(prefs_new, is_update, is_volat);
319 }
320
321 function prefs_save()
322 {
323     var ret;
324
325     if (typeof(g_prefs) == 'undefined')
326         return false;
327
328     ret = server_request('index_wr.php', sess, 'mesg', 'prefs|save','__POST__', 'prefs', JSON.stringify(g_prefs));
329
330     if (ret == 1) {
331         $('preferences').style.visibility = 'hidden';
332     }
333     else {
334         alert(ret);
335     }
336 }
337
338 function prefs_reset()
339 {
340     var ret;
341
342     ret = server_request('index_wr.php', sess, 'mesg', 'prefs|reset');
343 }
344
345 function prefs_update(field)
346 {
347     var i;
348     var prefs_new;
349     var relo = false;
350     // console.log("prefs_update("+field+")");
351
352     if (typeof(g_prefs) == 'undefined')
353         return false;
354
355     prefs_new = new client_prefs(g_prefs);
356
357     if (field == 'listen') {
358         /* listen management */
359         for (i = 0 ; i < l_prefs_list_idx.length ; i++) {
360             prefs_new.listen = get_checked_value($('ra_listen_'+l_prefs_list_id[i]));
361             if (prefs_new.listen != '')
362                 break;
363         }
364     }
365     else if (field == 'supp') {
366         for (i = 0 ; i < 6 ; i++) {
367             if (parseInt($(l_comps_name[i]).value) < 0 || parseInt($(l_comps_name[i]).value) > 255 ||
368                 isNaN(parseInt($(l_comps_name[i]).value))) {
369                 break;
370             }
371         }
372
373         if (i == 6) {
374             prefs_new.supp_comp = "";
375             for (i = 0 ; i < 6 ; i++) {
376                 prefs_new.supp_comp += dec2hex(parseInt($(l_comps_name[i]).value), 2);
377             }
378         }
379
380         // console.log("prefs_update:: i break "+i+" ["+prefs_new.supp_comp+"]");
381
382         for (i = 0 ; i < 6 ; i++) {
383             $(l_comps_name[i]).value = parseInt(prefs_new.supp_comp.substr(i*2, 2), 16);
384         }
385     }
386
387     /* from form to struct */
388     prefs_apply(prefs_new, true, true);
389 }
390
391 function list_set(what, is_update, info)
392 {
393     var i;
394     var relo = false;
395     var old_st = readCookie("CO_list");
396
397     if (what == 'auth') {
398         $('list_auth').style.color = 'red';
399         $('list_isol').style.color = 'black';
400         $('list_all').style.color = 'black';
401         if (old_st == 'isolation')
402             relo = true;
403         g_listen = l_list_auth;
404     }
405     else if (what == 'isolation') {
406         $('list_auth').style.color = 'black';
407         $('list_isol').style.color = 'red';
408         $('list_all').style.color = 'black';
409         if (old_st != 'isolation')
410             relo = true;
411         g_listen = l_list_isol;
412     }
413     else {
414         $('list_auth').style.color = 'black';
415         $('list_isol').style.color = 'black';
416         $('list_all').style.color = 'red';
417         if (old_st == 'isolation')
418             relo = true;
419         g_listen = l_list_all;
420     }
421
422     set_checked_value($('ra_listen_auth'), what);
423     set_checked_value($('ra_listen_isol'), what);
424     set_checked_value($('ra_listen_all'),  what);
425
426     $('list_info').innerHTML = info;
427     if (is_update) {
428         createCookie("CO_list", what, 24*365, cookiepath);
429     }
430
431
432     if (relo || !is_update) {
433         for (i = g_tables_auth_n ; i < g_tables_n ; i++) {
434
435             if (i % 4 == 0) {
436                 $('tr_noauth'+i).style.display = (what == 'isolation' ? 'none' : '');
437             }
438
439             $('td_noauth'+i).style.display = (what == 'isolation' ? 'none' : '');
440         }
441         if (what == 'isolation') {
442             tra.hide_noauth();
443         }
444         else {
445             tra.show_noauth();
446         }
447
448         if (false) {
449             // ricalculation of standup area
450             if (standup_data_old != null) {
451                 standup_data = standup_data_old;
452                 standup_data_old = null;
453                 j_stand_cont(standup_data);
454             }
455         }
456     }
457 }