Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************************************
- Copyright (C) 2011 by Mozilla Foundation
- 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.
- **********************************************************************************/
- (function( window, document, undefined, Butter ) {
- Butter.registerModule( "eventeditor", (function() {
- var editorTarget,
- defaultEditor,
- binding,
- commServer,
- editorHeight,
- editorWidth,
- targetWindow,
- customEditors = {},
- commWindowSetupComplete = false,
- setupServer = function( editorWindow, trackEvent) {
- var updateEditor = function( trackEvent ) {
- commServer.send( "editorCommLink", trackEvent.popcornOptions, "updatetrackevent" );
- },
- butter = this;
- commServer[ binding ]( "editorCommLink", editorWindow, function() {
- butter.listen( "trackeventupdated", updateEditor );
- butter.listen( "targetadded", function() {
- commServer.send( "editorCommLink", butter.getTargets(), "updatedomtargets" );
- });
- commServer.listen( "editorCommLink", "okayclicked", function( newOptions ){
- trackEvent.popcornOptions = newOptions;
- editorWindow.close && editorWindow.close();
- editorWindow && editorWindow.parentNode && editorWindow.parentNode.removeChild( editorWindow );
- commWindowSetupComplete = false;
- targetWindow = undefined;
- butter.unlisten ( "trackeventupdated", updateEditor );
- butter.trigger( "trackeditclosed" );
- butter.trigger( "trackeventupdated", trackEvent );
- });
- commServer.listen( "editorCommLink", "applyclicked", function( newOptions ) {
- trackEvent.popcornOptions = newOptions;
- butter.trigger( "trackeventupdated", trackEvent );
- });
- commServer.listen( "editorCommLink", "deleteclicked", function() {
- butter.removeTrackEvent( trackEvent );
- editorWindow.close && editorWindow.close();
- editorWindow && editorWindow.parentNode && editorWindow.parentNode.removeChild( editorWindow );
- butter.unlisten ( "trackeventupdated", updateEditor );
- butter.trigger( "trackeditclosed" );
- });
- commServer.listen( "editorCommLink", "cancelclicked", function() {
- editorWindow.close && editorWindow.close();
- editorWindow && editorWindow.parentNode && editorWindow.parentNode.removeChild( editorWindow );
- butter.unlisten ( "trackeventupdated", updateEditor );
- butter.trigger( "trackeditclosed" );
- });
- });
- commWindowSetupComplete = true;
- },
- constructEditor = function( trackEvent ) {
- var butter = this,
- editorSrc = customEditors[ trackEvent.type ] || trackEvent.manifest.customEditor || defaultEditor;
- editorTarget && clearTarget();
- if ( binding === "bindWindow" ) {
- editorWindow = targetWindow || window.open( "", "", "width=" + editorWidth + ",height=" + editorHeight + ",menubar=no,toolbar=no,location=no,status=no" );
- targetWindow = !targetWindow ? editorWindow : targetWindow;
- if ( !commWindowSetupComplete ) {
- editorWindow.location.href = editorSrc;
- }
- ( !commWindowSetupComplete && setupServer.call( butter, editorWindow, trackEvent ) ) || commServer.send( "editorCommLink", { trackEvent: trackEvent, targets: butter.getTargets() }, "edittrackevent");
- editorWindow.addEventListener( "beforeunload", function() {
- butter.unlisten ( "trackeventupdated", updateEditor );
- butter.trigger( "trackeditforcedclosed" );
- commWindowSetupComplete = false;
- targetWindow = undefined;
- }, false );
- } else if ( binding === "bindFrame" ) {
- editorWindow = document.createElement( "iframe" );
- if ( !commWindowSetupComplete ) {
- editorWindow.src = editorSrc;
- }
- ( !commWindowSetupComplete && setupServer.call( butter, editorWindow, trackEvent ) ) || commServer.send( "editorCommLink", { trackEvent: trackEvent, targets: butter.getTargets() }, "edittrackevent");
- editorTarget.appendChild( editorWindow );
- }
- editorWindow.addEventListener( "load", function() {
- commServer.send( "editorCommLink", { trackEvent: trackEvent, targets: butter.getTargets() }, "edittrackevent");
- }, false );
- },
- clearTarget = function() {
- while ( editorTarget.firstChild ) {
- editorTarget.removeChild( editorTarget.firstChild );
- }
- },
- setTarget = function( newTarget, type ) {
- commWindowSetupComplete = false;
- var setTheTarget = {
- "domtarget": function( targ ) {
- if ( typeof targ === "string" ) {
- editorTarget = document.getElementById( targ ) || {};
- } else if ( targ ) {
- editorTarget = targ;
- }
- },
- "window": function( targ ) {
- targetWindow = targ;
- }
- };
- setTheTarget[ type ]( newTarget );
- };
- return {
- setup: function( options ) {
- if ( !options ) {
- throw new Error( "Invalid Argument" );
- }
- editorWidth = options.editorWidth || 400;
- editorHeight = options.editorHeight || 400;
- ( options.target && setTarget( options.target, "domtarget" ) ) || ( options.targetWindow && setTarget( options.targetWindow, "window" ) );
- binding = editorTarget ? "bindFrame" : "bindWindow";
- defaultEditor = options.defaultEditor || "defaultEditor.html";
- commServer = new Butter.CommServer();
- },
- extend: {
- editTrackEvent: function( trackEvent ) {
- this.trigger( "trackeditstarted" );
- constructEditor.call( this, trackEvent );
- },
- addCustomEditor: function( editorSource, pluginType ) {
- if ( !pluginType || !editorSource ) {
- return;
- }
- customEditors[ pluginType ] = editorSource;
- },
- changeEditorTarget: function( newTarget, type ) {
- var types = [ "domtarget", "window" ],
- lowerCaseType;
- if ( !newTarget || !type || types.indexOf( lowerCaseType = type.toLowerCase() ) === -1 ) {
- return false;
- }
- setTarget( newTarget, lowerCaseType );
- },
- setDefaultEditor: function( newEditor ) {
- if ( !newEditor || typeof newEditor !== "string" ) {
- return;
- }
- defaultEditor = newEditor;
- },
- setEditorDims: function ( dims ) {
- if ( !dims ) {
- return;
- }
- editorWidth = dims.width || editorWidth;
- editorHeight = dims.Width || editorHeight;
- }
- }
- }
- })());
- })( window, document, undefined, Butter );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement