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