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