1 /*
  2 Copyright (c) 2003-2012, 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 pluginPath;
 13 
 14 	var previewCmd =
 15 	{
 16 		modes : { wysiwyg:1, source:1 },
 17 		canUndo : false,
 18 		readOnly : 1,
 19 		exec : function( editor )
 20 		{
 21 			var sHTML,
 22 				config = editor.config,
 23 				baseTag = config.baseHref ? '<base href="' + config.baseHref + '"/>' : '',
 24 				isCustomDomain = CKEDITOR.env.isCustomDomain();
 25 
 26 			if ( config.fullPage )
 27 			{
 28 				sHTML = editor.getData()
 29 						.replace( /<head>/, '$&' + baseTag )
 30 						.replace( /[^>]*(?=<\/title>)/, '$& — ' + editor.lang.preview );
 31 			}
 32 			else
 33 			{
 34 				var bodyHtml = '<body ',
 35 						body = editor.document && editor.document.getBody();
 36 
 37 				if ( body )
 38 				{
 39 					if ( body.getAttribute( 'id' ) )
 40 						bodyHtml += 'id="' + body.getAttribute( 'id' ) + '" ';
 41 					if ( body.getAttribute( 'class' ) )
 42 						bodyHtml += 'class="' + body.getAttribute( 'class' ) + '" ';
 43 				}
 44 
 45 				bodyHtml += '>';
 46 
 47 				sHTML =
 48 					editor.config.docType +
 49 					'<html dir="' + editor.config.contentsLangDirection + '">' +
 50 					'<head>' +
 51 					baseTag +
 52 					'<title>' + editor.lang.preview + '</title>' +
 53 					CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
 54 					'</head>' + bodyHtml +
 55 					editor.getData() +
 56 					'</body></html>';
 57 			}
 58 
 59 			var iWidth	= 640,	// 800 * 0.8,
 60 				iHeight	= 420,	// 600 * 0.7,
 61 				iLeft	= 80;	// (800 - 0.8 * 800) /2 = 800 * 0.1.
 62 			try
 63 			{
 64 				var screen = window.screen;
 65 				iWidth = Math.round( screen.width * 0.8 );
 66 				iHeight = Math.round( screen.height * 0.7 );
 67 				iLeft = Math.round( screen.width * 0.1 );
 68 			}
 69 			catch ( e ){}
 70 
 71 			var sOpenUrl = '';
 72 			if ( isCustomDomain )
 73 			{
 74 				window._cke_htmlToLoad = sHTML;
 75 				sOpenUrl = 'javascript:void( (function(){' +
 76 					'document.open();' +
 77 					'document.domain="' + document.domain + '";' +
 78 					'document.write( window.opener._cke_htmlToLoad );' +
 79 					'document.close();' +
 80 					'window.opener._cke_htmlToLoad = null;' +
 81 					'})() )';
 82 			}
 83 
 84 			// With Firefox only, we need to open a special preview page, so
 85 			// anchors will work properly on it. (#9047)
 86 			if ( CKEDITOR.env.gecko )
 87 			{
 88 				window._cke_htmlToLoad = sHTML;
 89 				sOpenUrl = pluginPath + 'preview.html';
 90 			}
 91 
 92 			var oWindow = window.open( sOpenUrl, null, 'toolbar=yes,location=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=' +
 93 				iWidth + ',height=' + iHeight + ',left=' + iLeft );
 94 
 95 			if ( !isCustomDomain && !CKEDITOR.env.gecko )
 96 			{
 97 				var doc = oWindow.document;
 98 				doc.open();
 99 				doc.write( sHTML );
100 				doc.close();
101 
102 				// Chrome will need this to show the embedded. (#8016)
103 				CKEDITOR.env.webkit && setTimeout( function() { doc.body.innerHTML += ''; }, 0 );
104 			}
105 		}
106 	};
107 
108 	var pluginName = 'preview';
109 
110 	// Register a plugin named "preview".
111 	CKEDITOR.plugins.add( pluginName,
112 	{
113 		init : function( editor )
114 		{
115 			pluginPath = this.path;
116 
117 			editor.addCommand( pluginName, previewCmd );
118 			editor.ui.addButton( 'Preview',
119 				{
120 					label : editor.lang.preview,
121 					command : pluginName
122 				});
123 		}
124 	});
125 })();
126