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