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