| 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 setData event for wysiwygarea mode. |
| 159 | editor.on( 'contentDom', function( event ) { |
| 160 | $element.trigger( 'setData' + '.ckeditor', [ editor, editor.getData() ] ); |
| 161 | }); |
| 162 | |
| 163 | // Forward setData only for sourcearea mode. |
| 164 | editor.on( 'setData', function( event ) { |
| 165 | if ( editor.mode != 'wysiwyg' ) |
| 166 | $element.trigger( 'setData' + '.ckeditor', [ editor, event.data.dataValue ] ); |
| 167 | }); |
| 168 | |
| 169 | // Forward getData. |
| 170 | editor.on( 'getData', function( event ) { |
| 171 | $element.trigger( 'getData' + '.ckeditor', [ editor, event.data.dataValue ] ); |
| 172 | }); |
| 173 | |
| 174 | // Forward destroy event. |
| 175 | editor.on( 'destroy', function( event ) |
| 176 | { |
| 177 | $element.trigger( 'destroy.ckeditor', [ editor ] ); |
| 178 | }); |
| 179 | |
| 180 | // Integrate with form submit. |
| 181 | if ( editor.config.autoUpdateElementJquery && $element.is( 'textarea' ) && $element.parents( 'form' ).length ) |
| 182 | { |
| 183 | var onSubmit = function() |
| 184 | { |
| 185 | $element.ckeditor( function() |
| 186 | { |
| 187 | editor.updateElement(); |
| 188 | }); |
| 189 | }; |
| 190 | |
| 191 | // Bind to submit event. |
| 192 | $element.parents( 'form' ).submit( onSubmit ); |
| 193 | |
| 194 | // Unbind when editor destroyed. |
| 195 | $element.bind( 'destroy.ckeditor', function() |
| 196 | { |
| 197 | $element.parents( 'form' ).unbind( 'submit', onSubmit ); |
| 198 | }); |
| 199 | } |
| 200 | |
| 201 | // Set instance reference in element's data. |
| 202 | $element.data( 'ckeditorInstance', editor ); |
| 203 | |
| 204 | // Garbage collect on destroy. |
| 205 | $element.bind( 'destroy.ckeditor', function() |
| 206 | { |
| 207 | $element.data( 'ckeditorInstance', null ); |
| 208 | }); |
| 209 | |
| 210 | // Remove lock. |
| 211 | $element.data( '_ckeditorInstanceLock', null ); |
| 212 | |
| 213 | // Fire instanceReady event. |
| 214 | $element.trigger( 'instanceReady.ckeditor', [ editor ] ); |
| 215 | |
| 216 | // Run given (first) code. |
| 217 | if ( callback ) |
| 218 | callback.apply( editor, [ element ] ); |
| 219 | }, 0 ); |
| 220 | }, null, null, 9999); |
| 221 | |
| 222 | // Trigger instance creation. |
| 223 | |
| 224 | // Handle config.autoUpdateElement inside this plugin if desired. |
| 225 | if ( config.autoUpdateElement |
| 226 | || ( typeof config.autoUpdateElement == 'undefined' && CKEDITOR.config.autoUpdateElement ) ) |
| 227 | { |
| 228 | config.autoUpdateElementJquery = true; |
| 229 | } |
| 230 | |
| 231 | // Always disable config.autoUpdateElement. |
| 232 | config.autoUpdateElement = false; |
| 233 | $element.data( '_ckeditorInstanceLock', true ); |
| 234 | CKEDITOR.replace( element, config ); |
| 235 | } |
| 236 | }); |
| 237 | return this; |
| 238 | } |
| 239 | }); |
| 240 | |
| 241 | // New val() method for objects. |
| 242 | if ( CKEDITOR.config.jqueryOverrideVal ) |
| 243 | { |
| 244 | jQuery.fn.val = CKEDITOR.tools.override( jQuery.fn.val, function( oldValMethod ) |
| 245 | { |
| 246 | /** |
| 247 | * CKEditor-aware val() method. |
| 248 | * |
| 249 | * Acts same as original jQuery val(), but for textareas which have CKEditor instances binded to them, method |
| 250 | * returns editor's content. It also works for settings values. |
| 251 | * |
| 252 | * @param oldValMethod |
| 253 | * @name jQuery.fn.val |
| 254 | */ |
| 255 | return function( newValue, forceNative ) |
| 256 | { |
| 257 | var isSetter = typeof newValue != 'undefined', |
| 258 | result; |
| 259 | |
| 260 | this.each( function() |
| 261 | { |
| 262 | var $this = jQuery( this ), |
| 263 | editor = $this.data( 'ckeditorInstance' ); |
| 264 | |
| 265 | if ( !forceNative && $this.is( 'textarea' ) && editor ) |
| 266 | { |
| 267 | if ( isSetter ) |
| 268 | editor.setData( newValue ); |
| 269 | else |
| 270 | { |
| 271 | result = editor.getData(); |
| 272 | // break; |
| 273 | return null; |
| 274 | } |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | if ( isSetter ) |
| 279 | oldValMethod.call( $this, newValue ); |
| 280 | else |
| 281 | { |
| 282 | result = oldValMethod.call( $this ); |
| 283 | // break; |
| 284 | return null; |
| 285 | } |
| 286 | } |
| 287 | }); |
| 288 | return isSetter ? this : result; |
| 289 | }; |
| 290 | }); |
| 291 | } |
| 292 | } |
| 293 | }); |
| 294 | })(); |
| 295 | |
| 296 | /** |
| 297 | * Allow CKEditor to override jQuery.fn.val(). This results in ability to use val() |
| 298 | * function on textareas as usual and having those calls synchronized with CKEditor |
| 299 | * Rich Text Editor component. |
| 300 | * |
| 301 | * This config option is global and executed during plugin load. |
| 302 | * Can't be customized across editor instances. |
| 303 | * |
| 304 | * @type Boolean |
| 305 | * @example |
| 306 | * $( 'textarea' ).ckeditor(); |
| 307 | * // ... |
| 308 | * $( 'textarea' ).val( 'New content' ); |
| 309 | */ |
| 310 | CKEDITOR.config.jqueryOverrideVal = true; |
| 311 | |
| 312 | /** |
| 313 | * Autoload jquery plugin if jQuery is available on page. |
| 314 | * |
| 315 | * This config option is global and executed during CKEditor load. |
| 316 | * Can't be customized across editor instances. |
| 317 | * |
| 318 | * @type Boolean |
| 319 | */ |
| 320 | CKEDITOR_autoLoad_jqueryPlugin = false; |
| 321 | |
| 322 | |
| 323 | // Finish autoloading after including source file. |
| 324 | if ( CKEDITOR_autoLoad_jqueryPlugin ) |
| 325 | CKEDITOR.plugins.load( 'jquery' ); |