JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fc4f694b6d74cfdb7f9aa3a65e2c26cc6e0bf9b9
[ckeditor.git] / _source / plugins / preview / plugin.js
1 /*\r
2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @file Preview plugin.\r
8  */\r
9 \r
10 (function()\r
11 {\r
12         var previewCmd =\r
13         {\r
14                 modes : { wysiwyg:1, source:1 },\r
15                 canUndo : false,\r
16                 exec : function( editor )\r
17                 {\r
18                         var sHTML,\r
19                                 isCustomDomain = CKEDITOR.env.isCustomDomain();\r
20                         if ( editor.config.fullPage )\r
21                                 sHTML = editor.getData();\r
22                         else\r
23                         {\r
24                                 var bodyHtml = '<body ',\r
25                                         body = CKEDITOR.document.getBody(),\r
26                                         baseTag = ( editor.config.baseHref.length > 0 ) ? '<base href="' + editor.config.baseHref + '" _cktemp="true"></base>' : '';\r
27 \r
28                                 if ( body.getAttribute( 'id' ) )\r
29                                         bodyHtml += 'id="' + body.getAttribute( 'id' ) + '" ';\r
30                                 if ( body.getAttribute( 'class' ) )\r
31                                         bodyHtml += 'class="' + body.getAttribute( 'class' ) + '" ';\r
32                                 bodyHtml += '>';\r
33 \r
34                                 sHTML =\r
35                                         editor.config.docType +\r
36                                         '<html dir="' + editor.config.contentsLangDirection + '">' +\r
37                                         '<head>' +\r
38                                         baseTag +\r
39                                         '<title>' + editor.lang.preview + '</title>' +\r
40                                         '<link type="text/css" rel="stylesheet" href="' +\r
41                                         [].concat( editor.config.contentsCss ).join( '"><link type="text/css" rel="stylesheet" href="' ) +\r
42                                         '">' +\r
43                                         '</head>' + bodyHtml +\r
44                                         editor.getData() +\r
45                                         '</body></html>';\r
46                         }\r
47 \r
48                         var iWidth      = 640,  // 800 * 0.8,\r
49                                 iHeight = 420,  // 600 * 0.7,\r
50                                 iLeft   = 80;   // (800 - 0.8 * 800) /2 = 800 * 0.1.\r
51                         try\r
52                         {\r
53                                 var screen = window.screen;\r
54                                 iWidth = Math.round( screen.width * 0.8 );\r
55                                 iHeight = Math.round( screen.height * 0.7 );\r
56                                 iLeft = Math.round( screen.width * 0.1 );\r
57                         }\r
58                         catch ( e ){}\r
59 \r
60                         var sOpenUrl = '';\r
61                         if ( isCustomDomain )\r
62                         {\r
63                                 window._cke_htmlToLoad = sHTML;\r
64                                 sOpenUrl = 'javascript:void( (function(){' +\r
65                                         'document.open();' +\r
66                                         'document.domain="' + document.domain + '";' +\r
67                                         'document.write( window.opener._cke_htmlToLoad );' +\r
68                                         'document.close();' +\r
69                                         'window.opener._cke_htmlToLoad = null;' +\r
70                                         '})() )';\r
71                         }\r
72 \r
73                         var oWindow = window.open( sOpenUrl, null, 'toolbar=yes,location=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=' +\r
74                                 iWidth + ',height=' + iHeight + ',left=' + iLeft );\r
75 \r
76                         if ( !isCustomDomain )\r
77                         {\r
78                                 oWindow.document.open();\r
79                                 oWindow.document.write( sHTML );\r
80                                 oWindow.document.close();\r
81                         }\r
82                 }\r
83         };\r
84 \r
85         var pluginName = 'preview';\r
86 \r
87         // Register a plugin named "preview".\r
88         CKEDITOR.plugins.add( pluginName,\r
89         {\r
90                 init : function( editor )\r
91                 {\r
92                         editor.addCommand( pluginName, previewCmd );\r
93                         editor.ui.addButton( 'Preview',\r
94                                 {\r
95                                         label : editor.lang.preview,\r
96                                         command : pluginName\r
97                                 });\r
98                 }\r
99         });\r
100 })();\r