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