content.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. var sect_count=0;
  14. var sect_active=-1;
  15. var cont_sect=[];
  16. var sect_count=0;
  17. var itm_count=0;
  18. var itm_active=-1;
  19. var cont_tmpids=0;
  20. var itm_tmpids=0;
  21. // ------------------------------------------------
  22. // Sections
  23. // ------------------------------------------------
  24. function cont_get_sect_index(id)
  25. {
  26. var i;
  27. for(i=0; i<cont_sect.length; i++)
  28. if(cont_sect[i].id == id)
  29. return i;
  30. return 0;
  31. }
  32. function cont_sect_show()
  33. {
  34. var sect = cont_get_sect_index(sect_active);
  35. var i;
  36. $('#input_2_sect_rename').val(cont_sect[sect].title);
  37. $('#input_2_itm_list').empty();
  38. for(i=0; i<cont_sect[sect].sections.length; i++)
  39. {
  40. cont_itm_add(cont_sect[sect].sections[i]);
  41. }
  42. }
  43. function cont_sect_select(x)
  44. {
  45. var y = false;
  46. if(x==sect_active) return;
  47. $("#input_2_sect_list a").removeClass("active");
  48. $("#input_2_sect_"+x).addClass('active');
  49. if(sect_active==-1) y=true;
  50. sect_active=x;
  51. cont_sect_show();
  52. $('#input_2_content_list').hide();
  53. $('#input_2_itm_root').hide();
  54. $('#input_2_itm_root').show(400);
  55. }
  56. function cont_sect_up()
  57. {
  58. if(sect_active<0) return 0;
  59. var i;
  60. for(i=1; i<cont_sect.length; i++)
  61. {
  62. if(cont_sect[i].id==sect_active)
  63. {
  64. var x = $('#input_2_sect_'+cont_sect[i].id);
  65. x.remove();
  66. x.insertBefore('#input_2_sect_'+cont_sect[i-1].id);
  67. var a = cont_sect[i];
  68. cont_sect[i]=cont_sect[i-1];
  69. cont_sect[i-1]=a;
  70. break;
  71. }
  72. }
  73. }
  74. function cont_sect_down()
  75. {
  76. if(sect_active<0) return 0;
  77. var i;
  78. for(i=0; i<cont_sect.length-1; i++)
  79. {
  80. if(cont_sect[i].id==sect_active)
  81. {
  82. var x = $('#input_2_sect_'+cont_sect[i].id);
  83. x.remove();
  84. x.insertAfter('#input_2_sect_'+cont_sect[i+1].id);
  85. var a = cont_sect[i];
  86. cont_sect[i]=cont_sect[i+1];
  87. cont_sect[i+1]=a;
  88. break;
  89. }
  90. }
  91. }
  92. function cont_sect_del()
  93. {
  94. if(sect_count<0) return;
  95. var x = cont_get_sect_index(sect_active);
  96. $('#input_2_sect_'+sect_active).remove();
  97. cont_sect.splice(x, 1);
  98. sect_active=-1;
  99. $('#input_2_itm_root').hide(400);
  100. $('#input_2_content_list').hide(400);
  101. }
  102. function cont_sect_add(obj)
  103. {
  104. obj = (typeof obj === 'undefined') ? null : obj;
  105. sect_count++;
  106. var id;
  107. if(obj==null)
  108. {
  109. id=sect_count;
  110. text=$("#input_2_sect").val();
  111. cont_sect.push({ "id" : id, "title" : $("#input_2_sect").val(), sections : [] });
  112. }else
  113. {
  114. id=cont_tmpids++;
  115. text=obj.title;
  116. obj.id=id;
  117. }
  118. var o = $('<a class="list-group-item" onclick="cont_sect_select('+id+')" id="input_2_sect_'+id+'">'+ text +'</a>');
  119. $('#input_2_sect_list').append(o);
  120. $("#input_2_sect").val("");
  121. }
  122. function cont_sect_rename()
  123. {
  124. console.log(JSON.stringify(cont_sect));
  125. var i = cont_get_sect_index(sect_active);
  126. $("#input_2_sect_"+sect_active).html($('#input_2_sect_rename').val());
  127. cont_sect[i].title=$('#input_2_sect_rename').val();
  128. console.log(JSON.stringify(cont_sect));
  129. console.log("\n\n");
  130. }
  131. // ------------------------------------------------
  132. // Items
  133. // ------------------------------------------------
  134. function cont_get_itm_index(sect, id)
  135. {
  136. var i;
  137. for(i=0; i<cont_sect[sect].sections.length; i++)
  138. if(cont_sect[sect].sections[i].id == id)
  139. return i;
  140. return 0;
  141. }
  142. function cont_itm_show()
  143. {
  144. var sect = cont_get_sect_index(sect_active);
  145. var itm = cont_get_itm_index(sect, itm_active);
  146. var i;
  147. $('#input_2_itm_rename').val(cont_sect[sect].title);
  148. $("#input_2_itm_rename").val(cont_sect[sect].sections[itm].name);
  149. $("#input_2_itm_title").val(cont_sect[sect].sections[itm].content);
  150. $("#input_2_itm_date").val(cont_sect[sect].sections[itm].date);
  151. $("#input_2_itm_location").val(cont_sect[sect].sections[itm].location);
  152. $("#input_2_itm_more").val(cont_sect[sect].sections[itm].more);
  153. $('#input_2_itm_profils_list').empty();
  154. var reg = get_regs();
  155. for(i=0; i<reg.profiles.length; i++)
  156. {
  157. var state = "";
  158. var j;
  159. if(cont_sect[sect].sections[itm].profiles)
  160. for(j=0; j<cont_sect[sect].sections[itm].profiles.length; j++)
  161. {
  162. if(cont_sect[sect].sections[itm].profiles[j] == reg.profiles[i][0])
  163. {
  164. state="checked='true'";
  165. break;
  166. }
  167. }
  168. $('#input_2_itm_profils_list').append($('<div><label class="btn btn-primary profiles-btn"><input class="profiles-ck" '+state+' autocomplete="off" id="input_2_prof_'+reg.profiles[i][0]
  169. +'" type="checkbox">'+reg.profiles[i][1]+'</label></div>'));
  170. }
  171. }
  172. function cont_itm_select(x)
  173. {
  174. var y = false;
  175. if(x==itm_active) return;
  176. $("#input_2_itm_list a").removeClass("active");
  177. $("#input_2_itm_"+x).addClass('active');
  178. if(itm_active==-1) y=true;
  179. itm_active=x;
  180. cont_itm_show();
  181. $('#input_2_content_list').hide();
  182. $('#input_2_content_list').show(400);
  183. }
  184. function cont_itm_up()
  185. {
  186. if(itm_active<0) return 0;
  187. var i;
  188. var sect = cont_get_sect_index(itm_active);
  189. console.log(JSON.stringify(cont_sect));
  190. for(i=1; i<cont_sect[sect].sections.length; i++)
  191. {
  192. if(cont_sect[sect].sections[i].id==itm_active)
  193. {
  194. var x = $('#input_2_itm_'+cont_sect[sect].sections[i].id);
  195. x.remove();
  196. x.insertBefore('#input_2_itm_'+cont_sect[sect].sections[i-1].id);
  197. var a = cont_sect[sect].sections[i];
  198. cont_sect[sect].sections[i]=cont_sect[sect].sections[i-1];
  199. cont_sect[sect].sections[i-1]=a;
  200. break;
  201. }
  202. }
  203. }
  204. function cont_sect_down()
  205. {
  206. if(sect_active<0) return 0;
  207. var i;
  208. for(i=0; i<cont_sect.length-1; i++)
  209. {
  210. if(cont_sect[i].id==sect_active)
  211. {
  212. var x = $('#input_2_sect_'+cont_sect[i].id);
  213. x.remove();
  214. x.insertAfter('#input_2_sect_'+cont_sect[i+1].id);
  215. var a = cont_sect[i];
  216. cont_sect[i]=cont_sect[i+1];
  217. cont_sect[i+1]=a;
  218. break;
  219. }
  220. }
  221. }
  222. function cont_itm_del()
  223. {
  224. if(itm_active<0) return;
  225. var x = cont_get_sect_index(sect_active);
  226. var y = cont_get_sect_index(x, itm_active);
  227. $('#input_2_itm_'+itm_active).remove();
  228. cont_sect[x].sections.splice(y, 1);
  229. itm_active=-1;
  230. $('#input_2_content_list').hide(400);
  231. }
  232. function cont_itm_add(obj)
  233. {
  234. obj = (typeof obj === 'undefined') ? null : obj;
  235. itm_count++;
  236. var id;
  237. if(obj==null)
  238. {
  239. id=itm_count;
  240. text=$("#input_2_itm").val();
  241. }else
  242. {
  243. id=itm_tmpids++;
  244. text=obj.name;
  245. obj.id=id;
  246. }
  247. var sect = cont_get_sect_index(sect_active);
  248. var o = $('<a class="list-group-item" onclick="cont_itm_select('+id+')" id="input_2_itm_'+id+'">'+ text+'</a>');
  249. $('#input_2_itm_list').append(o)
  250. if(obj==null) cont_sect[sect].sections.push({"id" : id, "name" :text, "content" : "", "date" : "", "more" : "", "location" : "", "date" : "", "profiles" : [] });
  251. $("#input_2_itm").val("");
  252. }
  253. // ---------------------------------
  254. // Contenu
  255. // ---------------------------------
  256. function cont_cont_rename()
  257. {
  258. var sect = cont_get_sect_index(sect_active);
  259. var itm = cont_get_itm_index(sect, itm_active);
  260. cont_sect[sect].sections[itm].name=$("#input_2_itm_rename").val();
  261. $("#input_2_itm_"+itm_active).html($("#input_2_itm_rename").val());
  262. }
  263. function cont_cont_save()
  264. {
  265. var sect = cont_get_sect_index(sect_active);
  266. var itm = cont_get_itm_index(sect, itm_active);
  267. var i;
  268. var reg = get_regs();
  269. cont_sect[sect].sections[itm].name=$("#input_2_itm_rename").val();
  270. cont_sect[sect].sections[itm].content=$("#input_2_itm_title").val();
  271. cont_sect[sect].sections[itm].date=$("#input_2_itm_date").val();
  272. cont_sect[sect].sections[itm].location=$("#input_2_itm_location").val();
  273. cont_sect[sect].sections[itm].more=$("#input_2_itm_more").val();
  274. $('#input_2_content_list').hide(400);
  275. $("#input_2_itm_list a").removeClass("active");
  276. cont_sect[sect].sections[itm].profiles=[];
  277. for(i=0; i<reg.profiles.length; i++)
  278. {
  279. if( $('#input_2_prof_'+reg.profiles[i][0]+":checked").val() == "on" )
  280. {
  281. cont_sect[sect].sections[itm].profiles.push(reg.profiles[i][0]);
  282. }
  283. }
  284. itm_active=-1;
  285. }
  286. function cont_open(main)
  287. {
  288. var i;
  289. cont_tmpids=0;
  290. itm_tmpids=1;
  291. $('#input_2_sect_list').empty();
  292. cont_sect=JSON.parse( JSON.stringify( main ));
  293. for(i=0; i<cont_sect.length; i++)
  294. cont_sect_add(cont_sect[i]);
  295. $('#input_2_itm_root').hide();
  296. $('#input_2_content_list').hide();
  297. $("#input_2_itm_list a").removeClass("active");
  298. $("#input_2_sect_list a").removeClass("active");
  299. sect_active=-1;
  300. }
  301. function get_conts()
  302. {
  303. return cont_sect;
  304. }