main.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. var mode = 'cause';
  2. $(document).ready(function(){
  3. init();
  4. });
  5. function initDragAndDrop(){
  6. $( ".toolbar li" ).draggable({
  7. helper: "clone"
  8. });
  9. $( ".workspace li" ).draggable({
  10. helper: "clone",
  11. cursorAt: { top: 15 }
  12. });
  13. $( ".place" ).droppable({
  14. hoverClass : "dragover" ,
  15. drop: function( event, ui ) {
  16. var element = $(ui.draggable[0]);
  17. if(element.hasClass('typeButton')){
  18. addLine({type:element.attr('data-type'),place:$(this),panel:mode});
  19. }else{
  20. moveLine({line:element,place:$(this),panel:mode});
  21. }
  22. }
  23. });
  24. }
  25. function init(){
  26. //Add lines from database
  27. $.action({
  28. action : 'plugin_story_get_causes_effects',
  29. id:$('#story').data('id')
  30. },function(r){
  31. if(r.results['causes'].length==0)
  32. addLine({type:'time',panel:'cause'});
  33. if(r.results['effects'].length==0)
  34. addLine({type:'talk',panel:'effect'});
  35. for(var key in r.results['causes'])
  36. addLine(r.results['causes'][key]);
  37. for(var key in r.results['effects'])
  38. addLine(r.results['effects'][key]);
  39. });
  40. //Init drag and drop
  41. initDragAndDrop();
  42. //Captor selectors events
  43. $(document).on('change','.plugin_selector',function(){
  44. $.action({action:'plugin_story_get_captors',plugin:$(this).val()},
  45. function(response){
  46. $('.captor_selector').append('<option value="">-</option>');
  47. for(var key in response.devices){
  48. var device = response.devices[key];
  49. $('.captor_selector').append('<option value="'+device.id+'">'+device.label+'</option>');
  50. }
  51. }
  52. );
  53. });
  54. $(document).on('change','.captor_selector',function(){
  55. $.action({action:'plugin_story_get_captor_values',id:$(this).val()},
  56. function(response){
  57. $('.captor_field_selector').append('<option value="">-</option>');
  58. for(var key in response.values){
  59. $('.captor_field_selector').append('<option value="'+key+'">'+key+'</option>');
  60. }
  61. }
  62. );
  63. });
  64. }
  65. function story_launch(id,elem){
  66. $(elem).addClass('loading');
  67. $('tr[data-log="'+id+'"] td pre').html('En cours d\'execution...');
  68. $.action({
  69. action:'plugin_story_launch_story',id:id},
  70. function(response){
  71. $('tr[data-log="'+id+'"] td pre').html(response.log);
  72. $('tr[data-log="'+id+'"]').slideDown();
  73. $(elem).removeClass('loading');
  74. }
  75. );
  76. }
  77. function story_change_state(id,elem){
  78. var icon = $(elem).find('i');
  79. var state = icon.hasClass('fa-check-square-o') ? 0 : 1;
  80. $.action({
  81. action:'plugin_story_change_state',id:id,state:state},
  82. function(){
  83. icon.removeClass('fa-square-o').removeClass('fa-check-square-o');
  84. icon.addClass((state == 1 ? 'fa-check-square-o' : 'fa-square-o'));
  85. }
  86. );
  87. }
  88. //Log story
  89. function story_log(id){
  90. $('tr[data-log="'+id+'"]').slideToggle();
  91. }
  92. //Delete story
  93. function story_delete(id,element){
  94. if(!confirm('Etes vous sûr de vouloir supprimer cette ligne ?')) return;
  95. $.action({
  96. action:'plugin_story_delete_story',id:id},
  97. function(response){
  98. $(element).parent().fadeOut();
  99. }
  100. );
  101. }
  102. //Switch mode (cause / effect)
  103. function switchCauseEffect(mode){
  104. $('#causePanelButton,#effectPanelButton').removeClass('active');
  105. $('#effectPanel,#causePanel').hide();
  106. $('#story').attr('data-mode',mode);
  107. switch(mode){
  108. case 'cause':
  109. $('#causePanel').fadeIn(200);
  110. $('#causePanelButton').addClass('active');
  111. break;
  112. case 'effect':
  113. $('#effectPanel').fadeIn(200);
  114. $('#effectPanelButton').addClass('active');
  115. break;
  116. }
  117. }
  118. function moveLine(options){
  119. var line = options.line;
  120. var number = $('.workspace li').index(line);
  121. if(options.place.attr('data-position')=='before'){
  122. options.place.parent().before(line.detach());
  123. }else{
  124. options.place.parent().after(line.detach());
  125. }
  126. }
  127. //Add line to board
  128. function addLine(options){
  129. options.data = options.data == null ? {value:'',target:'',operator:'',union:''} : options.data ;
  130. $.action({
  131. action : 'plugin_story_get_type_template',
  132. data : options.data,
  133. type : options.type,
  134. async:false
  135. },function(r){
  136. var line = '<li>';
  137. line += '<div data-element="place" data-position="before" class="place"></div>';
  138. line +='<div data-element="union" class="union">ET</div>';
  139. line += r.html;
  140. line += '<div data-element="place" data-position="after" class="place"></div></li>';
  141. if(options.place==null){
  142. $('.workspace-'+(options.panel=='cause'?'cause':'effect')).append(line);
  143. }else{
  144. if(options.place.attr('data-position')=='before'){
  145. $(options.place).parent().before(line);
  146. }else{
  147. $(options.place).parent().after(line);
  148. }
  149. }
  150. //Fill select by database values
  151. $('.workspace select[data-value]').each(function(i,elem){
  152. $(elem).find('option[value="'+$(elem).attr('data-value')+'"]').attr("selected", "selected");
  153. });
  154. //For captor only
  155. if(options.type=='event'){
  156. $.action(
  157. {action:'plugin_story_get_captors_plugins'},
  158. function(response){
  159. $('.plugin_selector').append('<option value="">-</option>');
  160. for(var key in response.plugins){
  161. var plugin = response.plugins[key];
  162. $('.plugin_selector').append('<option value="'+plugin+'">'+plugin+'</option>');
  163. }
  164. }
  165. );
  166. }
  167. initDragAndDrop();
  168. });
  169. }
  170. //Delete story line
  171. function deleteLine(elem){
  172. if($('.workspace-'+$('#story').attr('data-mode')+' li').length == 1){
  173. alert('Vous ne pouvez pas supprimer la dernière ligne');
  174. return;
  175. }
  176. if(!confirm('Sûr?')) return;
  177. var line = $(elem).closest('li');
  178. line.remove();
  179. }
  180. //Save story
  181. function saveStory(){
  182. story = {};
  183. if($('#story').attr('data-id')!='')story.id = $('#story').attr('data-id');
  184. story.label = $('.story h1 input').val();
  185. story.causes = [];
  186. story.effects = [];
  187. //causes
  188. lastunion ='';
  189. $('.story div#causePanel ul.workspace li div').each(function(i,elem){
  190. switch($(elem).attr('data-element')){
  191. case 'line':
  192. var line = { type : $(elem).data('type') }
  193. $('input,select',elem).each(function(i,input){
  194. line[$(input).data('field')] = $(input).val();
  195. });
  196. line.union = lastunion;
  197. story.causes.push(line);
  198. break;
  199. case 'union':
  200. if($(elem).find('select').length!=0) lastunion = $(elem).find('select').val();
  201. break;
  202. }
  203. });
  204. //effectS
  205. lastunion ='';
  206. $('.story div#effectPanel ul.workspace li div').each(function(i,elem){
  207. switch($(elem).attr('data-element')){
  208. case 'line':
  209. var line = { type : $(elem).data('type') }
  210. $('input,select',elem).each(function(i,input){
  211. line[$(input).data('field')] = $(input).val();
  212. });
  213. line.union = lastunion;
  214. story.effects.push(line);
  215. break;
  216. case 'union':
  217. if($(elem).find('select').length!=0) lastunion = $(elem).find('select').val();
  218. break;
  219. }
  220. });
  221. if(story.effects.length == 0 || story.causes.length == 0){
  222. alert('Pour valider le scénario, vous devez remplir au moins une cause et au moins un effet');
  223. return;
  224. }
  225. if(story.label == ''){
  226. $('.story h1 input').css('color','red')
  227. alert('Merci de bien vouloir nommer votre scénario');
  228. return;
  229. }
  230. $.action(
  231. {
  232. action:'plugin_story_save_story',
  233. story:story
  234. },
  235. function(response){
  236. $('#story').attr('data-id',response.id);
  237. alert('Scénario enregistré :)');
  238. }
  239. );
  240. }