prefs refactoring: partial implementation (wip)
[brisk.git] / web / prefs.js
index d2e3afc..93b50c6 100644 (file)
@@ -1,3 +1,36 @@
+/*
+ *  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_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 +38,144 @@ 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' },
+                                      g: { type: 'value' },
+                                      b: { type: 'value' }
+                                    }
+        };
+        fields.bg = {
+            type: 'fields', fields: { r: { type: 'value' },
+                                      g: { type: 'value' },
+                                      b: { type: 'value' }
+                                    }
+        };
+    }
+
+    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_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();
+        }
+    }
+}
+
+Prefsn.supp_change_cb = function(field)
+{
+    var i;
+    var prefs_new;
+    var relo = false;
+    // console.log("prefs_update("+field+")");
+
+    if (typeof(g_prefsn) == 'undefined')
+        return false;
+
+    for (i = 0 ; i < 6 ; i++) {
+        if (parseInt($(l_comps_name[i]).value) < 0 || parseInt($(l_comps_name[i]).value) > 255 ||
+            isNaN(parseInt($(l_comps_name[i]).value))) {
+            break;
+        }
+    }
+
+    if (i == 6) {
+        prefs_new.supp_comp = "";
+        for (i = 0 ; i < 6 ; i++) {
+            prefs_new.supp_comp += dec2hex(parseInt($(l_comps_name[i]).value), 2);
+        }
+    }
+
+    // console.log("prefs_update:: i break "+i+" ["+prefs_new.supp_comp+"]");
+
+    for (i = 0 ; i < 6 ; i++) {
+        $(l_comps_name[i]).value = parseInt(prefs_new.supp_comp.substr(i*2, 2), 16);
+    }
+
+    /* from form to struct */
+    // prefs_apply(prefs_new, true, true);
+};
+
+
+
+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 +215,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 +351,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 +389,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 +402,7 @@ function list_set(what, is_update, info)
         else {
             tra.show_noauth();
         }
-            
+
         if (false) {
             // ricalculation of standup area
             if (standup_data_old != null) {