123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- 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('<option value="">-</option>');
- for(var key in response.devices){
- var device = response.devices[key];
- $('.captor_selector').append('<option value="'+device.id+'">'+device.label+'</option>');
- }
- }
- );
- });
-
- $(document).on('change','.captor_selector',function(){
- $.action({action:'plugin_story_get_captor_values',id:$(this).val()},
- function(response){
- $('.captor_field_selector').append('<option value="">-</option>');
- for(var key in response.values){
- $('.captor_field_selector').append('<option value="'+key+'">'+key+'</option>');
- }
- }
- );
- });
-
- }
- 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 = '<li>';
- line += '<div data-element="place" data-position="before" class="place"></div>';
- line +='<div data-element="union" class="union">ET</div>';
- line += r.html;
- line += '<div data-element="place" data-position="after" class="place"></div></li>';
-
-
- if(options.place==null){
- $('.workspace-'+(options.panel=='cause'?'cause':'effect')).append(line);
- }else{
- if(options.place.attr('data-position')=='before'){
- $(options.place).parent().before(line);
- }else{
- $(options.place).parent().after(line);
- }
- }
-
- //Fill select by database values
- $('.workspace select[data-value]').each(function(i,elem){
- $(elem).find('option[value="'+$(elem).attr('data-value')+'"]').attr("selected", "selected");
- });
- //For captor only
- if(options.type=='event'){
- $.action(
- {action:'plugin_story_get_captors_plugins'},
- function(response){
- $('.plugin_selector').append('<option value="">-</option>');
- for(var key in response.plugins){
- var plugin = response.plugins[key];
- $('.plugin_selector').append('<option value="'+plugin+'">'+plugin+'</option>');
- }
- }
- );
- }
- initDragAndDrop();
- });
-
- }
- //Delete story line
- function deleteLine(elem){
- if($('.workspace-'+$('#story').attr('data-mode')+' li').length == 1){
- alert('Vous ne pouvez pas supprimer la dernière ligne');
- return;
- }
- if(!confirm('Sûr?')) return;
- var line = $(elem).closest('li');
- line.remove();
- }
- //Save story
- function saveStory(){
- story = {};
- if($('#story').attr('data-id')!='')story.id = $('#story').attr('data-id');
- story.label = $('.story h1 input').val();
- story.causes = [];
- story.effects = [];
-
- //causes
- lastunion ='';
- $('.story div#causePanel ul.workspace li div').each(function(i,elem){
- switch($(elem).attr('data-element')){
- case 'line':
- var line = { type : $(elem).data('type') }
- $('input,select',elem).each(function(i,input){
- line[$(input).data('field')] = $(input).val();
- });
-
- line.union = lastunion;
- story.causes.push(line);
- break;
- case 'union':
- if($(elem).find('select').length!=0) lastunion = $(elem).find('select').val();
- break;
- }
- });
-
- //effectS
- lastunion ='';
- $('.story div#effectPanel ul.workspace li div').each(function(i,elem){
-
- switch($(elem).attr('data-element')){
- case 'line':
-
- var line = { type : $(elem).data('type') }
- $('input,select',elem).each(function(i,input){
- line[$(input).data('field')] = $(input).val();
- });
-
- line.union = lastunion;
- story.effects.push(line);
- break;
- case 'union':
- if($(elem).find('select').length!=0) lastunion = $(elem).find('select').val();
- break;
- }
- });
-
- if(story.effects.length == 0 || story.causes.length == 0){
- alert('Pour valider le scénario, vous devez remplir au moins une cause et au moins un effet');
- return;
- }
- if(story.label == ''){
- $('.story h1 input').css('color','red')
- alert('Merci de bien vouloir nommer votre scénario');
- return;
- }
- $.action(
- {
- action:'plugin_story_save_story',
- story:story
- },
- function(response){
- $('#story').attr('data-id',response.id);
- alert('Scénario enregistré :)');
- }
- );
-
- }
|