123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- /*
- * 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 3 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
- MERCHANTABILITY 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, see <http://www.gnu.org/licenses/>.
- */
- var cat_count=0;
- var cat_active=-1;
- var info_cats=[];
- var tmpids;
- var kv_count=0;
- function print()
- {
- var x = "[";
- var i;
- for(i=0; i<info_cats.length; i++)
- {
- x+=" "+info_cats[i].id;
- }
- console.log(x+" ]");
- }
- function info_open(list)
- {
- var i;
- tmpids=0;
- info_cats=[];
- $('#input_1_cat_list').empty();
- for(i=0; i<list.length; i++)
- info_cat_add(i, list[i]);
-
- $('#input_1_root').hide();
- $("#input_1_cat_list a").removeClass("active");
- cat_active=-1;
- }
- function info_get_active_cat_index(cat)
- {
- if(cat<0) return -1;
- var i;
- for(i=0; i<info_cats.length; i++)
- {
- if(info_cats[i].id==cat)
- {
- return i;
- }
- }
- }
- function info_cat_active(x)
- {
- var y = false;
- $("#input_1_cat_list a").removeClass("active");
- $("#input_1_cat_"+x).addClass('active');
- if(cat_active==-1) y=true;
- cat_active=x;
- info_kv_show();
- $('#input_1_root').hide();
- $('#input_1_root').show(400);
- }
- function info_cat_up()
- {
- if(cat_active<0) return 0;
- var i;
- for(i=1; i<info_cats.length; i++)
- {
- if(info_cats[i].id==cat_active)
- {
- var x = $('#input_1_cat_'+info_cats[i].id);
- x.remove();
- x.insertBefore('#input_1_cat_'+info_cats[i-1].id);
-
-
- var a = info_cats[i];
- info_cats[i]=info_cats[i-1];
- info_cats[i-1]=a;
-
- break;
- }
- }
- print();
- }
- function info_cat_down()
- {
- if(cat_active<0) return 0;
- var i;
- for(i=0; i<info_cats.length-1; i++)
- {
- if(info_cats[i].id==cat_active)
- {
-
- var x = $('#input_1_cat_'+info_cats[i].id);
- x.remove();
- x.insertAfter('#input_1_cat_'+info_cats[i+1].id);
-
-
- var a = info_cats[i];
- info_cats[i]=info_cats[i+1];
- info_cats[i+1]=a;
-
- break;
- }
- }
- print();
- }
- function info_cat_del()
- {
- var i;
- if(cat_active<0) return 0;
- for(i=0; i<info_cats.length; i++)
- {
- if(info_cats[i].id==cat_active)
- {
- $('#input_1_cat_'+info_cats[i].id).remove();
- info_cats.splice(i, 1);
- break;
- }
- }
-
-
- cat_active=-1;
- $('#input_1_root').hide(400);
- print();
- }
- function info_cat_add(i, obj)
- {
- obj = (typeof obj === 'undefined') ? null : obj;
- i = (typeof i === 'undefined') ? -1 : i;
-
- cat_count++;
-
- var n;
- var val;
- var text;
- if(obj==null)
- {
- n=cat_count;
- text=$('#input_1_cat').val();
- val=[];
- }
- else
- {
- n=i;
- text=obj.title;
- val=obj.content;
- var j;
- for(j=0; j<val.length; j++)
- {
- val[j].unshift(tmpids++);
- }
- }
-
- var o = $('<a onclick="info_cat_active('+n+')" class="list-group-item" id="input_1_cat_'+n+'"></a>');
- o.append($('<h4 class="list-group-item-heading">'+text+'</h4>'));
- info_cats.push({ "title" : text, "id" : n, "content" : val});
-
- $('#input_1_cat_list').append(o);
- $('#input_1_cat').val("");
- }
- function info_cat_rename()
- {
- if(cat_active<0) return;
- var cat = info_get_active_cat_index(cat_active);
- var text = $('#input_1_cat_rename').val();
- $("#input_1_cat_"+cat_active).html($('<h4 class="list-group-item-heading">'+text+'</h4>'));
- info_cats[cat].title=text;
- }
- // [index, key, value]
- function info_get_kv_index(icat, kv)
- {
- if(icat<0) return;
- var i;
- for( i=0; i<info_cats[icat].content.length; i++)
- if(info_cats[icat].content[i][0]==kv)
- return i;
- return -1;
- }
- function info_kv_valid(index)
- {
- if(cat_active<0) return;
- var cat = info_get_active_cat_index(cat_active);
- var kv = info_get_kv_index(cat, index);
- var isValid = $('#input_1_kv_valid_'+index).hasClass('glyphicon-ok');
- var key = isValid ? $('#input_1_key_'+index).val() : $('#input_1_key_'+index).html();
- var value = isValid ? $('#input_1_value_'+index).val() : $('#input_1_value_'+index).html();
- var kp = $('#input_1_key_'+index).parent();
- var vp = $('#input_1_value_'+index).parent();
-
- kp.empty();
- vp.empty();
-
- if(isValid)
- {
- if(kv<0)
- {
- info_cats[cat].content.push([index,
- key,
- value]);
-
- }else
- {
- info_cats[cat].content[kv]=[index,
- key,
- value] ;
- }
-
-
- kp.append($('<span><h4 class="col-md-2" id="input_1_key_'+index+'" >'+key+'</h4></span>'));
- vp.append($('<span><h4 class="col-md-8" id="input_1_value_'+index+'">'+value+'</h4></span>'));
-
- $('#input_1_kv_valid_'+index).removeClass('glyphicon-ok');
- $('#input_1_kv_valid_'+index).addClass('glyphicon-pencil');
- }else
- {
- kp.append($('<span><input text="text" class="col-md-2" id="input_1_key_'+index+'" value="'+key+'" placeholder="Intitulé"></input></span>'));
- vp.append($('<span><input text="text" class="col-md-8" id="input_1_value_'+index+'" value="'+value+'" placeholder="Description (facultatif)"></input></span>'));
-
-
- $('#input_1_kv_valid_'+index).removeClass('glyphicon-pencil');
- $('#input_1_kv_valid_'+index).addClass('glyphicon-ok');
- }
-
-
- console.log("KV Valid: val='"+JSON.stringify(info_cats[cat].content)+"'");
-
-
- }
- function info_kv_del(index)
- {
- if(cat_active<0) return;
- var cat = info_get_active_cat_index(cat_active);
- var kv = info_get_kv_index(cat, index);
- if(kv<0)
- {
- $('#input_1_kv_'+index).remove();
- return;
- }
-
-
- $('#input_1_kv_'+info_cats[cat].content[kv][0]).remove();
- info_cats[cat].content.splice(kv, 1);
- }
- function info_kv_add(i, k, v)
- {
- i = (typeof i === 'undefined') ? -1 : i;
- k = (typeof k === 'undefined') ? '' : k;
- v = (typeof v === 'undefined') ? '' : v;
- console.log("i="+i+", v="+k+", v="+v);
- if(cat_active<0) return;
- var nb;
-
- if(i<0) nb=(kv_count++);
- else
- {
- if(i>kv_count) kv_count=i;
- nb=i;
- }
- console.log("N="+nb);
- var disable=(i==-1)?"":"disabled=true";
- var o = $("<div id='input_1_kv_"+nb+"' class='row'></div>");
-
- if(i==-1)o.append($('<span><input text="text" class="col-md-2" id="input_1_key_'+nb+'" value="'+k+'" placeholder="Intitulé" '+disable+'></input></span>'));
- else o.append($('<span><h4 class="col-md-2" id="input_1_key_'+nb+'" >'+k+'</h4></span>'));
-
- if(i==-1)o.append($('<span><input text="text" class="col-md-8" id="input_1_value_'+nb+'" value="'+v+'" placeholder="Description (facultatif)" '+disable+'></input></span>'));
- else o.append($('<span><h4 class="col-md-8" id="input_1_value_'+nb+'">'+v+'</h4></span>'));
-
- if(i==-1) o.append($('<span><button class="col-md-1 btn btn-default glyphicon glyphicon-ok" onclick="info_kv_valid('+nb+')" id="input_1_kv_valid_'+nb+'"></button></span>'));
- else o.append($('<span><button class="col-md-1 btn btn-default glyphicon glyphicon-pencil" onclick="info_kv_valid('+nb+')" id="input_1_kv_valid_'+nb+'"></button></span>'));
- o.append($('<span><button class="col-md-1 btn btn-default glyphicon glyphicon-minus" onclick="info_kv_del('+nb+')"></button></span>'));
-
- $('#input_1_kv_list').append(o);
- }
- function info_kv_show()
- {
- var cat = info_get_active_cat_index(cat_active);
- var i;
- $('#input_1_kv_list').empty();
- $('#input_1_cat_rename').val($($("#input_1_cat_"+cat_active).html()).html());
- for(i=0; i<info_cats[cat].content.length; i++)
- {
- info_kv_add(info_cats[cat].content[i][0],info_cats[cat].content[i][1],info_cats[cat].content[i][2]);
- }
-
- }
- function get_cats()
- {
- var ic= JSON.parse(JSON.stringify(info_cats));
- var i,j;
- for(i=0; i< info_cats.length; i++)
- for(j=0; j<info_cats[i].content.length; j++)
- ic[i].content[j].splice(0,1);
-
- return ic;
- }
|