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  * @file Preview plugin.
  8  */
  9
 10 (function()
 11 {
 12 	var previewCmd =
 13 	{
 14 		modes : { wysiwyg:1, source:1 },
 15
 16 		exec : function( editor )
 17 		{
 18 			var sHTML,
 19 				isCustomDomain = CKEDITOR.env.ie && document.domain != window.location.hostname;
 20 			if ( editor.config.fullPage )
 21 				sHTML = editor.getData();
 22 			else
 23 			{
 24 				var bodyHtml = '<body ',
 25 					body = CKEDITOR.document.getBody(),
 26 					baseTag = ( editor.config.baseHref.length > 0 ) ? '<base href="' + editor.config.baseHref + '" _cktemp="true"></base>' : '';
 27
 28 				if ( body.getAttribute( 'id' ) )
 29 					bodyHtml += 'id="' + body.getAttribute( 'id' ) + '" ';
 30 				if ( body.getAttribute( 'class' ) )
 31 					bodyHtml += 'class="' + body.getAttribute( 'class' ) + '" ';
 32 				bodyHtml += '>';
 33
 34 				sHTML =
 35 					editor.config.docType +
 36 					'<html dir="' + editor.config.contentsLangDirection + '">' +
 37 					'<head>' +
 38 					baseTag +
 39 					'<title>' + editor.lang.preview + '</title>' +
 40 					'<link href="' + editor.config.contentsCss + '" type="text/css" rel="stylesheet" _cktemp="true"/>' +
 41 					'</head>' + bodyHtml +
 42 					editor.getData() +
 43 					'</body></html>';
 44 			}
 45
 46 			var iWidth	= 640,	// 800 * 0.8,
 47 				iHeight	= 420,	// 600 * 0.7,
 48 				iLeft	= 80;	// (800 - 0.8 * 800) /2 = 800 * 0.1.
 49 			try
 50 			{
 51 				iWidth = Math.Round( screen.width * 0.8 );
 52 				iHeight = Math.Round( screen.height * 0.7 );
 53 				iLeft = Math.Round( screen.width * 0.1 );
 54 			}
 55 			catch ( e ){}
 56
 57 			var sOpenUrl = '';
 58 			if ( isCustomDomain )
 59 			{
 60 				window._cke_htmlToLoad = sHTML;
 61 				sOpenUrl = 'javascript:void( (function(){' +
 62 					'document.open();' +
 63 					'document.domain="' + document.domain + '";' +
 64 					'document.write( window.opener._cke_htmlToLoad );' +
 65 					'document.close();' +
 66 					'window.opener._cke_htmlToLoad = null;' +
 67 					'})() )';
 68 			}
 69
 70 			var oWindow = window.open( sOpenUrl, null, 'toolbar=yes,location=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=' +
 71 				iWidth + ',height=' + iHeight + ',left=' + iLeft );
 72
 73 			if ( !isCustomDomain )
 74 			{
 75 				oWindow.document.write( sHTML );
 76 				oWindow.document.close();
 77 			}
 78 		}
 79 	};
 80
 81 	var pluginName = 'preview';
 82
 83 	// Register a plugin named "preview".
 84 	CKEDITOR.plugins.add( pluginName,
 85 	{
 86 		init : function( editor )
 87 		{
 88 			editor.addCommand( pluginName, previewCmd );
 89 			editor.ui.addButton( 'Preview',
 90 				{
 91 					label : editor.lang.preview,
 92 					command : pluginName
 93 				});
 94 		}
 95 	});
 96 })();
 97