JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
234bbf019c255630d1da355feec4ab8d27a3815a
[ckeditor.git] / _source / plugins / sourcearea / 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  * @fileOverview The "sourcearea" plugin. It registers the "source" editing\r
8  *              mode, which displays the raw data being edited in the editor.\r
9  */\r
10 \r
11 CKEDITOR.plugins.add( 'sourcearea',\r
12 {\r
13         requires : [ 'editingblock' ],\r
14 \r
15         init : function( editor )\r
16         {\r
17                 var sourcearea = CKEDITOR.plugins.sourcearea,\r
18                         win = CKEDITOR.document.getWindow();\r
19 \r
20                 editor.on( 'editingBlockReady', function()\r
21                         {\r
22                                 var textarea,\r
23                                         onResize;\r
24 \r
25                                 editor.addMode( 'source',\r
26                                         {\r
27                                                 load : function( holderElement, data )\r
28                                                 {\r
29                                                         if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
30                                                                 holderElement.setStyle( 'position', 'relative' );\r
31 \r
32                                                         // Create the source area <textarea>.\r
33                                                         editor.textarea = textarea = new CKEDITOR.dom.element( 'textarea' );\r
34                                                         textarea.setAttributes(\r
35                                                                 {\r
36                                                                         dir : 'ltr',\r
37                                                                         tabIndex : CKEDITOR.env.webkit ? -1 : editor.tabIndex,\r
38                                                                         'role' : 'textbox',\r
39                                                                         'aria-label' : editor.lang.editorTitle.replace( '%1', editor.name )\r
40                                                                 });\r
41                                                         textarea.addClass( 'cke_source' );\r
42                                                         textarea.addClass( 'cke_enable_context_menu' );\r
43 \r
44                                                         var styles =\r
45                                                         {\r
46                                                                 // IE7 has overflow the <textarea> from wrapping table cell.\r
47                                                                 width   : CKEDITOR.env.ie7Compat ?  '99%' : '100%',\r
48                                                                 height  : '100%',\r
49                                                                 resize  : 'none',\r
50                                                                 outline : 'none',\r
51                                                                 'text-align' : 'left'\r
52                                                         };\r
53 \r
54                                                         // Having to make <textarea> fixed sized to conque the following bugs:\r
55                                                         // 1. The textarea height/width='100%' doesn't constraint to the 'td' in IE6/7.\r
56                                                         // 2. Unexpected vertical-scrolling behavior happens whenever focus is moving out of editor\r
57                                                         // if text content within it has overflowed. (#4762)\r
58                                                         if ( CKEDITOR.env.ie )\r
59                                                         {\r
60                                                                 onResize = function()\r
61                                                                 {\r
62                                                                         // Holder rectange size is stretched by textarea,\r
63                                                                         // so hide it just for a moment.\r
64                                                                         textarea.hide();\r
65                                                                         textarea.setStyle( 'height', holderElement.$.clientHeight + 'px' );\r
66                                                                         textarea.setStyle( 'width', holderElement.$.clientWidth + 'px' );\r
67                                                                         // When we have proper holder size, show textarea again.\r
68                                                                         textarea.show();\r
69                                                                 };\r
70 \r
71                                                                 editor.on( 'resize', onResize );\r
72                                                                 win.on( 'resize', onResize );\r
73                                                                 setTimeout( onResize, 0 );\r
74                                                         }\r
75                                                         // As we prevent click to put focus on editor container,\r
76                                                         // while 'mousedown' inside <textarea> is also captured,\r
77                                                         // but we must stop the even propagation, otherwise\r
78                                                         // it's not possible to place the caret inside of it (non IE and IE9).\r
79                                                         if ( document.addEventListener )\r
80                                                         {\r
81                                                                 textarea.on( 'mousedown', function( evt )\r
82                                                                         {\r
83                                                                                 evt.data.stopPropagation();\r
84                                                                         } );\r
85                                                         }\r
86 \r
87                                                         // Reset the holder element and append the\r
88                                                         // <textarea> to it.\r
89                                                         holderElement.setHtml( '' );\r
90                                                         holderElement.append( textarea );\r
91                                                         textarea.setStyles( styles );\r
92 \r
93                                                         editor.fire( 'ariaWidget', textarea );\r
94 \r
95                                                         textarea.on( 'blur', function()\r
96                                                                 {\r
97                                                                         editor.focusManager.blur();\r
98                                                                 });\r
99 \r
100                                                         textarea.on( 'focus', function()\r
101                                                                 {\r
102                                                                         editor.focusManager.focus();\r
103                                                                 });\r
104 \r
105                                                         // The editor data "may be dirty" after this point.\r
106                                                         editor.mayBeDirty = true;\r
107 \r
108                                                         // Set the <textarea> value.\r
109                                                         this.loadData( data );\r
110 \r
111                                                         var keystrokeHandler = editor.keystrokeHandler;\r
112                                                         if ( keystrokeHandler )\r
113                                                                 keystrokeHandler.attach( textarea );\r
114 \r
115                                                         setTimeout( function()\r
116                                                         {\r
117                                                                 editor.mode = 'source';\r
118                                                                 editor.fire( 'mode' );\r
119                                                         },\r
120                                                         ( CKEDITOR.env.gecko || CKEDITOR.env.webkit ) ? 100 : 0 );\r
121                                                 },\r
122 \r
123                                                 loadData : function( data )\r
124                                                 {\r
125                                                         textarea.setValue( data );\r
126                                                         editor.fire( 'dataReady' );\r
127                                                 },\r
128 \r
129                                                 getData : function()\r
130                                                 {\r
131                                                         return textarea.getValue();\r
132                                                 },\r
133 \r
134                                                 getSnapshotData : function()\r
135                                                 {\r
136                                                         return textarea.getValue();\r
137                                                 },\r
138 \r
139                                                 unload : function( holderElement )\r
140                                                 {\r
141                                                         textarea.clearCustomData();\r
142                                                         editor.textarea = textarea = null;\r
143 \r
144                                                         if ( onResize )\r
145                                                         {\r
146                                                                 editor.removeListener( 'resize', onResize );\r
147                                                                 win.removeListener( 'resize', onResize );\r
148                                                         }\r
149 \r
150                                                         if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
151                                                                 holderElement.removeStyle( 'position' );\r
152                                                 },\r
153 \r
154                                                 focus : function()\r
155                                                 {\r
156                                                         textarea.focus();\r
157                                                 }\r
158                                         });\r
159                         });\r
160 \r
161                 editor.addCommand( 'source', sourcearea.commands.source );\r
162 \r
163                 if ( editor.ui.addButton )\r
164                 {\r
165                         editor.ui.addButton( 'Source',\r
166                                 {\r
167                                         label : editor.lang.source,\r
168                                         command : 'source'\r
169                                 });\r
170                 }\r
171 \r
172                 editor.on( 'mode', function()\r
173                         {\r
174                                 editor.getCommand( 'source' ).setState(\r
175                                         editor.mode == 'source' ?\r
176                                                 CKEDITOR.TRISTATE_ON :\r
177                                                 CKEDITOR.TRISTATE_OFF );\r
178                         });\r
179         }\r
180 });\r
181 \r
182 /**\r
183  * Holds the definition of commands an UI elements included with the sourcearea\r
184  * plugin.\r
185  * @example\r
186  */\r
187 CKEDITOR.plugins.sourcearea =\r
188 {\r
189         commands :\r
190         {\r
191                 source :\r
192                 {\r
193                         modes : { wysiwyg:1, source:1 },\r
194                         editorFocus : false,\r
195 \r
196                         exec : function( editor )\r
197                         {\r
198                                 if ( editor.mode == 'wysiwyg' )\r
199                                         editor.fire( 'saveSnapshot' );\r
200                                 editor.getCommand( 'source' ).setState( CKEDITOR.TRISTATE_DISABLED );\r
201                                 editor.setMode( editor.mode == 'source' ? 'wysiwyg' : 'source' );\r
202                         },\r
203 \r
204                         canUndo : false\r
205                 }\r
206         }\r
207 };\r