JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.3
[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 \r
76                                                         // Reset the holder element and append the\r
77                                                         // <textarea> to it.\r
78                                                         holderElement.setHtml( '' );\r
79                                                         holderElement.append( textarea );\r
80                                                         textarea.setStyles( styles );\r
81 \r
82                                                         editor.fire( 'ariaWidget', textarea );\r
83 \r
84                                                         textarea.on( 'blur', function()\r
85                                                                 {\r
86                                                                         editor.focusManager.blur();\r
87                                                                 });\r
88 \r
89                                                         textarea.on( 'focus', function()\r
90                                                                 {\r
91                                                                         editor.focusManager.focus();\r
92                                                                 });\r
93 \r
94                                                         // The editor data "may be dirty" after this point.\r
95                                                         editor.mayBeDirty = true;\r
96 \r
97                                                         // Set the <textarea> value.\r
98                                                         this.loadData( data );\r
99 \r
100                                                         var keystrokeHandler = editor.keystrokeHandler;\r
101                                                         if ( keystrokeHandler )\r
102                                                                 keystrokeHandler.attach( textarea );\r
103 \r
104                                                         setTimeout( function()\r
105                                                         {\r
106                                                                 editor.mode = 'source';\r
107                                                                 editor.fire( 'mode' );\r
108                                                         },\r
109                                                         ( CKEDITOR.env.gecko || CKEDITOR.env.webkit ) ? 100 : 0 );\r
110                                                 },\r
111 \r
112                                                 loadData : function( data )\r
113                                                 {\r
114                                                         textarea.setValue( data );\r
115                                                         editor.fire( 'dataReady' );\r
116                                                 },\r
117 \r
118                                                 getData : function()\r
119                                                 {\r
120                                                         return textarea.getValue();\r
121                                                 },\r
122 \r
123                                                 getSnapshotData : function()\r
124                                                 {\r
125                                                         return textarea.getValue();\r
126                                                 },\r
127 \r
128                                                 unload : function( holderElement )\r
129                                                 {\r
130                                                         textarea.clearCustomData();\r
131                                                         editor.textarea = textarea = null;\r
132 \r
133                                                         if ( onResize )\r
134                                                         {\r
135                                                                 editor.removeListener( 'resize', onResize );\r
136                                                                 win.removeListener( 'resize', onResize );\r
137                                                         }\r
138 \r
139                                                         if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
140                                                                 holderElement.removeStyle( 'position' );\r
141                                                 },\r
142 \r
143                                                 focus : function()\r
144                                                 {\r
145                                                         textarea.focus();\r
146                                                 }\r
147                                         });\r
148                         });\r
149 \r
150                 editor.addCommand( 'source', sourcearea.commands.source );\r
151 \r
152                 if ( editor.ui.addButton )\r
153                 {\r
154                         editor.ui.addButton( 'Source',\r
155                                 {\r
156                                         label : editor.lang.source,\r
157                                         command : 'source'\r
158                                 });\r
159                 }\r
160 \r
161                 editor.on( 'mode', function()\r
162                         {\r
163                                 editor.getCommand( 'source' ).setState(\r
164                                         editor.mode == 'source' ?\r
165                                                 CKEDITOR.TRISTATE_ON :\r
166                                                 CKEDITOR.TRISTATE_OFF );\r
167                         });\r
168         }\r
169 });\r
170 \r
171 /**\r
172  * Holds the definition of commands an UI elements included with the sourcearea\r
173  * plugin.\r
174  * @example\r
175  */\r
176 CKEDITOR.plugins.sourcearea =\r
177 {\r
178         commands :\r
179         {\r
180                 source :\r
181                 {\r
182                         modes : { wysiwyg:1, source:1 },\r
183                         editorFocus : false,\r
184 \r
185                         exec : function( editor )\r
186                         {\r
187                                 if ( editor.mode == 'wysiwyg' )\r
188                                         editor.fire( 'saveSnapshot' );\r
189                                 editor.getCommand( 'source' ).setState( CKEDITOR.TRISTATE_DISABLED );\r
190                                 editor.setMode( editor.mode == 'source' ? 'wysiwyg' : 'source' );\r
191                         },\r
192 \r
193                         canUndo : false\r
194                 }\r
195         }\r
196 };\r