JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.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 \r
19                 editor.on( 'editingBlockReady', function()\r
20                         {\r
21                                 var textarea,\r
22                                         onResize;\r
23 \r
24                                 editor.addMode( 'source',\r
25                                         {\r
26                                                 load : function( holderElement, data )\r
27                                                 {\r
28                                                         if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
29                                                                 holderElement.setStyle( 'position', 'relative' );\r
30 \r
31                                                         // Create the source area <textarea>.\r
32                                                         editor.textarea = textarea = new CKEDITOR.dom.element( 'textarea' );\r
33                                                         textarea.setAttributes(\r
34                                                                 {\r
35                                                                         dir : 'ltr',\r
36                                                                         tabIndex : -1\r
37                                                                 });\r
38                                                         textarea.addClass( 'cke_source' );\r
39                                                         textarea.addClass( 'cke_enable_context_menu' );\r
40 \r
41                                                         var styles =\r
42                                                         {\r
43                                                                 // IE7 has overflow the <textarea> from wrapping table cell.\r
44                                                                 width   : CKEDITOR.env.ie7Compat ?  '99%' : '100%',\r
45                                                                 height  : '100%',\r
46                                                                 resize  : 'none',\r
47                                                                 outline : 'none',\r
48                                                                 'text-align' : 'left'\r
49                                                         };\r
50 \r
51                                                         // The textarea height/width='100%' doesn't\r
52                                                         // constraint to the 'td' in IE strick mode\r
53                                                         if ( CKEDITOR.env.ie )\r
54                                                         {\r
55                                                                 if ( !CKEDITOR.env.ie8Compat )\r
56                                                                 {\r
57                                                                         onResize = function()\r
58                                                                                 {\r
59                                                                                         // Holder rectange size is stretched by textarea,\r
60                                                                                         // so hide it just for a moment.\r
61                                                                                         textarea.hide();\r
62                                                                                         textarea.setStyle( 'height', holderElement.$.clientHeight + 'px' );\r
63                                                                                         // When we have proper holder size, show textarea again.\r
64                                                                                         textarea.show();\r
65                                                                                 };\r
66                                                                         editor.on( 'resize', onResize );\r
67                                                                         editor.on( 'afterCommandExec', function( event )\r
68                                                                         {\r
69                                                                                 if ( event.data.name == 'toolbarCollapse' )\r
70                                                                                         onResize();\r
71                                                                         });\r
72                                                                         styles.height = holderElement.$.clientHeight + 'px';\r
73                                                                 }\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                                                         textarea.on( 'blur', function()\r
94                                                                 {\r
95                                                                         editor.focusManager.blur();\r
96                                                                 });\r
97 \r
98                                                         textarea.on( 'focus', function()\r
99                                                                 {\r
100                                                                         editor.focusManager.focus();\r
101                                                                 });\r
102 \r
103                                                         // The editor data "may be dirty" after this point.\r
104                                                         editor.mayBeDirty = true;\r
105 \r
106                                                         // Set the <textarea> value.\r
107                                                         this.loadData( data );\r
108 \r
109                                                         var keystrokeHandler = editor.keystrokeHandler;\r
110                                                         if ( keystrokeHandler )\r
111                                                                 keystrokeHandler.attach( textarea );\r
112 \r
113                                                         setTimeout( function()\r
114                                                         {\r
115                                                                 editor.mode = 'source';\r
116                                                                 editor.fire( 'mode' );\r
117                                                         },\r
118                                                         ( CKEDITOR.env.gecko || CKEDITOR.env.webkit ) ? 100 : 0 );\r
119                                                 },\r
120 \r
121                                                 loadData : function( data )\r
122                                                 {\r
123                                                         textarea.setValue( data );\r
124                                                         editor.fire( 'dataReady' );\r
125                                                 },\r
126 \r
127                                                 getData : function()\r
128                                                 {\r
129                                                         return textarea.getValue();\r
130                                                 },\r
131 \r
132                                                 getSnapshotData : function()\r
133                                                 {\r
134                                                         return textarea.getValue();\r
135                                                 },\r
136 \r
137                                                 unload : function( holderElement )\r
138                                                 {\r
139                                                         editor.textarea = textarea = null;\r
140 \r
141                                                         if ( onResize )\r
142                                                                 editor.removeListener( 'resize', onResize );\r
143 \r
144                                                         if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
145                                                                 holderElement.removeStyle( 'position' );\r
146                                                 },\r
147 \r
148                                                 focus : function()\r
149                                                 {\r
150                                                         textarea.focus();\r
151                                                 }\r
152                                         });\r
153                         });\r
154 \r
155                 editor.addCommand( 'source', sourcearea.commands.source );\r
156 \r
157                 if ( editor.ui.addButton )\r
158                 {\r
159                         editor.ui.addButton( 'Source',\r
160                                 {\r
161                                         label : editor.lang.source,\r
162                                         command : 'source'\r
163                                 });\r
164                 }\r
165 \r
166                 editor.on( 'mode', function()\r
167                         {\r
168                                 editor.getCommand( 'source' ).setState(\r
169                                         editor.mode == 'source' ?\r
170                                                 CKEDITOR.TRISTATE_ON :\r
171                                                 CKEDITOR.TRISTATE_OFF );\r
172                         });\r
173         }\r
174 });\r
175 \r
176 /**\r
177  * Holds the definition of commands an UI elements included with the sourcearea\r
178  * plugin.\r
179  * @example\r
180  */\r
181 CKEDITOR.plugins.sourcearea =\r
182 {\r
183         commands :\r
184         {\r
185                 source :\r
186                 {\r
187                         modes : { wysiwyg:1, source:1 },\r
188 \r
189                         exec : function( editor )\r
190                         {\r
191                                 if ( editor.mode == 'wysiwyg' )\r
192                                         editor.fire( 'saveSnapshot' );\r
193                                 editor.getCommand( 'source' ).setState( CKEDITOR.TRISTATE_DISABLED );\r
194                                 editor.setMode( editor.mode == 'source' ? 'wysiwyg' : 'source' );\r
195                         },\r
196 \r
197                         canUndo : false\r
198                 }\r
199         }\r
200 };\r