JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2
[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 : 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                                                         else\r
76                                                         {\r
77                                                                 // By some yet unknown reason, we must stop the\r
78                                                                 // mousedown propagation for the textarea,\r
79                                                                 // otherwise it's not possible to place the caret\r
80                                                                 // inside of it (non IE).\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                                                         editor.textarea = textarea = null;\r
142 \r
143                                                         if ( onResize )\r
144                                                         {\r
145                                                                 editor.removeListener( 'resize', onResize );\r
146                                                                 win.removeListener( 'resize', onResize );\r
147                                                         }\r
148 \r
149                                                         if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
150                                                                 holderElement.removeStyle( 'position' );\r
151                                                 },\r
152 \r
153                                                 focus : function()\r
154                                                 {\r
155                                                         textarea.focus();\r
156                                                 }\r
157                                         });\r
158                         });\r
159 \r
160                 editor.addCommand( 'source', sourcearea.commands.source );\r
161 \r
162                 if ( editor.ui.addButton )\r
163                 {\r
164                         editor.ui.addButton( 'Source',\r
165                                 {\r
166                                         label : editor.lang.source,\r
167                                         command : 'source'\r
168                                 });\r
169                 }\r
170 \r
171                 editor.on( 'mode', function()\r
172                         {\r
173                                 editor.getCommand( 'source' ).setState(\r
174                                         editor.mode == 'source' ?\r
175                                                 CKEDITOR.TRISTATE_ON :\r
176                                                 CKEDITOR.TRISTATE_OFF );\r
177                         });\r
178         }\r
179 });\r
180 \r
181 /**\r
182  * Holds the definition of commands an UI elements included with the sourcearea\r
183  * plugin.\r
184  * @example\r
185  */\r
186 CKEDITOR.plugins.sourcearea =\r
187 {\r
188         commands :\r
189         {\r
190                 source :\r
191                 {\r
192                         modes : { wysiwyg:1, source:1 },\r
193 \r
194                         exec : function( editor )\r
195                         {\r
196                                 if ( editor.mode == 'wysiwyg' )\r
197                                         editor.fire( 'saveSnapshot' );\r
198                                 editor.getCommand( 'source' ).setState( CKEDITOR.TRISTATE_DISABLED );\r
199                                 editor.setMode( editor.mode == 'source' ? 'wysiwyg' : 'source' );\r
200                         },\r
201 \r
202                         canUndo : false\r
203                 }\r
204         }\r
205 };\r