Dotclear

source: plugins/dcCKEditor/js/ckeditor/adapters/jquery.js @ 3039:2883094c39d9

Revision 3039:2883094c39d9, 12.2 KB checked in by Nicolas <nikrou77@…>, 10 years ago (diff)

Update ckeditor to 4.5.1
Many changes and issues fixed.

Line 
1/**
2 * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or http://ckeditor.com/license
4 */
5
6/**
7 * @fileOverview Defines the {@link CKEDITOR_Adapters.jQuery jQuery Adapter}.
8 */
9
10/**
11 * @class CKEDITOR_Adapters.jQuery
12 * @singleton
13 *
14 * The jQuery Adapter allows for easy use of basic CKEditor functions and access to the internal API.
15 * To find more information about the jQuery Adapter, go to the [jQuery Adapter section](#!/guide/dev_jquery)
16 * of the Developer's Guide or see the "Create Editors with jQuery" sample.
17 *
18 * @aside guide dev_jquery
19 */
20
21( function( $ ) {
22     if ( typeof $ == 'undefined' ) {
23          throw new Error( 'jQuery should be loaded before CKEditor jQuery adapter.' );
24     }
25
26     if ( typeof CKEDITOR == 'undefined' ) {
27          throw new Error( 'CKEditor should be loaded before CKEditor jQuery adapter.' );
28     }
29
30     /**
31      * Allows CKEditor to override `jQuery.fn.val()`. When set to `true`, the `val()` function
32      * used on textarea elements replaced with CKEditor uses the CKEditor API.
33      *
34      * This configuration option is global and is executed during the loading of the jQuery Adapter.
35      * It cannot be customized across editor instances.
36      *
37      *        <script>
38      *             CKEDITOR.config.jqueryOverrideVal = true;
39      *        </script>
40      *
41      *        <!-- Important: The jQuery Adapter is loaded *after* setting jqueryOverrideVal. -->
42      *        <script src="/ckeditor/adapters/jquery.js"></script>
43      *
44      *        <script>
45      *             $( 'textarea' ).ckeditor();
46      *             // ...
47      *             $( 'textarea' ).val( 'New content' );
48      *        </script>
49      *
50      * @cfg {Boolean} [jqueryOverrideVal=true]
51      * @member CKEDITOR.config
52      */
53     CKEDITOR.config.jqueryOverrideVal =
54          typeof CKEDITOR.config.jqueryOverrideVal == 'undefined' ? true : CKEDITOR.config.jqueryOverrideVal;
55
56     // jQuery object methods.
57     $.extend( $.fn, {
58          /**
59           * Returns an existing CKEditor instance for the first matched element.
60           * Allows to easily use the internal API. Does not return a jQuery object.
61           *
62           * Raises an exception if the editor does not exist or is not ready yet.
63           *
64           * @returns CKEDITOR.editor
65           * @deprecated Use {@link #editor editor property} instead.
66           */
67          ckeditorGet: function() {
68               var instance = this.eq( 0 ).data( 'ckeditorInstance' );
69
70               if ( !instance )
71                    throw 'CKEditor is not initialized yet, use ckeditor() with a callback.';
72
73               return instance;
74          },
75
76          /**
77           * A jQuery function which triggers the creation of CKEditor with `<textarea>` and
78           * {@link CKEDITOR.dtd#$editable editable} elements.
79           * Every `<textarea>` element will be converted to a classic (`iframe`-based) editor,
80           * while any other supported element will be converted to an inline editor.
81           * This method binds the callback to the `instanceReady` event of all instances.
82           * If the editor has already been created, the callback is fired straightaway.
83           * You can also create multiple editors at once by using `$( '.className' ).ckeditor();`.
84           *
85           * **Note**: jQuery chaining and mixed parameter order is allowed.
86           *
87           * @param {Function} callback
88           * Function to be run on the editor instance. Callback takes the source element as a parameter.
89           *
90           *        $( 'textarea' ).ckeditor( function( textarea ) {
91           *             // Callback function code.
92           *        } );
93           *
94           * @param {Object} config
95           * Configuration options for new instance(s) if not already created.
96           *
97           *        $( 'textarea' ).ckeditor( {
98           *             uiColor: '#9AB8F3'
99           *        } );
100           *
101           * @returns jQuery.fn
102           */
103          ckeditor: function( callback, config ) {
104               if ( !CKEDITOR.env.isCompatible )
105                    throw new Error( 'The environment is incompatible.' );
106
107               // Reverse the order of arguments if the first one isn't a function.
108               if ( !$.isFunction( callback ) ) {
109                    var tmp = config;
110                    config = callback;
111                    callback = tmp;
112               }
113
114               // An array of instanceReady callback promises.
115               var promises = [];
116
117               config = config || {};
118
119               // Iterate over the collection.
120               this.each( function() {
121                    var $element = $( this ),
122                         editor = $element.data( 'ckeditorInstance' ),
123                         instanceLock = $element.data( '_ckeditorInstanceLock' ),
124                         element = this,
125                         dfd = new $.Deferred();
126
127                    promises.push( dfd.promise() );
128
129                    if ( editor && !instanceLock ) {
130                         if ( callback )
131                              callback.apply( editor, [ this ] );
132
133                         dfd.resolve();
134                    } else if ( !instanceLock ) {
135                         // CREATE NEW INSTANCE
136
137                         // Handle config.autoUpdateElement inside this plugin if desired.
138                         if ( config.autoUpdateElement || ( typeof config.autoUpdateElement == 'undefined' && CKEDITOR.config.autoUpdateElement ) ) {
139                              config.autoUpdateElementJquery = true;
140                         }
141
142                         // Always disable config.autoUpdateElement.
143                         config.autoUpdateElement = false;
144                         $element.data( '_ckeditorInstanceLock', true );
145
146                         // Set instance reference in element's data.
147                         if ( $( this ).is( 'textarea' ) )
148                              editor = CKEDITOR.replace( element, config );
149                         else
150                              editor = CKEDITOR.inline( element, config );
151
152                         $element.data( 'ckeditorInstance', editor );
153
154                         // Register callback.
155                         editor.on( 'instanceReady', function( evt ) {
156                              var editor = evt.editor;
157
158                              setTimeout( function() {
159                                   // Delay bit more if editor is still not ready.
160                                   if ( !editor.element ) {
161                                        setTimeout( arguments.callee, 100 );
162                                        return;
163                                   }
164
165                                   // Remove this listener. Triggered when new instance is ready.
166                                   evt.removeListener();
167
168                                   /**
169                                    * Forwards the CKEditor {@link CKEDITOR.editor#event-dataReady dataReady event} as a jQuery event.
170                                    *
171                                    * @event dataReady
172                                    * @param {CKEDITOR.editor} editor Editor instance.
173                                    */
174                                   editor.on( 'dataReady', function() {
175                                        $element.trigger( 'dataReady.ckeditor', [ editor ] );
176                                   } );
177
178                                   /**
179                                    * Forwards the CKEditor {@link CKEDITOR.editor#event-setData setData event} as a jQuery event.
180                                    *
181                                    * @event setData
182                                    * @param {CKEDITOR.editor} editor Editor instance.
183                                    * @param data
184                                    * @param {String} data.dataValue The data that will be used.
185                                    */
186                                   editor.on( 'setData', function( evt ) {
187                                        $element.trigger( 'setData.ckeditor', [ editor, evt.data ] );
188                                   } );
189
190                                   /**
191                                    * Forwards the CKEditor {@link CKEDITOR.editor#event-getData getData event} as a jQuery event.
192                                    *
193                                    * @event getData
194                                    * @param {CKEDITOR.editor} editor Editor instance.
195                                    * @param data
196                                    * @param {String} data.dataValue The data that will be returned.
197                                    */
198                                   editor.on( 'getData', function( evt ) {
199                                        $element.trigger( 'getData.ckeditor', [ editor, evt.data ] );
200                                   }, 999 );
201
202                                   /**
203                                    * Forwards the CKEditor {@link CKEDITOR.editor#event-destroy destroy event} as a jQuery event.
204                                    *
205                                    * @event destroy
206                                    * @param {CKEDITOR.editor} editor Editor instance.
207                                    */
208                                   editor.on( 'destroy', function() {
209                                        $element.trigger( 'destroy.ckeditor', [ editor ] );
210                                   } );
211
212                                   // Overwrite save button to call jQuery submit instead of javascript submit.
213                                   // Otherwise jQuery.forms does not work properly
214                                   editor.on( 'save', function() {
215                                        $( element.form ).submit();
216                                        return false;
217                                   }, null, null, 20 );
218
219                                   // Integrate with form submit.
220                                   if ( editor.config.autoUpdateElementJquery && $element.is( 'textarea' ) && $( element.form ).length ) {
221                                        var onSubmit = function() {
222                                             $element.ckeditor( function() {
223                                                  editor.updateElement();
224                                             } );
225                                        };
226
227                                        // Bind to submit event.
228                                        $( element.form ).submit( onSubmit );
229
230                                        // Bind to form-pre-serialize from jQuery Forms plugin.
231                                        $( element.form ).bind( 'form-pre-serialize', onSubmit );
232
233                                        // Unbind when editor destroyed.
234                                        $element.bind( 'destroy.ckeditor', function() {
235                                             $( element.form ).unbind( 'submit', onSubmit );
236                                             $( element.form ).unbind( 'form-pre-serialize', onSubmit );
237                                        } );
238                                   }
239
240                                   // Garbage collect on destroy.
241                                   editor.on( 'destroy', function() {
242                                        $element.removeData( 'ckeditorInstance' );
243                                   } );
244
245                                   // Remove lock.
246                                   $element.removeData( '_ckeditorInstanceLock' );
247
248                                   /**
249                                    * Forwards the CKEditor {@link CKEDITOR.editor#event-instanceReady instanceReady event} as a jQuery event.
250                                    *
251                                    * @event instanceReady
252                                    * @param {CKEDITOR.editor} editor Editor instance.
253                                    */
254                                   $element.trigger( 'instanceReady.ckeditor', [ editor ] );
255
256                                   // Run given (first) code.
257                                   if ( callback )
258                                        callback.apply( editor, [ element ] );
259
260                                   dfd.resolve();
261                              }, 0 );
262                         }, null, null, 9999 );
263                    } else {
264                         // Editor is already during creation process, bind our code to the event.
265                         editor.once( 'instanceReady', function() {
266                              setTimeout( function() {
267                                   // Delay bit more if editor is still not ready.
268                                   if ( !editor.element ) {
269                                        setTimeout( arguments.callee, 100 );
270                                        return;
271                                   }
272
273                                   // Run given code.
274                                   if ( editor.element.$ == element && callback )
275                                        callback.apply( editor, [ element ] );
276
277                                   dfd.resolve();
278                              }, 0 );
279                         }, null, null, 9999 );
280                    }
281               } );
282
283               /**
284                * The [jQuery Promise object]((http://api.jquery.com/promise/)) that handles the asynchronous constructor.
285                * This promise will be resolved after **all** of the constructors.
286                *
287                * @property {Function} promise
288                */
289               var dfd = new $.Deferred();
290
291               this.promise = dfd.promise();
292
293               $.when.apply( this, promises ).then( function() {
294                    dfd.resolve();
295               } );
296
297               /**
298                * Existing CKEditor instance. Allows to easily use the internal API.
299                *
300                * **Note**: This is not a jQuery object.
301                *
302                *        var editor = $( 'textarea' ).ckeditor().editor;
303                *
304                * @property {CKEDITOR.editor} editor
305                */
306               this.editor = this.eq( 0 ).data( 'ckeditorInstance' );
307
308               return this;
309          }
310     } );
311
312     /**
313      * Overwritten jQuery `val()` method for `<textarea>` elements that have bound CKEditor instances.
314      * This method gets or sets editor content by using the {@link CKEDITOR.editor#method-getData editor.getData()}
315      * or {@link CKEDITOR.editor#method-setData editor.setData()} methods. To handle
316      * the {@link CKEDITOR.editor#method-setData editor.setData()} callback (as `setData` is asynchronous),
317      * `val( 'some data' )` will return a [jQuery Promise object](http://api.jquery.com/promise/).
318      *
319      * @method val
320      * @returns String|Number|Array|jQuery.fn|function(jQuery Promise)
321      */
322     if ( CKEDITOR.config.jqueryOverrideVal ) {
323          $.fn.val = CKEDITOR.tools.override( $.fn.val, function( oldValMethod ) {
324               return function( value ) {
325                    // Setter, i.e. .val( "some data" );
326                    if ( arguments.length ) {
327                         var _this = this,
328                              promises = [], //use promise to handle setData callback
329
330                              result = this.each( function() {
331                                   var $elem = $( this ),
332                                        editor = $elem.data( 'ckeditorInstance' );
333
334                                   // Handle .val for CKEditor.
335                                   if ( $elem.is( 'textarea' ) && editor ) {
336                                        var dfd = new $.Deferred();
337
338                                        editor.setData( value, function() {
339                                             dfd.resolve();
340                                        } );
341
342                                        promises.push( dfd.promise() );
343                                        return true;
344                                        // Call default .val function for rest of elements
345                                   } else {
346                                        return oldValMethod.call( $elem, value );
347                                   }
348                              } );
349
350                         // If there is no promise return default result (jQuery object of chaining).
351                         if ( !promises.length )
352                              return result;
353                         // Create one promise which will be resolved when all of promises will be done.
354                         else {
355                              var dfd = new $.Deferred();
356
357                              $.when.apply( this, promises ).done( function() {
358                                   dfd.resolveWith( _this );
359                              } );
360
361                              return dfd.promise();
362                         }
363                    }
364                    // Getter .val();
365                    else {
366                         var $elem = $( this ).eq( 0 ),
367                              editor = $elem.data( 'ckeditorInstance' );
368
369                         if ( $elem.is( 'textarea' ) && editor )
370                              return editor.getData();
371                         else
372                              return oldValMethod.call( $elem );
373                    }
374               };
375          } );
376     }
377} )( window.jQuery );
Note: See TracBrowser for help on using the repository browser.

Sites map