main.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //CHARGEMENT DE LA PAGE
  2. function init_plugin_dynamicform(){
  3. switch($.urlParam('page')){
  4. case 'sheet.dynamic.form':
  5. dynamicform_form_layout();
  6. dynamicform_field_search();
  7. window.addEventListener("beforeunload", function (event) {
  8. if(window.unsavedModifications) event.returnValue = "Si vous n'enregistrez pas le formulaire, vos dernières modifications seront perdues";
  9. });
  10. break;
  11. default:
  12. break;
  13. }
  14. dynamicform_form_search();
  15. }
  16. function dynamicform_sortableFields(){
  17. $( ".fields" ).sortable({
  18. connectWith: ".fields",
  19. handle: ".btn-field-move"
  20. }).disableSelection();
  21. }
  22. function dynamicform_form_reset(){
  23. var layout = $('#form-layout');
  24. var rowTemplate = $('#layout-row').html();
  25. layout.find('.row').remove();
  26. //créeer la ligne/colonne par défaut
  27. var row = $(rowTemplate);
  28. layout.append(row);
  29. }
  30. function dynamicform_form_layout(){
  31. var layout = $('#form-layout');
  32. var rowTemplate = $('#layout-row').html();
  33. var defaultType = JSON.parse(atob($('#dynamic-form-form').attr('data-default-type')));
  34. dynamicform_form_reset();
  35. //créeer la ligne/colonne par défaut
  36. if(layout.find('.row').length == 0){
  37. var row = $(rowTemplate);
  38. layout.append(row);
  39. }
  40. layout.off('click');
  41. layout.on('click','.btn-row-add',function(){
  42. var button = $(this);
  43. var row = button.closest('.row');
  44. dynamicform_row_add(row);
  45. window.unsavedModifications = true;
  46. });
  47. layout.on('click','.btn-column-add',function(){
  48. var column = $(this).closest('.column');
  49. var row = column.closest('.row');
  50. dynamicform_column_add(row);
  51. window.unsavedModifications = true;
  52. });
  53. layout.on('click','.btn-row-remove',function(){
  54. var button = $(this);
  55. if(layout.find('>.row').length < 2) return;
  56. button.closest('.row').remove();
  57. window.unsavedModifications = true;
  58. });
  59. layout.on('click','.btn-column-remove',function(){
  60. var button = $(this);
  61. var row = $(this).closest('.row');
  62. if(row.find('>.column').length < 2) return;
  63. button.closest('.column').remove();
  64. window.unsavedModifications = true;
  65. });
  66. layout.on('click','.btn-field-add',function(){
  67. var button = $(this);
  68. var column = button.closest('.column');
  69. var field = dynamicform_field_add(null,column,{type : defaultType,label : 'Nouveau champ'});
  70. field.find('.btn-field-edit').trigger('click');
  71. });
  72. layout.on('click','.btn-field-edit',function(){
  73. var button = $(this);
  74. var field = button.closest('.field');
  75. var data = field.data();
  76. $.setForm('#dynamic-field-form',data);
  77. $('#dynamic-field-form').attr('data-id',data.id);
  78. $('#dynamic-field-form').data('field',field);
  79. $('.dynamic-field-panel').removeClass('fold');
  80. $('#dynamic-field-form .label').focus().select();
  81. var meta = atob_unicode(data.meta);
  82. if(meta) meta = JSON.parse(meta);
  83. if(!meta) meta = {};
  84. $('#deleted-operators input[type="checkbox"]').prop('checked',false);
  85. if(meta['operator-delete']){
  86. for(var k in meta['operator-delete']){
  87. $('#deleted-operators input[type="checkbox"][value="'+meta['operator-delete'][k]+'"]').prop('checked',true);
  88. }
  89. init_components('#deleted-operators')
  90. }
  91. $('#show-filter').prop('checked',(meta['show-filter'] == true));
  92. dynamicform_fieldtype_change(data.meta);
  93. });
  94. layout.on('click','.btn-field-remove',function(){
  95. var button = $(this);
  96. var field = button.closest('.field');
  97. if(!confirm('Êtes-vous sûr de vouloir supprimer ce champ ?')) return;
  98. field.remove();
  99. window.unsavedModifications = true;
  100. });
  101. }
  102. function dynamicform_row_add(after){
  103. var clone = $('#form-layout .row:eq(0)').clone();
  104. clone.find('.column .fields').html('');
  105. if(after){
  106. after.after(clone);
  107. }else{
  108. $('#form-layout').append(clone);
  109. }
  110. dynamicform_sortableFields();
  111. return clone;
  112. }
  113. function dynamicform_column_add(row){
  114. var column = $('#form-layout .column:eq(0)');
  115. var clone = column.clone();
  116. clone.find('.fields').html('');
  117. row.append(clone);
  118. dynamicform_sortableFields();
  119. return clone;
  120. }
  121. function dynamicform_field_add(field,column,data){
  122. var fieldTemplate = $('#field-template').html();
  123. var fieldLine = $(Mustache.render(fieldTemplate,data));
  124. if(field){
  125. field.replaceWith(fieldLine);
  126. }else{
  127. var fields = column.find('.fields');
  128. fields.append(fieldLine);
  129. }
  130. dynamicform_sortableFields();
  131. return fieldLine;
  132. }
  133. //Enregistrement des configurations
  134. function dynamicform_setting_save(){
  135. $.action({
  136. action: 'dynamicform_setting_save',
  137. fields: $('#dynamicform-setting-form').toJson()
  138. },function(){
  139. $.message('success','Enregistré');
  140. });
  141. }
  142. /** FORMULAIRE **/
  143. //Récuperation d'une liste formulaire dans le tableau #dynamicforms
  144. function dynamicform_form_search(callback){
  145. $('#dynamic-forms').fill({
  146. action:'dynamicform_form_search'
  147. },function(response){
  148. if(callback!=null) callback();
  149. });
  150. }
  151. //Ajout ou modification formulaire
  152. function dynamicform_form_save(){
  153. var data = $('.dynamic-form-form').toJson();
  154. data.rows = [];
  155. $('#form-layout > .row').each(function(i){
  156. var rowLine = this;
  157. $('> .column',rowLine).each(function(u){
  158. var columnLine = $(this);
  159. $('> .fields >li',columnLine).each(function(o){
  160. var fieldLine = $(this);
  161. var lineData = fieldLine.data();
  162. var field = {
  163. id : lineData.id,
  164. label : lineData.label,
  165. type : lineData.type,
  166. default : lineData.default,
  167. description : lineData.description,
  168. mandatory : lineData.mandatory ,
  169. readonly : lineData.readonly,
  170. slug : lineData.slug,
  171. meta : lineData.meta
  172. }
  173. if(!data.rows[i]) data.rows[i] = [];
  174. if(!data.rows[i][u]) data.rows[i][u] = [];
  175. data.rows[i][u].push(field);
  176. });
  177. });
  178. });
  179. $.action(data,function(r){
  180. $('#dynamic-form-form').attr('data-id',r.id);
  181. $.setForm($('#dynamic-form-form'),r);
  182. $.urlParam('id',r.id);
  183. window.unsavedModifications = null;
  184. dynamicform_field_search();
  185. $.message('success','Enregistré');
  186. });
  187. }
  188. //Suppression formulaire
  189. function dynamicform_form_delete(element){
  190. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  191. var line = $(element).closest('tr');
  192. $.action({
  193. action: 'dynamicform_form_delete',
  194. id: line.attr('data-id')
  195. },function(r){
  196. line.remove();
  197. $.message('info','Item supprimé');
  198. });
  199. }
  200. /** CHAMP **/
  201. //Récuperation d'une liste champ dans le tableau #dynamicfields
  202. function dynamicform_field_search(callback){
  203. $.action({
  204. action : 'dynamicform_field_search',
  205. form : $('#dynamic-form-form').attr('data-id')
  206. },function(response){
  207. dynamicform_form_reset();
  208. var layout = $('#form-layout');
  209. for (var k in response.rows) {
  210. var currentRow = dynamicform_row_add();
  211. for (var i in response.rows[k]) {
  212. var currentColumn = dynamicform_column_add(currentRow);
  213. for (var u in response.rows[k][i]) {
  214. var data = response.rows[k][i][u];
  215. dynamicform_field_add(null,currentColumn,data);
  216. }
  217. }
  218. currentRow.find('.column:eq(0)').remove();
  219. }
  220. if(layout.find('>.row').length>1) layout.find('>.row:eq(0)').remove();
  221. if(callback!=null) callback();
  222. });
  223. }
  224. //Ajout ou modification champ
  225. function dynamicform_field_save(){
  226. var data = $('.dynamic-field-main').toJson();
  227. var settings = {};
  228. $('.dynamic-field-settings span').find('>input,select,textarea').each(function(){
  229. var value = $(this).val();
  230. if(is_json_string(value))
  231. value = JSON.parse(value);
  232. settings[$(this).attr('id')] = value;
  233. });
  234. $('.dynamic-field-main #deleted-operators').find('input:checked').each(function(){
  235. if(!settings['operator-delete']) settings['operator-delete'] = [];
  236. settings['operator-delete'].push($(this).val());
  237. });
  238. if($('#show-filter').prop('checked'))
  239. settings['show-filter'] = true;
  240. if(Object.keys(settings).length > 0)
  241. data.meta = btoa_unicode(JSON.stringify(settings));
  242. var field = $('#dynamic-field-form').data('field');
  243. data.type = JSON.parse(atob($('#type option:selected').attr('data-options')));
  244. data.id = $('#dynamic-field-form').attr('data-id');
  245. $('#dynamic-field-form').clear();
  246. $('#dynamic-field-form').data('field',null);
  247. dynamicform_field_add(field,null,data);
  248. $('.dynamic-field-panel').addClass('fold');
  249. window.unsavedModifications = true;
  250. }
  251. //Mofdification du type de champ
  252. function dynamicform_fieldtype_change(meta){
  253. $('.dynamic-field-settings').html('');
  254. $.action({
  255. action:'dynamicform_field_setting_change',
  256. type:$('#type').val(),
  257. settings:meta != null ? meta : ''
  258. },function(response){
  259. $('.dynamic-field-settings').append(response);
  260. init_components($('.dynamic-field-settings'));
  261. });
  262. }