a31ebfad398bda0642ede38aadcc448316e6495f
[ckeditor.git] / _source / plugins / preview / plugin.js
1 /*
2 Copyright (c) 2003-2011, 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 readOnly : 1,
17 exec : function( editor )
18 {
19 var sHTML,
20 config = editor.config,
21 baseTag = config.baseHref ? '<base href="' + config.baseHref + '"/>' : '',
22 isCustomDomain = CKEDITOR.env.isCustomDomain();
23
24 if ( config.fullPage )
25 {
26 sHTML = editor.getData()
27 .replace( /<head>/, '$&' + baseTag )
28 .replace( /[^>]*(?=<\/title>)/, '$& &mdash; ' + editor.lang.preview );
29 }
30 else
31 {
32 var bodyHtml = '<body ',
33 body = editor.document && editor.document.getBody();
34
35 if ( body )
36 {
37 if ( body.getAttribute( 'id' ) )
38 bodyHtml += 'id="' + body.getAttribute( 'id' ) + '" ';
39 if ( body.getAttribute( 'class' ) )
40 bodyHtml += 'class="' + body.getAttribute( 'class' ) + '" ';
41 }
42
43 bodyHtml += '>';
44
45 sHTML =
46 editor.config.docType +
47 '<html dir="' + editor.config.contentsLangDirection + '">' +
48 '<head>' +
49 baseTag +
50 '<title>' + editor.lang.preview + '</title>' +
51 CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
52 '</head>' + bodyHtml +
53 editor.getData() +
54 '</body></html>';
55 }
56
57 var iWidth = 640, // 800 * 0.8,
58 iHeight = 420, // 600 * 0.7,
59 iLeft = 80; // (800 - 0.8 * 800) /2 = 800 * 0.1.
60 try
61 {
62 var screen = window.screen;
63 iWidth = Math.round( screen.width * 0.8 );
64 iHeight = Math.round( screen.height * 0.7 );
65 iLeft = Math.round( screen.width * 0.1 );
66 }
67 catch ( e ){}
68
69 var sOpenUrl = '';
70 if ( isCustomDomain )
71 {
72 window._cke_htmlToLoad = sHTML;
73 sOpenUrl = 'javascript:void( (function(){' +
74 'document.open();' +
75 'document.domain="' + document.domain + '";' +
76 'document.write( window.opener._cke_htmlToLoad );' +
77 'document.close();' +
78 'window.opener._cke_htmlToLoad = null;' +
79 '})() )';
80 }
81
82 var oWindow = window.open( sOpenUrl, null, 'toolbar=yes,location=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=' +
83 iWidth + ',height=' + iHeight + ',left=' + iLeft );
84
85 if ( !isCustomDomain )
86 {
87 oWindow.document.open();
88 oWindow.document.write( sHTML );
89 oWindow.document.close();
90 }
91 }
92 };
93
94 var pluginName = 'preview';
95
96 // Register a plugin named "preview".
97 CKEDITOR.plugins.add( pluginName,
98 {
99 init : function( editor )
100 {
101 editor.addCommand( pluginName, previewCmd );
102 editor.ui.addButton( 'Preview',
103 {
104 label : editor.lang.preview,
105 command : pluginName
106 });
107 }
108 });
109 })();