jquery.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
  3. For licensing, see LICENSE.html or http://ckeditor.com/license
  4. */
  5. /**
  6. * @fileOverview jQuery adapter provides easy use of basic CKEditor functions
  7. * and access to internal API. It also integrates some aspects of CKEditor with
  8. * jQuery framework.
  9. *
  10. * Every TEXTAREA, DIV and P elements can be converted to working editor.
  11. *
  12. * Plugin exposes some of editor's event to jQuery event system. All of those are namespaces inside
  13. * ".ckeditor" namespace and can be binded/listened on supported textarea, div and p nodes.
  14. *
  15. * Available jQuery events:
  16. * - instanceReady.ckeditor( editor, rootNode )
  17. * Triggered when new instance is ready.
  18. * - destroy.ckeditor( editor )
  19. * Triggered when instance is destroyed.
  20. * - getData.ckeditor( editor, eventData )
  21. * Triggered when getData event is fired inside editor. It can change returned data using eventData reference.
  22. * - setData.ckeditor( editor )
  23. * Triggered when getData event is fired inside editor.
  24. *
  25. * @example
  26. * <script src="jquery.js"></script>
  27. * <script src="ckeditor.js"></script>
  28. * <script src="adapters/jquery/adapter.js"></script>
  29. */
  30. (function()
  31. {
  32. /**
  33. * Allows CKEditor to override jQuery.fn.val(), making it possible to use the val()
  34. * function on textareas, as usual, having it synchronized with CKEditor.<br>
  35. * <br>
  36. * This configuration option is global and executed during the jQuery Adapter loading.
  37. * It can't be customized across editor instances.
  38. * @type Boolean
  39. * @example
  40. * &lt;script&gt;
  41. * CKEDITOR.config.jqueryOverrideVal = true;
  42. * &lt;/script&gt;
  43. * &lt;!-- Important: The JQuery adapter is loaded *after* setting jqueryOverrideVal --&gt;
  44. * &lt;script src="/ckeditor/adapters/jquery.js"&gt;&lt;/script&gt;
  45. * @example
  46. * // ... then later in the code ...
  47. *
  48. * $( 'textarea' ).ckeditor();
  49. * // ...
  50. * $( 'textarea' ).val( 'New content' );
  51. */
  52. CKEDITOR.config.jqueryOverrideVal = typeof CKEDITOR.config.jqueryOverrideVal == 'undefined'
  53. ? true : CKEDITOR.config.jqueryOverrideVal;
  54. var jQuery = window.jQuery;
  55. if ( typeof jQuery == 'undefined' )
  56. return;
  57. // jQuery object methods.
  58. jQuery.extend( jQuery.fn,
  59. /** @lends jQuery.fn */
  60. {
  61. /**
  62. * Return existing CKEditor instance for first matched element.
  63. * Allows to easily use internal API. Doesn't return jQuery object.
  64. *
  65. * Raised exception if editor doesn't exist or isn't ready yet.
  66. *
  67. * @name jQuery.ckeditorGet
  68. * @return CKEDITOR.editor
  69. * @see CKEDITOR.editor
  70. */
  71. ckeditorGet: function()
  72. {
  73. var instance = this.eq( 0 ).data( 'ckeditorInstance' );
  74. if ( !instance )
  75. throw "CKEditor not yet initialized, use ckeditor() with callback.";
  76. return instance;
  77. },
  78. /**
  79. * Triggers creation of CKEditor in all matched elements (reduced to DIV, P and TEXTAREAs).
  80. * Binds callback to instanceReady event of all instances. If editor is already created, than
  81. * callback is fired right away.
  82. *
  83. * Mixed parameter order allowed.
  84. *
  85. * @param callback Function to be run on editor instance. Passed parameters: [ textarea ].
  86. * Callback is fiered in "this" scope being ckeditor instance and having source textarea as first param.
  87. *
  88. * @param config Configuration options for new instance(s) if not already created.
  89. * See URL
  90. *
  91. * @example
  92. * $( 'textarea' ).ckeditor( function( textarea ) {
  93. * $( textarea ).val( this.getData() )
  94. * } );
  95. *
  96. * @name jQuery.fn.ckeditor
  97. * @return jQuery.fn
  98. */
  99. ckeditor: function( callback, config )
  100. {
  101. if ( !CKEDITOR.env.isCompatible )
  102. return this;
  103. if ( !jQuery.isFunction( callback ))
  104. {
  105. var tmp = config;
  106. config = callback;
  107. callback = tmp;
  108. }
  109. config = config || {};
  110. this.filter( 'textarea, div, p' ).each( function()
  111. {
  112. var $element = jQuery( this ),
  113. editor = $element.data( 'ckeditorInstance' ),
  114. instanceLock = $element.data( '_ckeditorInstanceLock' ),
  115. element = this;
  116. if ( editor && !instanceLock )
  117. {
  118. if ( callback )
  119. callback.apply( editor, [ this ] );
  120. }
  121. else if ( !instanceLock )
  122. {
  123. // CREATE NEW INSTANCE
  124. // Handle config.autoUpdateElement inside this plugin if desired.
  125. if ( config.autoUpdateElement
  126. || ( typeof config.autoUpdateElement == 'undefined' && CKEDITOR.config.autoUpdateElement ) )
  127. {
  128. config.autoUpdateElementJquery = true;
  129. }
  130. // Always disable config.autoUpdateElement.
  131. config.autoUpdateElement = false;
  132. $element.data( '_ckeditorInstanceLock', true );
  133. // Set instance reference in element's data.
  134. editor = CKEDITOR.replace( element, config );
  135. $element.data( 'ckeditorInstance', editor );
  136. // Register callback.
  137. editor.on( 'instanceReady', function( event )
  138. {
  139. var editor = event.editor;
  140. setTimeout( function()
  141. {
  142. // Delay bit more if editor is still not ready.
  143. if ( !editor.element )
  144. {
  145. setTimeout( arguments.callee, 100 );
  146. return;
  147. }
  148. // Remove this listener.
  149. event.removeListener( 'instanceReady', this.callee );
  150. // Forward setData on dataReady.
  151. editor.on( 'dataReady', function()
  152. {
  153. $element.trigger( 'setData' + '.ckeditor', [ editor ] );
  154. });
  155. // Forward getData.
  156. editor.on( 'getData', function( event ) {
  157. $element.trigger( 'getData' + '.ckeditor', [ editor, event.data ] );
  158. }, 999 );
  159. // Forward destroy event.
  160. editor.on( 'destroy', function()
  161. {
  162. $element.trigger( 'destroy.ckeditor', [ editor ] );
  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. // Bind to submit event.
  175. $element.parents( 'form' ).submit( onSubmit );
  176. // Bind to form-pre-serialize from jQuery Forms plugin.
  177. $element.parents( 'form' ).bind( 'form-pre-serialize', onSubmit );
  178. // Unbind when editor destroyed.
  179. $element.bind( 'destroy.ckeditor', function()
  180. {
  181. $element.parents( 'form' ).unbind( 'submit', onSubmit );
  182. $element.parents( 'form' ).unbind( 'form-pre-serialize', onSubmit );
  183. });
  184. }
  185. // Garbage collect on destroy.
  186. editor.on( 'destroy', function()
  187. {
  188. $element.data( 'ckeditorInstance', null );
  189. });
  190. // Remove lock.
  191. $element.data( '_ckeditorInstanceLock', null );
  192. // Fire instanceReady event.
  193. $element.trigger( 'instanceReady.ckeditor', [ editor ] );
  194. // Run given (first) code.
  195. if ( callback )
  196. callback.apply( editor, [ element ] );
  197. }, 0 );
  198. }, null, null, 9999);
  199. }
  200. else
  201. {
  202. // Editor is already during creation process, bind our code to the event.
  203. CKEDITOR.on( 'instanceReady', function( event )
  204. {
  205. var editor = event.editor;
  206. setTimeout( function()
  207. {
  208. // Delay bit more if editor is still not ready.
  209. if ( !editor.element )
  210. {
  211. setTimeout( arguments.callee, 100 );
  212. return;
  213. }
  214. if ( editor.element.$ == element )
  215. {
  216. // Run given code.
  217. if ( callback )
  218. callback.apply( editor, [ element ] );
  219. }
  220. }, 0 );
  221. }, null, null, 9999);
  222. }
  223. });
  224. return this;
  225. }
  226. });
  227. // New val() method for objects.
  228. if ( CKEDITOR.config.jqueryOverrideVal )
  229. {
  230. jQuery.fn.val = CKEDITOR.tools.override( jQuery.fn.val, function( oldValMethod )
  231. {
  232. /**
  233. * CKEditor-aware val() method.
  234. *
  235. * Acts same as original jQuery val(), but for textareas which have CKEditor instances binded to them, method
  236. * returns editor's content. It also works for settings values.
  237. *
  238. * @param oldValMethod
  239. * @name jQuery.fn.val
  240. */
  241. return function( newValue, forceNative )
  242. {
  243. var isSetter = typeof newValue != 'undefined',
  244. result;
  245. this.each( function()
  246. {
  247. var $this = jQuery( this ),
  248. editor = $this.data( 'ckeditorInstance' );
  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. return true;
  272. });
  273. return isSetter ? this : result;
  274. };
  275. });
  276. }
  277. })();