| | 1 | /* |
| | 2 | Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. |
| | 3 | For 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 | * @name jQuery.ckeditorGet |
| | 58 | * @return CKEDITOR.editor |
| | 59 | * @see CKEDITOR.editor |
| | 60 | */ |
| | 61 | ckeditorGet: function() |
| | 62 | { |
| | 63 | var instance = this.eq( 0 ).data( 'ckeditorInstance' ); |
| | 64 | if ( !instance ) |
| | 65 | throw "CKEditor not yet initialized, use ckeditor() with callback."; |
| | 66 | return instance; |
| | 67 | }, |
| | 68 | /** |
| | 69 | * Triggers creation of CKEditor in all matched elements (reduced to DIV, P and TEXTAREAs). |
| | 70 | * Binds callback to instanceReady event of all instances. If editor is already created, than |
| | 71 | * callback is fired right away. |
| | 72 | * |
| | 73 | * Mixed parameter order allowed. |
| | 74 | * |
| | 75 | * @param callback Function to be run on editor instance. Passed parameters: [ textarea ]. |
| | 76 | * Callback is fiered in "this" scope being ckeditor instance and having source textarea as first param. |
| | 77 | * |
| | 78 | * @param config Configuration options for new instance(s) if not already created. |
| | 79 | * See URL |
| | 80 | * |
| | 81 | * @example |
| | 82 | * $( 'textarea' ).ckeditor( function( textarea ) { |
| | 83 | * $( textarea ).val( this.getData() ) |
| | 84 | * } ); |
| | 85 | * |
| | 86 | * @name jQuery.fn.ckeditor |
| | 87 | * @return jQuery.fn |
| | 88 | */ |
| | 89 | ckeditor: function( callback, config ) |
| | 90 | { |
| | 91 | if ( !jQuery.isFunction( callback )) |
| | 92 | { |
| | 93 | var tmp = config; |
| | 94 | config = callback; |
| | 95 | callback = tmp; |
| | 96 | } |
| | 97 | config = config || {}; |
| | 98 | |
| | 99 | this.filter( 'textarea, div, p' ).each( function() |
| | 100 | { |
| | 101 | var $element = jQuery( this ), |
| | 102 | instance = $element.data( 'ckeditorInstance' ), |
| | 103 | element = this; |
| | 104 | |
| | 105 | if ( instance ) |
| | 106 | { |
| | 107 | if ( callback ) |
| | 108 | callback.apply( instance, [ this ] ); |
| | 109 | } |
| | 110 | else if ( $element.data( '_ckeditorInstanceLock' ) ) |
| | 111 | { |
| | 112 | // Editor is already during creation process, bind our code to the event. |
| | 113 | CKEDITOR.on( 'instanceReady', function( event ) |
| | 114 | { |
| | 115 | var editor = event.editor; |
| | 116 | setTimeout( function() |
| | 117 | { |
| | 118 | // Delay bit more if editor is still not ready. |
| | 119 | if ( !editor.element ) |
| | 120 | return setTimeout( arguments.callee, 100 ); |
| | 121 | |
| | 122 | if ( editor.element.$ == element ) |
| | 123 | { |
| | 124 | // Run given code. |
| | 125 | if ( callback ) |
| | 126 | callback.apply( editor, [ element ] ); |
| | 127 | } |
| | 128 | }, 0 ); |
| | 129 | }, null, null, 9999); |
| | 130 | } |
| | 131 | else |
| | 132 | { |
| | 133 | // CREATE NEW INSTANCE |
| | 134 | // Register callback. |
| | 135 | CKEDITOR.on( 'instanceReady', function( event ) |
| | 136 | { |
| | 137 | var editor = event.editor; |
| | 138 | setTimeout( function() |
| | 139 | { |
| | 140 | // Delay bit more if editor is still not ready. |
| | 141 | if ( !editor.element ) |
| | 142 | return setTimeout( arguments.callee, 100 ); |
| | 143 | |
| | 144 | if ( editor.element.$ != element ) |
| | 145 | return; |
| | 146 | |
| | 147 | // Remove this listener. |
| | 148 | event.removeListener( 'instanceReady', this.callee ); |
| | 149 | |
| | 150 | // Forward setData and getData events. |
| | 151 | jQuery.each( [ 'setData', 'getData' ], function( i, name ) |
| | 152 | { |
| | 153 | editor.on( name, function( event ) { |
| | 154 | $element.trigger( name + '.ckeditor', [ editor, event.data.dataValue ] ); |
| | 155 | }); |
| | 156 | }); |
| | 157 | |
| | 158 | // Forward destroy event. |
| | 159 | editor.on( 'destroy', function( event ) |
| | 160 | { |
| | 161 | $element.trigger( 'destroy.ckeditor', [ editor ] ); |
| | 162 | }); |
| | 163 | |
| | 164 | // Integrate with form submit. |
| | 165 | if ( editor.config.autoUpdateElementJquery && $element.is( 'textarea' ) && $element.parents( 'form' ).length ) |
| | 166 | { |
| | 167 | var onSubmit = function() |
| | 168 | { |
| | 169 | $element.ckeditor( function() |
| | 170 | { |
| | 171 | editor.updateElement(); |
| | 172 | }); |
| | 173 | }; |
| | 174 | |
| | 175 | // Bind to submit event. |
| | 176 | $element.parents( 'form' ).submit( onSubmit ); |
| | 177 | |
| | 178 | // Unbind when editor destroyed. |
| | 179 | $element.bind( 'destroy.ckeditor', function() |
| | 180 | { |
| | 181 | $element.parents( 'form' ).unbind( 'submit', onSubmit ); |
| | 182 | }); |
| | 183 | } |
| | 184 | |
| | 185 | // Set instance reference in element's data. |
| | 186 | $element.data( 'ckeditorInstance', editor ); |
| | 187 | |
| | 188 | // Garbage collect on destroy. |
| | 189 | $element.bind( 'destroy.ckeditor', function() |
| | 190 | { |
| | 191 | $element.data( 'ckeditorInstance', null ); |
| | 192 | }); |
| | 193 | |
| | 194 | // Remove lock. |
| | 195 | $element.data( '_ckeditorInstanceLock', null ); |
| | 196 | |
| | 197 | // Fire instanceReady event. |
| | 198 | $element.trigger( 'instanceReady.ckeditor', [ editor ] ); |
| | 199 | |
| | 200 | // Run given (first) code. |
| | 201 | if ( callback ) |
| | 202 | callback.apply( editor, [ element ] ); |
| | 203 | }, 0 ); |
| | 204 | }, null, null, 9999); |
| | 205 | |
| | 206 | // Trigger instance creation. |
| | 207 | |
| | 208 | // Handle config.autoUpdateElement inside this plugin if desired. |
| | 209 | if ( config.autoUpdateElement |
| | 210 | || ( typeof config.autoUpdateElement == 'undefined' && CKEDITOR.config.autoUpdateElement ) ) |
| | 211 | { |
| | 212 | config.autoUpdateElementJquery = true; |
| | 213 | } |
| | 214 | |
| | 215 | // Always disable config.autoUpdateElement. |
| | 216 | config.autoUpdateElement = false; |
| | 217 | $element.data( '_ckeditorInstanceLock', true ); |
| | 218 | CKEDITOR.replace( element, config ); |
| | 219 | } |
| | 220 | }); |
| | 221 | return this; |
| | 222 | } |
| | 223 | }); |
| | 224 | |
| | 225 | // New val() method for objects. |
| | 226 | if ( CKEDITOR.config.jqueryOverrideVal ) |
| | 227 | { |
| | 228 | jQuery.fn.val = CKEDITOR.tools.override( jQuery.fn.val, function( oldValMethod ) |
| | 229 | { |
| | 230 | /** |
| | 231 | * CKEditor-aware val() method. |
| | 232 | * |
| | 233 | * Acts same as original jQuery val(), but for textareas which have CKEditor instances binded to them, method |
| | 234 | * returns editor's content. It also works for settings values. |
| | 235 | * |
| | 236 | * @param oldValMethod |
| | 237 | * @name jQuery.fn.val |
| | 238 | */ |
| | 239 | return function( newValue, forceNative ) |
| | 240 | { |
| | 241 | var isSetter = typeof newValue != 'undefined', |
| | 242 | result; |
| | 243 | |
| | 244 | this.each( function() |
| | 245 | { |
| | 246 | var $this = jQuery( this ), |
| | 247 | editor = $this.data( 'ckeditorInstance' ); |
| | 248 | |
| | 249 | if ( !forceNative && $this.is( 'textarea' ) && editor ) |
| | 250 | { |
| | 251 | if ( isSetter ) |
| | 252 | editor.setData( newValue ); |
| | 253 | else |
| | 254 | { |
| | 255 | result = editor.getData(); |
| | 256 | // break; |
| | 257 | return null; |
| | 258 | } |
| | 259 | } |
| | 260 | else |
| | 261 | { |
| | 262 | if ( isSetter ) |
| | 263 | oldValMethod.call( $this, newValue ); |
| | 264 | else |
| | 265 | { |
| | 266 | result = oldValMethod.call( $this ); |
| | 267 | // break; |
| | 268 | return null; |
| | 269 | } |
| | 270 | } |
| | 271 | }); |
| | 272 | return isSetter ? this : result; |
| | 273 | }; |
| | 274 | }); |
| | 275 | } |
| | 276 | } |
| | 277 | }); |
| | 278 | })(); |
| | 279 | |
| | 280 | /** |
| | 281 | * Allow CKEditor to override jQuery.fn.val(). This results in ability to use val() |
| | 282 | * function on textareas as usual and having those calls synchronized with CKEditor |
| | 283 | * Rich Text Editor component. |
| | 284 | * |
| | 285 | * This config option is global and executed during plugin load. |
| | 286 | * Can't be customized across editor instances. |
| | 287 | * |
| | 288 | * @type Boolean |
| | 289 | * @example |
| | 290 | * $( 'textarea' ).ckeditor(); |
| | 291 | * // ... |
| | 292 | * $( 'textarea' ).val( 'New content' ); |
| | 293 | */ |
| | 294 | CKEDITOR.config.jqueryOverrideVal = true; |
| | 295 | |
| | 296 | /** |
| | 297 | * Autoload jquery plugin if jQuery is available on page. |
| | 298 | * |
| | 299 | * This config option is global and executed during CKEditor load. |
| | 300 | * Can't be customized across editor instances. |
| | 301 | * |
| | 302 | * @type Boolean |
| | 303 | */ |
| | 304 | CKEDITOR.config.autoLoad_jqueryPlugin = false; |