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 * Allow CKEditor to override jQuery.fn.val(). This results in ability to use val()
36 * function on textareas as usual and having those calls synchronized with CKEditor
37 * Rich Text Editor component.
39 * This config option is global and executed during plugin load.
40 * Can't be customized across editor instances.
44 * $( 'textarea' ).ckeditor();
46 * $( 'textarea' ).val( 'New content' );
48 CKEDITOR.config.jqueryOverrideVal = typeof CKEDITOR.config.jqueryOverrideVal == 'undefined'
49 ? true : CKEDITOR.config.jqueryOverrideVal;
51 var jQuery = window.jQuery;
53 if ( typeof jQuery == 'undefined' )
56 // jQuery object methods.
57 jQuery.extend( jQuery.fn,
58 /** @lends jQuery.fn */
61 * Return existing CKEditor instance for first matched element.
62 * Allows to easily use internal API. Doesn't return jQuery object.
64 * Raised exception if editor doesn't exist or isn't ready yet.
66 * @name jQuery.ckeditorGet
67 * @return CKEDITOR.editor
68 * @see CKEDITOR.editor
70 ckeditorGet: function()
72 var instance = this.eq( 0 ).data( 'ckeditorInstance' );
74 throw "CKEditor not yet initialized, use ckeditor() with callback.";
78 * Triggers creation of CKEditor in all matched elements (reduced to DIV, P and TEXTAREAs).
79 * Binds callback to instanceReady event of all instances. If editor is already created, than
80 * callback is fired right away.
82 * Mixed parameter order allowed.
84 * @param callback Function to be run on editor instance. Passed parameters: [ textarea ].
85 * Callback is fiered in "this" scope being ckeditor instance and having source textarea as first param.
87 * @param config Configuration options for new instance(s) if not already created.
91 * $( 'textarea' ).ckeditor( function( textarea ) {
92 * $( textarea ).val( this.getData() )
95 * @name jQuery.fn.ckeditor
98 ckeditor: function( callback, config )
100 if ( !CKEDITOR.env.isCompatible )
103 if ( !jQuery.isFunction( callback ))
109 config = config || {};
111 this.filter( 'textarea, div, p' ).each( function()
113 var $element = jQuery( this ),
114 editor = $element.data( 'ckeditorInstance' ),
115 instanceLock = $element.data( '_ckeditorInstanceLock' ),
118 if ( editor && !instanceLock )
121 callback.apply( editor, [ this ] );
123 else if ( !instanceLock )
125 // CREATE NEW INSTANCE
127 // Handle config.autoUpdateElement inside this plugin if desired.
128 if ( config.autoUpdateElement
129 || ( typeof config.autoUpdateElement == 'undefined' && CKEDITOR.config.autoUpdateElement ) )
131 config.autoUpdateElementJquery = true;
134 // Always disable config.autoUpdateElement.
135 config.autoUpdateElement = false;
136 $element.data( '_ckeditorInstanceLock', true );
138 // Set instance reference in element's data.
139 editor = CKEDITOR.replace( element, config );
140 $element.data( 'ckeditorInstance', editor );
142 // Register callback.
143 editor.on( 'instanceReady', function( event )
145 var editor = event.editor;
146 setTimeout( function()
148 // Delay bit more if editor is still not ready.
149 if ( !editor.element )
151 setTimeout( arguments.callee, 100 );
155 // Remove this listener.
156 event.removeListener( 'instanceReady', this.callee );
158 // Forward setData on dataReady.
159 editor.on( 'dataReady', function()
161 $element.trigger( 'setData' + '.ckeditor', [ editor ] );
165 editor.on( 'getData', function( event ) {
166 $element.trigger( 'getData' + '.ckeditor', [ editor, event.data ] );
169 // Forward destroy event.
170 editor.on( 'destroy', function()
172 $element.trigger( 'destroy.ckeditor', [ editor ] );
175 // Integrate with form submit.
176 if ( editor.config.autoUpdateElementJquery && $element.is( 'textarea' ) && $element.parents( 'form' ).length )
178 var onSubmit = function()
180 $element.ckeditor( function()
182 editor.updateElement();
186 // Bind to submit event.
187 $element.parents( 'form' ).submit( onSubmit );
189 // Bind to form-pre-serialize from jQuery Forms plugin.
190 $element.parents( 'form' ).bind( 'form-pre-serialize', onSubmit );
192 // Unbind when editor destroyed.
193 $element.bind( 'destroy.ckeditor', function()
195 $element.parents( 'form' ).unbind( 'submit', onSubmit );
196 $element.parents( 'form' ).unbind( 'form-pre-serialize', onSubmit );
200 // Garbage collect on destroy.
201 editor.on( 'destroy', function()
203 $element.data( 'ckeditorInstance', null );
207 $element.data( '_ckeditorInstanceLock', null );
209 // Fire instanceReady event.
210 $element.trigger( 'instanceReady.ckeditor', [ editor ] );
212 // Run given (first) code.
214 callback.apply( editor, [ element ] );
216 }, null, null, 9999);
220 // Editor is already during creation process, bind our code to the event.
221 CKEDITOR.on( 'instanceReady', function( event )
223 var editor = event.editor;
224 setTimeout( function()
226 // Delay bit more if editor is still not ready.
227 if ( !editor.element )
229 setTimeout( arguments.callee, 100 );
233 if ( editor.element.$ == element )
237 callback.apply( editor, [ element ] );
240 }, null, null, 9999);
247 // New val() method for objects.
248 if ( CKEDITOR.config.jqueryOverrideVal )
250 jQuery.fn.val = CKEDITOR.tools.override( jQuery.fn.val, function( oldValMethod )
253 * CKEditor-aware val() method.
255 * Acts same as original jQuery val(), but for textareas which have CKEditor instances binded to them, method
256 * returns editor's content. It also works for settings values.
258 * @param oldValMethod
259 * @name jQuery.fn.val
261 return function( newValue, forceNative )
263 var isSetter = typeof newValue != 'undefined',
266 this.each( function()
268 var $this = jQuery( this ),
269 editor = $this.data( 'ckeditorInstance' );
271 if ( !forceNative && $this.is( 'textarea' ) && editor )
274 editor.setData( newValue );
277 result = editor.getData();
285 oldValMethod.call( $this, newValue );
288 result = oldValMethod.call( $this );
296 return isSetter ? this : result;