1 /*
  2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
  3 For licensing, see LICENSE.html or http://ckeditor.com/license
  4 */
  5
  6 /**
  7  * @fileOverview Plugin for making iframe based dialogs.
  8  */
  9
 10 CKEDITOR.plugins.add( 'iframedialog',
 11 {
 12 	requires : [ 'dialog' ],
 13 	onLoad : function()
 14 	{
 15 		CKEDITOR.dialog.addIframe = function( name, title, src, width, height, onContentLoad )
 16 		{
 17 			var element =
 18 			{
 19 				type : 'iframe',
 20 				src : src,
 21 				width : '100%',
 22 				height : '100%'
 23 			};
 24
 25 			if ( typeof( onContentLoad ) == 'function' )
 26 				element.onContentLoad = onContentLoad;
 27
 28 			var definition =
 29 			{
 30 				title : title,
 31 				minWidth : width,
 32 				minHeight : height,
 33 				contents :
 34 				[
 35 					{
 36 						id : 'iframe',
 37 						label : title,
 38 						expand : true,
 39 						elements : [ element ]
 40 					}
 41 				]
 42 			};
 43
 44 			return this.add( name, function(){ return definition; } );
 45 		};
 46
 47 		(function()
 48 		{
 49 			/**
 50 			 * An iframe element.
 51 			 * @extends CKEDITOR.ui.dialog.uiElement
 52 			 * @example
 53 			 * @constructor
 54 			 * @param {CKEDITOR.dialog} dialog
 55 			 * Parent dialog object.
 56 			 * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition
 57 			 * The element definition. Accepted fields:
 58 			 * <ul>
 59 			 * 	<li><strong>src</strong> (Required) The src field of the iframe. </li>
 60 			 * 	<li><strong>width</strong> (Required) The iframe's width.</li>
 61 			 * 	<li><strong>height</strong> (Required) The iframe's height.</li>
 62 			 * 	<li><strong>onContentLoad</strong> (Optional) A function to be executed
 63 			 * 	after the iframe's contents has finished loading.</li>
 64 			 * </ul>
 65 			 * @param {Array} htmlList
 66 			 * List of HTML code to output to.
 67 			 */
 68 			var iframeElement = function( dialog, elementDefinition, htmlList )
 69 			{
 70 				if ( arguments.length < 3 )
 71 					return;
 72
 73 				var _ = ( this._ || ( this._ = {} ) ),
 74 					contentLoad = elementDefinition.onContentLoad && CKEDITOR.tools.bind( elementDefinition.onContentLoad, this ),
 75 					cssWidth = CKEDITOR.tools.cssLength( elementDefinition.width ),
 76 					cssHeight = CKEDITOR.tools.cssLength( elementDefinition.height );
 77 				_.frameId = CKEDITOR.tools.getNextNumber() + '_iframe';
 78
 79 				// IE BUG: Parent container does not resize to contain the iframe automatically.
 80 				dialog.on( 'load', function()
 81 					{
 82 						var iframe = CKEDITOR.document.getById( _.frameId ),
 83 							parentContainer = iframe.getParent();
 84
 85 						parentContainer.setStyles(
 86 							{
 87 								width : cssWidth,
 88 								height : cssHeight
 89 							} );
 90 					} );
 91
 92 				var attributes =
 93 				{
 94 					src : '%2',
 95 					id : _.frameId,
 96 					frameborder : 0,
 97 					allowtransparency : true
 98 				};
 99 				var myHtml = [];
100
101 				if ( typeof( elementDefinition.onContentLoad ) == 'function' )
102 					attributes.onload = 'CKEDITOR.tools.callFunction(%1);';
103
104 				CKEDITOR.ui.dialog.uiElement.call( this, dialog, elementDefinition, myHtml, 'iframe',
105 						{
106 							width : cssWidth,
107 							height : cssHeight
108 						}, attributes, '' );
109
110 				// Put a placeholder for the first time.
111 				htmlList.push( '<div style="width:' + cssWidth + ';height:' + cssHeight + ';" id="' + this.domId + '"></div>' );
112
113 				// Iframe elements should be refreshed whenever it is shown.
114 				myHtml = myHtml.join( '' );
115 				dialog.on( 'show', function()
116 					{
117 						var iframe = CKEDITOR.document.getById( _.frameId ),
118 							parentContainer = iframe.getParent(),
119 							callIndex = CKEDITOR.tools.addFunction( contentLoad ),
120 							html = myHtml.replace( '%1', callIndex ).replace( '%2', CKEDITOR.tools.htmlEncode( elementDefinition.src ) );
121 						parentContainer.setHtml( html );
122 					} );
123 			};
124
125 			iframeElement.prototype = new CKEDITOR.ui.dialog.uiElement;
126
127 			CKEDITOR.dialog.addUIElement( 'iframe',
128 				{
129 					build : function( dialog, elementDefinition, output )
130 					{
131 						return new iframeElement( dialog, elementDefinition, output );
132 					}
133 				} );
134 		})();
135 	}
136 } );
137