JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1.1
[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 : -1\r
38                                                                 });\r
39                                                         textarea.addClass( 'cke_source' );\r
40                                                         textarea.addClass( 'cke_enable_context_menu' );\r
41 \r
42                                                         var styles =\r
43                                                         {\r
44                                                                 // IE7 has overflow the <textarea> from wrapping table cell.\r
45                                                                 width   : CKEDITOR.env.ie7Compat ?  '99%' : '100%',\r
46                                                                 height  : '100%',\r
47                                                                 resize  : 'none',\r
48                                                                 outline : 'none',\r
49                                                                 'text-align' : 'left'\r
50                                                         };\r
51 \r
52                                                         // Having to make <textarea> fixed sized to conque the following bugs:\r
53                                                         // 1. The textarea height/width='100%' doesn't constraint to the 'td' in IE6/7.\r
54                                                         // 2. Unexpected vertical-scrolling behavior happens whenever focus is moving out of editor\r
55                                                         // if text content within it has overflowed. (#4762)\r
56                                                         if ( CKEDITOR.env.ie )\r
57                                                         {\r
58                                                                 onResize = function()\r
59                                                                 {\r
60                                                                         // Holder rectange size is stretched by textarea,\r
61                                                                         // so hide it just for a moment.\r
62                                                                         textarea.hide();\r
63                                                                         textarea.setStyle( 'height', holderElement.$.clientHeight + 'px' );\r
64                                                                         textarea.setStyle( 'width', holderElement.$.clientWidth + 'px' );\r
65                                                                         // When we have proper holder size, show textarea again.\r
66                                                                         textarea.show();\r
67                                                                 };\r
68 \r
69                                                                 editor.on( 'resize', onResize );\r
70                                                                 win.on( 'resize', onResize );\r
71                                                                 setTimeout( onResize, 0 );\r
72                                                         }\r
73                                                         else\r
74                                                         {\r
75                                                                 // By some yet unknown reason, we must stop the\r
76                                                                 // mousedown propagation for the textarea,\r
77                                                                 // otherwise it's not possible to place the caret\r
78                                                                 // inside of it (non IE).\r
79                                                                 textarea.on( 'mousedown', function( evt )\r
80                                                                         {\r
81                                                                                 evt.data.stopPropagation();\r
82                                                                         } );\r
83                                                         }\r
84 \r
85                                                         // Reset the holder element and append the\r
86                                                         // <textarea> to it.\r
87                                                         holderElement.setHtml( '' );\r
88                                                         holderElement.append( textarea );\r
89                                                         textarea.setStyles( styles );\r
90 \r
91                                                         textarea.on( 'blur', function()\r
92                                                                 {\r
93                                                                         editor.focusManager.blur();\r
94                                                                 });\r
95 \r
96                                                         textarea.on( 'focus', function()\r
97                                                                 {\r
98                                                                         editor.focusManager.focus();\r
99                                                                 });\r
100 \r
101                                                         // The editor data "may be dirty" after this point.\r
102                                                         editor.mayBeDirty = true;\r
103 \r
104                                                         // Set the <textarea> value.\r
105                                                         this.loadData( data );\r
106 \r
107                                                         var keystrokeHandler = editor.keystrokeHandler;\r
108                                                         if ( keystrokeHandler )\r
109                                                                 keystrokeHandler.attach( textarea );\r
110 \r
111                                                         setTimeout( function()\r
112                                                         {\r
113                                                                 editor.mode = 'source';\r
114                                                                 editor.fire( 'mode' );\r
115                                                         },\r
116                                                         ( CKEDITOR.env.gecko || CKEDITOR.env.webkit ) ? 100 : 0 );\r
117                                                 },\r
118 \r
119                                                 loadData : function( data )\r
120                                                 {\r
121                                                         textarea.setValue( data );\r
122                                                         editor.fire( 'dataReady' );\r
123                                                 },\r
124 \r
125                                                 getData : function()\r
126                                                 {\r
127                                                         return textarea.getValue();\r
128                                                 },\r
129 \r
130                                                 getSnapshotData : function()\r
131                                                 {\r
132                                                         return textarea.getValue();\r
133                                                 },\r
134 \r
135                                                 unload : function( holderElement )\r
136                                                 {\r
137                                                         editor.textarea = textarea = null;\r
138 \r
139                                                         if ( onResize )\r
140                                                         {\r
141                                                                 editor.removeListener( 'resize', onResize );\r
142                                                                 win.removeListener( 'resize', onResize );\r
143                                                         }\r
144 \r
145                                                         if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
146                                                                 holderElement.removeStyle( 'position' );\r
147                                                 },\r
148 \r
149                                                 focus : function()\r
150                                                 {\r
151                                                         textarea.focus();\r
152                                                 }\r
153                                         });\r
154                         });\r
155 \r
156                 editor.addCommand( 'source', sourcearea.commands.source );\r
157 \r
158                 if ( editor.ui.addButton )\r
159                 {\r
160                         editor.ui.addButton( 'Source',\r
161                                 {\r
162                                         label : editor.lang.source,\r
163                                         command : 'source'\r
164                                 });\r
165                 }\r
166 \r
167                 editor.on( 'mode', function()\r
168                         {\r
169                                 editor.getCommand( 'source' ).setState(\r
170                                         editor.mode == 'source' ?\r
171                                                 CKEDITOR.TRISTATE_ON :\r
172                                                 CKEDITOR.TRISTATE_OFF );\r
173                         });\r
174         }\r
175 });\r
176 \r
177 /**\r
178  * Holds the definition of commands an UI elements included with the sourcearea\r
179  * plugin.\r
180  * @example\r
181  */\r
182 CKEDITOR.plugins.sourcearea =\r
183 {\r
184         commands :\r
185         {\r
186                 source :\r
187                 {\r
188                         modes : { wysiwyg:1, source:1 },\r
189 \r
190                         exec : function( editor )\r
191                         {\r
192                                 if ( editor.mode == 'wysiwyg' )\r
193                                         editor.fire( 'saveSnapshot' );\r
194                                 editor.getCommand( 'source' ).setState( CKEDITOR.TRISTATE_DISABLED );\r
195                                 editor.setMode( editor.mode == 'source' ? 'wysiwyg' : 'source' );\r
196                         },\r
197 \r
198                         canUndo : false\r
199                 }\r
200         }\r
201 };\r