import WindowService from '../../services/WindowService';
import GlobalEventService from '../../services/GlobalEventService';
const THROTTLE_DELAY = 100;
const classes = {
OPEN: 'open',
DROPDOWN_DOWN: 'dropdown__wrp_down',
DROPDOWN_WRP_OVER: 'dropdown__wrp_down-over',
DROPDOWN_UP: 'dropdown__wrp_up',
DROPDOWN_UP_OVER: 'dropdown__wrp_up-over',
VISIBLE_COLLECTION: 'visible-collection',
CUSTOM_ANCHOR_BUTTON: 'popout__action-btn',
DEFAULT_ANCHOR_BUTTON: 'popout__action',
DEFAULT_ANCHOR: 'angle-down'
};
const WINDOW_BORDER_OFFSET = 10;
const MIN_DROPDOWN_PANEL_WIDTH = 100;
const MIN_HEIGHT_TO_OPEN_DOWN = 300;
enum popoutFlow {
LEFT = 'left',
CENTER = 'center',
RIGHT = 'right'
};
enum panelPosition {
DOWN = 'down',
UP = 'up',
RIGHT = 'right',
LEFT = 'left'
};
type optionsType = {
popoutFlow?: popoutFlow,
autoOpen?: boolean,
renderAfterClose?: boolean,
panelPosition?: panelPosition,
panelOffsetLeft?: number,
panelMinWidth?: number,
allowNestedFocus?: boolean,
externalBlurHandler?: Function,
panelViewOptions?: object,
buttonViewOptions?: object,
showDropdownAnchor?: boolean,
customAnchor?: boolean,
fadeBackground?: boolean,
panelOffsets?: object
alwaysAlignByButton?: boolean,
adjustmentPosition?: {
x: number,
y: number
}
};
const defaultOptions: optionsType = {
popoutFlow: popoutFlow.LEFT,
autoOpen: true,
renderAfterClose: true,
panelPosition: panelPosition.DOWN,
panelOffsets: {
up: 0,
down: 0,
right: 0,
left: 0
},
panelMinWidth: MIN_DROPDOWN_PANEL_WIDTH,
allowNestedFocus: true,
externalBlurHandler: () => false,
showDropdownAnchor: false,
customAnchor: false,
fadeBackground: false,
alwaysAlignByButton: false
};
/**
* @name DropdownView
* @memberof module:core.dropdown.views
* @class Composite View that implements dropdown logic similar to SELECT HTML-element.
* Unlike {@link module:core.dropdown.views.PopoutView PopoutView}, a panel doesn't have a speech bubble triangle and
* it's min-width is always determined and equal to the width of a button view.
*
* A dropdown view contains button and panel regions that can be fully customizable by the properties buttonView and panelView.
*
*
Button View is used for displaying a button. Click on that button trigger a panel to open.
*
Panel View is used to display a panel that drops down.
*
*
* Panel width is determined by its layout but it cannot be less than the button's width. Panel height is fully determined by its layout.
* A place where the panel appears depends on the panelPosition option.
* Possible events:
*
'open' (dropdownView) - fires after the panel has opened.
*
'close' (dropdownView, ...) - fires after the panel has closed.
* If the panel was closed via close(...) method, the arguments of this method are transferred into this event.
*
'button:\*' - all events the buttonView triggers are repeated by this view with 'button:' prefix.
*
'panel:\*' - all events the panelView triggers are repeated by this view with 'panel:' prefix.
*
* @constructor
* @param {Object} options Options object.
* @param {Marionette.View} options.buttonView View class for displaying the button.
* @param {(Object|Function)} [options.buttonViewOptions] Options passed into the view on its creation.
* @param {Marionette.View} options.panelView View class for displaying the panel. The view is created every time the panel is triggered to open.
* @param {(Object|Function)} [options.panelViewOptions] Options passed into the view on its creation.
* @param {Object} [options.offsets = panelOffsets] Options for offsets.
* @param {Boolean} [options.autoOpen=true] Whether click on the button should trigger the panel to open.
* @param {String} [options.panelPosition='down'] Opening direction:
*
'down' - opens down.
*
'down-over' - opens down and the panel is located above the button overlapping it.
*
'up' - opens up.
*
'up-over' - opens up and the panel is located above the button overlapping it.
* @param {Boolean} [options.renderAfterClose=true] Whether to trigger button render when the panel has closed.
* */
const getClass = (options: optionsType) => {
const classList = [];
if (options.buttonViewOptions?.class) {
classList.push(options.buttonViewOptions.class);
}
if (options.class) {
classList.push(options.class);
}
return classList.join(' ');
};
export default class DropdownView {
constructor(options: optionsType) {
this.options = _.defaults({}, options, defaultOptions);
this.__observedEntities = [];
this.maxWidth = options.panelViewOptions && options.panelViewOptions.maxWidth ? options.panelViewOptions.maxWidth : 0;
this.__checkElements = _.throttle(this.__checkElements.bind(this), THROTTLE_DELAY);
this.__onBlur = this.__onBlur.bind(this)
Object.assign(this, Backbone.Events);
this.button = new this.options.buttonView(
Object.assign(
{
parent: this
},
this.options.buttonViewOptions,
{
class: getClass(this.options)
}
)
);
this.button.close = this.close.bind(this);
this.button.open = this.open.bind(this);
this.button.adjustPosition = this.adjustPosition.bind(this);
const buttonEl: Element = this.button.el;
this.button.once('render', () => {
this.isShown = true;
this.button.on('change:content', () => this.__adjustPosition(true));
this.button.on('toggle', () => this.toggle());
buttonEl.addEventListener('click', this.__handleClick.bind(this));
buttonEl.addEventListener('mouseenter', this.__handleMousenter.bind(this));
buttonEl.addEventListener('blur', this.__onBlur.bind(this));
buttonEl.addEventListener('touchend', e => e.stopPropagation());
});
buttonEl.classList.toggle(classes.CUSTOM_ANCHOR_BUTTON, this.options.customAnchor);
buttonEl.classList.toggle(classes.DEFAULT_ANCHOR_BUTTON, !this.options.customAnchor);
if (!this.options.customAnchor && this.options.showDropdownAnchor) {
this.button.on('render', () => {
buttonEl.insertAdjacentHTML('beforeend', ``);
});
}
this.button.on('destroy', this.__onDestroy, this);
// dropdown bind on existing DOM element without rendering ButtonView, refactoring needed
if (options.element) {
this.button.el = options.element;
this.button.$el = Backbone.$(options.element);
}
return this.button;
}
panelEl: Element
options: optionsType
maxWidth: number
__observedEntities: Array