main.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //CHARGEMENT DE LA PAGE
  2. function init_plugin_factory(event){
  3. $('#factoryForm').on('keyup','input',function(){
  4. //Evite le flood de render lorsqu'on tape un texte
  5. if(window.factoryRenderTimeout) clearTimeout(window.factoryRenderTimeout);
  6. window.factoryRenderTimeout = setTimeout(function(){
  7. factory_render();
  8. },300);
  9. });
  10. $('#factoryForm').on('change','#plugin-color,#plugin-icon',factory_render);
  11. $('#factoryForm').on('change','select',factory_render);
  12. $('#factoryForm').on('keydown','input,select',factory_shortcut);
  13. factory_change_template();
  14. $('#factoryParts').on('click','li a',function(){
  15. $('#factoryParts li a').removeClass('active');
  16. $(this).addClass('active');
  17. factory_render(event);
  18. });
  19. $('#factoryParts .nav-link.active').trigger('click');
  20. $('#plugin').autocomplete({
  21. action : 'factory_autocomplete_plugin',
  22. onClick : function(selected,element){
  23. $('#plugin').val(selected.value);
  24. $('#pluginLabel').val(selected.label);
  25. $('#description').val(selected.description);
  26. $('#plugin-color').val(selected.color);
  27. $('#plugin-icon').val(selected.icon);
  28. init_components('#factoryForm');
  29. }
  30. });
  31. $('#plugin').focus();
  32. factory_entity_picker('#entity',function(element,r){
  33. $('#entityLabel').val(r.label);
  34. $('#factoryForm .field:not(:eq(0))').remove();
  35. var firstField = $('#factoryForm .field:eq(0)');
  36. $('.fieldSlug',firstField).val(r.rows[0].key);
  37. $('.fieldLabel',firstField).val(r.rows[0].label);
  38. $('.fieldType',firstField).val(r.rows[0].type);
  39. if(r.rows[0].link){
  40. $('.entity-picker',firstField).val(r.rows[0].link).removeClass('hidden');
  41. init_components(firstField);
  42. }
  43. for(var k in r.rows){
  44. if(k==0) continue;
  45. factory_addline(null,r.rows[k]);
  46. }
  47. factory_render();
  48. });
  49. $('#entity-fields').sortable({
  50. axis : 'y',
  51. handle : '.btn-move',
  52. update : function(){
  53. factory_render();
  54. }
  55. });
  56. }
  57. function factory_entity_picker(element,callback){
  58. var element = $(element);
  59. element.attr('autocomplete','off').typeahead({
  60. items: 5,
  61. minLength: 2,
  62. autoSelect : false,
  63. selectOnBlur : false,
  64. displayText : function(item){
  65. return '<span class="text-muted">'+item.parent+'/</span><span class="font-weight-bold">'+item.name+'</span>';
  66. },
  67. source: function(keyword, response){
  68. $.action({
  69. action: 'factory_autocomplete_entity',
  70. keyword: element.val(),
  71. plugin: element.is('[data-all-plugin]')? false : $('#plugin').val()
  72. },function(r){
  73. if(r.rows != null)
  74. response(r.rows);
  75. });
  76. },
  77. matcher: function(r){
  78. return '<div>'+r.name+'</div>';
  79. },
  80. afterSelect: function(item) {
  81. element.data('selected',true);
  82. element.val(item.name);
  83. element.attr('data-path',item.path);
  84. $.action({
  85. action: 'factory_autocomplete_entity_select',
  86. entity: item.name,
  87. plugin: element.is('[data-all-plugin]')? false : $('#plugin').val()
  88. },function(r){
  89. if(callback) callback(element,r);
  90. });
  91. }
  92. });
  93. }
  94. function factory_change_type(element){
  95. var select = $(element);
  96. var parent = select.parent();
  97. if(select.val()=='entity-external-entity'){
  98. var picker = parent.find('.entity-picker');
  99. picker.removeClass('hidden');
  100. picker.focus();
  101. init_components(picker.parent());
  102. }else{
  103. parent.find('.entity-picker').addClass('hidden');
  104. }
  105. }
  106. function factory_change_template(event){
  107. factory_search_part();
  108. factory_search_filters();
  109. if(event) event.stopPropagation();
  110. }
  111. function factory_shortcut(event){
  112. if(this.id=='entity' && event.keyCode==9){
  113. event.preventDefault();
  114. $('#factoryForm .field:eq(0) input:eq(0)').focus();
  115. }
  116. if($(this).hasClass('fieldType') && event.keyCode==9){
  117. event.preventDefault();
  118. return factory_addline(this);
  119. }
  120. if(event.keyCode == 107){
  121. event.preventDefault();
  122. return factory_addline(this);
  123. }
  124. if(event.keyCode == 109){
  125. event.preventDefault();
  126. return factory_removeline(this);
  127. }
  128. }
  129. function factory_search_part(callback){
  130. $('#factoryParts').fill({
  131. action : 'factory_search_part',
  132. template : $('#template').val()
  133. },
  134. function(){
  135. factory_render();
  136. if(callback) callback();
  137. });
  138. }
  139. function factory_search_filters(callback){
  140. $('#factoryFilters ul').fill({
  141. action : 'factory_search_filters',
  142. template : $('#template').val(),
  143. part : $('#factoryParts li a.active').attr('data-part'),
  144. },
  145. function(){
  146. if($('#factoryFilters ul li:visible').length!=0){
  147. $('#factoryFilters h4').removeClass('hidden');
  148. }else{
  149. $('#factoryFilters h4').addClass('hidden');
  150. }
  151. $('#factoryFilters input[data-checked="true"]').prop('checked',true);
  152. if(callback) callback();
  153. });
  154. }
  155. function factory_render(event,generate){
  156. if(generate)
  157. if(!confirm("Souhaitez-vous générer le fichier associé à l'onglet courant ?\nSi le fichier existe déjà, il sera écrasé.")) return;
  158. var data = $('#factoryForm').toJson();
  159. data.part = $('#factoryParts li a.active').attr('data-part');
  160. if(!data.part) return;
  161. data.template = $('#template').val();
  162. data.fields = {};
  163. $('#factoryForm .field').each(function(i,line){
  164. var field= {type:$('.fieldType',line).val(),label:$('.fieldLabel',line).val()} ;
  165. var entityPicker = $(line).find('.entity-picker').val();
  166. if(entityPicker) field.entityPath = $(line).find('.entity-picker').val();
  167. data.fields[$('.fieldSlug',line).val()] = field;
  168. });
  169. data.filters = [];
  170. $('#factoryFilters li:visible input:checked').each(function(i,line){
  171. data.filters.push($(line).attr('data-id')) ;
  172. });
  173. data.generate = generate ? 1 : 0;
  174. $('#factoryCode').load('action.php?action='+data.action,data,function(){
  175. if(generate) $('#factory-generate-message').text("Fichier "+data.part+" généré");
  176. $('#factoryCode').attr('class',$('#factoryParts li a.active').attr('data-langage')) ;
  177. hljs.highlightBlock($('#factoryCode').get(0));
  178. });
  179. }
  180. function factory_addline(element,data){
  181. if(element){
  182. var line = $(element).closest('.field');
  183. var newline = $(line).clone();
  184. newline.find('.entity-picker').addClass('hidden');
  185. newline.find('.data-type-entitypicker').remove();
  186. newline.find('input').val('');
  187. $(line).after(newline);
  188. }else{
  189. var line = $('#entity-fields .field:eq(0)');
  190. var newline = $(line).clone();
  191. newline.find('input').val('');
  192. $('#entity-fields').append(newline);
  193. }
  194. newline.find('input:eq(0)').focus();
  195. if(data){
  196. $('.fieldSlug',newline).val(data.key);
  197. $('.fieldLabel',newline).val(data.label);
  198. $('.fieldType',newline).val(data.type);
  199. if(data.link){
  200. $('.entity-picker',newline).val(data.link).removeClass('hidden');
  201. }else{
  202. $('.entity-picker',newline).val('').addClass('hidden');
  203. }
  204. }
  205. }
  206. function factory_removeline(element){
  207. var line = $(element).closest('.field');
  208. if($('.field').length==1){
  209. line.find('input').val('');
  210. line.find('input:eq(0)').focus();
  211. }else{
  212. prevline = line.prev('.field');
  213. line.remove();
  214. prevline.find('input:eq(0)').focus();
  215. }
  216. }