plugin.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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( 'menu', {
  6. requires: 'floatpanel',
  7. beforeInit: function( editor ) {
  8. var groups = editor.config.menu_groups.split( ',' ),
  9. groupsOrder = editor._.menuGroups = {},
  10. menuItems = editor._.menuItems = {};
  11. for ( var i = 0; i < groups.length; i++ )
  12. groupsOrder[ groups[ i ] ] = i + 1;
  13. /**
  14. * Registers an item group to the editor context menu in order to make it
  15. * possible to associate it with menu items later.
  16. *
  17. * @param {String} name Specify a group name.
  18. * @param {Number} [order=100] Define the display sequence of this group
  19. * inside the menu. A smaller value gets displayed first.
  20. * @member CKEDITOR.editor
  21. */
  22. editor.addMenuGroup = function( name, order ) {
  23. groupsOrder[ name ] = order || 100;
  24. };
  25. /**
  26. * Adds an item from the specified definition to the editor context menu.
  27. *
  28. * @method
  29. * @param {String} name The menu item name.
  30. * @param {Object} definition The menu item definition.
  31. * @member CKEDITOR.editor
  32. */
  33. editor.addMenuItem = function( name, definition ) {
  34. if ( groupsOrder[ definition.group ] )
  35. menuItems[ name ] = new CKEDITOR.menuItem( this, name, definition );
  36. };
  37. /**
  38. * Adds one or more items from the specified definition array to the editor context menu.
  39. *
  40. * @method
  41. * @param {Array} definitions List of definitions for each menu item as if {@link #addMenuItem} is called.
  42. * @member CKEDITOR.editor
  43. */
  44. editor.addMenuItems = function( definitions ) {
  45. for ( var itemName in definitions ) {
  46. this.addMenuItem( itemName, definitions[ itemName ] );
  47. }
  48. };
  49. /**
  50. * Retrieves a particular menu item definition from the editor context menu.
  51. *
  52. * @method
  53. * @param {String} name The name of the desired menu item.
  54. * @returns {Object}
  55. * @member CKEDITOR.editor
  56. */
  57. editor.getMenuItem = function( name ) {
  58. return menuItems[ name ];
  59. };
  60. /**
  61. * Removes a particular menu item added before from the editor context menu.
  62. *
  63. * @since 3.6.1
  64. * @method
  65. * @param {String} name The name of the desired menu item.
  66. * @member CKEDITOR.editor
  67. */
  68. editor.removeMenuItem = function( name ) {
  69. delete menuItems[ name ];
  70. };
  71. }
  72. } );
  73. ( function() {
  74. var menuItemSource = '<span class="cke_menuitem">' +
  75. '<a id="{id}"' +
  76. ' class="cke_menubutton cke_menubutton__{name} cke_menubutton_{state} {cls}" href="{href}"' +
  77. ' title="{title}"' +
  78. ' tabindex="-1"' +
  79. '_cke_focus=1' +
  80. ' hidefocus="true"' +
  81. ' role="{role}"' +
  82. ' aria-haspopup="{hasPopup}"' +
  83. ' aria-disabled="{disabled}"' +
  84. ' {ariaChecked}';
  85. // Some browsers don't cancel key events in the keydown but in the
  86. // keypress.
  87. // TODO: Check if really needed.
  88. if ( CKEDITOR.env.gecko && CKEDITOR.env.mac )
  89. menuItemSource += ' onkeypress="return false;"';
  90. // With Firefox, we need to force the button to redraw, otherwise it
  91. // will remain in the focus state.
  92. if ( CKEDITOR.env.gecko )
  93. menuItemSource += ' onblur="this.style.cssText = this.style.cssText;"';
  94. // #188
  95. menuItemSource += ' onmouseover="CKEDITOR.tools.callFunction({hoverFn},{index});"' +
  96. ' onmouseout="CKEDITOR.tools.callFunction({moveOutFn},{index});" ' +
  97. ( CKEDITOR.env.ie ? 'onclick="return false;" onmouseup' : 'onclick' ) +
  98. '="CKEDITOR.tools.callFunction({clickFn},{index}); return false;"' +
  99. '>';
  100. menuItemSource +=
  101. '<span class="cke_menubutton_inner">' +
  102. '<span class="cke_menubutton_icon">' +
  103. '<span class="cke_button_icon cke_button__{iconName}_icon" style="{iconStyle}"></span>' +
  104. '</span>' +
  105. '<span class="cke_menubutton_label">' +
  106. '{label}' +
  107. '</span>' +
  108. '{arrowHtml}' +
  109. '</span>' +
  110. '</a></span>';
  111. var menuArrowSource = '<span class="cke_menuarrow">' +
  112. '<span>{label}</span>' +
  113. '</span>';
  114. var menuItemTpl = CKEDITOR.addTemplate( 'menuItem', menuItemSource ),
  115. menuArrowTpl = CKEDITOR.addTemplate( 'menuArrow', menuArrowSource );
  116. /**
  117. * @class
  118. * @todo
  119. */
  120. CKEDITOR.menu = CKEDITOR.tools.createClass( {
  121. /**
  122. * @constructor
  123. */
  124. $: function( editor, definition ) {
  125. definition = this._.definition = definition || {};
  126. this.id = CKEDITOR.tools.getNextId();
  127. this.editor = editor;
  128. this.items = [];
  129. this._.listeners = [];
  130. this._.level = definition.level || 1;
  131. var panelDefinition = CKEDITOR.tools.extend( {}, definition.panel, {
  132. css: [ CKEDITOR.skin.getPath( 'editor' ) ],
  133. level: this._.level - 1,
  134. block: {}
  135. } );
  136. var attrs = panelDefinition.block.attributes = ( panelDefinition.attributes || {} );
  137. // Provide default role of 'menu'.
  138. !attrs.role && ( attrs.role = 'menu' );
  139. this._.panelDefinition = panelDefinition;
  140. },
  141. _: {
  142. onShow: function() {
  143. var selection = this.editor.getSelection(),
  144. start = selection && selection.getStartElement(),
  145. path = this.editor.elementPath(),
  146. listeners = this._.listeners;
  147. this.removeAll();
  148. // Call all listeners, filling the list of items to be displayed.
  149. for ( var i = 0; i < listeners.length; i++ ) {
  150. var listenerItems = listeners[ i ]( start, selection, path );
  151. if ( listenerItems ) {
  152. for ( var itemName in listenerItems ) {
  153. var item = this.editor.getMenuItem( itemName );
  154. if ( item && ( !item.command || this.editor.getCommand( item.command ).state ) ) {
  155. item.state = listenerItems[ itemName ];
  156. this.add( item );
  157. }
  158. }
  159. }
  160. }
  161. },
  162. onClick: function( item ) {
  163. this.hide();
  164. if ( item.onClick )
  165. item.onClick();
  166. else if ( item.command )
  167. this.editor.execCommand( item.command );
  168. },
  169. onEscape: function( keystroke ) {
  170. var parent = this.parent;
  171. // 1. If it's sub-menu, close it, with focus restored on this.
  172. // 2. In case of a top-menu, close it, with focus returned to page.
  173. if ( parent )
  174. parent._.panel.hideChild( 1 );
  175. else if ( keystroke == 27 )
  176. this.hide( 1 );
  177. return false;
  178. },
  179. onHide: function() {
  180. this.onHide && this.onHide();
  181. },
  182. showSubMenu: function( index ) {
  183. var menu = this._.subMenu,
  184. item = this.items[ index ],
  185. subItemDefs = item.getItems && item.getItems();
  186. // If this item has no subitems, we just hide the submenu, if
  187. // available, and return back.
  188. if ( !subItemDefs ) {
  189. // Hide sub menu with focus returned.
  190. this._.panel.hideChild( 1 );
  191. return;
  192. }
  193. // Create the submenu, if not available, or clean the existing
  194. // one.
  195. if ( menu )
  196. menu.removeAll();
  197. else {
  198. menu = this._.subMenu = new CKEDITOR.menu( this.editor, CKEDITOR.tools.extend( {}, this._.definition, { level: this._.level + 1 }, true ) );
  199. menu.parent = this;
  200. menu._.onClick = CKEDITOR.tools.bind( this._.onClick, this );
  201. }
  202. // Add all submenu items to the menu.
  203. for ( var subItemName in subItemDefs ) {
  204. var subItem = this.editor.getMenuItem( subItemName );
  205. if ( subItem ) {
  206. subItem.state = subItemDefs[ subItemName ];
  207. menu.add( subItem );
  208. }
  209. }
  210. // Get the element representing the current item.
  211. var element = this._.panel.getBlock( this.id ).element.getDocument().getById( this.id + String( index ) );
  212. // Show the submenu.
  213. // This timeout is needed to give time for the sub-menu get
  214. // focus when JAWS is running. (#9844)
  215. setTimeout( function() {
  216. menu.show( element, 2 );
  217. }, 0 );
  218. }
  219. },
  220. proto: {
  221. /**
  222. * Adds an item.
  223. *
  224. * @param item
  225. */
  226. add: function( item ) {
  227. // Later we may sort the items, but Array#sort is not stable in
  228. // some browsers, here we're forcing the original sequence with
  229. // 'order' attribute if it hasn't been assigned. (#3868)
  230. if ( !item.order )
  231. item.order = this.items.length;
  232. this.items.push( item );
  233. },
  234. /**
  235. * Removes all items.
  236. */
  237. removeAll: function() {
  238. this.items = [];
  239. },
  240. /**
  241. * Shows the menu in given location.
  242. *
  243. * @param {CKEDITOR.dom.element} offsetParent
  244. * @param {Number} [corner]
  245. * @param {Number} [offsetX]
  246. * @param {Number} [offsetY]
  247. */
  248. show: function( offsetParent, corner, offsetX, offsetY ) {
  249. // Not for sub menu.
  250. if ( !this.parent ) {
  251. this._.onShow();
  252. // Don't menu with zero items.
  253. if ( !this.items.length )
  254. return;
  255. }
  256. corner = corner || ( this.editor.lang.dir == 'rtl' ? 2 : 1 );
  257. var items = this.items,
  258. editor = this.editor,
  259. panel = this._.panel,
  260. element = this._.element;
  261. // Create the floating panel for this menu.
  262. if ( !panel ) {
  263. panel = this._.panel = new CKEDITOR.ui.floatPanel( this.editor, CKEDITOR.document.getBody(), this._.panelDefinition, this._.level );
  264. panel.onEscape = CKEDITOR.tools.bind( function( keystroke ) {
  265. if ( this._.onEscape( keystroke ) === false )
  266. return false;
  267. }, this );
  268. panel.onShow = function() {
  269. // Menu need CSS resets, compensate class name.
  270. var holder = panel._.panel.getHolderElement();
  271. holder.getParent().addClass( 'cke' ).addClass( 'cke_reset_all' );
  272. };
  273. panel.onHide = CKEDITOR.tools.bind( function() {
  274. this._.onHide && this._.onHide();
  275. }, this );
  276. // Create an autosize block inside the panel.
  277. var block = panel.addBlock( this.id, this._.panelDefinition.block );
  278. block.autoSize = true;
  279. var keys = block.keys;
  280. keys[ 40 ] = 'next'; // ARROW-DOWN
  281. keys[ 9 ] = 'next'; // TAB
  282. keys[ 38 ] = 'prev'; // ARROW-UP
  283. keys[ CKEDITOR.SHIFT + 9 ] = 'prev'; // SHIFT + TAB
  284. keys[ ( editor.lang.dir == 'rtl' ? 37 : 39 ) ] = CKEDITOR.env.ie ? 'mouseup' : 'click'; // ARROW-RIGHT/ARROW-LEFT(rtl)
  285. keys[ 32 ] = CKEDITOR.env.ie ? 'mouseup' : 'click'; // SPACE
  286. CKEDITOR.env.ie && ( keys[ 13 ] = 'mouseup' ); // Manage ENTER, since onclick is blocked in IE (#8041).
  287. element = this._.element = block.element;
  288. var elementDoc = element.getDocument();
  289. elementDoc.getBody().setStyle( 'overflow', 'hidden' );
  290. elementDoc.getElementsByTag( 'html' ).getItem( 0 ).setStyle( 'overflow', 'hidden' );
  291. this._.itemOverFn = CKEDITOR.tools.addFunction( function( index ) {
  292. clearTimeout( this._.showSubTimeout );
  293. this._.showSubTimeout = CKEDITOR.tools.setTimeout( this._.showSubMenu, editor.config.menu_subMenuDelay || 400, this, [ index ] );
  294. }, this );
  295. this._.itemOutFn = CKEDITOR.tools.addFunction( function() {
  296. clearTimeout( this._.showSubTimeout );
  297. }, this );
  298. this._.itemClickFn = CKEDITOR.tools.addFunction( function( index ) {
  299. var item = this.items[ index ];
  300. if ( item.state == CKEDITOR.TRISTATE_DISABLED ) {
  301. this.hide( 1 );
  302. return;
  303. }
  304. if ( item.getItems )
  305. this._.showSubMenu( index );
  306. else
  307. this._.onClick( item );
  308. }, this );
  309. }
  310. // Put the items in the right order.
  311. sortItems( items );
  312. // Apply the editor mixed direction status to menu.
  313. var path = editor.elementPath(),
  314. mixedDirCls = ( path && path.direction() != editor.lang.dir ) ? ' cke_mixed_dir_content' : '';
  315. // Build the HTML that composes the menu and its items.
  316. var output = [ '<div class="cke_menu' + mixedDirCls + '" role="presentation">' ];
  317. var length = items.length,
  318. lastGroup = length && items[ 0 ].group;
  319. for ( var i = 0; i < length; i++ ) {
  320. var item = items[ i ];
  321. if ( lastGroup != item.group ) {
  322. output.push( '<div class="cke_menuseparator" role="separator"></div>' );
  323. lastGroup = item.group;
  324. }
  325. item.render( this, i, output );
  326. }
  327. output.push( '</div>' );
  328. // Inject the HTML inside the panel.
  329. element.setHtml( output.join( '' ) );
  330. CKEDITOR.ui.fire( 'ready', this );
  331. // Show the panel.
  332. if ( this.parent )
  333. this.parent._.panel.showAsChild( panel, this.id, offsetParent, corner, offsetX, offsetY );
  334. else
  335. panel.showBlock( this.id, offsetParent, corner, offsetX, offsetY );
  336. editor.fire( 'menuShow', [ panel ] );
  337. },
  338. /**
  339. * Adds a callback executed on opening the menu. Items
  340. * returned by that callback are added to the menu.
  341. *
  342. * @param {Function} listenerFn
  343. * @param {CKEDITOR.dom.element} listenerFn.startElement The selection start anchor element.
  344. * @param {CKEDITOR.dom.selection} listenerFn.selection The current selection.
  345. * @param {CKEDITOR.dom.elementPath} listenerFn.path The current elements path.
  346. * @param listenerFn.return Object (`commandName` => `state`) of items that should be added to the menu.
  347. */
  348. addListener: function( listenerFn ) {
  349. this._.listeners.push( listenerFn );
  350. },
  351. /**
  352. * Hides the menu.
  353. *
  354. * @param {Boolean} [returnFocus]
  355. */
  356. hide: function( returnFocus ) {
  357. this._.onHide && this._.onHide();
  358. this._.panel && this._.panel.hide( returnFocus );
  359. }
  360. }
  361. } );
  362. function sortItems( items ) {
  363. items.sort( function( itemA, itemB ) {
  364. if ( itemA.group < itemB.group )
  365. return -1;
  366. else if ( itemA.group > itemB.group )
  367. return 1;
  368. return itemA.order < itemB.order ? -1 : itemA.order > itemB.order ? 1 : 0;
  369. } );
  370. }
  371. /**
  372. * @class
  373. * @todo
  374. */
  375. CKEDITOR.menuItem = CKEDITOR.tools.createClass( {
  376. $: function( editor, name, definition ) {
  377. CKEDITOR.tools.extend( this, definition,
  378. // Defaults
  379. {
  380. order: 0,
  381. className: 'cke_menubutton__' + name
  382. } );
  383. // Transform the group name into its order number.
  384. this.group = editor._.menuGroups[ this.group ];
  385. this.editor = editor;
  386. this.name = name;
  387. },
  388. proto: {
  389. render: function( menu, index, output ) {
  390. var id = menu.id + String( index ),
  391. state = ( typeof this.state == 'undefined' ) ? CKEDITOR.TRISTATE_OFF : this.state,
  392. ariaChecked = '';
  393. var stateName = state == CKEDITOR.TRISTATE_ON ? 'on' : state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' : 'off';
  394. if ( this.role in { menuitemcheckbox: 1, menuitemradio: 1 } )
  395. ariaChecked = ' aria-checked="' + ( state == CKEDITOR.TRISTATE_ON ? 'true' : 'false' ) + '"';
  396. var hasSubMenu = this.getItems;
  397. // ltr: BLACK LEFT-POINTING POINTER
  398. // rtl: BLACK RIGHT-POINTING POINTER
  399. var arrowLabel = '&#' + ( this.editor.lang.dir == 'rtl' ? '9668' : '9658' ) + ';';
  400. var iconName = this.name;
  401. if ( this.icon && !( /\./ ).test( this.icon ) )
  402. iconName = this.icon;
  403. var params = {
  404. id: id,
  405. name: this.name,
  406. iconName: iconName,
  407. label: this.label,
  408. cls: this.className || '',
  409. state: stateName,
  410. hasPopup: hasSubMenu ? 'true' : 'false',
  411. disabled: state == CKEDITOR.TRISTATE_DISABLED,
  412. title: this.label,
  413. href: 'javascript:void(\'' + ( this.label || '' ).replace( "'" + '' ) + '\')', // jshint ignore:line
  414. hoverFn: menu._.itemOverFn,
  415. moveOutFn: menu._.itemOutFn,
  416. clickFn: menu._.itemClickFn,
  417. index: index,
  418. iconStyle: CKEDITOR.skin.getIconStyle( iconName, ( this.editor.lang.dir == 'rtl' ), iconName == this.icon ? null : this.icon, this.iconOffset ),
  419. arrowHtml: hasSubMenu ? menuArrowTpl.output( { label: arrowLabel } ) : '',
  420. role: this.role ? this.role : 'menuitem',
  421. ariaChecked: ariaChecked
  422. };
  423. menuItemTpl.output( params, output );
  424. }
  425. }
  426. } );
  427. } )();
  428. /**
  429. * The amount of time, in milliseconds, the editor waits before displaying submenu
  430. * options when moving the mouse over options that contain submenus, like the
  431. * "Cell Properties" entry for tables.
  432. *
  433. * // Remove the submenu delay.
  434. * config.menu_subMenuDelay = 0;
  435. *
  436. * @cfg {Number} [menu_subMenuDelay=400]
  437. * @member CKEDITOR.config
  438. */
  439. /**
  440. * Fired when a menu is shown.
  441. *
  442. * @event menuShow
  443. * @member CKEDITOR.editor
  444. * @param {CKEDITOR.editor} editor This editor instance.
  445. * @param {CKEDITOR.ui.panel[]} data
  446. */
  447. /**
  448. * A comma separated list of items group names to be displayed in the context
  449. * menu. The order of items will reflect the order specified in this list if
  450. * no priority was defined in the groups.
  451. *
  452. * config.menu_groups = 'clipboard,table,anchor,link,image';
  453. *
  454. * @cfg {String} [menu_groups=see source]
  455. * @member CKEDITOR.config
  456. */
  457. CKEDITOR.config.menu_groups = 'clipboard,' +
  458. 'form,' +
  459. 'tablecell,tablecellproperties,tablerow,tablecolumn,table,' +
  460. 'anchor,link,image,flash,' +
  461. 'checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea,div';