Ticket #4210: 4210_3.patch

File 4210_3.patch, 8.4 KB (added by tobiasz.cudnik, 3 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; 
     28                         
     29                        // jQuery namespace methods. 
     30                        jQuery.extend( jQuery, 
     31                        /** @lends jQuery */ 
     32                        { 
     33                                /** 
     34                                 * Easily extend global CKEDITOR.config object. 
     35                                 * 
     36                                 * @name jQuery.ckeditorConfig 
     37                                 * @param myConfig 
     38                                 * @example 
     39                                 * jQuery.ckeditorConfig( { language: 'ar' } ); 
     40                                 */ 
     41                                ckeditorConfig: function( myConfig ) 
     42                                { 
     43                                        jQuery.extend( CKEDITOR.config, myConfig ); 
     44                                } 
     45                        }); 
     46                         
     47                        // jQuery object methods. 
     48                        jQuery.extend( jQuery.fn, 
     49                        /** @lends jQuery.fn */ 
     50                        { 
     51                                /** 
     52                                 * Return existing CKEditor instance for first matched element. 
     53                                 * Allows to easily use internal API. Doesn't return jQuery object. 
     54                                 * 
     55                                 * Raised exception if editor doesn't exist or isn't ready yet. 
     56                                 * 
     57                                 * @return CKEDITOR.editor 
     58                                 * @see CKEDITOR.editor 
     59                                 */ 
     60                                ckeditorGet: function() 
     61                                { 
     62                                        var instance = this.eq( 0 ).data( 'ckeditorInstance' ); 
     63                                        if ( !instance ) 
     64                                                throw "CKEditor not yet initialized, use ckeditor() with callback."; 
     65                                        return instance; 
     66                                }, 
     67                                /** 
     68                                 * Triggers creation of CKEditor in all matched elements (reduced to DIV, P and TEXTAREAs). 
     69                                 * Binds callback to instanceReady event. If editor is already TODO 
     70                                 * 
     71                                 * Mixed parameter order allowed. 
     72                                 * 
     73                                 * @param callback Function to be run on editor instance. Passed parameters: [ textarea ]. 
     74                                 * Callback is fiered in "this" scope being ckeditor instance and having source textarea as first param. 
     75                                 * 
     76                                 * @param config Configuration options for new instance(s) if not already created. 
     77                                 * See URL 
     78                                 * 
     79                                 * @example 
     80                                 * $( 'textarea' ).ckeditor( function( textarea ) { 
     81                                 *   $( textarea ).val( this.getData() ) 
     82                                 * } ); 
     83                                 */ 
     84                                ckeditor: function( callback, config ) 
     85                                { 
     86                                        if ( !jQuery.isFunction( callback )) 
     87                                        { 
     88                                                var tmp = config; 
     89                                                config = callback; 
     90                                                callback = tmp; 
     91                                        } 
     92                                        config = config || {}; 
     93                                         
     94                                        this.filter( 'textarea, div, p' ).each( function() 
     95                                        { 
     96                                                var $element = jQuery( this ), 
     97                                                        instance = $element.data( 'ckeditorInstance' ), 
     98                                                        element = this; 
     99 
     100                                                if ( instance ) 
     101                                                { 
     102                                                        if ( callback ) 
     103                                                                callback.apply( instance, [ this ] ); 
     104                                                } 
     105                                                else if ( $element.data( '_ckeditorInstanceLock' ) ) 
     106                                                { 
     107                                                        // Editor is alredy during creation process, bind our code to the event. 
     108                                                        CKEDITOR.on( 'instanceReady', function( event ) 
     109                                                        { 
     110                                                                var editor = event.editor; 
     111                                                                setTimeout( function() 
     112                                                                { 
     113                                                                        // Delay bit more if editor is still not ready. 
     114                                                                        if ( !editor.element ) 
     115                                                                                return setTimeout( arguments.callee, 100 ); 
     116 
     117                                                                        if ( editor.element.$ == element ) 
     118                                                                        { 
     119                                                                                // Run given code. 
     120                                                                                if ( callback ) 
     121                                                                                        callback.apply( editor, [ element ] ); 
     122                                                                        } 
     123                                                                }, 0 ); 
     124                                                        }, null, null, 9999); 
     125                                                } 
     126                                                else 
     127                                                { 
     128                                                        // CREATE NEW INSTANCE 
     129                                                        // Register callback. 
     130                                                        CKEDITOR.on( 'instanceReady', function( event ) 
     131                                                        { 
     132                                                                var editor = event.editor; 
     133                                                                setTimeout( function() 
     134                                                                { 
     135                                                                        // Delay bit more if editor is still not ready. 
     136                                                                        if ( !editor.element ) 
     137                                                                                return setTimeout( arguments.callee, 100 ); 
     138 
     139                                                                        if ( editor.element.$ != element ) 
     140                                                                                return; 
     141 
     142                                                                        // Remove this listener. 
     143                                                                        event.removeListener( 'instanceReady', this.callee ); 
     144 
     145                                                                        // Forward setData and getData events. 
     146                                                                        jQuery.each( [ 'setData', 'getData' ], function( i, name ) 
     147                                                                        { 
     148                                                                                editor.on( name, function( event ) { 
     149                                                                                        $element.trigger( name + '.ckeditor', [ editor, event.data.dataValue ] ); 
     150                                                                                }); 
     151                                                                        }); 
     152 
     153                                                                        // Forward destroy event. 
     154                                                                        editor.on( 'destroy', function( event ) 
     155                                                                        { 
     156                                                                                $element.trigger( 'destroy.ckeditor', [ editor ] ); 
     157                                                                        }); 
     158 
     159                                                                        // Integrate with form submit. 
     160                                                                        if ( editor.config.autoUpdateElementJquery && $element.is( 'textarea' ) && $element.parents( 'form' ).length ) 
     161                                                                        { 
     162                                                                                var onSubmit = function() 
     163                                                                                { 
     164                                                                                        $element.ckeditor( function() 
     165                                                                                        { 
     166                                                                                                editor.updateElement(); 
     167                                                                                        }); 
     168                                                                                }; 
     169 
     170                                                                                // Bind to submit event. 
     171                                                                                $element.parents( 'form' ).submit( onSubmit ); 
     172 
     173                                                                                // Unbind when editor destroyed. 
     174                                                                                $element.bind( 'destroy.ckeditor', function() 
     175                                                                                { 
     176                                                                                        $element.parents( 'form' ).unbind( 'submit', onSubmit ); 
     177                                                                                }); 
     178                                                                        } 
     179 
     180                                                                        // Set instance reference in element's data. 
     181                                                                        $element.data( 'ckeditorInstance', editor ); 
     182 
     183                                                                        // Garbage collect on destroy. 
     184                                                                        $element.bind( 'destroy.ckeditor', function() 
     185                                                                        { 
     186                                                                                $element.data( 'ckeditorInstance', null ); 
     187                                                                        }); 
     188 
     189                                                                        // Remove lock. 
     190                                                                        $element.data( '_ckeditorInstanceLock', null ); 
     191 
     192                                                                        // Fire instanceReady event. 
     193                                                                        $element.trigger( 'instanceReady.ckeditor', [ editor ] ); 
     194 
     195                                                                        // Run given (first) code. 
     196                                                                        if ( callback ) 
     197                                                                                callback.apply( editor, [ element ] ); 
     198                                                                }, 0 ); 
     199                                                        }, null, null, 9999); 
     200                                                         
     201                                                        // Trigger instance creation. 
     202 
     203                                                        // Handle config.autoUpdateElement inside this plugin if desired. 
     204                                                        if ( config.autoUpdateElement 
     205                                                                || ( typeof config.autoUpdateElement == 'undefined' && CKEDITOR.config.autoUpdateElement ) ) 
     206                                                        { 
     207                                                                config.autoUpdateElementJquery = true; 
     208                                                        } 
     209 
     210                                                        // Always disable config.autoUpdateElement. 
     211                                                        config.autoUpdateElement = false; 
     212                                                        $element.data( '_ckeditorInstanceLock', true ); 
     213                                                        CKEDITOR.replace( element, config ); 
     214                                                } 
     215                                        }); 
     216                                        return this; 
     217                                } 
     218                        }); 
     219 
     220                        // New val() method for objects. 
     221                        if ( CKEDITOR.config.jqueryOverrideVal ) 
     222                        { 
     223                                jQuery.fn.val = CKEDITOR.tools.override( jQuery.fn.val, function( oldValMethod ) 
     224                                { 
     225                                        return function( newValue, forceNative ) 
     226                                        { 
     227                                                var isSetter = typeof newValue != 'undefined', 
     228                                                        result; 
     229 
     230                                                this.each( function() 
     231                                                { 
     232                                                        var $this = jQuery( this ), 
     233                                                                editor = $this.data( 'ckeditorInstance' ); 
     234 
     235                                                        if ( !forceNative && $this.is( 'textarea' ) && editor ) 
     236                                                        { 
     237                                                                if ( isSetter ) 
     238                                                                        editor.setData( newValue ); 
     239                                                                else 
     240                                                                { 
     241                                                                        result = editor.getData(); 
     242                                                                        // break; 
     243                                                                        return null; 
     244                                                                } 
     245                                                        } 
     246                                                        else 
     247                                                        { 
     248                                                                if ( isSetter ) 
     249                                                                        oldValMethod.call( $this, newValue ); 
     250                                                                else 
     251                                                                { 
     252                                                                        result = oldValMethod.call( $this ); 
     253                                                                        // break; 
     254                                                                        return null; 
     255                                                                } 
     256                                                        } 
     257                                                }); 
     258                                                return isSetter ? this : result; 
     259                                        }; 
     260                                }); 
     261                        } 
     262                } 
     263        }); 
     264})(); 
     265 
     266/** 
     267 * Allow CKEditor to override jQuery.fn.val(). This results in ability to use val() 
     268 * function on textareas as usual and having those calls synchronized with CKEditor 
     269 * Rich Text Editor component. 
     270 * 
     271 * This config option is global and executed during plugin load. Can't be customized accross 
     272 * editor instances. 
     273 * 
     274 * @type Boolean 
     275 * @example 
     276 * $( 'textarea' ).ckeditor(); 
     277 * // ... 
     278 * $( 'textarea' ).val( 'New editor content' ); 
     279 */ 
     280CKEDITOR.config.jqueryOverrideVal = true; 
© 2003 – 2011 CKSource – Frederico Knabben. All rights reserved. | Terms of use | Privacy policy