Nation Notes module contributed by Z&H Healthcare.
[openemr.git] / library / custom_template / ckeditor / _source / adapters / jquery.js
blob72bdb2246511da6b6d925e6d17bc999b735f4817
1 /*
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
6 /**
7  * @fileOverview jQuery adapter 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  * Every TEXTAREA, DIV and P elements can be converted to working editor.
12  *
13  * Plugin exposes some of editor's event to jQuery event system. All of those are namespaces inside
14  * ".ckeditor" namespace and can be binded/listened on supported textarea, div and p nodes.
15  *
16  * Available jQuery events:
17  * - instanceReady.ckeditor( editor, rootNode )
18  *   Triggered when new instance is ready.
19  * - destroy.ckeditor( editor )
20  *   Triggered when instance is destroyed.
21  * - getData.ckeditor( editor, eventData )
22  *   Triggered when getData event is fired inside editor. It can change returned data using eventData reference.
23  * - setData.ckeditor( editor )
24  *   Triggered when getData event is fired inside editor.
25  *
26  * @example
27  * <script src="jquery.js"></script>
28  * <script src="ckeditor.js"></script>
29  * <script src="adapters/jquery/adapter.js"></script>
30  */
32 (function()
34         /**
35          * Allow CKEditor to override jQuery.fn.val(). This results in ability to use val()
36          * function on textareas as usual and having those calls synchronized with CKEditor
37          * Rich Text Editor component.
38          *
39          * This config option is global and executed during plugin load.
40          * Can't be customized across editor instances.
41          *
42          * @type Boolean
43          * @example
44          * $( 'textarea' ).ckeditor();
45          * // ...
46          * $( 'textarea' ).val( 'New content' );
47          */
48         CKEDITOR.config.jqueryOverrideVal = typeof CKEDITOR.config.jqueryOverrideVal == 'undefined'
49                 ? true : CKEDITOR.config.jqueryOverrideVal;
51         var jQuery = window.jQuery;
53         if ( typeof jQuery == 'undefined' )
54                 return;
56         // jQuery object methods.
57         jQuery.extend( jQuery.fn,
58         /** @lends jQuery.fn */
59         {
60                 /**
61                  * Return existing CKEditor instance for first matched element.
62                  * Allows to easily use internal API. Doesn't return jQuery object.
63                  *
64                  * Raised exception if editor doesn't exist or isn't ready yet.
65                  *
66                  * @name jQuery.ckeditorGet
67                  * @return CKEDITOR.editor
68                  * @see CKEDITOR.editor
69                  */
70                 ckeditorGet: function()
71                 {
72                         var instance = this.eq( 0 ).data( 'ckeditorInstance' );
73                         if ( !instance )
74                                 throw "CKEditor not yet initialized, use ckeditor() with callback.";
75                         return instance;
76                 },
77                 /**
78                  * Triggers creation of CKEditor in all matched elements (reduced to DIV, P and TEXTAREAs).
79                  * Binds callback to instanceReady event of all instances. If editor is already created, than
80                  * callback is fired right away.
81                  *
82                  * Mixed parameter order allowed.
83                  *
84                  * @param callback Function to be run on editor instance. Passed parameters: [ textarea ].
85                  * Callback is fiered in "this" scope being ckeditor instance and having source textarea as first param.
86                  *
87                  * @param config Configuration options for new instance(s) if not already created.
88                  * See URL
89                  *
90                  * @example
91                  * $( 'textarea' ).ckeditor( function( textarea ) {
92                  *   $( textarea ).val( this.getData() )
93                  * } );
94                  *
95                  * @name jQuery.fn.ckeditor
96                  * @return jQuery.fn
97                  */
98                 ckeditor: function( callback, config )
99                 {
100                         if ( !CKEDITOR.env.isCompatible )
101                                 return this;
103                         if ( !jQuery.isFunction( callback ))
104                         {
105                                 var tmp = config;
106                                 config = callback;
107                                 callback = tmp;
108                         }
109                         config = config || {};
111                         this.filter( 'textarea, div, p' ).each( function()
112                         {
113                                 var $element = jQuery( this ),
114                                         editor = $element.data( 'ckeditorInstance' ),
115                                         instanceLock = $element.data( '_ckeditorInstanceLock' ),
116                                         element = this;
118                                 if ( editor && !instanceLock )
119                                 {
120                                         if ( callback )
121                                                 callback.apply( editor, [ this ] );
122                                 }
123                                 else if ( !instanceLock )
124                                 {
125                                         // CREATE NEW INSTANCE
127                                         // Handle config.autoUpdateElement inside this plugin if desired.
128                                         if ( config.autoUpdateElement
129                                                 || ( typeof config.autoUpdateElement == 'undefined' && CKEDITOR.config.autoUpdateElement ) )
130                                         {
131                                                 config.autoUpdateElementJquery = true;
132                                         }
134                                         // Always disable config.autoUpdateElement.
135                                         config.autoUpdateElement = false;
136                                         $element.data( '_ckeditorInstanceLock', true );
138                                         // Set instance reference in element's data.
139                                         editor = CKEDITOR.replace( element, config );
140                                         $element.data( 'ckeditorInstance', editor );
142                                         // Register callback.
143                                         editor.on( 'instanceReady', function( event )
144                                         {
145                                                 var editor = event.editor;
146                                                 setTimeout( function()
147                                                 {
148                                                         // Delay bit more if editor is still not ready.
149                                                         if ( !editor.element )
150                                                         {
151                                                                 setTimeout( arguments.callee, 100 );
152                                                                 return;
153                                                         }
155                                                         // Remove this listener.
156                                                         event.removeListener( 'instanceReady', this.callee );
158                                                         // Forward setData on dataReady.
159                                                         editor.on( 'dataReady', function()
160                                                         {
161                                                                 $element.trigger( 'setData' + '.ckeditor', [ editor ] );
162                                                         });
164                                                         // Forward getData.
165                                                         editor.on( 'getData', function( event ) {
166                                                                 $element.trigger( 'getData' + '.ckeditor', [ editor, event.data ] );
167                                                         }, 999 );
169                                                         // Forward destroy event.
170                                                         editor.on( 'destroy', function()
171                                                         {
172                                                                 $element.trigger( 'destroy.ckeditor', [ editor ] );
173                                                         });
175                                                         // Integrate with form submit.
176                                                         if ( editor.config.autoUpdateElementJquery && $element.is( 'textarea' ) && $element.parents( 'form' ).length )
177                                                         {
178                                                                 var onSubmit = function()
179                                                                 {
180                                                                         $element.ckeditor( function()
181                                                                         {
182                                                                                 editor.updateElement();
183                                                                         });
184                                                                 };
186                                                                 // Bind to submit event.
187                                                                 $element.parents( 'form' ).submit( onSubmit );
189                                                                 // Bind to form-pre-serialize from jQuery Forms plugin.
190                                                                 $element.parents( 'form' ).bind( 'form-pre-serialize', onSubmit );
192                                                                 // Unbind when editor destroyed.
193                                                                 $element.bind( 'destroy.ckeditor', function()
194                                                                 {
195                                                                         $element.parents( 'form' ).unbind( 'submit', onSubmit );
196                                                                         $element.parents( 'form' ).unbind( 'form-pre-serialize', onSubmit );
197                                                                 });
198                                                         }
200                                                         // Garbage collect on destroy.
201                                                         editor.on( 'destroy', function()
202                                                         {
203                                                                 $element.data( 'ckeditorInstance', null );
204                                                         });
206                                                         // Remove lock.
207                                                         $element.data( '_ckeditorInstanceLock', null );
209                                                         // Fire instanceReady event.
210                                                         $element.trigger( 'instanceReady.ckeditor', [ editor ] );
212                                                         // Run given (first) code.
213                                                         if ( callback )
214                                                                 callback.apply( editor, [ element ] );
215                                                 }, 0 );
216                                         }, null, null, 9999);
217                                 }
218                                 else
219                                 {
220                                         // Editor is already during creation process, bind our code to the event.
221                                         CKEDITOR.on( 'instanceReady', function( event )
222                                         {
223                                                 var editor = event.editor;
224                                                 setTimeout( function()
225                                                 {
226                                                         // Delay bit more if editor is still not ready.
227                                                         if ( !editor.element )
228                                                         {
229                                                                 setTimeout( arguments.callee, 100 );
230                                                                 return;
231                                                         }
233                                                         if ( editor.element.$ == element )
234                                                         {
235                                                                 // Run given code.
236                                                                 if ( callback )
237                                                                         callback.apply( editor, [ element ] );
238                                                         }
239                                                 }, 0 );
240                                         }, null, null, 9999);
241                                 }
242                         });
243                         return this;
244                 }
245         });
247         // New val() method for objects.
248         if ( CKEDITOR.config.jqueryOverrideVal )
249         {
250                 jQuery.fn.val = CKEDITOR.tools.override( jQuery.fn.val, function( oldValMethod )
251                 {
252                         /**
253                          * CKEditor-aware val() method.
254                          *
255                          * Acts same as original jQuery val(), but for textareas which have CKEditor instances binded to them, method
256                          * returns editor's content. It also works for settings values.
257                          *
258                          * @param oldValMethod
259                          * @name jQuery.fn.val
260                          */
261                         return function( newValue, forceNative )
262                         {
263                                 var isSetter = typeof newValue != 'undefined',
264                                         result;
266                                 this.each( function()
267                                 {
268                                         var $this = jQuery( this ),
269                                                 editor = $this.data( 'ckeditorInstance' );
271                                         if ( !forceNative && $this.is( 'textarea' ) && editor )
272                                         {
273                                                 if ( isSetter )
274                                                         editor.setData( newValue );
275                                                 else
276                                                 {
277                                                         result = editor.getData();
278                                                         // break;
279                                                         return null;
280                                                 }
281                                         }
282                                         else
283                                         {
284                                                 if ( isSetter )
285                                                         oldValMethod.call( $this, newValue );
286                                                 else
287                                                 {
288                                                         result = oldValMethod.call( $this );
289                                                         // break;
290                                                         return null;
291                                                 }
292                                         }
294                                         return true;
295                                 });
296                                 return isSetter ? this : result;
297                         };
298                 });
299         }
300 })();