|
| 1 | +/** |
| 2 | + * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. |
| 3 | + * For licensing, see LICENSE.md. |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * @module ui/dropdown/button/createbuttondropdown |
| 8 | + */ |
| 9 | + |
| 10 | +import createDropdown from '../createdropdown'; |
| 11 | + |
| 12 | +import ToolbarView from '../../toolbar/toolbarview'; |
| 13 | +import { closeDropdownOnBlur, closeDropdownOnExecute, focusDropdownContentsOnArrows } from '../utils'; |
| 14 | + |
| 15 | +import '../../../theme/components/dropdown/buttondropdown.css'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Creates an instance of {@link module:ui/dropdown/button/buttondropdownview~ButtonDropdownView} class using |
| 19 | + * a provided {@link module:ui/dropdown/button/buttondropdownmodel~ButtonDropdownModel}. |
| 20 | + * |
| 21 | + * const buttons = []; |
| 22 | + * |
| 23 | + * buttons.push( new ButtonView() ); |
| 24 | + * buttons.push( editor.ui.componentFactory.get( 'someButton' ) ); |
| 25 | + * |
| 26 | + * const model = new Model( { |
| 27 | + * label: 'A button dropdown', |
| 28 | + * isVertical: true, |
| 29 | + * buttons |
| 30 | + * } ); |
| 31 | + * |
| 32 | + * const dropdown = createButtonDropdown( model, locale ); |
| 33 | + * |
| 34 | + * // Will render a vertical button dropdown labeled "A button dropdown" |
| 35 | + * // with a button group in the panel containing two buttons. |
| 36 | + * dropdown.render() |
| 37 | + * document.body.appendChild( dropdown.element ); |
| 38 | + * |
| 39 | + * The model instance remains in control of the dropdown after it has been created. E.g. changes to the |
| 40 | + * {@link module:ui/dropdown/dropdownmodel~DropdownModel#label `model.label`} will be reflected in the |
| 41 | + * dropdown button's {@link module:ui/button/buttonview~ButtonView#label} attribute and in DOM. |
| 42 | + * |
| 43 | + * See {@link module:ui/dropdown/createdropdown~createDropdown}. |
| 44 | + * |
| 45 | + * @param {module:ui/dropdown/button/buttondropdownmodel~ButtonDropdownModel} model Model of the list dropdown. |
| 46 | + * @param {module:utils/locale~Locale} locale The locale instance. |
| 47 | + * @returns {module:ui/dropdown/button/buttondropdownview~ButtonDropdownView} The button dropdown view instance. |
| 48 | + * @returns {module:ui/dropdown/dropdownview~DropdownView} |
| 49 | + */ |
| 50 | +export default function createButtonDropdown( model, locale ) { |
| 51 | + // Make disabled when all buttons are disabled |
| 52 | + model.bind( 'isEnabled' ).to( |
| 53 | + // Bind to #isEnabled of each command... |
| 54 | + ...getBindingTargets( model.buttons, 'isEnabled' ), |
| 55 | + // ...and set it true if any command #isEnabled is true. |
| 56 | + ( ...areEnabled ) => areEnabled.some( isEnabled => isEnabled ) |
| 57 | + ); |
| 58 | + |
| 59 | + // If defined `staticIcon` use the `defautlIcon` without binding it to active a button. |
| 60 | + if ( model.staticIcon ) { |
| 61 | + model.bind( 'icon' ).to( model, 'defaultIcon' ); |
| 62 | + } else { |
| 63 | + // Make dropdown icon as any active button. |
| 64 | + model.bind( 'icon' ).to( |
| 65 | + // Bind to #isOn of each button... |
| 66 | + ...getBindingTargets( model.buttons, 'isOn' ), |
| 67 | + // ...and chose the title of the first one which #isOn is true. |
| 68 | + ( ...areActive ) => { |
| 69 | + const index = areActive.findIndex( value => value ); |
| 70 | + |
| 71 | + // If none of the commands is active, display either defaultIcon or first button icon. |
| 72 | + if ( index < 0 && model.defaultIcon ) { |
| 73 | + return model.defaultIcon; |
| 74 | + } |
| 75 | + |
| 76 | + return model.buttons[ index < 0 ? 0 : index ].icon; |
| 77 | + } |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + const dropdownView = createDropdown( model, locale ); |
| 82 | + const toolbarView = dropdownView.toolbarView = new ToolbarView(); |
| 83 | + |
| 84 | + toolbarView.bind( 'isVertical', 'className' ).to( model, 'isVertical', 'toolbarClassName' ); |
| 85 | + |
| 86 | + model.buttons.map( view => toolbarView.items.add( view ) ); |
| 87 | + |
| 88 | + dropdownView.extendTemplate( { |
| 89 | + attributes: { |
| 90 | + class: [ 'ck-buttondropdown' ] |
| 91 | + } |
| 92 | + } ); |
| 93 | + |
| 94 | + dropdownView.panelView.children.add( toolbarView ); |
| 95 | + |
| 96 | + closeDropdownOnBlur( dropdownView ); |
| 97 | + closeDropdownOnExecute( dropdownView, toolbarView.items ); |
| 98 | + focusDropdownContentsOnArrows( dropdownView, toolbarView ); |
| 99 | + |
| 100 | + return dropdownView; |
| 101 | +} |
| 102 | + |
| 103 | +// Returns an array of binding components for |
| 104 | +// {@link module:utils/observablemixin~Observable#bind} from a set of iterable |
| 105 | +// buttons. |
| 106 | +// |
| 107 | +// @private |
| 108 | +// @param {Iterable.<module:ui/button/buttonview~ButtonView>} buttons |
| 109 | +// @param {String} attribute |
| 110 | +// @returns {Array.<String>} |
| 111 | +function getBindingTargets( buttons, attribute ) { |
| 112 | + return Array.prototype.concat( ...buttons.map( button => [ button, attribute ] ) ); |
| 113 | +} |
0 commit comments