var mode = 'cause'; $(document).ready(function(){ init(); }); function initDragAndDrop(){ $( ".toolbar li" ).draggable({ helper: "clone" }); $( ".workspace li" ).draggable({ helper: "clone", cursorAt: { top: 15 } }); $( ".place" ).droppable({ hoverClass : "dragover" , drop: function( event, ui ) { var element = $(ui.draggable[0]); if(element.hasClass('typeButton')){ addLine({type:element.attr('data-type'),place:$(this),panel:mode}); }else{ moveLine({line:element,place:$(this),panel:mode}); } } }); } function init(){ //Add lines from database $.action({ action : 'plugin_story_get_causes_effects', id:$('#story').data('id') },function(r){ if(r.results['causes'].length==0) addLine({type:'time',panel:'cause'}); if(r.results['effects'].length==0) addLine({type:'talk',panel:'effect'}); for(var key in r.results['causes']) addLine(r.results['causes'][key]); for(var key in r.results['effects']) addLine(r.results['effects'][key]); }); //Init drag and drop initDragAndDrop(); //Captor selectors events $(document).on('change','.plugin_selector',function(){ $.action({action:'plugin_story_get_captors',plugin:$(this).val()}, function(response){ $('.captor_selector').append(''); for(var key in response.devices){ var device = response.devices[key]; $('.captor_selector').append(''); } } ); }); $(document).on('change','.captor_selector',function(){ $.action({action:'plugin_story_get_captor_values',id:$(this).val()}, function(response){ $('.captor_field_selector').append(''); for(var key in response.values){ $('.captor_field_selector').append(''); } } ); }); } function story_launch(id,elem){ $(elem).addClass('loading'); $('tr[data-log="'+id+'"] td pre').html('En cours d\'execution...'); $.action({ action:'plugin_story_launch_story',id:id}, function(response){ $('tr[data-log="'+id+'"] td pre').html(response.log); $('tr[data-log="'+id+'"]').slideDown(); $(elem).removeClass('loading'); } ); } function story_change_state(id,elem){ var icon = $(elem).find('i'); var state = icon.hasClass('fa-check-square-o') ? 0 : 1; $.action({ action:'plugin_story_change_state',id:id,state:state}, function(){ icon.removeClass('fa-square-o').removeClass('fa-check-square-o'); icon.addClass((state == 1 ? 'fa-check-square-o' : 'fa-square-o')); } ); } //Log story function story_log(id){ $('tr[data-log="'+id+'"]').slideToggle(); } //Delete story function story_delete(id,element){ if(!confirm('Etes vous sûr de vouloir supprimer cette ligne ?')) return; $.action({ action:'plugin_story_delete_story',id:id}, function(response){ $(element).parent().fadeOut(); } ); } //Switch mode (cause / effect) function switchCauseEffect(mode){ $('#causePanelButton,#effectPanelButton').removeClass('active'); $('#effectPanel,#causePanel').hide(); $('#story').attr('data-mode',mode); switch(mode){ case 'cause': $('#causePanel').fadeIn(200); $('#causePanelButton').addClass('active'); break; case 'effect': $('#effectPanel').fadeIn(200); $('#effectPanelButton').addClass('active'); break; } } function moveLine(options){ var line = options.line; var number = $('.workspace li').index(line); if(options.place.attr('data-position')=='before'){ options.place.parent().before(line.detach()); }else{ options.place.parent().after(line.detach()); } } //Add line to board function addLine(options){ options.data = options.data == null ? {value:'',target:'',operator:'',union:''} : options.data ; $.action({ action : 'plugin_story_get_type_template', data : options.data, type : options.type, async:false },function(r){ var line = '