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