lastest modifies
[brisk.git] / web / prefs.js
index d2e3afc..eb664ed 100644 (file)
@@ -1,3 +1,46 @@
+/*
+ *  brisk - prefs.js
+ *
+ *  Copyright (C) 2015      Matteo Nastasi
+ *                          mailto: nastasi@alternativeoutput.it
+ *                                  matteo.nastasi@milug.org
+ *                          web: http://www.alternativeoutput.it
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details. You should have received a
+ * copy of the GNU General Public License along with this program; if
+ * not, write to the Free Software Foundation, Inc, 59 Temple Place -
+ * Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+var mlang_prefsf = { 'btn_close' : { 'it' : 'Chiudi.',
+                                     'en' : 'Close.' },
+                     'tit_list'  : { '0'  : { 'it' : '',
+                                              'en' : '' },
+                                     '1'  : { 'it' : '(solo aut.)',
+                                              'en' : '(only aut.)' },
+                                     '2'  : { 'it' : '(isolam.to)',
+                                              'en' : '(isolation)' } }
+                   };
+
+var mlang_prefsn = { 'btn_close' : { 'it' : 'Chiudi.',
+                                     'en' : 'Close.' },
+                     'tit_list'  : { '0'  : { 'it' : '',
+                                              'en' : '' },
+                                     '1'  : { 'it' : '(solo aut.)',
+                                              'en' : '(only aut.)' },
+                                     '2'  : { 'it' : '(isolam.to)',
+                                              'en' : '(isolation)' } }
+                   };
+
 var l_list_all  = 0x00;
 var l_list_auth = 0x01;
 var l_list_isol = 0x02;
@@ -5,6 +48,186 @@ var l_prefs_list_idx = new Array( 0x00, 0x01, 0x02 );
 var l_prefs_list_id  = new Array( "all", "auth", "isol" );
 var l_comps_name     = new Array('s_fg_r', 's_fg_g', 's_fg_b', 's_bg_r',  's_bg_g',  's_bg_b');
 
+/* FIXME: move to html page with dynamic valorization */
+var is_supp_custom = true;
+
+function Prefsn(obj)
+{
+    var fields = { listen: { type: 'radio', cb: Prefsn.listen_change_cb } };
+    if (is_supp_custom) {
+        fields.fg = {
+            type: 'fields', fields: { r: { type: 'value', cb: Prefsn.supp_change_cb },
+                                      g: { type: 'value', cb: Prefsn.supp_change_cb },
+                                      b: { type: 'value', cb: Prefsn.supp_change_cb }
+                                    }
+        };
+        fields.bg = {
+            type: 'fields', fields: { r: { type: 'value', cb: Prefsn.supp_change_cb },
+                                      g: { type: 'value', cb: Prefsn.supp_change_cb },
+                                      b: { type: 'value', cb: Prefsn.supp_change_cb }
+                                    }
+        };
+    }
+
+    this.fieldify = new Fieldify([obj], fields);
+    this.obj = obj;
+    this.data = null;
+}
+
+Prefsn.listen_change_cb = function(field)
+{
+    var i;
+    var prefs_new;
+    var relo = false;
+
+    console.log("listen_change_cb:" + field.value);
+    console.log("xx" + g_prefsn.data);
+
+    /* listen management */
+    if (g_prefsn.data == null || g_prefsn.data.listen != field.value) {
+        $('list_info').innerHTML = mlang_prefsn['tit_list'][field.value][g_lang];
+        console.log('update listen');
+        for (i = g_tables_cert_n + g_tables_auth_n ; i < g_tables_n ; i++) {
+            if (i % 4 == 0) {
+                $('tr_noauth'+i).style.display = (field.value == l_list_isol ? 'none' : '');
+            }
+
+            $('td_noauth'+i).style.display = (field.value == l_list_isol ? 'none' : '');
+        }
+        if (field.value == l_list_isol) {
+            tra.hide_noauth();
+        }
+        else {
+            tra.show_noauth();
+        }
+    }
+}
+
+function obj_clone(obj) // begin = {} or [] is optional
+{
+    var ret, ty, begin;
+
+    if (arguments.length > 1)
+        begin = arguments[1];
+    else
+        begin = {};
+
+    for (i in obj) {
+        ty = typeof(obj[i]);
+
+        if (ty == 'object') {
+            if (obj[i].constructor == Array) {
+                ret[i] = obj_clone(obj[i], []);
+            }
+            else {
+                ret[i] = obj_clone(obj[i], {});
+            }
+        }
+        else {
+            ret[i] = obj[i];
+        }
+    }
+    return ret;
+}
+
+function obj_update(cur, delta)
+{
+    for (i in cur) {
+        if (i in delta) {
+            ty = typeof(cur[i]);
+            if (ty == 'object') {
+                obj_update(cur[i], delta[i]);
+            }
+            else {
+                cur[i] = delta[i];
+            }
+        }
+    }
+}
+
+Prefsn.supp_change_cb = function(field)
+{
+    var i;
+    var supp_new;
+    var relo = false;
+
+    if (g_prefsn == null)
+        return false;
+
+    supp_new = Fieldify.item2tree([g_prefsn.obj], field);
+    supp_cur = {};
+    console.log(g_prefsn.data.fg);
+    supp_cur['fg'] = obj_clone(g_prefsn.data.fg);
+    supp_cur['bg'] = obj_clone(g_prefsn.data.bg);
+
+    for (i in ['fg', 'bg']) {
+        if (! i in supp_new) {
+            continue;
+        }
+        obj_update(supp_cur[i], supp_new[i]);
+    }
+
+    var col, ct = 0, s = "";
+    for (col in [ 'fg', 'bg' ]) {
+        for (cmp in [ 'r', 'g', 'b' ]) {
+            var val_s = supp_cur[col][cmp];
+            if (parseInt(val_s) < 0 || parseInt(val_s) > 255 || isNaN(parseInt(val_s)))
+                return false;
+        }
+        s += dec2hex(parseInt(val_s), 2);
+    }
+    $('s_img').src = 'suprend.php?comp=' + s;
+};
+
+
+
+Prefsn.prototype = {
+    obj: null,
+    fieldify: null,
+    data: null,
+
+    load: function(data) {
+        var in_data = JSON.parse(data);
+        this.fieldify.json2dom(in_data);
+        this.data = in_data;
+    },
+
+    reset: function() {
+        console.log('here');
+        Fieldify.reset(this.obj);
+    },
+
+    show: function() {
+        this.fieldify.visible(true);
+    }
+}
+
+function prefsn_show(username)
+{
+    var prefsn_in_in = server_request('index_wr.php', sess, 'mesg', 'chatt|/prefsn');
+    var prefsn_in = JSON.parse(prefsn_in_in);
+    var prefsn = null;
+
+    if (prefsn_in.ret == 0) {
+        prefsn = prefsn_fld($('prefsn'));
+        prefsn.json2dom(prefsn_in);
+        prefsn.visible(true);
+    }
+    else {
+        var noti = new notify(gst, prefsn_in.mesg, 0, mlang_prefsn['btn_close'][g_lang], 400, 150);
+    }
+}
+
+function prefsn_reset()
+{
+    Fieldify.reset($('prefsn'));
+}
+
+
+
+
+
+
 
 function client_prefs(old)
 {
@@ -44,7 +267,7 @@ function prefs_apply(prefs_new, is_update, is_volat)
             if (prefs_new.listen == l_prefs_list_idx[i]) {
                 if (!is_volat)
                     $('list_'+l_prefs_list_id[i]).style.color = 'red';
-                $('list_info').innerHTML = mlang_commons['tit_list'][i][g_lang];
+                $('list_info').innerHTML = mlang_prefsn['tit_list'][i][g_lang];
             }
             else {
                 if (!is_volat)
@@ -180,7 +403,7 @@ function list_set(what, is_update, info)
     var i;
     var relo = false;
     var old_st = readCookie("CO_list");
-    
+
     if (what == 'auth') {
         $('list_auth').style.color = 'red';
         $('list_isol').style.color = 'black';
@@ -218,11 +441,11 @@ function list_set(what, is_update, info)
 
     if (relo || !is_update) {
         for (i = g_tables_auth_n ; i < g_tables_n ; i++) {
-            
+
             if (i % 4 == 0) {
                 $('tr_noauth'+i).style.display = (what == 'isolation' ? 'none' : '');
             }
-            
+
             $('td_noauth'+i).style.display = (what == 'isolation' ? 'none' : '');
         }
         if (what == 'isolation') {
@@ -231,7 +454,7 @@ function list_set(what, is_update, info)
         else {
             tra.show_noauth();
         }
-            
+
         if (false) {
             // ricalculation of standup area
             if (standup_data_old != null) {
@@ -242,3 +465,82 @@ function list_set(what, is_update, info)
         }
     }
 }
+
+var g_prefsf = null;
+function g_prefsf_listen_cb(item)
+{
+    var whats = [ 'all', 'auth', 'isolation' ];
+    var what = whats[item.value];
+
+    console.log("change listen", item.value);
+
+    $('list_info').innerHTML = mlang_prefsf.tit_list[item.value][g_lang];
+
+    for (i = g_tables_cert_n + g_tables_auth_n ; i < g_tables_n ; i++) {
+        if (i % 4 == 0) {
+            $('tr_noauth'+i).style.display = (what == 'isolation' ? 'none' : '');
+        }
+        $('td_noauth'+i).style.display = (what == 'isolation' ? 'none' : '');
+    }
+    if (what == 'isolation') {
+        tra.hide_noauth();
+    }
+    else {
+        tra.show_noauth();
+    }
+}
+
+function g_prefsf_comps_cb(item, type)
+{
+    console.log(item);
+    console.log(g_prefsf);
+
+    var col, ct = 0, s = "";
+    var col_arr = [ 'fg', 'bg' ];
+    var cmp_arr = [ 'r', 'g', 'b' ];
+    for (col in col_arr) {
+        for (cmp in cmp_arr) {
+            console.log(g_prefsf.bsk_comps[col_arr[col]][cmp_arr[cmp]].value);
+            var val_s = g_prefsf.bsk_comps[col_arr[col]][cmp_arr[cmp]].value;
+            if (parseInt(val_s) < 0 || parseInt(val_s) > 255 || isNaN(parseInt(val_s)))
+                return false;
+            s += dec2hex(parseInt(val_s), 2);
+        }
+        console.log("sub: [" + s + "]");
+    }
+    $('s_img').src = 'suprend.php?comp=' + s;
+}
+
+function prefsf_status_cb(st)
+{
+    // console.log('status_show HERE ' + st);
+    switch(st) {
+    case Fieldify.STATUS_OK:
+        $('prefsf-status-flag').src = 'img/light-green.png';
+        break;
+    case Fieldify.STATUS_PROC:
+        $('prefsf-status-flag').src = 'img/light-orange.png';
+        break;
+    default:  // Fieldify.STATUS_ERR
+        $('prefsf-status-flag').src = 'img/light-red.png';
+        break;
+    }
+}
+
+function prefsf_initialize()
+{
+    clie_cb = { 'listen': { cb: g_prefsf_listen_cb, type: "fast" },
+                'fg': { 'r': { cb: g_prefsf_comps_cb, type: "fast"},
+                        'g': { cb: g_prefsf_comps_cb, type: "fast"},
+                        'b': { cb: g_prefsf_comps_cb, type: "fast"}},
+                'bg': { 'r': { cb: g_prefsf_comps_cb, type: "fast"},
+                        'g': { cb: g_prefsf_comps_cb, type: "fast"},
+                        'b': { cb: g_prefsf_comps_cb, type: "fast"}}};
+    g_prefsf = new Fieldify(null, null, null, clie_cb, "prefsf", [$("prefsf")], prefsf_status_cb);
+    g_prefsf.bsk_comps = { 'bg': { 'r': $C('bg_r') != undefined ? $C('bg_r')[0] : null,
+                                   'g': $C('bg_g') != undefined ? $C('bg_g')[0] : null,
+                                   'b': $C('bg_b') != undefined ? $C('bg_b')[0] : null },
+                           'fg': { 'r': $C('fg_r') != undefined ? $C('fg_r')[0] : null,
+                                   'g': $C('fg_g') != undefined ? $C('fg_g')[0] : null,
+                                   'b': $C('fg_b') != undefined ? $C('fg_b')[0] : null} };
+}