User Tools

Site Tools


script_event_bus

====== Differences ====== This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
script_event_bus [2015/05/30 23:55]
cdfa
script_event_bus [2015/06/01 06:16] (current)
cdfa
Line 14: Line 14:
 ====== Script code ====== ====== Script code ======
 <sxh javascript>​ <sxh javascript>​
-script = LL.getCurrentScript() +var script = LL.getCurrentScript(); 
-event = LL.getEvent() +var event = LL.getEvent(); 
-src = event.getSource() +var src = event.getSource(); 
-data = JSON.parse(event.getData()) +var data = JSON.parse(event.getData()); 
-cont = event.getContainer()+var cont = event.getContainer()
 +var i, id
  
 function getOb(id){ function getOb(id){
-  if(obId.toString().length==6){ +  if(id.toString().length==6){ 
-    ob=LL.getItemById(obId)+    ob=LL.getItemById(id);
   }else{   }else{
-    ob=LL.getContainerById(obId)+    ob=LL.getContainerById(id);
   }   }
   return ob   return ob
Line 30: Line 31:
  
 if(src=="​RUN_SCRIPT"​){ if(src=="​RUN_SCRIPT"​){
-  ​//​alert(JSON.stringify(data)) +  if(data==null)throw "​object is null"; 
-  ​if(data==null)throw "​object is null"​ +  ​var obId=data.obId; 
-  obId=data.obId +  ​var name=data.name; 
-  name=data.name +  ​var tagName = obId+name; 
-  tagName = obId+name +  ​var eventBus = JSON.parse(script.getTag(tagName)); 
-  eventBus = JSON.parse(script.getTag(tagName)) +  ​if(eventBus==null)eventBus=[] 
-  evHa = {action:​data.action,​ data:​data.extra}+  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(data.funct=="​sub"​){
-    ob = getOb(obId) +    ​if(!reAdding){ 
-    if(eventBus==null)eventBus=[] +      var ob = getOb(obId); 
-    prop = ob.getProperties() +      if(eventBus==null)eventBus=[]; 
-    curEvha = prop.getEventHandler(name) +      ​var ​prop = ob.getProperties(); 
-    curAction = curEvha.getAction() +      ​var ​curEvha = prop.getEventHandler(name); 
-    curData = curEvha.getData() +      ​var ​curAction = curEvha.getAction(); 
-    if(curAction!=EventHandler.RUN_SCRIPT || curData!=script.getId().toString()){ +      ​var ​curData = curEvha.getData(); 
-      if(curAction!=0)eventBus.push({action:​curAction,​ data:​curData}) +      if(curAction!=EventHandler.RUN_SCRIPT || curData!=script.getId().toString()){ 
-      prop.edit().setEventHandler(name,​ EventHandler.RUN_SCRIPT,​ script.getId()).commit()+        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);​
     }     }
-    eventBus.push(evHa) 
   }else if(data.funct=="​unsub"​){   }else if(data.funct=="​unsub"​){
-    ​for(i=0;​i<​eventBus.length;​i++){ +    ​if(reAdding){ 
-      ​if(eventBus[i].action==data.action && eventBus[i].data==data.extra){ +      eventBus.splice(i,​ 1);
-        ​eventBus.splice(i,​ 1) +
-        break +
-      }+
     }     }
     if(eventBus.length==0){     if(eventBus.length==0){
-      ob = getOb(obId) +      ​var ob = getOb(obId); 
-      ob.getProperties().edit().setEventHandler(name,​ EventHandler.UNSET,​ null).commit() +      ob.getProperties().edit().setEventHandler(name,​ EventHandler.UNSET,​ null).commit(); 
-      ob.setTag(tagName,​ null)+      ob.setTag(tagName,​ null);
     }     }
   }   }
-  script.setTag(tagName,​ JSON.stringify(eventBus))+  script.setTag(tagName,​ JSON.stringify(eventBus));
 }else{ }else{
 +  var name;
   switch(src.slice(0,​ 1)){   switch(src.slice(0,​ 1)){
   case "​K":​   case "​K":​
-    name = src.slice(2).toLowerCase()+"​Key"​+    name = src.slice(2).toLowerCase()+"​Key"​;
     if(src.slice(-1)=="​L"​){     if(src.slice(-1)=="​L"​){
-      name = "​long"​+src.slice(2,​ 3)+src.slice(3,​6).toLowerCase()+"​Key"​+      name = "​long"​+src.slice(2,​ 3)+src.slice(3,​6).toLowerCase()+"​Key"​;
     }     }
-    break+    break;
   case "​I":​   case "​I":​
     if(src.slice(-5)=="​CLICK"​){     if(src.slice(-5)=="​CLICK"​){
       if(src.length==7){       if(src.length==7){
-        name = "​i.tap"​+        name = "​i.tap"​;
       }else{       }else{
-        name="​i.longTap"​+        name="​i.longTap"​;
       }       }
     }else if(src.slice(2,​7)=="​SWIPE"​){     }else if(src.slice(2,​7)=="​SWIPE"​){
-      name="​i.swipe"​+ src.slice(8,​9)+src.slice(9).toLowerCase()+      name="​i.swipe"​+ src.slice(8,​9)+src.slice(9).toLowerCase();
     }else{     }else{
-      name="​i."​+src.slice(2).toLowerCase()+      name="​i."​+src.slice(2).toLowerCase();
     }     }
     break     break
Line 87: Line 95:
     if(src.slice(-5)=="​CLICK"​){     if(src.slice(-5)=="​CLICK"​){
       if(src.length==7){       if(src.length==7){
-        name = "​bg.tap"​+        name = "​bg.tap"​;
       }else if(src.length==12){       }else if(src.length==12){
-        name="​bg.longTap"​+        name="​bg.longTap"​;
       }else{       }else{
-        name="​bg.doubleTap"​+        name="​bg.doubleTap"​;
       }       }
     }else if(src.slice(2,​7)=="​SWIPE"​){     }else if(src.slice(2,​7)=="​SWIPE"​){
       if(src.slice(7,​ 8)=="​2"​){       if(src.slice(7,​ 8)=="​2"​){
-        name="​swipe2"​+src.slice(9,​10)+src.slice(10).toLowerCase()+        name="​swipe2"​+src.slice(9,​10)+src.slice(10).toLowerCase();
       }else{       }else{
-        name="​swipe"​+src.slice(8,​9)+src.slice(9).toLowerCase()+        name="​swipe"​+src.slice(8,​9)+src.slice(9).toLowerCase();
       }       }
     }else if(src=="​C_LOADED"​){     }else if(src=="​C_LOADED"​){
-      name="​load"​+      name="​load"​;
     }else if(src=="​C_POSITION_CHANGED"​){     }else if(src=="​C_POSITION_CHANGED"​){
-      nams="​posChanged"​+      nams="​posChanged"​;
     }else{     }else{
-      name="​i."​+src.slice(2).toLowerCase()+      name=src.slice(2).toLowerCase();
     }     }
-    break+    break;
   case "​P":​   case "​P":​
-    name="​orientationPortrait"​ +    name="​orientationPortrait"​; 
-    break+    break;
   case "​L":​   case "​L":​
-    name="​orientationLandscape"​ +    name="​orientationLandscape"​; 
-    break+    break;
   }   }
-  cont = event.getContainer() +  ​var cont = event.getContainer(); 
-  if(cont==null)cont=LL.getCurrentDesktop() +  if(cont==null) cont=LL.getCurrentDesktop(); 
-  id = cont.getId() +  ​var id = cont.getId(); 
-  item=event.getItem()+  ​var item=event.getItem();
   if(item==null){   if(item==null){
-    eventBus = JSON.parse(script.getTag(id+name))+    ​var eventBus = JSON.parse(script.getTag(id+name));
     for(i=0;​i<​eventBus.length;​i++){     for(i=0;​i<​eventBus.length;​i++){
-      ​alert(JSON.stringify(eventBus)) +      ​var action = eventBus[i].action 
-      LL.runAction(eventBus[i].action, ​eventBus[i].data)+      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{   }else{
-    id=item.getId() +    ​var id=item.getId() 
-    eventBus = JSON.parse(script.getTag(id+name))+    ​var eventBus = JSON.parse(script.getTag(id+name))
     for(i=0;​i<​eventBus.length;​i++){     for(i=0;​i<​eventBus.length;​i++){
-      LL.runAction(eventBus[i].action,​ item, eventBus[i].data)+      LL.runAction(eventBus[i].action,​ item, eventBus[i].data);
     }     }
   }   }
script_event_bus.1433030116.txt.gz · Last modified: 2015/05/30 23:55 by cdfa