User Tools

Site Tools


script_event_bus

**This is an old revision of the document!** ----

A PCRE internal error occured. This might be caused by a faulty plugin

====== 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> script = LL.getCurrentScript() event = LL.getEvent() src = event.getSource() data = JSON.parse(event.getData()) cont = event.getContainer() function getOb(id){ if(obId.toString().length==6){ ob=LL.getItemById(obId) }else{ ob=LL.getContainerById(obId) } return ob } if(src=="RUN_SCRIPT"){ //alert(JSON.stringify(data)) if(data==null)throw "object is null" obId=data.obId name=data.name tagName = obId+name eventBus = JSON.parse(script.getTag(tagName)) evHa = {action:data.action, data:data.extra} if(data.funct=="sub"){ ob = getOb(obId) if(eventBus==null)eventBus=[] prop = ob.getProperties() curEvha = prop.getEventHandler(name) curAction = curEvha.getAction() curData = curEvha.getData() if(curAction!=EventHandler.RUN_SCRIPT || curData!=script.getId().toString()){ if(curAction!=0)eventBus.push({action:curAction, data:curData}) prop.edit().setEventHandler(name, EventHandler.RUN_SCRIPT, script.getId()).commit() } eventBus.push(evHa) }else if(data.funct=="unsub"){ for(i=0;i<eventBus.length;i++){ if(eventBus[i].action==data.action && eventBus[i].data==data.extra){ eventBus.splice(i, 1) break } } if(eventBus.length==0){ ob = getOb(obId) ob.getProperties().edit().setEventHandler(name, EventHandler.UNSET, null).commit() ob.setTag(tagName, null) } } script.setTag(tagName, JSON.stringify(eventBus)) }else{ 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="i."+src.slice(2).toLowerCase() } break case "P": name="orientationPortrait" break case "L": name="orientationLandscape" break } cont = event.getContainer() if(cont==null)cont=LL.getCurrentDesktop() id = cont.getId() item=event.getItem() if(item==null){ eventBus = JSON.parse(script.getTag(id+name)) for(i=0;i<eventBus.length;i++){ alert(JSON.stringify(eventBus)) LL.runAction(eventBus[i].action, eventBus[i].data) } }else{ id=item.getId() 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.1433015725.txt.gz · Last modified: 2015/05/30 19:55 by cdfa