User Tools

Site Tools


script_event_bus

====== About the script ====== * Purpose : This script lets you subscribe and unsubscribe to an eventbus for each event. * Author : cdfa * Link: if needed, you may add a link here ====== How to use the script ====== Import this script, when you want to subscribe to a eventbus, copy/paste this in your script: LL.runScript("Eventbus", JSON.stringify({funct:"sub", obId:id_of_the_object_the_event_is_fired_on, name:"name of the event", action:EventHandler.action_you_want_to_execute, extra:any_extra_data_you_want_to_pass})) so for example LL.runScript("eventBus", JSON.stringify({funct:"sub", obId:196666, name:"i.tap", action:EventHandler.RUN_SCRIPT, extra:LL.getScriptByName("External Editor Script Importer").getId()})) Everything is save as a tag on the script, so if you delete the script it won't work anymore. If you want to unsubscribe to an eventbus just replace "sub" behind funct: with "unsub". ====== Script code ====== <sxh javascript> var script = LL.getCurrentScript(); var event = LL.getEvent(); var src = event.getSource(); var data = JSON.parse(event.getData()); var cont = event.getContainer(); var i, id function getOb(id){ if(id.toString().length==6){ ob=LL.getItemById(id); }else{ ob=LL.getContainerById(id); } return ob } if(src=="RUN_SCRIPT"){ if(data==null)throw "object is null"; var obId=data.obId; var name=data.name; var tagName = obId+name; var eventBus = JSON.parse(script.getTag(tagName)); if(eventBus==null)eventBus=[] var evHa = {action:data.action, data:data.extra}; var reAdding = false for(i=0;i<eventBus.length;i++){ if(eventBus[i].action==data.action && eventBus[i].data==data.extra){ reAdding=true break; } } if(data.funct=="sub"){ if(!reAdding){ var ob = getOb(obId); if(eventBus==null)eventBus=[]; var prop = ob.getProperties(); var curEvha = prop.getEventHandler(name); var curAction = curEvha.getAction(); var curData = curEvha.getData(); if(curAction!=EventHandler.RUN_SCRIPT || curData!=script.getId().toString()){ if(curAction!=0 && curAction!=EventHandler.UNSET)eventBus.push({action:curAction, data:curData}); prop.edit().setEventHandler(name, EventHandler.RUN_SCRIPT, script.getId()).commit(); } eventBus.push(evHa); } }else if(data.funct=="unsub"){ if(reAdding){ eventBus.splice(i, 1); } if(eventBus.length==0){ var ob = getOb(obId); ob.getProperties().edit().setEventHandler(name, EventHandler.UNSET, null).commit(); ob.setTag(tagName, null); } } script.setTag(tagName, JSON.stringify(eventBus)); }else{ var name; switch(src.slice(0, 1)){ case "K": name = src.slice(2).toLowerCase()+"Key"; if(src.slice(-1)=="L"){ name = "long"+src.slice(2, 3)+src.slice(3,6).toLowerCase()+"Key"; } break; case "I": if(src.slice(-5)=="CLICK"){ if(src.length==7){ name = "i.tap"; }else{ name="i.longTap"; } }else if(src.slice(2,7)=="SWIPE"){ name="i.swipe"+ src.slice(8,9)+src.slice(9).toLowerCase(); }else{ name="i."+src.slice(2).toLowerCase(); } break case "C": if(src.slice(-5)=="CLICK"){ if(src.length==7){ name = "bg.tap"; }else if(src.length==12){ name="bg.longTap"; }else{ name="bg.doubleTap"; } }else if(src.slice(2,7)=="SWIPE"){ if(src.slice(7, 8)=="2"){ name="swipe2"+src.slice(9,10)+src.slice(10).toLowerCase(); }else{ name="swipe"+src.slice(8,9)+src.slice(9).toLowerCase(); } }else if(src=="C_LOADED"){ name="load"; }else if(src=="C_POSITION_CHANGED"){ nams="posChanged"; }else{ name=src.slice(2).toLowerCase(); } break; case "P": name="orientationPortrait"; break; case "L": name="orientationLandscape"; break; } var cont = event.getContainer(); if(cont==null) cont=LL.getCurrentDesktop(); var id = cont.getId(); var item=event.getItem(); if(item==null){ var eventBus = JSON.parse(script.getTag(id+name)); for(i=0;i<eventBus.length;i++){ var action = eventBus[i].action var data = eventBus[i].data if(action==EventHandler.RUN_SCRIPT){ if(typeof data=="number"){ id = data }else{ var slash = data.indexOf("/") id = data.slice(0,slash) data = data.slice(slash+1) } LL.runScript(LL.getScriptById(id).getName(), data) }else{ LL.runAction(action, data); } } }else{ var id=item.getId() var eventBus = JSON.parse(script.getTag(id+name)) for(i=0;i<eventBus.length;i++){ LL.runAction(eventBus[i].action, item, eventBus[i].data); } } } </sxh>

script_event_bus.txt · Last modified: 2015/06/01 06:16 by cdfa