      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 		canUndo : false,
     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 				var screen = window.screen;
     52 				iWidth = Math.round( screen.width * 0.8 );
     53 				iHeight = Math.round( screen.height * 0.7 );
     54 				iLeft = Math.round( screen.width * 0.1 );
     55 			}
     56 			catch ( e ){}
     57
     58 			var sOpenUrl = '';
     59 			if ( isCustomDomain )
     60 			{
     61 				window._cke_htmlToLoad = sHTML;
     62 				sOpenUrl = 'javascript:void( (function(){' +
     63 					'document.open();' +
     64 					'document.domain="' + document.domain + '";' +
     65 					'document.write( window.opener._cke_htmlToLoad );' +
     66 					'document.close();' +
     67 					'window.opener._cke_htmlToLoad = null;' +
     68 					'})() )';
     69 			}
     70
     71 			var oWindow = window.open( sOpenUrl, null, 'toolbar=yes,location=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=' +
     72 				iWidth + ',height=' + iHeight + ',left=' + iLeft );
     73
     74 			if ( !isCustomDomain )
     75 			{
     76 				oWindow.document.write( sHTML );
     77 				oWindow.document.close();
     78 			}
     79 		}
     80 	};
     81
     82 	var pluginName = 'preview';
     83
     84 	// Register a plugin named "preview".
     85 	CKEDITOR.plugins.add( pluginName,
     86 	{
     87 		init : function( editor )
     88 		{
     89 			editor.addCommand( pluginName, previewCmd );
     90 			editor.ui.addButton( 'Preview',
     91 				{
     92 					label : editor.lang.preview,
     93 					command : pluginName
     94 				});
     95 		}
     96 	});
     97 })();
     98