Ticket #4210: 4210_2.patch

File 4210_2.patch, 6.8 KB (added by Tobiasz Cudnik, 15 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                                        config = config || {};
     73                                       
     74                                        this.filter( 'textarea, div, p' ).each( function()
     75                                        {
     76                                                var $element = jQuery( this ),
     77                                                        instance = $element.data( 'ckeditorInstance' ),
     78                                                        element = this;
     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                                                                var editor = event.editor;
     92                                                                setTimeout( function()
     93                                                                {
     94                                                                        // Delay bit more if editor is still not ready.
     95                                                                        if ( !editor.element )
     96                                                                                return setTimeout( arguments.callee, 100 );
     97
     98                                                                        if ( editor.element.$ == element )
     99                                                                        {
     100                                                                                // Remove this listener.
     101                                                                                event.removeListener( 'instanceReady', this.callee );
     102
     103                                                                                // Remove lock.
     104                                                                                $element.data( '_ckeditorInstanceLock', null );
     105
     106                                                                                // Forward setData and getData events.
     107                                                                                jQuery.each( [ 'setData', 'getData' ], function( i, name )
     108                                                                                {
     109                                                                                        editor.on( name, function( event ) {
     110                                                                                                $element.trigger( name + '.ckeditor', [ editor, event.data.dataValue ] );
     111                                                                                        });
     112                                                                                });
     113
     114                                                                                // Forward destroy event.
     115                                                                                editor.on( 'destroy', function( event ) {
     116                                                                                        $element.trigger( 'destroy.ckeditor', [ editor ] );
     117                                                                                });
     118
     119                                                                                // Integrate with form submit.
     120                                                                                if ( editor.config.autoUpdateElementJquery && $element.is( 'textarea' ) && $element.parents( 'form' ).length )
     121                                                                                {
     122                                                                                        var onSubmit = function()
     123                                                                                        {
     124                                                                                                $element.ckeditor( function()
     125                                                                                                {
     126                                                                                                        editor.updateElement();
     127                                                                                                });
     128                                                                                        };
     129
     130                                                                                        // Bind to submit event.
     131                                                                                        $element.parents( 'form' ).submit( onSubmit );
     132                                                                                       
     133                                                                                        // Unbind when editor destroyed.
     134                                                                                        $element.bind( 'destroy.ckeditor', function() {
     135                                                                                                        $element.parents( 'form' ).unbind( 'submit', onSubmit );
     136                                                                                                });
     137                                                                                }
     138                                                                               
     139                                                                                // Fire instanceReady event.
     140                                                                                $element.trigger( 'instanceReady.ckeditor', [ editor ] );
     141
     142                                                                                // Run given code.
     143                                                                                if ( callback )
     144                                                                                        callback.apply( editor, [ element ] );
     145                                                                        }
     146                                                                });
     147                                                        }, null, null, 9999);
     148                                                       
     149                                                        // Trigger instance creation.
     150                                                        if ( !$element.data( '_ckeditorInstanceLock' ) )
     151                                                        {
     152                                                                // Handle config.autoUpdateElement inside this plugin if desired
     153                                                                if ( config.autoUpdateElement
     154                                                                        || ( typeof config.autoUpdateElement == 'undefined' && CKEDITOR.config.autoUpdateElement ) )
     155                                                                {
     156                                                                        config.autoUpdateElementJquery = true;
     157                                                                }
     158                                                               
     159                                                                // Always disable config.autoUpdateElement
     160                                                                config.autoUpdateElement = false;
     161                                                                $element
     162                                                                        .data( '_ckeditorInstanceLock', true )
     163                                                                        .data( 'ckeditorInstance', CKEDITOR.replace( element, config ) );
     164                                                        }
     165                                                }
     166                                        });
     167                                        return this;
     168                                }
     169                        });
     170
     171                        // New val() method for objects.
     172                        if ( CKEDITOR.config.jqueryOverrideVal )
     173                        {
     174                                jQuery.fn.val = CKEDITOR.tools.override( jQuery.fn.val, function( oldValMethod )
     175                                {
     176                                        return function( newValue, forceNative )
     177                                        {
     178                                                var isSetter = typeof newValue != 'undefined',
     179                                                        result;
     180
     181                                                this.each( function()
     182                                                {
     183                                                        var $this = jQuery( this ),
     184                                                                editor = $this.data( 'ckeditorInstance' );
     185
     186                                                        if ( !forceNative && $this.is( 'textarea' ) && editor )
     187                                                        {
     188                                                                if ( isSetter )
     189                                                                        editor.setData( newValue );
     190                                                                else
     191                                                                {
     192                                                                        result = editor.getData();
     193                                                                        // break;
     194                                                                        return null;
     195                                                                }
     196                                                        }
     197                                                        else
     198                                                        {
     199                                                                if ( isSetter )
     200                                                                        oldValMethod.call( $this, newValue );
     201                                                                else
     202                                                                {
     203                                                                        result = oldValMethod.call( $this );
     204                                                                        // break;
     205                                                                        return null;
     206                                                                }
     207                                                        }
     208                                                });
     209                                                return isSetter ? this : result;
     210                                        };
     211                                });
     212                        }
     213                }
     214        });
     215})();
     216
     217/**
     218 * Allow CKEditor to override jQuery.fn.val(). This results in ability to use val()
     219 * function on textareas as usual and having those calls synchronized with CKEditor
     220 * Rich Text Editor component.
     221 *
     222 * This config option is global and executed during plugin load. Can't be customized accross
     223 * editor instances.
     224 *
     225 * @type Boolean
     226 * @example
     227 * $( 'textarea' ).ckeditor();
     228 * // ...
     229 * $( 'textarea' ).val( 'New editor content' );
     230 */
     231CKEDITOR.config.jqueryOverrideVal = true;
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy