main.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. var navigationMenuEdited = false;
  2. //CHARGEMENT DE LA PAGE SETTING
  3. function init_setting_global_navigation(){
  4. window.onbeforeunload = function(e){
  5. if(!navigationMenuEdited) return e = null;
  6. return "Certaines modifications sur le menu n'ont pas été sauvgardées.\nÊtes-vous sûr de vouloir quitter la page ?";
  7. };
  8. navigation_menu_setting_search();
  9. }
  10. function navigation_menu_setting_search(){
  11. navigation_tree_search();
  12. $( ".navigation-tree-list" ).sortable({
  13. handle: ".navigation-item-header",
  14. cancel: ".not-editable",
  15. helper: "clone",
  16. opacity: 0.8,
  17. distance : 15,
  18. grid: [ 50, 1 ],
  19. update : function(){
  20. navigationMenuEdited = true;
  21. },
  22. start: function(e,ui) {
  23. draggedItem = $(ui.item[0]);
  24. if(draggedItem.hasClass('navigation-line-child')) return;
  25. children = [];
  26. var borderReached = false;
  27. $.each(draggedItem.nextAll('li.navigation-line'), function(i, childLine){
  28. var line = $(childLine);
  29. if(borderReached || !line.hasClass('navigation-line-child')) {
  30. borderReached = true;
  31. return;
  32. }
  33. children.push(line);
  34. line.addClass('hidden');
  35. });
  36. },
  37. sort: function(event,ui){
  38. var leftLimit = ui.item.hasClass('navigation-line-child') ? -20 : 15;
  39. if(ui.position.left < leftLimit){
  40. ui.helper.css('left',leftLimit+'px');
  41. }
  42. if(ui.position.left > 65){
  43. left = 65;
  44. ui.helper.css('left',left+'px');
  45. }
  46. if(ui.item.index()==1){
  47. ui.helper.css('left',leftLimit+'px');
  48. }
  49. },
  50. stop: function(event,ui){
  51. if(ui.position.left>15){
  52. if(!ui.item.hasClass('navigation-line-child')) navigationMenuEdited = true;
  53. ui.item.addClass('navigation-line-child');
  54. }else{
  55. if(ui.item.hasClass('navigation-line-child')) navigationMenuEdited = true;
  56. ui.item.removeClass('navigation-line-child');
  57. }
  58. if(!children.length) return;
  59. $.each(children.reverse(), function(i, row){
  60. $(row).insertAfter(draggedItem).removeClass('hidden');
  61. });
  62. },
  63. // placeholder: "navigation-item-placeholder",
  64. placeholder: {
  65. element: function(clone, ui) {
  66. var placeholderRow = $('<li class="navigation-item-placeholder"></li>');
  67. /** Lié à la feature de drag&drop de plusieurs lignes à la fois **/
  68. if(!clone.hasClass('navigation-line-child')){
  69. var rowNb = 1;
  70. var borderReached = false;
  71. $.each(clone.nextAll('li.navigation-line'), function(i, childLine){
  72. var line = $(childLine);
  73. if(borderReached || !line.hasClass('navigation-line-child')) {
  74. borderReached = true;
  75. return;
  76. }
  77. rowNb += 1
  78. });
  79. placeholderRow.css({
  80. height: 'calc(44px * '+rowNb+')',
  81. });
  82. }
  83. return placeholderRow;
  84. },
  85. update: function() {
  86. return;
  87. }
  88. }
  89. });
  90. $(".navigation-tree > ul").disableSelection();
  91. }
  92. /** Éditeur d'arborescence **/
  93. //Enregistrement des configurations
  94. function navigation_setting_save(){
  95. if(isProcessing) return;
  96. var items = [];
  97. var lastParentIndex = 0;
  98. $('.navigation-line:not(.hidden)').each(function(i,element){
  99. var line = $(element);
  100. var item = {
  101. id: line.attr('data-id'),
  102. label: line.find('.item-label').val(),
  103. visibility: line.find('input.item-visibility').val(),
  104. url: line.find('.item-url').val(),
  105. icon: line.find('.item-icon').val(),
  106. target: line.find('[name="item-target"]:checked').val(),
  107. };
  108. if(line.hasClass('navigation-line-child')){
  109. items[lastParentIndex].childs.push(item);
  110. }else{
  111. lastParentIndex = items.length;
  112. item.childs = [];
  113. items.push(item);
  114. }
  115. });
  116. isProcessing = true;
  117. $.action({
  118. action: 'navigation_setting_save',
  119. fields: $('#navigation-setting-form').toJson(),
  120. items: items,
  121. firm: $('#navigation-firm').val(),
  122. menu: $('#navigation-menu').val()
  123. },function(){
  124. isProcessing = false;
  125. navigationMenuEdited = false;
  126. $.message('success','Enregistré');
  127. navigation_tree_search();
  128. $('#navbarCollapse').load(document.URL+' #navbarCollapse > *');
  129. },function(){
  130. isProcessing = false;
  131. });
  132. }
  133. function navigation_tree_search(callback){
  134. var menu = $('#navigation-menu').val();
  135. if(!menu || menu=='' || isProcessing) return;
  136. isProcessing = true;
  137. $.action({
  138. action: 'navigation_tree_search',
  139. firm: $('#navigation-firm').val(),
  140. menu: menu
  141. },function(r){
  142. $('.navigation-tree > ul > li:visible').remove();
  143. for(var key in r.rows){
  144. var line = r.rows[key];
  145. navigation_tree_add(line);
  146. if(line.childs){
  147. for(var key2 in line.childs){
  148. navigation_tree_add(line.childs[key2]);
  149. }
  150. }
  151. }
  152. isProcessing = false;
  153. },function(r){
  154. isProcessing = false;
  155. });
  156. }
  157. function navigation_tree_new(element){
  158. var data = {
  159. label: 'Sans titre',
  160. icon: 'far fa-bookmark',
  161. url: '',
  162. shortcut: $('#navigation-menu>option:selected').attr('data-slug') == 'shortcut-menu',
  163. target: {slug:'redirect'}
  164. };
  165. if(element){
  166. data = $.extend(data, $(element).closest('.list-group-item').data());
  167. $.message('info','Lien "'+data.label+'" ajouté au menu');
  168. }
  169. var line = navigation_tree_add(data);
  170. navigation_tree_toggle(line.find('.navigation-item-toggle'));
  171. }
  172. function navigation_tree_add(data){
  173. var tpl = $('.navigation-line:hidden').get(0).outerHTML;
  174. if(data.visibility==null) data.visibility = $('.item-visibility',tpl).attr('data-default');
  175. tpl = tpl.replace('data-type="user-tpl"','data-type="user"');
  176. var line = $(Mustache.render(tpl,data));
  177. line.removeClass('hidden');
  178. $('.navigation-tree > ul').append(line);
  179. line.find('[name="item-target"][value="'+data.target.slug+'"]').prop('checked',true);
  180. line.off('keyup','.item-label').on('keyup','.item-label', function(){
  181. navigationMenuEdited = true;
  182. line.find('.navigation-item-label').text($(this).val());
  183. });
  184. line.off('keyup','.item-url').on('keyup','.item-url', function(){
  185. navigationMenuEdited = true;
  186. line.find('.navigation-item-url').text($(this).val());
  187. });
  188. line.off('click','[name="item-target"]').on('click','[name="item-target"]', function(){
  189. navigationMenuEdited = true;
  190. line.find('.navigation-item-type span').text($(this).parent().text());
  191. });
  192. line.off('change','.item-icon').on('change','.item-icon', function(){
  193. navigationMenuEdited = true;
  194. line.find('.navigation-item-icon i').attr('class',$(this).val());
  195. });
  196. return line;
  197. }
  198. function navigation_tree_toggle(element){
  199. var toggler = $(element).find('i');
  200. var line = toggler.closest('.navigation-line');
  201. toggler.toggleClass('fa-angle-right fa-angle-down');
  202. $('.navigation-item-form',line).toggleClass('hidden');
  203. init_components(line);
  204. }
  205. function navigation_tree_remove(element){
  206. if(!confirm('Êtes-vous sûr de vouloir supprimer cet élement de menu ?')) return;
  207. navigationMenuEdited = true;
  208. $(element).closest('.navigation-line').remove()
  209. }