main.js 6.6 KB

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