Ticket #4210: 4210.patch

File 4210.patch, 6.8 KB (added by Tobiasz Cudnik, 14 years ago)
  • _source/plugins/jquery/plugin.js

     
     1/*
     2Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
     3For licensing, see LICENSE.html or http://ckeditor.com/license
     4*/
     5
     6/**
     7 * @fileOverview jQuery plugin provides easy use of basic CKEditor functions
     8 *   and access to internal API. It also integrates some aspects of CKEditor with
     9 *   jQuery framework.
     10 */
     11
     12(function()
     13{
     14        CKEDITOR.plugins.add( 'jquery',
     15        {
     16                load : function()
     17                {
     18                        if ( !this.onLoad._called )
     19                        {
     20                                this.onLoad();
     21                                this.onLoad._called = 1;
     22                        }
     23                },
     24                onLoad : function()
     25                {
     26                        if ( typeof jQuery == 'undefined' )
     27                                return console.log('no jQuery');
     28                       
     29                        // jQuery namespace methods.
     30                        jQuery.extend( jQuery,
     31                        {
     32                                ckeditorConfig: function( myConfig )
     33                                {
     34                                        jQuery.extend( CKEDITOR.config, myConfig );
     35                                }
     36                        });
     37                       
     38                        // jQuery object methods.
     39                        jQuery.extend( jQuery.fn,
     40                        {
     41                                ckeditorGet: function()
     42                                {
     43                                        var instance = this.eq( 0 ).data( 'ckeditorInstance' );
     44                                        if ( !instance )
     45                                                throw "CKEditor not yet initialized, use ckeditor() with callback.";
     46                                        return instance;
     47                                },
     48                                /**
     49                                 *
     50                                 *
     51                                 * Mixed parameter order allowed.
     52                                 *
     53                                 * @param callback Function to be run on editor instance. Passed parameters: [ textarea ].
     54                                 * Callback is fiered in "this" scope being ckeditor instance and having source textarea as first param.
     55                                 *
     56                                 * @param config Configuration options for new instance(s) if not already created.
     57                                 * See URL
     58                                 *
     59                                 * @example
     60                                 * $( 'textarea' ).ckeditor( function( textarea ) {
     61                                 *   $( textarea ).val( this.getData() )
     62                                 * });
     63                                 */
     64                                ckeditor: function( callback, config )
     65                                {
     66                                        if ( !jQuery.isFunction( callback ))
     67                                        {
     68                                                var tmp = config;
     69                                                config = callback;
     70                                                callback = tmp;
     71                                        }
     72                                        this.each( function()
     73                                        {
     74                                                var $element = jQuery( this ),
     75                                                        instance = $element.data( 'ckeditorInstance' ),
     76                                                        element = this;
     77
     78                                                // TODO filter elements ?
     79
     80                                                if ( instance )
     81                                                {
     82                                                        if ( callback )
     83                                                                callback.apply( instance, [ this ] );
     84                                                }
     85                                                else
     86                                                {
     87                                                        // CREATE NEW INSTANCE
     88                                                        // Register callback.
     89                                                        CKEDITOR.on( 'instanceReady', function( event )
     90                                                        {
     91
     92                                                                var editor = event.editor;
     93                                                                setTimeout( function()
     94                                                                {
     95                                                                        // Delay bit more if editor is still not ready.
     96                                                                        if ( !editor.element )
     97                                                                                return setTimeout( arguments.callee, 100 );
     98
     99                                                                        if ( editor.element.$ == element )
     100                                                                        {
     101                                                                                // Remove this listener.
     102                                                                                event.removeListener( 'instanceReady', this.callee );
     103
     104                                                                                // Remove lock.
     105                                                                                $element.data( '_ckeditorInstanceLock', null );
     106
     107                                                                                // Forward setData and getData events.
     108                                                                                jQuery.each( [ 'setData', 'getData' ], function( i, name )
     109                                                                                {
     110
     111                                                                                        editor.on( name, function( event ) {
     112
     113                                                                                                $element.trigger( name + '.ckeditor', [ editor, event.data.dataValue ] );
     114                                                                                        });
     115                                                                                });
     116
     117                                                                                // Forward destroy event.
     118                                                                                editor.on( 'destroy', function( event ) {
     119                                                                                        $element.trigger( 'destroy.ckeditor', [ editor ] );
     120                                                                                });
     121
     122                                                                                // Integrate with form submit.
     123                                                                                if ( editor.config.jquerySyncTextareaOnSubmit && $element.is( 'textarea' ) && $element.parents( 'form' ).length )
     124                                                                                {
     125                                                                                        var onSubmit = function()
     126                                                                                        {
     127
     128                                                                                                $element.ckeditor( function()
     129                                                                                                {
     130
     131        //                                                                                              $textarea.val( this.getData(), true );
     132                                                                                                        editor.updateElement();
     133                                                                                                });
     134                                                                                        };
     135
     136                                                                                        // Bind to submit event.
     137                                                                                        $element.parents( 'form' ).submit( onSubmit );
     138                                                                                       
     139                                                                                        // Unbind when editor destroyed.
     140                                                                                        $element.bind( 'destroy.ckeditor', function() {
     141                                                                                                        $element.parents( 'form' ).unbind( 'submit', onSubmit );
     142                                                                                                });
     143                                                                                }
     144                                                                                // Fire instanceReady event.
     145                                                                                $element.trigger( 'instanceReady.ckeditor', [ editor ] );
     146
     147                                                                                // Run given code.
     148                                                                                if ( callback )
     149                                                                                        callback.apply( editor, [ element ] );
     150                                                                        }
     151                                                                });
     152                                                        }, null, null, 9999);
     153                                                       
     154                                                        // Trigger instance creation.
     155                                                        if ( !$element.data( '_ckeditorInstanceLock' ) )
     156                                                        {
     157                                                                $element
     158                                                                        .data( '_ckeditorInstanceLock', true )
     159                                                                        .data( 'ckeditorInstance', CKEDITOR.replace( element, config ) );
     160
     161                                                        }
     162                                                }
     163                                        });
     164                                        return this;
     165                                }
     166                        });
     167
     168                        // New val() method for objects.
     169                        if ( CKEDITOR.config.jqueryOverrideVal )
     170                        {
     171                                jQuery.fn.val = CKEDITOR.tools.override( jQuery.fn.val, function( oldValMethod )
     172                                {
     173                                        return function( newValue, forceNative )
     174                                        {
     175                                                var isSetter = typeof newValue != 'undefined',
     176                                                        result;
     177
     178                                                this.each( function()
     179                                                {
     180
     181                                                        var $this = jQuery( this ),
     182                                                                editor = $this.data( 'ckeditorInstance' );
     183
     184                                                        if ( !forceNative && $this.is( 'textarea' ) && editor )
     185                                                        {
     186                                                                if ( isSetter )
     187                                                                        editor.setData( newValue );
     188                                                                else
     189                                                                {
     190                                                                        result = editor.getData();
     191
     192                                                                        // break;
     193                                                                        return null;
     194                                                                }
     195                                                        }
     196                                                        else
     197                                                        {
     198
     199                                                                if ( isSetter )
     200                                                                        oldValMethod.call( $this, newValue );
     201                                                                else
     202                                                                {
     203                                                                        result = oldValMethod.call( $this );
     204
     205                                                                        // break;
     206                                                                        return null;
     207                                                                }
     208
     209                                                        }
     210                                                });
     211                                                return isSetter ? this : result;
     212                                        };
     213                                });
     214                        }
     215                }
     216        });
     217})();
     218
     219/**
     220 * Synchronizes textarea content on submit.
     221 *
     222 * Works with AJAX forms only if CKEditor is created before calling
     223 * .ajaxForm() function on form element.
     224 *
     225 * @type Boolean
     226 * @example
     227 * $( 'form' )
     228 *   .find( 'textarea' ).ckeditor().end()
     229 *   .ajaxForm();
     230 */
     231CKEDITOR.config.jquerySyncTextareaOnSubmit = false;
     232
     233/**
     234 * Allow CKEditor to override jQuery.fn.val(). This results in ability to use val()
     235 * function on textareas as usual and having those calls synchronized with CKEditor
     236 * Rich Text Editor component.
     237 *
     238 * This config option is global and executed during plugin load. Can't be customized accross
     239 * editor instances.
     240 *
     241 * @type Boolean
     242 * @example
     243 * $( 'textarea' ).ckeditor();
     244 * // ...
     245 * $( 'textarea' ).val( 'New editor content' );
     246 */
     247CKEDITOR.config.jqueryOverrideVal = true;
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy