main.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //CHARGEMENT DE LA PAGE
  2. var isProcessing = false;
  3. function init_plugin_export(){
  4. switch($.urlParam('page')){
  5. case 'sheet':
  6. export_model_get_dataset($('#dataset'));
  7. export_model_format_refresh();
  8. break;
  9. default:
  10. break;
  11. }
  12. }
  13. //Init des settings
  14. function init_setting_export(parameter){
  15. switch(parameter.section){
  16. case 'export':
  17. break;
  18. }
  19. $('#exportmodels').sortable_table({
  20. onSort : export_model_search
  21. });
  22. }
  23. /* EXPORTMODELE */
  24. //Récuperation d'une liste de exportmodel dans le tableau #exportmodels
  25. function export_model_search(callback){
  26. var box = new FilterBox('#filters');
  27. $('#exportmodels').fill({
  28. action:'export_model_search',
  29. filters : box.filters(),
  30. sort : $('#exportmodels').sortable_table('get')
  31. }, function(){
  32. if(callback!=null) callback();
  33. });
  34. }
  35. //Ajout ou modification d'élément exportmodel
  36. function export_model_save(){
  37. var data = $('#exportmodel-form').toJson();
  38. data.plugin = $('#dataset option:selected').attr('data-plugin');
  39. $.action(data,function(r){
  40. $('#exportmodel-form').attr('data-id',r.id);
  41. $.urlParam('id',r.id);
  42. $('#document_temporary').val('');
  43. var modelFile = $('#exportmodel-form [data-type="dropzone"] ul > li');
  44. modelFile.attr('data-path', r.relativePath);
  45. $('> a',modelFile).attr('href', r.filePath);
  46. $('> i.fa-times',modelFile).attr('onclick', 'export_model_delete_document(this)');
  47. $.message('success','Export modèle enregistré');
  48. });
  49. }
  50. function export_model_format_refresh(){
  51. var li = $('#document > ul > li:visible');
  52. var tpl = '<option value="{{uid}}">{{label}}</option>';
  53. $('#export_format').html('');
  54. $('.options-box').addClass('hidden');
  55. if(li.length==0 || li.attr('data-ext')=='') return;
  56. $.action({
  57. action: 'export_model_format_refresh',
  58. ext: li.attr('data-ext')
  59. },function(r){
  60. for(var key in r.rows)
  61. $('#export_format').append(Mustache.render(tpl,r.rows[key]));
  62. $('#export_format').val( ($('#export_format').attr('data-value').length ? $('#export_format').attr('data-value') : li.attr('data-ext')) );
  63. if(r.rows.length>0) $('.options-box').removeClass('hidden');
  64. });
  65. }
  66. //Récuperation ou edition d'élément exportmodel
  67. function export_model_edit(element){
  68. var line = $(element).closest('tr');
  69. $.action({
  70. action:'export_model_edit',
  71. id:line.attr('data-id')
  72. },function(r){
  73. $.setForm('#exportmodel-form',r);
  74. $('#exportmodel-form').attr('data-id',r.id);
  75. });
  76. }
  77. //Suppression d'élement exportmodel
  78. function export_model_delete(element){
  79. if(!confirm('Êtes-vous sûr de vouloir supprimer cet item ?')) return;
  80. var line = $(element).closest('tr');
  81. $.action({
  82. action : 'export_model_delete',
  83. id : line.attr('data-id')
  84. },function(r){
  85. line.remove();
  86. $.message('info','Export modèle supprimé');
  87. });
  88. }
  89. //Ajout de document
  90. function export_model_add_document(files){
  91. var form = $('#exportmodel-form');
  92. var exportmodelId = form.attr('data-id');
  93. $.action({
  94. action : 'export_model_add_document',
  95. id: exportmodelId,
  96. files : files,
  97. plugin: $('#dataset option:selected').attr('data-plugin')
  98. }, function(r){
  99. form.attr('data-id', r.id);
  100. $.each(r.files, function(i, file){
  101. var line = $('li[data-path="'+file.oldPath+'"]', form);
  102. line.attr('data-path', file.relative);
  103. line.find('a').attr('href', file.url);
  104. line.find('i.pointer').attr('onclick', 'export_model_delete_document(this)');
  105. $('[data-type="dropzone"] input:not(:visible)', form).val('');
  106. $.message('success', 'Fichier "'+file.name+'" sauvegardé');
  107. });
  108. export_model_format_refresh();
  109. });
  110. }
  111. //Suppression de document
  112. function export_model_delete_document(element){
  113. if(!confirm("Êtes-vous sûr de vouloir supprimer ce fichier ?")) return;
  114. var line = $(element).closest('li');
  115. $.action({
  116. action : 'export_model_delete_document',
  117. path : line.attr('data-path')
  118. },function(r){
  119. line.remove();
  120. export_model_format_refresh();
  121. $.message('info','Élement supprimé');
  122. });
  123. }
  124. //Récupération du jeu de données
  125. //associé à l'élément exportmodel
  126. function export_model_get_dataset(element){
  127. var container = $('#dataset-container');
  128. container.addClass('hidden');
  129. $('#empty-files').removeClass('hidden');
  130. $('li:not(.template)', container).remove();
  131. var datasetSelect = $(element);
  132. var curVal = datasetSelect.val();
  133. var pluginVal = $('option:selected',datasetSelect).attr('data-plugin');
  134. if(!curVal || !pluginVal) return;
  135. var label = $('#label');
  136. if(!label.length) label.val(pluginVal+': '+$('option:selected',datasetSelect).text());
  137. if($('#slug').length && !$('#slug').val().length){
  138. var slug = label.val().toString().toLowerCase().replace(/[^a-z0-9]/g, '-').replace(/\-\-+/g, '-');
  139. $('#slug').val(slug);
  140. }
  141. $.action({
  142. action: 'export_model_get_dataset',
  143. params: {
  144. dataset: curVal,
  145. plugin: pluginVal
  146. }
  147. }, function(r){
  148. container.removeClass('hidden');
  149. if(r.dataset){
  150. var showcase = $('.dataset-showcase');
  151. var tpl = $('li.hidden',showcase).get(0).outerHTML;
  152. export_model_recursive_dataset($('> ul', showcase),tpl,r.dataset,'');
  153. }
  154. if(r.files && r.files.length)
  155. $('.dataset-example-files > ul').addLine(r.files);
  156. });
  157. }
  158. function export_copy_macro(element,event){
  159. event.stopPropagation();
  160. var element = $(element);
  161. value = export_get_macro_recursive(element, []);
  162. value.reverse();
  163. if(element.attr('data-type')!='list') value = '{{'+value.join('.').replace(/[}{]/ig,'')+'}}';
  164. copy_string(value,element.find('>code'));
  165. }
  166. function export_get_macro_recursive(element,path){
  167. var parent = element.parent().parent();
  168. if(parent.attr('data-macro')!=null && parent.attr('data-macro')!='' && parent.attr('data-type')!='list'){
  169. path.push(element.attr('data-macro'));
  170. path.concat(export_get_macro_recursive(parent,path));
  171. }else{
  172. path.push(element.attr('data-macro'));
  173. }
  174. return path;
  175. }
  176. function export_model_recursive_dataset(container,tpl,dataset){
  177. var typeLabel = {
  178. 'list' : 'Liste',
  179. 'image' : 'Image',
  180. 'value': 'Valeur',
  181. 'object': 'Objet'
  182. };
  183. $.each(dataset, function(macro, data){
  184. data.macro = data.type == 'list' ?'{{#'+macro+'}}{{/'+macro+'}}':'{{'+macro+'}}';
  185. data.type = data.type && data.type!='' ? data.type : 'value';
  186. data.badge = typeLabel[data.type];
  187. var li = $(Mustache.render(tpl, data));
  188. li.removeClass('hidden template');
  189. container.append(li);
  190. if(data.type=='object' || data.type=='list'){
  191. var ul = $('<ul class="subitems"></ul>');
  192. li.append(ul);
  193. if(data.type=='list'){
  194. data.value = data.value[0];
  195. }
  196. export_model_recursive_dataset(ul,tpl,data.value);
  197. }
  198. });
  199. }
  200. //Export des données en fonction du modèle
  201. //sélectionné dans la modal d'export
  202. function export_model_export(callback){
  203. if(isProcessing) return;
  204. var modal = $('#export-modal');
  205. var model = $('#exportModel').val();
  206. if(model=='none') return $.message('error', 'Vous devez choisir un modèle d\'export');
  207. callback = callback!=null ? callback : modal.attr('data-post-callback');
  208. var parameters = JSON.parse($('#exportmodel-form').attr('data-parameters'));
  209. parameters.model = model;
  210. parameters.previousUrl = window.location.href.substring(window.location.href.lastIndexOf('/')+1);
  211. isProcessing = true;
  212. $('#export-button', modal).addClass('btn-preloader');
  213. $.action({
  214. action: 'export_model_export',
  215. parameters: parameters,
  216. downloadResponse: !("destination" in parameters)
  217. }, function(r){
  218. isProcessing = false;
  219. $.message('success', "Export modèle correctement généré");
  220. modal.modal('hide');
  221. if(callback!=null && callback.length) window[callback](r);
  222. }, function(r){
  223. isProcessing = false;
  224. });
  225. }