main.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //CHARGEMENT DE LA PAGE
  2. function init_plugin_factory(event){
  3. $('#factoryForm').on('keyup','input',factory_render);
  4. $('#factoryForm').on('keydown','input,select',factory_shortcut);
  5. $('#factoryForm').on('change','select',factory_render);
  6. $('#factoryParts').on('click','li a',function(){
  7. $('#factoryParts li a').removeClass('active');
  8. $(this).addClass('active');
  9. factory_render(event);
  10. });
  11. $('#factoryParts .nav-link.active').trigger('click');
  12. $('#plugin').autocomplete({
  13. action : 'factory_autocomplete_plugin',
  14. onClick : function(selected,element){
  15. $('#plugin').val(selected.name);
  16. console.log('click on '+selected.name);
  17. $.action({action:'factory_entity_search',plugin:selected.name},function(response){
  18. if(response.rows.length==0) return;
  19. var html = '<optgroup label="Entités">';
  20. for(var key in response.rows){
  21. html +='<option value="entity-'+response.rows[key]+'">'+response.rows[key]+'</option>';
  22. }
  23. html += '</optgroup>';
  24. $('.fieldType').append(html);
  25. });
  26. }
  27. });
  28. $('#entity').attr('autocomplete','off').typeahead({
  29. items: 5,
  30. minLength: 2,
  31. autoSelect : false,
  32. selectOnBlur : false,
  33. displayText : function(item){
  34. return item.name || item;
  35. },
  36. source: function(keyword, response){
  37. $.action({
  38. action: 'factory_autocomplete_entity',
  39. keyword: $('#entity').val(),
  40. plugin: $('#plugin').val()
  41. },function(r){
  42. if(r.rows != null)
  43. response(r.rows);
  44. });
  45. },
  46. matcher: function(r){
  47. return '<div>'+r.name+'</div>';
  48. },
  49. afterSelect: function(item) {
  50. $('#entity').data('selected',true);
  51. $.action({
  52. action: 'factory_autocomplete_entity_select',
  53. entity: item.name,
  54. plugin: $('#plugin').val()
  55. },function(r){
  56. $('#factoryForm .field:not(:eq(0))').remove();
  57. $('#factoryForm .field:eq(0) input').val(r.rows[0].label);
  58. $('#factoryForm .field:eq(0) select').val(r.rows[0].type);
  59. for(var k in r.rows){
  60. if(k==0) continue;
  61. factory_addline($('#factoryForm .field').eq(0),r.rows[k]);
  62. }
  63. factory_render();
  64. });
  65. }
  66. });
  67. }
  68. function factory_change_template(event){
  69. factory_search_part();
  70. factory_search_filters();
  71. event.stopPropagation();
  72. }
  73. function factory_shortcut(event){
  74. if(this.id=='entity' && event.keyCode==9){
  75. event.preventDefault();
  76. $('#factoryForm .field:eq(0) input:eq(0)').focus();
  77. }
  78. if($(this).hasClass('fieldType') && event.keyCode==9){
  79. event.preventDefault();
  80. return factory_addline(this);
  81. }
  82. if(event.keyCode == 107){
  83. event.preventDefault();
  84. return factory_addline(this);
  85. }
  86. if(event.keyCode == 109){
  87. event.preventDefault();
  88. return factory_removeline(this);
  89. }
  90. }
  91. function factory_search_part(callback){
  92. $('#factoryParts').fill({
  93. action : 'factory_search_part',
  94. template : $('#template').val()
  95. },
  96. function(){
  97. if(callback) callback();
  98. });
  99. }
  100. function factory_search_filters(callback){
  101. $('#factoryFilters ul').fill({
  102. action : 'factory_search_filters',
  103. template : $('#template').val(),
  104. part : $('#factoryParts li a.active').attr('data-part'),
  105. },
  106. function(){
  107. if($('#factoryFilters ul li:visible').length!=0){
  108. $('#factoryFilters h4').removeClass('hidden');
  109. }else{
  110. $('#factoryFilters h4').addClass('hidden');
  111. }
  112. $('#factoryFilters input[data-checked="checked"]').prop('checked',true);
  113. if(callback) callback();
  114. });
  115. }
  116. function factory_render(event,generate){
  117. if(generate)
  118. if(!confirm("Souhaitez-vous générer le fichier associé à l'onglet courant ?\nSi le fichier existe déjà, il sera écrasé.")) return;
  119. var data = $('#factoryForm').toJson();
  120. data.part = $('#factoryParts li a.active').attr('data-part');
  121. if(!data.part) return;
  122. data.template = $('#template').val();
  123. data.fields = {};
  124. $('#factoryForm .field').each(function(i,line){
  125. data.fields[$('.fieldSlug',line).val()] = {type:$('.fieldType',line).val(),label:$('.fieldLabel',line).val()} ;
  126. });
  127. data.filters = [];
  128. $('#factoryFilters li:visible input:checked').each(function(i,line){
  129. data.filters.push($(line).attr('data-section')) ;
  130. });
  131. data.generate = generate ? 1 : 0;
  132. $('#factoryCode').load('action.php?action='+data.action,data,function(){
  133. if(generate) $('#factory-generate-message').text("Fichier "+data.part+" généré");
  134. $('#factoryCode').attr('class',$('#factoryParts li a.active').attr('data-langage')) ;
  135. hljs.highlightBlock($('#factoryCode').get(0));
  136. });
  137. }
  138. function factory_addline(element,data){
  139. var line = $(element).closest('.field');
  140. var newline = $(line).clone();
  141. newline.find('input').val('');
  142. $(line).after(newline);
  143. newline.find('input:eq(0)').focus();
  144. if(data){
  145. newline.find('input').val(data.label);
  146. newline.find('select').val(data.type);
  147. }
  148. }
  149. function factory_removeline(element){
  150. var line = $(element).closest('.field');
  151. if($('.field').length==1){
  152. line.find('input').val('');
  153. line.find('input:eq(0)').focus();
  154. }else{
  155. prevline = line.prev('.field');
  156. line.remove();
  157. prevline.find('input:eq(0)').focus();
  158. }
  159. }