Icontem

File: intents.js

Recommend this page to a friend!
  Classes of Till Wehowski  >  webfan JavaScript Intents Service Server  >  intents.js  >  Download  
File: intents.js
Role: Class source
Content type: text/plain
Description: webfanIntents Applications Server
Class: webfan JavaScript Intents Service Server
Register and call services to handle Web intents
Author: By
Last change: - added initFn + Async resolve
Date: 2 years ago
Size: 6,390 bytes
 

Contents

Class file image Download
/*
@copyright (c) Till Wehowski - All rights reserved
@license (Basic/EULA) http://look-up.webfan.de/webdof-license
@license (Source Code Re-Usage) http://look-up.webfan.de/bsd-license
Copyright (c) 2015, Till Wehowski All rights reserved.
@component http://webfan.de/cdn/frdl/flow/components/webfan/webfat/#!js/intents.js

* Services Server for webfanIntents http://frdl.webfan.de/webfanintents.285.html
* Read more: 
*/



(function(){
    'use strict';
    
(function(){if('undefined'===typeof frdl && 0===document.querySelectorAll('script[src*="http://api.webfan.de\/api-d\/4\/js-api\/library.js"]').length && 0===document.querySelectorAll('script[src*="flow.js"]').length){var h=document.getElementsByTagName("head",document)[0];var s=document.createElement('script');s.setAttribute('src','http://api.webfan.de/api-d/4/js-api/library.js');h.insertBefore(s,h.firstChild);}}());

(function(){if('undefined'===typeof window.webfanIntent && 0===document.querySelectorAll('script[src*="frdl.webfan.de\/cdn\/frdl\/flow\/components\/frdl\/intent\/webintents.js"]').length){var h=document.getElementsByTagName("head",document)[0];var s=document.createElement('script');s.setAttribute('src','http://frdl.webfan.de/cdn/frdl/flow/components/frdl/intent/webintents.js');h.insertBefore(s,h.firstChild);}}());



if('undefined' === typeof exports )var exports = {
	
};

if('undefined' === typeof module )var module = {
	exports : {
		
	}
};


var ACTBAT = exports = module.exports = new (function(){

var _In = [];	
var _R = {};	


  var delIn = function(id) {	
       var i=0, l = _In.length;
       if(0 === l)return this;
             
       for(var i = 0; i < (l = _In.length); i++)
        {
           if("undefined" !== typeof _In[i] && _In[i]._id === id)
              {
                _In.splice(i,1); 
                i--;
               _In = _In.filter(function(val){return (val !== undefined && val !== null) ? true : false;});
                l = _In.length;
                i = 0;
              }
        }
       return this;
     };
        	
        		
   this.bat = function(bat){
	   	frdl.each(bat, function(i,In){
	   		_In.push(In);
	   	});
	  return this;  
   };
   
   this.i = function(action){
   	  this.bat([action]);
   	  return this;
   };
   	
   this.register=function(mime, action, callback, initFn, config, appData){
   	    if('undefined'===typeof _R[mime] || null ===_R[mime])_R[mime]={};
   	    _R[mime][action]={
			 config : config,
			 app : appData,
			 callback : callback,
			 initFn : initFn
		};
		

		if(0<frdl.debug.mode())console.log('Found intent action service for: '+JSON.stringify(_R[mime][action]));
		
		return this;
   }; 	


   this.run=function(){
   	 var _SELF = this;
   	 
    	var args = Array.prototype.slice.call(arguments);
    	var _intent = args[0] || null;
    //	var type = args[1] || null;
    //	var action = args[2] || null;
    	if(!!_intent && !!_R[_intent.type] && !!_R[_intent.type][_intent.action]){
    		
    		if(0<frdl.debug.mode())
    		 console.log('Found intent action service for: '+JSON.stringify(_intent));
    		
    		 frdl.when(
    		    function(_intent){return 'undefined'!==typeof _intent.result 
    		                                             && 'function'!==typeof _intent.result 
    		                                             && true!== _intent.result instanceof Promise 
    		            ? true : false;
    		    },
    		   function(_intent){
    		   	   delIn(_intent._id); 
    		   	   
    		   	   var r =_R[_intent.type][_intent.action].callback(_intent, _intent.config, _intent.appData);
    		       if( 'function'!==typeof r){
    		      
				   	 _intent.postResult(r);
				   }else{
				   	  
				   	 return (_intent.result = r());
				   }
    		   },
    		   
    		  _intent, 
    		  
    		     function(_intent){_intent.postFailure({error:'The Service could not handle your action, sorry!'});},
    		     
    		  1, function(_intent){
    		  	 if('function' === typeof _R[_intent.type][_intent.action].initFn){
				 	_R[_intent.type][_intent.action].initFn(_intent, _intent.config, _intent.appData);
				 }
    		    	
    		  });
			
			return this;
		}else if(0===args.length){
			if(0<frdl.debug.mode())
			      console.log('Serach intent action for: '+JSON.stringify(_In));
			      
			if(0<frdl.debug.mode())
			    console.log('In Services: '+JSON.stringify(_R));
			    
			frdl.each(_In, function(i,In){
				_SELF.run(In);
	   	    });
			return this;
		}else{
			if(0<frdl.debug.mode())console.log('ACTBAT.run no matching service for the action actions found! count(Actions.unresolved)='+_In.length);
		}
    	
   };	
   	
  return this; 	
   	
});
    
    
    
(function(){
 window.addEventListener('message', function(ev){
     // if(!ev.origin || !window.intent)return;
      if(!window.intent)return;
  	      ACTBAT.i(window.intent)
  	    ;
  	     // intent.postResult("Test result from service"); 
  	        // if(!!intent)console.log(JSON.stringify(intent));
              //  if(!!ev.data)console.log(JSON.stringify(ev.data));
              //  if(!!ev.origin)console.log(JSON.stringify(ev.origin));
             //  if(!!intent)intent.postResult("Service coming soon (in few hours)!");  
             //   if(!!intent)alert(frdl.debug.getLog()); 
      });	  
}());   

   	
	
	
	
(function when(condition, callback, args, delay){
	
	if(('function'===typeof condition && true === condition())
	   || (true === (condition))
	){
		callback(args);
		return when;
	}else{
     	setTimeout(function(){ 
   	       when(condition, callback, args);
   	    },(delay || 1));
   	  return when;		
	}
}(function(){
	      return 'undefined'!==typeof frdl;
  },
  function(){
    frdl.frdl$intentService=ACTBAT;
    frdl_webfan_flow_advertising_external_linking(new frdl.Url(frdl.__FILE__()).getHost());
    
    //  frdl loaded   

   
     
  
})(function(){
	      return 'undefined'!==typeof window.webfanIntent && 'undefined'!==typeof frdl.a;
  },
 function(){
    //webfanIntents and angular loaded

    //look for matching actions and services and invoke the callbacks if found
    ACTBAT.run();
 
})

());



   

}());