main.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //CHARGEMENT DE LA PAGE
  2. var isProcessing = false;
  3. function init_plugin_export(){
  4. switch($.urlParam('page')){
  5. case 'sheet':
  6. // export_exportmodel_list_dataset();
  7. export_exportmodel_get_dataset($('#dataset'));
  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_exportmodel_search
  21. });
  22. }
  23. //Initialisation du composant d'export modèle
  24. function init_components_export_model(input){
  25. var cbLoaded = input.attr('data-callback') ? input.attr('data-callback') : '';
  26. var cbParams = input.attr('data-params') ? input.attr('data-params').split(',') : [];
  27. var parameters = JSON.parse(input.attr('data-parameters'));
  28. $(document).ready(function(e){
  29. input.on('click', function(e){
  30. $.ajax({
  31. type: 'GET',
  32. url: 'plugin/export/modal.export.model.php',
  33. async: true,
  34. success : function(modal){
  35. if(cbLoaded) window[cbLoaded].apply(null,cbParams);
  36. }
  37. }).done(function(modalContent){
  38. if(!$('#export-modal').length)
  39. $('body').append(modalContent);
  40. var modal = $('#export-modal');
  41. reset_inputs(modal);
  42. $.action({
  43. action: 'export_exportmodel_search',
  44. params: parameters
  45. }, function(r){
  46. if(r.rows){
  47. var selectExport = $('#exportModel');
  48. selectExport.find('option:not([value="none"])').remove();
  49. $.each(r.rows,function(i, option){
  50. selectExport.append('<option value="'+option.id+'">'+option.label+' - '+option.description+'</option>');
  51. });
  52. }
  53. modal.modal('show');
  54. init_components($('#export-modal'));
  55. $('#exportmodel-form').attr('data-parameters', JSON.stringify(parameters));
  56. });
  57. });
  58. });
  59. });
  60. }
  61. /* EXPORTMODELE */
  62. //Récuperation d'une liste de exportmodel dans le tableau #exportmodels
  63. function export_exportmodel_search(callback){
  64. $('#exportmodels').fill({
  65. action:'export_exportmodel_search',
  66. filters : $('#filters').filters(),
  67. sort : $('#exportmodels').sortable_table('get')
  68. }, function(){
  69. if(callback!=null) callback();
  70. });
  71. }
  72. //Ajout ou modification d'élément exportmodel
  73. function export_exportmodel_save(){
  74. var data = $('#exportmodel-form').toJson();
  75. $.action(data,function(r){
  76. $('#exportmodel-form').attr('data-id',r.id);
  77. $('#document_temporary').val('');
  78. $('#exportmodel-form [data-type="dropzone"] ul > li').attr('data-path', r.relativePath);
  79. $('#exportmodel-form [data-type="dropzone"] ul > li > a').attr('href', r.filePath);
  80. $('#exportmodel-form [data-type="dropzone"] ul > li > i.fa-times').attr('onclick', 'export_exportmodel_delete_document(this)');
  81. $('.options-box').load(document.URL + ' .options-box');
  82. $.message('success','Export modèle enregistré');
  83. });
  84. }
  85. //Récuperation ou edition d'élément exportmodel
  86. function export_exportmodel_edit(element){
  87. var line = $(element).closest('tr');
  88. $.action({
  89. action:'export_exportmodel_edit',
  90. id:line.attr('data-id')
  91. },function(r){
  92. $.setForm('#exportmodel-form',r);
  93. $('#exportmodel-form').attr('data-id',r.id);
  94. });
  95. }
  96. //Suppression d'élement exportmodel
  97. function export_exportmodel_delete(element){
  98. if(!confirm('Êtes-vous sûr de vouloir supprimer cet item ?')) return;
  99. var line = $(element).closest('tr');
  100. $.action({
  101. action : 'export_exportmodel_delete',
  102. id : line.attr('data-id')
  103. },function(r){
  104. line.remove();
  105. $.message('info','Export modèle supprimé');
  106. });
  107. }
  108. //Ajout de document
  109. function export_exportmodel_add_document(files){
  110. var form = $('#exportmodel-form');
  111. var exportmodelId = form.attr('data-id');
  112. $.action({
  113. action : 'export_exportmodel_add_document',
  114. id: exportmodelId,
  115. files : files,
  116. plugin: $('#plugin').val()
  117. }, function(r){
  118. form.attr('data-id', r.id);
  119. $.each(r.files, function(i, file){
  120. var line = $('li[data-path="'+file.oldPath+'"]', form);
  121. line.attr('data-path', file.relative);
  122. line.find('a').attr('href', file.url);
  123. line.find('i.pointer').attr('onclick', 'export_exportmodel_delete_document(this)');
  124. $('[data-type="dropzone"] input:not(:visible)', form).val('');
  125. $.message('success', 'Fichier "'+file.name+'" sauvegardé');
  126. });
  127. $('.options-box').load(document.URL + ' .options-box>*');
  128. });
  129. }
  130. //Suppression de document
  131. function export_exportmodel_delete_document(element){
  132. if(!confirm("Êtes-vous sûr de vouloir supprimer ce fichier ?")) return;
  133. var line = $(element).closest('li');
  134. $.action({
  135. action : 'export_exportmodel_delete_document',
  136. path : line.attr('data-path')
  137. },function(r){
  138. $('.options-box').load(document.URL + ' .options-box>*');
  139. line.remove();
  140. $.message('info','Élement supprimé');
  141. });
  142. }
  143. //Permet de récupérer les jeux de
  144. //données associés au plugin choisi
  145. function export_exportmodel_list_dataset(){
  146. var parameters = {
  147. plugin: $('#plugin').val()
  148. }
  149. $.action({
  150. action: 'export_exportmodel_list_dataset',
  151. params: parameters
  152. }, function(r){
  153. var datasetSelect = $('#dataset');
  154. var tpl = datasetSelect.find('option.hidden').get(0).outerHTML;
  155. datasetSelect.find('option:not(.template)').remove();
  156. datasetSelect.val('');
  157. $.each(r.rows, function(slug, dataset){
  158. var option = $(Mustache.render(tpl, dataset));
  159. option.removeClass('hidden template');
  160. datasetSelect.find('option.hidden').before(option);
  161. });
  162. export_exportmodel_get_dataset($('#dataset'));
  163. });
  164. }
  165. //Récupération du jeu de données
  166. //associé à l'élément exportmodel
  167. function export_exportmodel_get_dataset(element){
  168. var container = $('#dataset-container');
  169. container.addClass('hidden');
  170. $('#empty-files').show();
  171. $('li:not(.template)', container).remove();
  172. var datasetSelect = $(element);
  173. var curVal = datasetSelect.val();
  174. var pluginVal = $('#plugin').val();
  175. if(!curVal || !pluginVal) return;
  176. var parameters = {
  177. dataset: curVal,
  178. plugin: pluginVal
  179. }
  180. $.action({
  181. action: 'export_exportmodel_get_dataset',
  182. params: parameters
  183. }, function(r){
  184. container.removeClass('hidden');
  185. if(r.dataset){
  186. var showcase = $('.dataset-showcase');
  187. var tpl = $('li.hidden',showcase).get(0).outerHTML;
  188. $.each(r.dataset, function(macro, info){
  189. var datas = {
  190. macro: (info.type == 'list')?'{{#'+macro+'}}{{/'+macro+'}}':'{{'+macro+'}}',
  191. description:info.desc,
  192. type:info.type,
  193. badge: (info.type == 'list')?'Liste':'Valeur'
  194. };
  195. if(info.type == 'photo') datas.badge = 'Photo';
  196. var li = $(Mustache.render(tpl, datas));
  197. if(info.subitems) {
  198. var ul = $('<ul class="subitems"></ul>');
  199. $.each(info.subitems, function(tag, val){
  200. var subDatas = {
  201. macro: '{{'+tag+'}}',
  202. description: val !== null && typeof val === 'object' ? val.desc : val,
  203. type: val.type ? val.type : 'value',
  204. badge: val.type == 'photo' ? 'Photo' : 'Valeur'
  205. };
  206. var subLi = $(Mustache.render(tpl, subDatas));
  207. subLi.removeClass('hidden template');
  208. ul.append(subLi);
  209. });
  210. li.append(ul);
  211. }
  212. li.removeClass('hidden template');
  213. $('> ul', showcase).append(li);
  214. });
  215. }
  216. if(r.files && r.files.length){
  217. var exampleFiles = $('.dataset-example-files');
  218. $('#empty-files').hide();
  219. var aTpl = $('li.hidden',exampleFiles).get(0).outerHTML;
  220. $.each(r.files, function(i, file){
  221. var a = $(Mustache.render(aTpl, file));
  222. a.removeClass('hidden template');
  223. $('ul', exampleFiles).append(a);
  224. });
  225. }
  226. });
  227. }
  228. //Suppression de document
  229. function export_exportmodel_export(element){
  230. if(isProcessing) return;
  231. var exportmodelId = $('#exportModel').val();
  232. if(exportmodelId=='none') {
  233. $.message('error', 'Il faut choisir un modèle d\'export');
  234. return;
  235. }
  236. var parameters = $('#exportmodel-form').attr('data-parameters');
  237. var url = window.location.href;
  238. var previousUrl = url.substring(url.lastIndexOf('/')+1);
  239. isProcessing = true;
  240. // overlay_preloader();
  241. // toggle_preloader('#send-offer-form', true);
  242. var exportUrl = 'action.php?action=export_exportmodel_export&exportmodel=';
  243. window.location = exportUrl+btoa(exportmodelId)+'&params='+btoa(parameters)+'&url='+btoa(previousUrl);
  244. isProcessing = false;
  245. }