dropdown.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window */
  6. import Model from '../../../src/model';
  7. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  8. import testUtils from '../../_utils/utils';
  9. import alignLeftIcon from '@ckeditor/ckeditor5-core/theme/icons/object-left.svg';
  10. import alignRightIcon from '@ckeditor/ckeditor5-core/theme/icons/object-right.svg';
  11. import alignCenterIcon from '@ckeditor/ckeditor5-core/theme/icons/object-center.svg';
  12. import ButtonView from '../../../src/button/buttonview';
  13. import SplitButtonView from '../../../src/dropdown/button/splitbuttonview';
  14. import { createDropdown, addToolbarToDropdown, addListToDropdown } from '../../../src/dropdown/utils';
  15. const ui = testUtils.createTestUIView( {
  16. dropdown: '#dropdown',
  17. listDropdown: '#list-dropdown',
  18. dropdownLabel: '#dropdown-label',
  19. toolbarDropdown: '#dropdown-toolbar',
  20. splitButton: '#dropdown-splitbutton'
  21. } );
  22. function testEmpty() {
  23. const dropdownView = createDropdown( {} );
  24. dropdownView.buttonView.set( {
  25. label: 'Dropdown',
  26. isEnabled: true,
  27. isOn: false,
  28. withText: true
  29. } );
  30. ui.dropdown.add( dropdownView );
  31. dropdownView.panelView.element.innerHTML = 'Empty panel. There is no child view in this DropdownPanelView.';
  32. }
  33. function testList() {
  34. const collection = new Collection( { idProperty: 'label' } );
  35. [ '0.8em', '1em', '1.2em', '1.5em', '2.0em', '3.0em' ].forEach( font => {
  36. collection.add( new Model( {
  37. label: font,
  38. style: `font-size: ${ font }`
  39. } ) );
  40. } );
  41. const dropdownView = createDropdown( {} );
  42. dropdownView.buttonView.set( {
  43. label: 'ListDropdown',
  44. isEnabled: true,
  45. isOn: false,
  46. withText: true
  47. } );
  48. addListToDropdown( dropdownView, collection );
  49. dropdownView.on( 'execute', evt => {
  50. /* global console */
  51. console.log( 'List#execute:', evt.source.label );
  52. } );
  53. ui.listDropdown.add( dropdownView );
  54. window.listDropdownCollection = collection;
  55. window.Model = Model;
  56. }
  57. function testLongLabel() {
  58. const dropdownView = createDropdown( {} );
  59. dropdownView.buttonView.set( {
  60. label: 'Dropdown with a very long label',
  61. isEnabled: true,
  62. isOn: false,
  63. withText: true
  64. } );
  65. ui.dropdownLabel.add( dropdownView );
  66. dropdownView.panelView.element.innerHTML = 'Empty panel. There is no child view in this DropdownPanelView.';
  67. }
  68. function testToolbar() {
  69. const locale = {};
  70. const icons = { left: alignLeftIcon, right: alignRightIcon, center: alignCenterIcon };
  71. // Buttons to be obtained from factory later on.
  72. const buttons = Object.keys( icons ).map( icon => new Model( { label: icon, isEnabled: true, isOn: false, icon: icons[ icon ] } ) );
  73. const buttonViews = buttons
  74. .map( buttonModel => {
  75. const buttonView = new ButtonView( locale );
  76. buttonView.bind( 'isEnabled', 'isOn', 'icon', 'label' ).to( buttonModel );
  77. buttonView.on( 'execute', () => console.log( `Execute: ${ buttonModel.label }` ) );
  78. return buttonView;
  79. } );
  80. const toolbarDropdown = createDropdown( locale );
  81. toolbarDropdown.set( 'isVertical', true );
  82. addToolbarToDropdown( toolbarDropdown, buttonViews );
  83. // This will change icon to button with `isOn = true`.
  84. toolbarDropdown.buttonView.bind( 'icon' ).toMany( buttons, 'isOn', ( ...areActive ) => {
  85. // Get the index of an active button.
  86. const index = areActive.findIndex( value => value );
  87. // If none of the commands is active, display either defaultIcon or the first button's icon.
  88. if ( index < 0 ) {
  89. return buttons[ 0 ].icon;
  90. }
  91. // Return active button's icon.
  92. return buttons[ index ].icon;
  93. } );
  94. // This will disable dropdown button when all buttons have `isEnabled = false`.
  95. toolbarDropdown.bind( 'isEnabled' ).toMany( buttons, 'isEnabled', ( ...areEnabled ) => areEnabled.some( isEnabled => isEnabled ) );
  96. ui.toolbarDropdown.add( toolbarDropdown );
  97. window.buttons = buttons;
  98. }
  99. function testSplitButton() {
  100. const dropdownView = createDropdown( {}, SplitButtonView );
  101. dropdownView.buttonView.set( {
  102. label: 'Dropdown',
  103. icon: alignCenterIcon
  104. } );
  105. ui.splitButton.add( dropdownView );
  106. dropdownView.panelView.element.innerHTML = 'Empty panel. There is no child view in this DropdownPanelView.';
  107. dropdownView.buttonView.on( 'execute', () => {
  108. /* global console */
  109. console.log( 'SplitButton#execute' );
  110. } );
  111. dropdownView.buttonView.on( 'open', () => {
  112. /* global console */
  113. console.log( 'SplitButton#open' );
  114. } );
  115. }
  116. testEmpty();
  117. testList();
  118. testLongLabel();
  119. testToolbar();
  120. testSplitButton();