JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2
[ckeditor.git] / _source / plugins / editingblock / 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 default editing block plugin, which holds the editing area\r
8  *              and source view.\r
9  */\r
10 \r
11 (function()\r
12 {\r
13         var getMode = function( editor, mode )\r
14         {\r
15                 return editor._.modes && editor._.modes[ mode || editor.mode ];\r
16         };\r
17 \r
18         // This is a semaphore used to avoid recursive calls between\r
19         // the following data handling functions.\r
20         var isHandlingData;\r
21 \r
22         CKEDITOR.plugins.add( 'editingblock',\r
23         {\r
24                 init : function( editor )\r
25                 {\r
26                         if ( !editor.config.editingBlock )\r
27                                 return;\r
28 \r
29                         editor.on( 'themeSpace', function( event )\r
30                                 {\r
31                                         if ( event.data.space == 'contents' )\r
32                                                 event.data.html += '<br>';\r
33                                 });\r
34 \r
35                         editor.on( 'themeLoaded', function()\r
36                                 {\r
37                                         editor.fireOnce( 'editingBlockReady' );\r
38                                 });\r
39 \r
40                         editor.on( 'uiReady', function()\r
41                                 {\r
42                                         editor.setMode( editor.config.startupMode );\r
43                                 });\r
44 \r
45                         editor.on( 'afterSetData', function()\r
46                                 {\r
47                                         if ( !isHandlingData )\r
48                                         {\r
49                                                 function setData()\r
50                                                 {\r
51                                                         isHandlingData = true;\r
52                                                         getMode( editor ).loadData( editor.getData() );\r
53                                                         isHandlingData = false;\r
54                                                 }\r
55 \r
56                                                 if ( editor.mode )\r
57                                                         setData();\r
58                                                 else\r
59                                                 {\r
60                                                         editor.on( 'mode', function()\r
61                                                                 {\r
62                                                                         setData();\r
63                                                                         editor.removeListener( 'mode', arguments.callee );\r
64                                                                 });\r
65                                                 }\r
66                                         }\r
67                                 });\r
68 \r
69                         editor.on( 'beforeGetData', function()\r
70                                 {\r
71                                         if ( !isHandlingData && editor.mode )\r
72                                         {\r
73                                                 isHandlingData = true;\r
74                                                 editor.setData( getMode( editor ).getData() );\r
75                                                 isHandlingData = false;\r
76                                         }\r
77                                 });\r
78 \r
79                         editor.on( 'getSnapshot', function( event )\r
80                                 {\r
81                                         if ( editor.mode )\r
82                                                 event.data = getMode( editor ).getSnapshotData();\r
83                                 });\r
84 \r
85                         editor.on( 'loadSnapshot', function( event )\r
86                                 {\r
87                                         if ( editor.mode )\r
88                                                 getMode( editor ).loadSnapshotData( event.data );\r
89                                 });\r
90 \r
91                         // For the first "mode" call, we'll also fire the "instanceReady"\r
92                         // event.\r
93                         editor.on( 'mode', function( event )\r
94                                 {\r
95                                         // Do that once only.\r
96                                         event.removeListener();\r
97 \r
98                                         if ( editor.config.startupFocus )\r
99                                                 editor.focus();\r
100 \r
101                                         // Fire instanceReady for both the editor and CKEDITOR, but\r
102                                         // defer this until the whole execution has completed\r
103                                         // to guarantee the editor is fully responsible.\r
104                                         setTimeout( function(){\r
105                                                 editor.fireOnce( 'instanceReady' );\r
106                                                 CKEDITOR.fire( 'instanceReady', null, editor );\r
107                                         } );\r
108                                 });\r
109                 }\r
110         });\r
111 \r
112         /**\r
113          * The current editing mode. An editing mode is basically a viewport for\r
114          * editing or content viewing. By default the possible values for this\r
115          * property are "wysiwyg" and "source".\r
116          * @type String\r
117          * @example\r
118          * alert( CKEDITOR.instances.editor1.mode );  // "wysiwyg" (e.g.)\r
119          */\r
120         CKEDITOR.editor.prototype.mode = '';\r
121 \r
122         /**\r
123          * Registers an editing mode. This function is to be used mainly by plugins.\r
124          * @param {String} mode The mode name.\r
125          * @param {Object} modeEditor The mode editor definition.\r
126          * @example\r
127          */\r
128         CKEDITOR.editor.prototype.addMode = function( mode, modeEditor )\r
129         {\r
130                 modeEditor.name = mode;\r
131                 ( this._.modes || ( this._.modes = {} ) )[ mode ] = modeEditor;\r
132         };\r
133 \r
134         /**\r
135          * Sets the current editing mode in this editor instance.\r
136          * @param {String} mode A registered mode name.\r
137          * @example\r
138          * // Switch to "source" view.\r
139          * CKEDITOR.instances.editor1.setMode( 'source' );\r
140          */\r
141         CKEDITOR.editor.prototype.setMode = function( mode )\r
142         {\r
143                 var data,\r
144                         holderElement = this.getThemeSpace( 'contents' ),\r
145                         isDirty = this.checkDirty();\r
146 \r
147                 // Unload the previous mode.\r
148                 if ( this.mode )\r
149                 {\r
150                         if ( mode == this.mode )\r
151                                 return;\r
152 \r
153                         this.fire( 'beforeModeUnload' );\r
154 \r
155                         var currentMode = getMode( this );\r
156                         data = currentMode.getData();\r
157                         currentMode.unload( holderElement );\r
158                         this.mode = '';\r
159                 }\r
160 \r
161                 holderElement.setHtml( '' );\r
162 \r
163                 // Load required mode.\r
164                 var modeEditor = getMode( this, mode );\r
165                 if ( !modeEditor )\r
166                         throw '[CKEDITOR.editor.setMode] Unknown mode "' + mode + '".';\r
167 \r
168                 if ( !isDirty )\r
169                 {\r
170                         this.on( 'mode', function()\r
171                                 {\r
172                                         this.resetDirty();\r
173                                         this.removeListener( 'mode', arguments.callee );\r
174                                 });\r
175                 }\r
176 \r
177                 modeEditor.load( holderElement, ( typeof data ) != 'string'  ? this.getData() : data);\r
178         };\r
179 \r
180         /**\r
181          * Moves the selection focus to the editing are space in the editor.\r
182          */\r
183         CKEDITOR.editor.prototype.focus = function()\r
184         {\r
185                 var mode = getMode( this );\r
186                 if ( mode )\r
187                         mode.focus();\r
188         };\r
189 })();\r
190 \r
191 /**\r
192  * The mode to load at the editor startup. It depends on the plugins\r
193  * loaded. By default, the "wysiwyg" and "source" modes are available.\r
194  * @type String\r
195  * @default 'wysiwyg'\r
196  * @example\r
197  * config.startupMode = 'source';\r
198  */\r
199 CKEDITOR.config.startupMode = 'wysiwyg';\r
200 \r
201 /**\r
202  * Sets whether the editor should have the focus when the page loads.\r
203  * @type Boolean\r
204  * @default false\r
205  * @example\r
206  * config.startupFocus = true;\r
207  */\r
208 CKEDITOR.config.startupFocus = false;\r
209 \r
210 /**\r
211  * Whether to render or not the editing block area in the editor interface.\r
212  * @type Boolean\r
213  * @default true\r
214  * @example\r
215  * config.editingBlock = false;\r
216  */\r
217 CKEDITOR.config.editingBlock = true;\r
218 \r
219 /**\r
220  * Fired when a CKEDITOR instance is created, fully initialized and ready for interaction.\r
221  * @name CKEDITOR#instanceReady\r
222  * @event\r
223  * @param {CKEDITOR.editor} editor The editor instance that has been created.\r
224  */\r