/** * PostMessager * * Implements/holds functionality to send messages and executes functions * between two windows/frames. * * Author ...... : CI:A - Certus In Audio * Contact ..... : http://certus.in/ * Copyright ... : CI:A - Certus In Audio * Version ..... : 1.0.0 * Website ..... : http://certus.in/ */ /** * @license * Copyright CI:A - Certus In Audio * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */ /** * Implements/holds functionality to send messages and executes functions * between two windows/frames (sender class). * * @access public * @author CI:A - Certus In Audio * @class * @copyright CI:A - Certus In Audio * @kind class * @name PostMessager_Sender * @param {object} [objTarget] - The target window/frame * @param {string} [strCallback] - The name of the function to handle answers * @see PostMessager_Sender::send * @since 1.0.0 * @version 1.0.0 */ function PostMessager_Sender( objTarget , strCallback ) { /** * Sends a message/executes a function to/on specified window/frame. * * @access public * @author CI:A - Certus In Audio * @copyright CI:A - Certus In Audio * @kind function * @name send * @param {string} [strMessage] - The message to send/the function name * @returns {boolean} - Returns 'true' on success * @since 1.0.0 * @version 1.0.0 */ this.send = function( strMessage ) { if( this.objTarget !== null ) { this.objTarget.postMessage( strMessage , '*' ); return true; } return false; }; /** * Receives an answer from specified window/frame (and executes callback). * * @access private * @author CI:A - Certus In Audio * @copyright CI:A - Certus In Audio * @kind function * @name _receive * @param {object} [objEvent] - The event object * @returns {boolean} - Returns 'true' on success * @since 1.0.0 * @version 1.0.0 */ this._receive = function( objEvent ) { if( this.strCallback !== '' ) { window[ this.strCallback ]( objEvent.data ); return true; } return false; }; var objSelf = this; this.objTarget = ( typeof objTarget !== 'object' ) ? null : objTarget; this.strCallback = ( typeof strCallback !== 'string' ) ? '' : strCallback; window.addEventListener( 'message' , function( objEvent ) { objSelf._receive( objEvent ); } , false ); return this; } /** * Implements/holds functionality to send messages and executes functions * between two windows/frames (receiver class). * * @access public * @author CI:A - Certus In Audio * @class * @copyright CI:A - Certus In Audio * @kind class * @name PostMessager_Receiver * @param {string} [strFunctionsPrefix] - The prefix of handler functions * @since 1.0.0 * @version 1.0.0 */ function PostMessager_Receiver( strFunctionsPrefix ) { /** * Receives messages and executes corresponding functions. * * @access private * @author CI:A - Certus In Audio * @copyright CI:A - Certus In Audio * @kind function * @name _receive * @param {object|string} [objEvent] - The event object or command string * @returns {mixed} - Returns return value from function call or 'false' * @since 1.0.0 * @version 1.0.0 */ this._receive = function( objEvent ) { if( typeof objEvent === 'string' ) { var strFunctionName = this.strFunctionsPrefix + objEvent.split( '=' )[ 0 ]; strFunctionName = strFunctionName.trim(); var strFunctionParameter = objEvent.split( '=' )[ 1 ]; if( typeof strFunctionParameter !== 'string' ) { strFunctionParameter = ''; } strFunctionParameter = strFunctionParameter.trim(); if( typeof window[ strFunctionName ] === 'function' ) { var mxRV = window[ strFunctionName ]( strFunctionParameter ); return mxRV; } return false; } else { var strFunctionName = this.strFunctionsPrefix + objEvent.data.split( '=' )[ 0 ]; strFunctionName = strFunctionName.trim(); var strFunctionParameter = objEvent.data.split( '=' )[ 1 ]; if( typeof strFunctionParameter !== 'string' ) { strFunctionParameter = ''; } strFunctionParameter = strFunctionParameter.trim(); if( typeof window[ strFunctionName ] === 'function' ) { var mxRV = window[ strFunctionName ]( strFunctionParameter ); objEvent.source.postMessage( mxRV , objEvent.origin ); return mxRV; } objEvent.source.postMessage( false , objEvent.origin ); return false; } }; /** * Returns all parameters from given URL. * * @access private * @author CI:A - Certus In Audio * @copyright CI:A - Certus In Audio * @kind function * @name _getParameters * @param {string} [strURL] - An optional URL * @returns {array} - Returns all parameters from given URL * @since 1.0.0 * @version 1.0.0 */ this._getParameters = function( strURL ) { var arrRV = {}; var objE = document.createElement( 'a' ); objE.href = ( typeof strURL === 'string' ) ? strURL : window.location.href; var strQ = objE.search.substring( 1 ); var arrV = strQ.split( '&' ); var intI = 0; for( intI = 0 ; intI < arrV.length ; intI++ ) { var arrP = arrV[ intI ].split( '=' ); arrRV[ arrP[ 0 ] ] = decodeURIComponent( arrP[ 1 ] ); } return arrRV; }; var objSelf = this; this.strFunctionsPrefix = ( typeof strFunctionsPrefix !== 'string' ) ? 'postmessager_' : strFunctionsPrefix; window.addEventListener( 'message' , function( objEvent ) { objSelf._receive( objEvent ); } , false ); var mxFCFP = null; var arrFCFP = this._getParameters(); for( mxFCFP in arrFCFP ) { if( typeof arrFCFP[ mxFCFP ] !== 'function' ) { this._receive( mxFCFP + '=' + ( ( arrFCFP[ mxFCFP ] == 'undefined' ) ? '' : arrFCFP[ mxFCFP ] ) ); } } return this; }