(function(window) { window.StageWebViewBridge = (function() { /* PROPIERTIES */ /* Stores callBack functions */ var callBacks = []; /* Stores StageWebViewBridge.ready() functions */ var onReadyHandlers = []; /* Stores the extensions parsed with the cache System */ var cached_extensions = []; /* Stores the default function called on DOMContentLoaded */ var DOMContentLoadedCallBack =function(){ return null }; /* Stores the default function called on deviceReady */ var devicereadyCallBack = function(){}; /* Stores the default root path of filesystem */ var rootPath = ""; /* Stores the documentsDirectory path of filesystem */ var docsPath = ""; /* Stores the default sourceFolder path of filesystem */ var sourcePath = ""; /* Stores the regexp used to filter files by extension */ var fileRegex; /* Used by the .ready method to store temporally callback function called in fakeEvent Dispathch */ var currentReadyHandler; /* Used to determine OS */ var checker = { iphone: navigator.userAgent.match(/(iPhone|iPod|iPad)/) === null ? false:true, android: navigator.userAgent.match(/Android/) === null ? false: navigator.platform.match(/Linux/) == null ? false:true }; /* Used to determine if the paths has been initialized */ var pathsReady = false; /* Used to determine last time call to AS3 was made */ var lastCallTime = new Date().getTime(); /* Used to determine the delay between calls to AS3 */ var aggregatedCallDelay = 0; /* Used to determine the minimun consecutive delay between calls to AS3 */ /* AT YOUR RISK!!!!! Use StageWebViewBridge.setCallDelay( ms ), to change the default value. */ var defaultCallDelay = 500; /* Used to determine the "protocol" to do the comm with AS3 */ var sendingProtocol = checker.iphone ? 'about:':'tuoba:'; /* METHODS */ /* Used internally to parse call funcions from AS3 */ var doCall = function( jsonArgs ) { setTimeout(function() { deferredDoCall(jsonArgs); },0 ); }; /* Used internally to parse call funcions from AS3 */ var deferredDoCall = function( jsonArgs ) { var _serializeObject = JSON.parse( atob( jsonArgs ) ); var method = _serializeObject.method; var returnValue = true; if( method.indexOf('[SWVMethod]')==-1 ) { var targetFunction; if( method.indexOf('.')==-1) { targetFunction = window[ method ]; } else { var splitedPath = method.split('.'); targetFunction=window; for( var i=0; i2) { for (var i = 2; i < arguments.length; i++) { argumentsArray.push( arguments[ i ] ); }; }; _serializeObject.arguments = argumentsArray; if( _serializeObject.callBack !=undefined ) { addCallback('[SWVMethod]'+arguments[ 0 ], arguments[ 1 ] ); }; setTimeout( function(){ window.location.href=sendingProtocol+'[SWVData]'+btoa( JSON.stringify( _serializeObject ) );},aggregatedCallDelay ); lastCallTime = new Date().getTime(); }; /* Used to change the defaultCallDelay value. */ var setCallDelay = function( ms ) { defaultCallDelay = ms; }; /* Used internally to store callback functions for call methods */ var addCallback = function( name, fn ) { callBacks[ name ] = fn; }; /* Use it to get the path to a file from JS */ var getFilePath = function( fileName ) { if( !pathsReady ) { throw "StageWebViewBridge.getFilePath('"+fileName+"').Paths still not set. Listen to document.deviceready event before access this method."; } else { if( fileName.indexOf('jsfile:') !=-1 ) { if( fileRegex.exec(fileName) != null ) { return rootPath+'/'+fileName.split('jsfile:/')[1]; } else { return sourcePath+'/'+fileName.split('jsfile:/')[1]; }; }; if( fileName.indexOf('jsdocfile:') !=-1 ) { return docsPath+'/'+fileName.split('jsdocfile:/')[1]; }; } }; /* fakeEventDispatcher */ var dispatchFakeEvent = function( name ) { var fakeEvent = document.createEvent("UIEvents"); fakeEvent.initEvent( name , false,false ); document.dispatchEvent(fakeEvent); }; /*[Event("ready")]*/ var ready = function( handler ) { onReadyHandlers.push( handler ); }; /* Fired on DOMContentLoaded */ var onReady = function( ) { document.addEventListener('SWVBReady', function() { currentReadyHandler(); }, false ); for (var i=0; i