plugin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. CKEDITOR.plugins.add( 'richcombo', {
  6. requires: 'floatpanel,listblock,button',
  7. beforeInit: function( editor ) {
  8. editor.ui.addHandler( CKEDITOR.UI_RICHCOMBO, CKEDITOR.ui.richCombo.handler );
  9. }
  10. } );
  11. ( function() {
  12. var template = '<span id="{id}"' +
  13. ' class="cke_combo cke_combo__{name} {cls}"' +
  14. ' role="presentation">' +
  15. '<span id="{id}_label" class="cke_combo_label">{label}</span>' +
  16. '<a class="cke_combo_button" title="{title}" tabindex="-1"' +
  17. ( CKEDITOR.env.gecko && !CKEDITOR.env.hc ? '' : ' href="javascript:void(\'{titleJs}\')"' ) +
  18. ' hidefocus="true"' +
  19. ' role="button"' +
  20. ' aria-labelledby="{id}_label"' +
  21. ' aria-haspopup="true"';
  22. // Some browsers don't cancel key events in the keydown but in the
  23. // keypress.
  24. // TODO: Check if really needed.
  25. if ( CKEDITOR.env.gecko && CKEDITOR.env.mac )
  26. template += ' onkeypress="return false;"';
  27. // With Firefox, we need to force the button to redraw, otherwise it
  28. // will remain in the focus state.
  29. if ( CKEDITOR.env.gecko )
  30. template += ' onblur="this.style.cssText = this.style.cssText;"';
  31. template +=
  32. ' onkeydown="return CKEDITOR.tools.callFunction({keydownFn},event,this);"' +
  33. ' onfocus="return CKEDITOR.tools.callFunction({focusFn},event);" ' +
  34. ( CKEDITOR.env.ie ? 'onclick="return false;" onmouseup' : 'onclick' ) + // #188
  35. '="CKEDITOR.tools.callFunction({clickFn},this);return false;">' +
  36. '<span id="{id}_text" class="cke_combo_text cke_combo_inlinelabel">{label}</span>' +
  37. '<span class="cke_combo_open">' +
  38. '<span class="cke_combo_arrow">' +
  39. // BLACK DOWN-POINTING TRIANGLE
  40. ( CKEDITOR.env.hc ? '&#9660;' : CKEDITOR.env.air ? '&nbsp;' : '' ) +
  41. '</span>' +
  42. '</span>' +
  43. '</a>' +
  44. '</span>';
  45. var rcomboTpl = CKEDITOR.addTemplate( 'combo', template );
  46. /**
  47. * Button UI element.
  48. *
  49. * @readonly
  50. * @property {String} [='richcombo']
  51. * @member CKEDITOR
  52. */
  53. CKEDITOR.UI_RICHCOMBO = 'richcombo';
  54. /**
  55. * @class
  56. * @todo
  57. */
  58. CKEDITOR.ui.richCombo = CKEDITOR.tools.createClass( {
  59. $: function( definition ) {
  60. // Copy all definition properties to this object.
  61. CKEDITOR.tools.extend( this, definition,
  62. // Set defaults.
  63. {
  64. // The combo won't participate in toolbar grouping.
  65. canGroup: false,
  66. title: definition.label,
  67. modes: { wysiwyg: 1 },
  68. editorFocus: 1
  69. } );
  70. // We don't want the panel definition in this object.
  71. var panelDefinition = this.panel || {};
  72. delete this.panel;
  73. this.id = CKEDITOR.tools.getNextNumber();
  74. this.document = ( panelDefinition.parent && panelDefinition.parent.getDocument() ) || CKEDITOR.document;
  75. panelDefinition.className = 'cke_combopanel';
  76. panelDefinition.block = {
  77. multiSelect: panelDefinition.multiSelect,
  78. attributes: panelDefinition.attributes
  79. };
  80. panelDefinition.toolbarRelated = true;
  81. this._ = {
  82. panelDefinition: panelDefinition,
  83. items: {}
  84. };
  85. },
  86. proto: {
  87. renderHtml: function( editor ) {
  88. var output = [];
  89. this.render( editor, output );
  90. return output.join( '' );
  91. },
  92. /**
  93. * Renders the combo.
  94. *
  95. * @param {CKEDITOR.editor} editor The editor instance which this button is
  96. * to be used by.
  97. * @param {Array} output The output array to which append the HTML relative
  98. * to this button.
  99. */
  100. render: function( editor, output ) {
  101. var env = CKEDITOR.env;
  102. var id = 'cke_' + this.id;
  103. var clickFn = CKEDITOR.tools.addFunction( function( el ) {
  104. // Restore locked selection in Opera.
  105. if ( selLocked ) {
  106. editor.unlockSelection( 1 );
  107. selLocked = 0;
  108. }
  109. instance.execute( el );
  110. }, this );
  111. var combo = this;
  112. var instance = {
  113. id: id,
  114. combo: this,
  115. focus: function() {
  116. var element = CKEDITOR.document.getById( id ).getChild( 1 );
  117. element.focus();
  118. },
  119. execute: function( el ) {
  120. var _ = combo._;
  121. if ( _.state == CKEDITOR.TRISTATE_DISABLED )
  122. return;
  123. combo.createPanel( editor );
  124. if ( _.on ) {
  125. _.panel.hide();
  126. return;
  127. }
  128. combo.commit();
  129. var value = combo.getValue();
  130. if ( value )
  131. _.list.mark( value );
  132. else
  133. _.list.unmarkAll();
  134. _.panel.showBlock( combo.id, new CKEDITOR.dom.element( el ), 4 );
  135. },
  136. clickFn: clickFn
  137. };
  138. function updateState() {
  139. // Don't change state while richcombo is active (#11793).
  140. if ( this.getState() == CKEDITOR.TRISTATE_ON )
  141. return;
  142. var state = this.modes[ editor.mode ] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;
  143. if ( editor.readOnly && !this.readOnly )
  144. state = CKEDITOR.TRISTATE_DISABLED;
  145. this.setState( state );
  146. this.setValue( '' );
  147. // Let plugin to disable button.
  148. if ( state != CKEDITOR.TRISTATE_DISABLED && this.refresh )
  149. this.refresh();
  150. }
  151. // Update status when activeFilter, mode, selection or readOnly changes.
  152. editor.on( 'activeFilterChange', updateState, this );
  153. editor.on( 'mode', updateState, this );
  154. editor.on( 'selectionChange', updateState, this );
  155. // If this combo is sensitive to readOnly state, update it accordingly.
  156. !this.readOnly && editor.on( 'readOnly', updateState, this );
  157. var keyDownFn = CKEDITOR.tools.addFunction( function( ev, element ) {
  158. ev = new CKEDITOR.dom.event( ev );
  159. var keystroke = ev.getKeystroke();
  160. // ARROW-DOWN
  161. // This call is duplicated in plugins/toolbar/plugin.js in itemKeystroke().
  162. // Move focus to the first element after drop down was opened by the arrow down key.
  163. if ( keystroke == 40 ) {
  164. editor.once( 'panelShow', function( evt ) {
  165. evt.data._.panel._.currentBlock.onKeyDown( 40 );
  166. } );
  167. }
  168. switch ( keystroke ) {
  169. case 13: // ENTER
  170. case 32: // SPACE
  171. case 40: // ARROW-DOWN
  172. // Show panel
  173. CKEDITOR.tools.callFunction( clickFn, element );
  174. break;
  175. default:
  176. // Delegate the default behavior to toolbar button key handling.
  177. instance.onkey( instance, keystroke );
  178. }
  179. // Avoid subsequent focus grab on editor document.
  180. ev.preventDefault();
  181. } );
  182. var focusFn = CKEDITOR.tools.addFunction( function() {
  183. instance.onfocus && instance.onfocus();
  184. } );
  185. var selLocked = 0;
  186. // For clean up
  187. instance.keyDownFn = keyDownFn;
  188. var params = {
  189. id: id,
  190. name: this.name || this.command,
  191. label: this.label,
  192. title: this.title,
  193. cls: this.className || '',
  194. titleJs: env.gecko && !env.hc ? '' : ( this.title || '' ).replace( "'", '' ),
  195. keydownFn: keyDownFn,
  196. focusFn: focusFn,
  197. clickFn: clickFn
  198. };
  199. rcomboTpl.output( params, output );
  200. if ( this.onRender )
  201. this.onRender();
  202. return instance;
  203. },
  204. createPanel: function( editor ) {
  205. if ( this._.panel )
  206. return;
  207. var panelDefinition = this._.panelDefinition,
  208. panelBlockDefinition = this._.panelDefinition.block,
  209. panelParentElement = panelDefinition.parent || CKEDITOR.document.getBody(),
  210. namedPanelCls = 'cke_combopanel__' + this.name,
  211. panel = new CKEDITOR.ui.floatPanel( editor, panelParentElement, panelDefinition ),
  212. list = panel.addListBlock( this.id, panelBlockDefinition ),
  213. me = this;
  214. panel.onShow = function() {
  215. this.element.addClass( namedPanelCls );
  216. me.setState( CKEDITOR.TRISTATE_ON );
  217. me._.on = 1;
  218. me.editorFocus && !editor.focusManager.hasFocus && editor.focus();
  219. if ( me.onOpen )
  220. me.onOpen();
  221. // The "panelShow" event is fired assinchronously, after the
  222. // onShow method call.
  223. editor.once( 'panelShow', function() {
  224. list.focus( !list.multiSelect && me.getValue() );
  225. } );
  226. };
  227. panel.onHide = function( preventOnClose ) {
  228. this.element.removeClass( namedPanelCls );
  229. me.setState( me.modes && me.modes[ editor.mode ] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );
  230. me._.on = 0;
  231. if ( !preventOnClose && me.onClose )
  232. me.onClose();
  233. };
  234. panel.onEscape = function() {
  235. // Hide drop-down with focus returned.
  236. panel.hide( 1 );
  237. };
  238. list.onClick = function( value, marked ) {
  239. if ( me.onClick )
  240. me.onClick.call( me, value, marked );
  241. panel.hide();
  242. };
  243. this._.panel = panel;
  244. this._.list = list;
  245. panel.getBlock( this.id ).onHide = function() {
  246. me._.on = 0;
  247. me.setState( CKEDITOR.TRISTATE_OFF );
  248. };
  249. if ( this.init )
  250. this.init();
  251. },
  252. setValue: function( value, text ) {
  253. this._.value = value;
  254. var textElement = this.document.getById( 'cke_' + this.id + '_text' );
  255. if ( textElement ) {
  256. if ( !( value || text ) ) {
  257. text = this.label;
  258. textElement.addClass( 'cke_combo_inlinelabel' );
  259. } else {
  260. textElement.removeClass( 'cke_combo_inlinelabel' );
  261. }
  262. textElement.setText( typeof text != 'undefined' ? text : value );
  263. }
  264. },
  265. getValue: function() {
  266. return this._.value || '';
  267. },
  268. unmarkAll: function() {
  269. this._.list.unmarkAll();
  270. },
  271. mark: function( value ) {
  272. this._.list.mark( value );
  273. },
  274. hideItem: function( value ) {
  275. this._.list.hideItem( value );
  276. },
  277. hideGroup: function( groupTitle ) {
  278. this._.list.hideGroup( groupTitle );
  279. },
  280. showAll: function() {
  281. this._.list.showAll();
  282. },
  283. add: function( value, html, text ) {
  284. this._.items[ value ] = text || value;
  285. this._.list.add( value, html, text );
  286. },
  287. startGroup: function( title ) {
  288. this._.list.startGroup( title );
  289. },
  290. commit: function() {
  291. if ( !this._.committed ) {
  292. this._.list.commit();
  293. this._.committed = 1;
  294. CKEDITOR.ui.fire( 'ready', this );
  295. }
  296. this._.committed = 1;
  297. },
  298. setState: function( state ) {
  299. if ( this._.state == state )
  300. return;
  301. var el = this.document.getById( 'cke_' + this.id );
  302. el.setState( state, 'cke_combo' );
  303. state == CKEDITOR.TRISTATE_DISABLED ?
  304. el.setAttribute( 'aria-disabled', true ) :
  305. el.removeAttribute( 'aria-disabled' );
  306. this._.state = state;
  307. },
  308. getState: function() {
  309. return this._.state;
  310. },
  311. enable: function() {
  312. if ( this._.state == CKEDITOR.TRISTATE_DISABLED )
  313. this.setState( this._.lastState );
  314. },
  315. disable: function() {
  316. if ( this._.state != CKEDITOR.TRISTATE_DISABLED ) {
  317. this._.lastState = this._.state;
  318. this.setState( CKEDITOR.TRISTATE_DISABLED );
  319. }
  320. }
  321. },
  322. /**
  323. * Represents richCombo handler object.
  324. *
  325. * @class CKEDITOR.ui.richCombo.handler
  326. * @singleton
  327. * @extends CKEDITOR.ui.handlerDefinition
  328. */
  329. statics: {
  330. handler: {
  331. /**
  332. * Transforms a richCombo definition in a {@link CKEDITOR.ui.richCombo} instance.
  333. *
  334. * @param {Object} definition
  335. * @returns {CKEDITOR.ui.richCombo}
  336. */
  337. create: function( definition ) {
  338. return new CKEDITOR.ui.richCombo( definition );
  339. }
  340. }
  341. }
  342. } );
  343. /**
  344. * @param {String} name
  345. * @param {Object} definition
  346. * @member CKEDITOR.ui
  347. * @todo
  348. */
  349. CKEDITOR.ui.prototype.addRichCombo = function( name, definition ) {
  350. this.add( name, CKEDITOR.UI_RICHCOMBO, definition );
  351. };
  352. } )();