JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
b37d8fdb8281f7bd3ff499ff656f2625a5522567
[ckeditor.git] / _source / plugins / clipboard / dialogs / paste.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 CKEDITOR.dialog.add( 'paste', function( editor )\r
7 {\r
8         var lang = editor.lang.clipboard;\r
9         var isCustomDomain = CKEDITOR.env.isCustomDomain();\r
10 \r
11         function onPasteFrameLoad( win )\r
12         {\r
13                 var doc =  new CKEDITOR.dom.document( win.document ),\r
14                         docElement = doc.$;\r
15 \r
16                 doc.getById( "cke_actscrpt" ).remove();\r
17 \r
18                 CKEDITOR.env.ie ?\r
19                         docElement.body.contentEditable = "true" :\r
20                         docElement.designMode = "on";\r
21 \r
22                 // IE before version 8 will leave cursor blinking inside the document after\r
23                 // editor blurred unless we clean up the selection. (#4716)\r
24                 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
25                 {\r
26                         doc.getWindow().on( 'blur', function()\r
27                         {\r
28                                 docElement.selection.empty();\r
29                         } );\r
30                 }\r
31 \r
32                 doc.on( "keydown", function( e )\r
33                 {\r
34                         var domEvent = e.data,\r
35                                 key = domEvent.getKeystroke(),\r
36                                 processed;\r
37 \r
38                         switch( key )\r
39                         {\r
40                                 case 27 :\r
41                                         this.hide();\r
42                                         processed = 1;\r
43                                         break;\r
44 \r
45                                 case 9 :\r
46                                 case CKEDITOR.SHIFT + 9 :\r
47                                         this.changeFocus( true );\r
48                                         processed = 1;\r
49                         }\r
50 \r
51                         processed && domEvent.preventDefault();\r
52                 }, this );\r
53 \r
54                 editor.fire( 'ariaWidget', new CKEDITOR.dom.element( win.frameElement ) );\r
55         }\r
56 \r
57         return {\r
58                 title : lang.title,\r
59 \r
60                 minWidth : CKEDITOR.env.ie && CKEDITOR.env.quirks ? 370 : 350,\r
61                 minHeight : CKEDITOR.env.quirks ? 250 : 245,\r
62                 onShow : function()\r
63                 {\r
64                         // FIREFOX BUG: Force the browser to render the dialog to make the to-be-\r
65                         // inserted iframe editable. (#3366)\r
66                         this.parts.dialog.$.offsetHeight;\r
67 \r
68                         var htmlToLoad =\r
69                                 '<html dir="' + editor.config.contentsLangDirection + '"' +\r
70                                 ' lang="' + ( editor.config.contentsLanguage || editor.langCode ) + '">' +\r
71                                         '<head><style>body { margin: 3px; height: 95%; } </style></head><body>' +\r
72                                         '<script id="cke_actscrpt" type="text/javascript">' +\r
73                                         'window.parent.CKEDITOR.tools.callFunction( ' + CKEDITOR.tools.addFunction( onPasteFrameLoad, this ) + ', this );' +\r
74                                         '</script></body>' +\r
75                                 '</html>';\r
76 \r
77                         var iframe = CKEDITOR.dom.element.createFromHtml(\r
78                                                 '<iframe' +\r
79                                                 ' class="cke_pasteframe"' +\r
80                                                 ' frameborder="0" ' +\r
81                                                 ' allowTransparency="true"' +\r
82                                                 // Support for custom document.domain in IE.\r
83                                                 ( isCustomDomain ?\r
84                                                         ' src="javascript:void((function(){' +\r
85                                                                 'document.open();' +\r
86                                                                 'document.domain=\'' + document.domain + '\';' +\r
87                                                                 'document.close();' +\r
88                                                         '})())"' : '' ) +\r
89                                                 ' role="region"' +\r
90                                                 ' aria-label="' + lang.pasteArea + '"' +\r
91                                                 ' aria-describedby="' + this.getContentElement( 'general', 'pasteMsg' ).domId + '"' +\r
92                                                 ' aria-multiple="true"' +\r
93                                                 '></iframe>' );\r
94 \r
95                         iframe.on( 'load', function( e )\r
96                         {\r
97                                 e.removeListener();\r
98                                 var doc = iframe.getFrameDocument().$;\r
99                                 // Custom domain handling is needed after each document.open().\r
100                                 doc.open();\r
101                                 if ( isCustomDomain )\r
102                                         doc.domain = document.domain;\r
103                                 doc.write( htmlToLoad );\r
104                                 doc.close();\r
105                         }, this );\r
106 \r
107                         iframe.setCustomData( 'dialog', this );\r
108 \r
109                         var field = this.getContentElement( 'general', 'editing_area' ),\r
110                                 container = field.getElement();\r
111                         container.setHtml( '' );\r
112                         container.append( iframe );\r
113 \r
114                         // IE need a redirect on focus to make\r
115                         // the cursor blinking inside iframe. (#5461)\r
116                         if ( CKEDITOR.env.ie )\r
117                         {\r
118                                 var focusGrabber = CKEDITOR.dom.element.createFromHtml( '<span tabindex="-1" style="position:absolute;" role="presentation"></span>' );\r
119                                 focusGrabber.on( 'focus', function()\r
120                                 {\r
121                                         iframe.$.contentWindow.focus();\r
122                                 });\r
123                                 container.append( focusGrabber );\r
124 \r
125                                 // Override focus handler on field.\r
126                                 field.focus = function()\r
127                                 {\r
128                                         focusGrabber.focus();\r
129                                         this.fire( 'focus' );\r
130                                 };\r
131                         }\r
132 \r
133                         field.getInputElement = function(){ return iframe; };\r
134 \r
135                         // Force container to scale in IE.\r
136                         if ( CKEDITOR.env.ie )\r
137                         {\r
138                                 container.setStyle( 'display', 'block' );\r
139                                 container.setStyle( 'height', ( iframe.$.offsetHeight + 2 ) + 'px' );\r
140                         }\r
141                 },\r
142 \r
143                 onHide : function()\r
144                 {\r
145                         if ( CKEDITOR.env.ie )\r
146                                 this.getParentEditor().document.getBody().$.contentEditable = 'true';\r
147                 },\r
148 \r
149                 onLoad : function()\r
150                 {\r
151                         if ( ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) && editor.lang.dir == 'rtl' )\r
152                                 this.parts.contents.setStyle( 'overflow', 'hidden' );\r
153                 },\r
154 \r
155                 onOk : function()\r
156                 {\r
157                         var container = this.getContentElement( 'general', 'editing_area' ).getElement(),\r
158                                 iframe = container.getElementsByTag( 'iframe' ).getItem( 0 ),\r
159                                 editor = this.getParentEditor(),\r
160                                 html = iframe.$.contentWindow.document.body.innerHTML;\r
161 \r
162                         setTimeout( function(){\r
163                                 editor.fire( 'paste', { 'html' : html } );\r
164                         }, 0 );\r
165 \r
166                 },\r
167 \r
168                 contents : [\r
169                         {\r
170                                 id : 'general',\r
171                                 label : editor.lang.common.generalTab,\r
172                                 elements : [\r
173                                         {\r
174                                                 type : 'html',\r
175                                                 id : 'securityMsg',\r
176                                                 html : '<div style="white-space:normal;width:340px;">' + lang.securityMsg + '</div>'\r
177                                         },\r
178                                         {\r
179                                                 type : 'html',\r
180                                                 id : 'pasteMsg',\r
181                                                 html : '<div style="white-space:normal;width:340px;">'+lang.pasteMsg +'</div>'\r
182                                         },\r
183                                         {\r
184                                                 type : 'html',\r
185                                                 id : 'editing_area',\r
186                                                 style : 'width: 100%; height: 100%;',\r
187                                                 html : '',\r
188                                                 focus : function()\r
189                                                 {\r
190                                                         var win = this.getInputElement().$.contentWindow;\r
191 \r
192                                                         // #3291 : JAWS needs the 500ms delay to detect that the editor iframe\r
193                                                         // iframe is no longer editable. So that it will put the focus into the\r
194                                                         // Paste from Word dialog's editable area instead.\r
195                                                         setTimeout( function()\r
196                                                         {\r
197                                                                 win.focus();\r
198                                                         }, 500 );\r
199                                                 }\r
200                                         }\r
201                                 ]\r
202                         }\r
203                 ]\r
204         };\r
205 });\r