main.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //CHARGEMENT DE LA PAGE
  2. function init_plugin_workflow(){
  3. switch($.urlParam('page')){
  4. case 'sheet.workflow':
  5. workflow_workflow_type_change();
  6. workflow_effect_search();
  7. workflow_lock_input();
  8. break;
  9. }
  10. //Tri possible des effets de workflow
  11. $('#workflow-effects tbody').sortable({
  12. handle :'.fa-arrows-alt',
  13. axis :'y',
  14. update : function( event, ui ){
  15. var data = $('#workflow-effect-form').toJson();
  16. data.action = 'workflow_workflow_effect_sort';
  17. data.workflow = $('#workflow-form').attr('data-id');
  18. data.sort = [];
  19. $('#workflow-effects tbody tr:visible').each(function(){
  20. var tr = $(this);
  21. data.sort.push(tr.attr('data-id'));
  22. });
  23. $.action(data);
  24. }
  25. });
  26. $('#workflows').sortable_table({
  27. onSort : workflow_workflow_search
  28. });
  29. }
  30. //verouillez les champs en fonction du type de workflow (liste, entité ou général)
  31. function workflow_lock_input(){
  32. if( $('#workflow-form').attr('data-id') != ''){
  33. $('.create-only').attr('disabled','disabled');
  34. }else{
  35. $('.create-only').removeAttr('disabled','disabled');
  36. }
  37. }
  38. /** WORKFLOW **/
  39. //Récuperation d'une liste de workflow dans le tableau #workflows
  40. function workflow_workflow_search(callback){
  41. var box = new FilterBox('#filters');
  42. $('#workflows').fill({
  43. action:'workflow_workflow_search',
  44. filters: box.filters(),
  45. sort: $('#workflows').sortable_table('get')
  46. },function(response){
  47. $('.results-count span').text(response.pagination.total);
  48. if(callback!=null) callback();
  49. });
  50. }
  51. //Ajout ou modification d'élément workflow
  52. function workflow_workflow_save(){
  53. var data = $('#workflow-form').toJson();
  54. if($('#workflow-effects [data-slug="entity"].open').length!=0){
  55. $.message('warning','Un effet est encore en cours d\'edition, merci de le valider avant d\'enregistrer le workflow entier');
  56. return;
  57. }
  58. data.cause = new FilterBox('#cause-filters').filters().advanced;
  59. data.effect = new FilterBox('#effect-filters').filters().advanced;
  60. $.action(data,function(r){
  61. $.urlParam('id',r.id);
  62. $('#workflow-form').attr('data-id',r.id);
  63. workflow_lock_input();
  64. workflow_cause_search();
  65. workflow_effect_search();
  66. workflow_event_search();
  67. $.message('success','Enregistré');
  68. });
  69. }
  70. //Suppression d'élement workflow
  71. function workflow_workflow_delete(element,event){
  72. event.stopPropagation();
  73. event.preventDefault();
  74. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  75. var line = $(element).closest('li');
  76. $.action({
  77. action: 'workflow_workflow_delete',
  78. id: line.attr('data-id')
  79. },function(r){
  80. line.remove();
  81. $.message('info','Item supprimé');
  82. });
  83. }
  84. //Lancement manuel d'un workflow
  85. function workflow_workflow_run(element){
  86. $('.btn-workflow-execute').addClass('btn-preloader');
  87. $.action({
  88. action : 'workflow_workflow_run',
  89. id : $('#workflow-form').attr('data-id'),
  90. eventSlug : $(element).closest('li').attr('data-slug')
  91. },function(r){
  92. $.message('success','Executé');
  93. });
  94. }
  95. //Modification du type d'un workflow (verouillage de certains champs et récuperations des causes/effets/evenement lié a ce type)
  96. function workflow_workflow_type_change(){
  97. if($('#type').val() == 'global'){
  98. $('.entity-group').addClass('hidden');
  99. }else{
  100. $('.entity-group').removeClass('hidden');
  101. }
  102. workflow_cause_search();
  103. workflow_event_search();
  104. }
  105. /** WORKFLOWEVENT **/
  106. //Récuperation d'une liste de workflowevent dans le tableau #workflowevents
  107. function workflow_event_search(callback){
  108. var box = new FilterBox('#filters');
  109. $('#workflow-events').fill({
  110. action:'workflow_event_search',
  111. workflow : $('#workflow-form').attr('data-id'),
  112. filters: box.filters(),
  113. type: $('#type').val(),
  114. entity: $('#entity').val(),
  115. sort: $('#workflow-events').sortable_table('get')
  116. },function(response){
  117. $('#event').html('<option value="">-</option>');
  118. for(var slug in response.events){
  119. $('#event').append('<option value="'+slug+'">'+response.events[slug]+'</option>');
  120. }
  121. if(callback!=null) callback();
  122. });
  123. }
  124. //Ajout ou modification d'élément workflowevent
  125. function workflow_workflow_event_save(){
  126. var data = $('#workflow-event-form').toJson();
  127. data.workflow = $('#workflow-form').attr('data-id');
  128. $.action(data,function(r){
  129. $('#workflow-event-form').attr('data-id','');
  130. workflow_event_search();
  131. $.message('success','Enregistré');
  132. });
  133. }
  134. //Suppression d'élement workflowevent
  135. function workflow_workflow_event_delete(element){
  136. var line = $(element).closest('li');
  137. $.action({
  138. action: 'workflow_workflow_event_delete',
  139. id: line.attr('data-id')
  140. },function(r){
  141. line.remove();
  142. $.message('info','Item supprimé');
  143. });
  144. }
  145. /* WORKFLOW CAUSES */
  146. //Récuperation d'une liste de workfloweffect dans le tableau #workfloweffects
  147. function workflow_cause_search(callback){
  148. $.action({
  149. action:'workflow_cause_search',
  150. workflow: $('#workflow-form').attr('data-id'),
  151. type: $('#type').val(),
  152. entity: $('#entity').val(),
  153. sort: $('#workflow-effects').sortable_table('get')
  154. },function(response){
  155. $('#cause-filters').remove();
  156. $('.workflow-causes .advanced-search-box').remove();
  157. $('.workflow-cause-filters').html('');
  158. for (var i in response.filters) {
  159. $('.workflow-cause-filters').append(response.filters[i]);
  160. }
  161. var filters = '';
  162. for(var k in response.causes.available){
  163. filters+='<option value="'+response.causes.available[k].slug+'" '+response.causes.available[k].attributes+'>'+response.causes.available[k].label+'</option>'
  164. }
  165. var causes = $('#cause-filters-template').template({enabled : response.causes.enabled,filters : filters});
  166. causes.removeClass('hidden');
  167. causes.attr('id','cause-filters').attr('data-type','filter');
  168. $('#cause-filters-template').after(causes);
  169. init_components();
  170. if(callback!=null) callback();
  171. });
  172. }
  173. /** WORKFLOWEFFECT **/
  174. //Récuperation d'une liste de workfloweffect dans le tableau #workfloweffects
  175. function workflow_effect_search(callback){
  176. $('#workflow-effects').fill({
  177. action:'workflow_effect_search',
  178. workflow: $('#workflow-form').attr('data-id'),
  179. type: $('#type').val(),
  180. entity: $('#entity').val(),
  181. sort: $('#workflow-effects').sortable_table('get')
  182. },function(response){
  183. $('#workflow-effects tbody tr:visible').each(function(){
  184. var slug = $(this).attr('data-slug');
  185. if(window['workflow_effect_'+slug+'_init']) window['workflow_effect_'+slug+'_init']($(this));
  186. });
  187. init_components('#workflow-effects');
  188. if(callback!=null) callback();
  189. });
  190. }
  191. //Ajout ou modification d'élément workfloweffect
  192. function workflow_workflow_effect_save(){
  193. var data = $('#workflow-effect-form').toJson();
  194. data.workflow = $('#workflow-form').attr('data-id');
  195. $.action(data,function(r){
  196. $('#workflow-effect-form').attr('data-id','');
  197. workflow_effect_search(function(){
  198. workflow_workflow_effect_edit($('#workflow-effects [data-id="'+r.id+'"] .btn-edit'));
  199. });
  200. $.message('success','Enregistré');
  201. });
  202. }
  203. //Récuperation ou edition d'élément workfloweffect
  204. function workflow_workflow_effect_edit(element){
  205. //var line = $(element).closest('tr');
  206. var button = $(element);
  207. var tr = button.closest('tr');
  208. var form = tr.find('.workflow-effect-form');
  209. if(!tr.hasClass('open')){
  210. $('#workflow-effects tr').removeClass('open');
  211. //open
  212. tr.addClass('open');
  213. form.find('input:eq(0)').focus();
  214. }else{
  215. //save & close
  216. data = {};
  217. data.id = tr.attr('data-id');
  218. data.action = 'workflow_workflow_effect_save';
  219. data.values = {};
  220. $('.workflow-effect-form [data-id]',tr).each(function(){
  221. var input = $(this) ;
  222. var id = input.attr('data-id');
  223. if(!id) return;
  224. if(input.attr("type")=='checkbox' || input.attr("type")=='radio'){
  225. data.values[id] = (input.is(':checked')?1:0);
  226. } else if(input.attr("type")=='file'){
  227. if(!input[0].files.length) return;
  228. if(input[0].files.length == 1){
  229. data.values[id] = input[0].files[0];
  230. } else {
  231. data.values[id] = {};
  232. for(var i=0; i<input[0].files.length; ++i)
  233. data.values[id][i] = input[0].files[i];
  234. }
  235. } else {
  236. data.values[id] = input.val();
  237. }
  238. });
  239. $('.workflow-effect-form [data-type="filter"]',tr).each(function(){
  240. var box = new FilterBox(this);
  241. data.values[$(this).attr('data-id')] = box.filters();
  242. });
  243. $.action(data,function(r){
  244. tr.removeClass('open');
  245. $.message('success','Enregistré');
  246. });
  247. }
  248. }
  249. //Suppression d'élement workfloweffect
  250. function workflow_workflow_effect_delete(element){
  251. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  252. var line = $(element).closest('tr');
  253. $.action({
  254. action: 'workflow_workflow_effect_delete',
  255. id: line.attr('data-id')
  256. },function(r){
  257. line.remove();
  258. $.message('info','Item supprimé');
  259. });
  260. }
  261. //Enregistrement des configurations
  262. function workflow_setting_save(){
  263. $.action({
  264. action: 'workflow_setting_save',
  265. fields: $('#workflow-setting-form').toJson()
  266. },function(){
  267. $.message('success','Enregistré');
  268. });
  269. }