2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
7 * @fileOverview jQuery adapter provides easy use of basic CKEditor functions
8 * and access to internal API. It also integrates some aspects of CKEditor with
11 * Every TEXTAREA, DIV and P elements can be converted to working editor.
13 * Plugin exposes some of editor's event to jQuery event system. All of those are namespaces inside
14 * ".ckeditor" namespace and can be binded/listened on supported textarea, div and p nodes.
16 * Available jQuery events:
17 * - instanceReady.ckeditor( editor, rootNode )
18 * Triggered when new instance is ready.
19 * - destroy.ckeditor( editor )
20 * Triggered when instance is destroyed.
21 * - getData.ckeditor( editor, eventData )
22 * Triggered when getData event is fired inside editor. It can change returned data using eventData reference.
23 * - setData.ckeditor( editor )
24 * Triggered when getData event is fired inside editor.
27 * <script src="jquery.js"></script>
28 * <script src="ckeditor.js"></script>
29 * <script src="adapters/jquery/adapter.js"></script>
35 * Allows CKEditor to override jQuery.fn.val(), making it possible to use the val()
36 * function on textareas, as usual, having it synchronized with CKEditor.<br>
38 * This configuration option is global and executed during the jQuery Adapter loading.
39 * It can't be customized across editor instances.
43 * CKEDITOR.config.jqueryOverrideVal = true;
45 * <!-- Important: The JQuery adapter is loaded *after* setting jqueryOverrideVal -->
46 * <script src="/ckeditor/adapters/jquery.js"></script>
48 * // ... then later in the code ...
50 * $( 'textarea' ).ckeditor();
52 * $( 'textarea' ).val( 'New content' );
54 CKEDITOR
.config
.jqueryOverrideVal
= typeof CKEDITOR
.config
.jqueryOverrideVal
== 'undefined'
55 ? true : CKEDITOR
.config
.jqueryOverrideVal
;
57 var jQuery
= window
.jQuery
;
59 if ( typeof jQuery
== 'undefined' )
62 // jQuery object methods.
63 jQuery
.extend( jQuery
.fn
,
64 /** @lends jQuery.fn */
67 * Return existing CKEditor instance for first matched element.
68 * Allows to easily use internal API. Doesn't return jQuery object.
70 * Raised exception if editor doesn't exist or isn't ready yet.
72 * @name jQuery.ckeditorGet
73 * @return CKEDITOR.editor
74 * @see CKEDITOR.editor
76 ckeditorGet: function()
78 var instance
= this.eq( 0 ).data( 'ckeditorInstance' );
80 throw "CKEditor not yet initialized, use ckeditor() with callback.";
84 * Triggers creation of CKEditor in all matched elements (reduced to DIV, P and TEXTAREAs).
85 * Binds callback to instanceReady event of all instances. If editor is already created, than
86 * callback is fired right away.
88 * Mixed parameter order allowed.
90 * @param callback Function to be run on editor instance. Passed parameters: [ textarea ].
91 * Callback is fiered in "this" scope being ckeditor instance and having source textarea as first param.
93 * @param config Configuration options for new instance(s) if not already created.
97 * $( 'textarea' ).ckeditor( function( textarea ) {
98 * $( textarea ).val( this.getData() )
101 * @name jQuery.fn.ckeditor
104 ckeditor: function( callback
, config
)
106 if ( !CKEDITOR
.env
.isCompatible
)
109 if ( !jQuery
.isFunction( callback
))
115 config
= config
|| {};
117 this.filter( 'textarea, div, p' ).each( function()
119 var $element
= jQuery( this ),
120 editor
= $element
.data( 'ckeditorInstance' ),
121 instanceLock
= $element
.data( '_ckeditorInstanceLock' ),
124 if ( editor
&& !instanceLock
)
127 callback
.apply( editor
, [ this ] );
129 else if ( !instanceLock
)
131 // CREATE NEW INSTANCE
133 // Handle config.autoUpdateElement inside this plugin if desired.
134 if ( config
.autoUpdateElement
135 || ( typeof config
.autoUpdateElement
== 'undefined' && CKEDITOR
.config
.autoUpdateElement
) )
137 config
.autoUpdateElementJquery
= true;
140 // Always disable config.autoUpdateElement.
141 config
.autoUpdateElement
= false;
142 $element
.data( '_ckeditorInstanceLock', true );
144 // Set instance reference in element's data.
145 editor
= CKEDITOR
.replace( element
, config
);
146 $element
.data( 'ckeditorInstance', editor
);
148 // Register callback.
149 editor
.on( 'instanceReady', function( event
)
151 var editor
= event
.editor
;
152 setTimeout( function()
154 // Delay bit more if editor is still not ready.
155 if ( !editor
.element
)
157 setTimeout( arguments
.callee
, 100 );
161 // Remove this listener.
162 event
.removeListener( 'instanceReady', this.callee
);
164 // Forward setData on dataReady.
165 editor
.on( 'dataReady', function()
167 $element
.trigger( 'setData' + '.ckeditor', [ editor
] );
171 editor
.on( 'getData', function( event
) {
172 $element
.trigger( 'getData' + '.ckeditor', [ editor
, event
.data
] );
175 // Forward destroy event.
176 editor
.on( 'destroy', function()
178 $element
.trigger( 'destroy.ckeditor', [ editor
] );
181 // Integrate with form submit.
182 if ( editor
.config
.autoUpdateElementJquery
&& $element
.is( 'textarea' ) && $element
.parents( 'form' ).length
)
184 var onSubmit = function()
186 $element
.ckeditor( function()
188 editor
.updateElement();
192 // Bind to submit event.
193 $element
.parents( 'form' ).submit( onSubmit
);
195 // Bind to form-pre-serialize from jQuery Forms plugin.
196 $element
.parents( 'form' ).bind( 'form-pre-serialize', onSubmit
);
198 // Unbind when editor destroyed.
199 $element
.bind( 'destroy.ckeditor', function()
201 $element
.parents( 'form' ).unbind( 'submit', onSubmit
);
202 $element
.parents( 'form' ).unbind( 'form-pre-serialize', onSubmit
);
206 // Garbage collect on destroy.
207 editor
.on( 'destroy', function()
209 $element
.data( 'ckeditorInstance', null );
213 $element
.data( '_ckeditorInstanceLock', null );
215 // Fire instanceReady event.
216 $element
.trigger( 'instanceReady.ckeditor', [ editor
] );
218 // Run given (first) code.
220 callback
.apply( editor
, [ element
] );
222 }, null, null, 9999);
226 // Editor is already during creation process, bind our code to the event.
227 CKEDITOR
.on( 'instanceReady', function( event
)
229 var editor
= event
.editor
;
230 setTimeout( function()
232 // Delay bit more if editor is still not ready.
233 if ( !editor
.element
)
235 setTimeout( arguments
.callee
, 100 );
239 if ( editor
.element
.$ == element
)
243 callback
.apply( editor
, [ element
] );
246 }, null, null, 9999);
253 // New val() method for objects.
254 if ( CKEDITOR
.config
.jqueryOverrideVal
)
256 jQuery
.fn
.val
= CKEDITOR
.tools
.override( jQuery
.fn
.val
, function( oldValMethod
)
259 * CKEditor-aware val() method.
261 * Acts same as original jQuery val(), but for textareas which have CKEditor instances binded to them, method
262 * returns editor's content. It also works for settings values.
264 * @param oldValMethod
265 * @name jQuery.fn.val
267 return function( newValue
, forceNative
)
269 var isSetter
= typeof newValue
!= 'undefined',
272 this.each( function()
274 var $this = jQuery( this ),
275 editor
= $this.data( 'ckeditorInstance' );
277 if ( !forceNative
&& $this.is( 'textarea' ) && editor
)
280 editor
.setData( newValue
);
283 result
= editor
.getData();
291 oldValMethod
.call( $this, newValue
);
294 result
= oldValMethod
.call( $this );
302 return isSetter
? this : result
;