lastest modifies
[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_prefsf = { '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 mlang_prefsn = { 'btn_close' : { 'it' : 'Chiudi.',
35                                      'en' : 'Close.' },
36                      'tit_list'  : { '0'  : { 'it' : '',
37                                               'en' : '' },
38                                      '1'  : { 'it' : '(solo aut.)',
39                                               'en' : '(only aut.)' },
40                                      '2'  : { 'it' : '(isolam.to)',
41                                               'en' : '(isolation)' } }
42                    };
43
44 var l_list_all  = 0x00;
45 var l_list_auth = 0x01;
46 var l_list_isol = 0x02;
47 var l_prefs_list_idx = new Array( 0x00, 0x01, 0x02 );
48 var l_prefs_list_id  = new Array( "all", "auth", "isol" );
49 var l_comps_name     = new Array('s_fg_r', 's_fg_g', 's_fg_b', 's_bg_r',  's_bg_g',  's_bg_b');
50
51 /* FIXME: move to html page with dynamic valorization */
52 var is_supp_custom = true;
53
54 function Prefsn(obj)
55 {
56     var fields = { listen: { type: 'radio', cb: Prefsn.listen_change_cb } };
57     if (is_supp_custom) {
58         fields.fg = {
59             type: 'fields', fields: { r: { type: 'value', cb: Prefsn.supp_change_cb },
60                                       g: { type: 'value', cb: Prefsn.supp_change_cb },
61                                       b: { type: 'value', cb: Prefsn.supp_change_cb }
62                                     }
63         };
64         fields.bg = {
65             type: 'fields', fields: { r: { type: 'value', cb: Prefsn.supp_change_cb },
66                                       g: { type: 'value', cb: Prefsn.supp_change_cb },
67                                       b: { type: 'value', cb: Prefsn.supp_change_cb }
68                                     }
69         };
70     }
71
72     this.fieldify = new Fieldify([obj], fields);
73     this.obj = obj;
74     this.data = null;
75 }
76
77 Prefsn.listen_change_cb = function(field)
78 {
79     var i;
80     var prefs_new;
81     var relo = false;
82
83     console.log("listen_change_cb:" + field.value);
84     console.log("xx" + g_prefsn.data);
85
86     /* listen management */
87     if (g_prefsn.data == null || g_prefsn.data.listen != field.value) {
88         $('list_info').innerHTML = mlang_prefsn['tit_list'][field.value][g_lang];
89         console.log('update listen');
90         for (i = g_tables_cert_n + g_tables_auth_n ; i < g_tables_n ; i++) {
91             if (i % 4 == 0) {
92                 $('tr_noauth'+i).style.display = (field.value == l_list_isol ? 'none' : '');
93             }
94
95             $('td_noauth'+i).style.display = (field.value == l_list_isol ? 'none' : '');
96         }
97         if (field.value == l_list_isol) {
98             tra.hide_noauth();
99         }
100         else {
101             tra.show_noauth();
102         }
103     }
104 }
105
106 function obj_clone(obj) // begin = {} or [] is optional
107 {
108     var ret, ty, begin;
109
110     if (arguments.length > 1)
111         begin = arguments[1];
112     else
113         begin = {};
114
115     for (i in obj) {
116         ty = typeof(obj[i]);
117
118         if (ty == 'object') {
119             if (obj[i].constructor == Array) {
120                 ret[i] = obj_clone(obj[i], []);
121             }
122             else {
123                 ret[i] = obj_clone(obj[i], {});
124             }
125         }
126         else {
127             ret[i] = obj[i];
128         }
129     }
130     return ret;
131 }
132
133 function obj_update(cur, delta)
134 {
135     for (i in cur) {
136         if (i in delta) {
137             ty = typeof(cur[i]);
138             if (ty == 'object') {
139                 obj_update(cur[i], delta[i]);
140             }
141             else {
142                 cur[i] = delta[i];
143             }
144         }
145     }
146 }
147
148 Prefsn.supp_change_cb = function(field)
149 {
150     var i;
151     var supp_new;
152     var relo = false;
153
154     if (g_prefsn == null)
155         return false;
156
157     supp_new = Fieldify.item2tree([g_prefsn.obj], field);
158     supp_cur = {};
159     console.log(g_prefsn.data.fg);
160     supp_cur['fg'] = obj_clone(g_prefsn.data.fg);
161     supp_cur['bg'] = obj_clone(g_prefsn.data.bg);
162
163     for (i in ['fg', 'bg']) {
164         if (! i in supp_new) {
165             continue;
166         }
167         obj_update(supp_cur[i], supp_new[i]);
168     }
169
170     var col, ct = 0, s = "";
171     for (col in [ 'fg', 'bg' ]) {
172         for (cmp in [ 'r', 'g', 'b' ]) {
173             var val_s = supp_cur[col][cmp];
174             if (parseInt(val_s) < 0 || parseInt(val_s) > 255 || isNaN(parseInt(val_s)))
175                 return false;
176         }
177         s += dec2hex(parseInt(val_s), 2);
178     }
179     $('s_img').src = 'suprend.php?comp=' + s;
180 };
181
182
183
184 Prefsn.prototype = {
185     obj: null,
186     fieldify: null,
187     data: null,
188
189     load: function(data) {
190         var in_data = JSON.parse(data);
191         this.fieldify.json2dom(in_data);
192         this.data = in_data;
193     },
194
195     reset: function() {
196         console.log('here');
197         Fieldify.reset(this.obj);
198     },
199
200     show: function() {
201         this.fieldify.visible(true);
202     }
203 }
204
205 function prefsn_show(username)
206 {
207     var prefsn_in_in = server_request('index_wr.php', sess, 'mesg', 'chatt|/prefsn');
208     var prefsn_in = JSON.parse(prefsn_in_in);
209     var prefsn = null;
210
211     if (prefsn_in.ret == 0) {
212         prefsn = prefsn_fld($('prefsn'));
213         prefsn.json2dom(prefsn_in);
214         prefsn.visible(true);
215     }
216     else {
217         var noti = new notify(gst, prefsn_in.mesg, 0, mlang_prefsn['btn_close'][g_lang], 400, 150);
218     }
219 }
220
221 function prefsn_reset()
222 {
223     Fieldify.reset($('prefsn'));
224 }
225
226
227
228
229
230
231
232 function client_prefs(old)
233 {
234     if (old != null) {
235         this.listen    = old.listen;
236         this.supp_comp = old.supp_comp;
237     }
238 }
239
240 client_prefs.prototype = {
241     listen:    -1,
242     supp_comp: 'ff00ffff00'
243 }
244
245 function prefs_assign(content)
246 {
247     var prefs_new;
248     var s;
249
250     s = "prefs_new = " + content;
251     eval(s);
252
253     return (prefs_new);
254 }
255
256 function prefs_apply(prefs_new, is_update, is_volat)
257 {
258     var i;
259     var relo = false;
260
261     if (typeof(g_prefs) == 'undefined')
262         return false;
263     /* listen management */
264     if (g_prefs.listen != prefs_new.listen || is_update) {
265         for (i = 0 ; i < l_prefs_list_idx.length ; i++) {
266             set_checked_value($('ra_listen_'+l_prefs_list_id[i]), prefs_new.listen);
267             if (prefs_new.listen == l_prefs_list_idx[i]) {
268                 if (!is_volat)
269                     $('list_'+l_prefs_list_id[i]).style.color = 'red';
270                 $('list_info').innerHTML = mlang_prefsn['tit_list'][i][g_lang];
271             }
272             else {
273                 if (!is_volat)
274                     $('list_'+l_prefs_list_id[i]).style.color = 'black';
275             }
276         }
277
278         relo = true;
279     }
280
281     // supporter component management
282     if (g_prefs.supp_comp != prefs_new.supp_comp || is_update) {
283
284         for (i = 0 ; i < 6 ; i++) {
285             $(l_comps_name[i]).value = parseInt(prefs_new.supp_comp.substr(i*2,2), 16);
286         }
287         $('s_img').src = 'suprend.php?comp='+prefs_new.supp_comp;
288     }
289
290     if (relo || !is_update) {
291         for (i = g_tables_auth_n ; i < g_tables_n ; i++) {
292             if (i % 4 == 0) {
293                 $('tr_noauth'+i).style.display = (prefs_new.listen == l_list_isol ? 'none' : '');
294             }
295
296             $('td_noauth'+i).style.display = (prefs_new.listen == l_list_isol ? 'none' : '');
297         }
298         if (prefs_new.listen == l_list_isol) {
299             tra.hide_noauth();
300         }
301         else {
302             tra.show_noauth();
303         }
304
305         if (false) {
306             // ricalculation of standup area
307             if (standup_data_old != null) {
308                 standup_data = standup_data_old;
309                 standup_data_old = null;
310                 j_stand_cont(standup_data);
311             }
312         }
313     }
314
315     g_prefs.listen    = prefs_new.listen;
316     g_prefs.supp_comp = prefs_new.supp_comp;
317 }
318
319 function prefs_load(content, is_update, is_volat)
320 {
321     var prefs_new;
322
323     // console.log('prefs_load('+content+')');
324
325     if ((prefs_new = prefs_assign(content)) == null)
326         return false;
327
328     return prefs_apply(prefs_new, is_update, is_volat);
329 }
330
331 function prefs_save()
332 {
333     var ret;
334
335     if (typeof(g_prefs) == 'undefined')
336         return false;
337
338     ret = server_request('index_wr.php', sess, 'mesg', 'prefs|save','__POST__', 'prefs', JSON.stringify(g_prefs));
339
340     if (ret == 1) {
341         $('preferences').style.visibility = 'hidden';
342     }
343     else {
344         alert(ret);
345     }
346 }
347
348 function prefs_reset()
349 {
350     var ret;
351
352     ret = server_request('index_wr.php', sess, 'mesg', 'prefs|reset');
353 }
354
355 function prefs_update(field)
356 {
357     var i;
358     var prefs_new;
359     var relo = false;
360     // console.log("prefs_update("+field+")");
361
362     if (typeof(g_prefs) == 'undefined')
363         return false;
364
365     prefs_new = new client_prefs(g_prefs);
366
367     if (field == 'listen') {
368         /* listen management */
369         for (i = 0 ; i < l_prefs_list_idx.length ; i++) {
370             prefs_new.listen = get_checked_value($('ra_listen_'+l_prefs_list_id[i]));
371             if (prefs_new.listen != '')
372                 break;
373         }
374     }
375     else if (field == 'supp') {
376         for (i = 0 ; i < 6 ; i++) {
377             if (parseInt($(l_comps_name[i]).value) < 0 || parseInt($(l_comps_name[i]).value) > 255 ||
378                 isNaN(parseInt($(l_comps_name[i]).value))) {
379                 break;
380             }
381         }
382
383         if (i == 6) {
384             prefs_new.supp_comp = "";
385             for (i = 0 ; i < 6 ; i++) {
386                 prefs_new.supp_comp += dec2hex(parseInt($(l_comps_name[i]).value), 2);
387             }
388         }
389
390         // console.log("prefs_update:: i break "+i+" ["+prefs_new.supp_comp+"]");
391
392         for (i = 0 ; i < 6 ; i++) {
393             $(l_comps_name[i]).value = parseInt(prefs_new.supp_comp.substr(i*2, 2), 16);
394         }
395     }
396
397     /* from form to struct */
398     prefs_apply(prefs_new, true, true);
399 }
400
401 function list_set(what, is_update, info)
402 {
403     var i;
404     var relo = false;
405     var old_st = readCookie("CO_list");
406
407     if (what == 'auth') {
408         $('list_auth').style.color = 'red';
409         $('list_isol').style.color = 'black';
410         $('list_all').style.color = 'black';
411         if (old_st == 'isolation')
412             relo = true;
413         g_listen = l_list_auth;
414     }
415     else if (what == 'isolation') {
416         $('list_auth').style.color = 'black';
417         $('list_isol').style.color = 'red';
418         $('list_all').style.color = 'black';
419         if (old_st != 'isolation')
420             relo = true;
421         g_listen = l_list_isol;
422     }
423     else {
424         $('list_auth').style.color = 'black';
425         $('list_isol').style.color = 'black';
426         $('list_all').style.color = 'red';
427         if (old_st == 'isolation')
428             relo = true;
429         g_listen = l_list_all;
430     }
431
432     set_checked_value($('ra_listen_auth'), what);
433     set_checked_value($('ra_listen_isol'), what);
434     set_checked_value($('ra_listen_all'),  what);
435
436     $('list_info').innerHTML = info;
437     if (is_update) {
438         createCookie("CO_list", what, 24*365, cookiepath);
439     }
440
441
442     if (relo || !is_update) {
443         for (i = g_tables_auth_n ; i < g_tables_n ; i++) {
444
445             if (i % 4 == 0) {
446                 $('tr_noauth'+i).style.display = (what == 'isolation' ? 'none' : '');
447             }
448
449             $('td_noauth'+i).style.display = (what == 'isolation' ? 'none' : '');
450         }
451         if (what == 'isolation') {
452             tra.hide_noauth();
453         }
454         else {
455             tra.show_noauth();
456         }
457
458         if (false) {
459             // ricalculation of standup area
460             if (standup_data_old != null) {
461                 standup_data = standup_data_old;
462                 standup_data_old = null;
463                 j_stand_cont(standup_data);
464             }
465         }
466     }
467 }
468
469 var g_prefsf = null;
470 function g_prefsf_listen_cb(item)
471 {
472     var whats = [ 'all', 'auth', 'isolation' ];
473     var what = whats[item.value];
474
475     console.log("change listen", item.value);
476
477     $('list_info').innerHTML = mlang_prefsf.tit_list[item.value][g_lang];
478
479     for (i = g_tables_cert_n + g_tables_auth_n ; i < g_tables_n ; i++) {
480         if (i % 4 == 0) {
481             $('tr_noauth'+i).style.display = (what == 'isolation' ? 'none' : '');
482         }
483         $('td_noauth'+i).style.display = (what == 'isolation' ? 'none' : '');
484     }
485     if (what == 'isolation') {
486         tra.hide_noauth();
487     }
488     else {
489         tra.show_noauth();
490     }
491 }
492
493 function g_prefsf_comps_cb(item, type)
494 {
495     console.log(item);
496     console.log(g_prefsf);
497
498     var col, ct = 0, s = "";
499     var col_arr = [ 'fg', 'bg' ];
500     var cmp_arr = [ 'r', 'g', 'b' ];
501     for (col in col_arr) {
502         for (cmp in cmp_arr) {
503             console.log(g_prefsf.bsk_comps[col_arr[col]][cmp_arr[cmp]].value);
504             var val_s = g_prefsf.bsk_comps[col_arr[col]][cmp_arr[cmp]].value;
505             if (parseInt(val_s) < 0 || parseInt(val_s) > 255 || isNaN(parseInt(val_s)))
506                 return false;
507             s += dec2hex(parseInt(val_s), 2);
508         }
509         console.log("sub: [" + s + "]");
510     }
511     $('s_img').src = 'suprend.php?comp=' + s;
512 }
513
514 function prefsf_status_cb(st)
515 {
516     // console.log('status_show HERE ' + st);
517     switch(st) {
518     case Fieldify.STATUS_OK:
519         $('prefsf-status-flag').src = 'img/light-green.png';
520         break;
521     case Fieldify.STATUS_PROC:
522         $('prefsf-status-flag').src = 'img/light-orange.png';
523         break;
524     default:  // Fieldify.STATUS_ERR
525         $('prefsf-status-flag').src = 'img/light-red.png';
526         break;
527     }
528 }
529
530 function prefsf_initialize()
531 {
532     clie_cb = { 'listen': { cb: g_prefsf_listen_cb, type: "fast" },
533                 'fg': { 'r': { cb: g_prefsf_comps_cb, type: "fast"},
534                         'g': { cb: g_prefsf_comps_cb, type: "fast"},
535                         'b': { cb: g_prefsf_comps_cb, type: "fast"}},
536                 'bg': { 'r': { cb: g_prefsf_comps_cb, type: "fast"},
537                         'g': { cb: g_prefsf_comps_cb, type: "fast"},
538                         'b': { cb: g_prefsf_comps_cb, type: "fast"}}};
539     g_prefsf = new Fieldify(null, null, null, clie_cb, "prefsf", [$("prefsf")], prefsf_status_cb);
540     g_prefsf.bsk_comps = { 'bg': { 'r': $C('bg_r') != undefined ? $C('bg_r')[0] : null,
541                                    'g': $C('bg_g') != undefined ? $C('bg_g')[0] : null,
542                                    'b': $C('bg_b') != undefined ? $C('bg_b')[0] : null },
543                            'fg': { 'r': $C('fg_r') != undefined ? $C('fg_r')[0] : null,
544                                    'g': $C('fg_g') != undefined ? $C('fg_g')[0] : null,
545                                    'b': $C('fg_b') != undefined ? $C('fg_b')[0] : null} };
546 }