| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- /**
- * DevExtreme (ui/drop_down_button.js)
- * Version: 19.1.16
- * Build date: Tue Oct 18 2022
- *
- * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
- * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
- */
- "use strict";
- var _renderer = require("../core/renderer");
- var _renderer2 = _interopRequireDefault(_renderer);
- var _ui = require("./widget/ui.widget");
- var _ui2 = _interopRequireDefault(_ui);
- var _function_template = require("./widget/function_template");
- var _function_template2 = _interopRequireDefault(_function_template);
- var _component_registrator = require("../core/component_registrator");
- var _component_registrator2 = _interopRequireDefault(_component_registrator);
- var _button_group = require("./button_group");
- var _button_group2 = _interopRequireDefault(_button_group);
- var _popup = require("./popup");
- var _popup2 = _interopRequireDefault(_popup);
- var _list = require("./list");
- var _list2 = _interopRequireDefault(_list);
- var _data = require("../core/utils/data");
- var _dom = require("../core/utils/dom");
- var _dom2 = _interopRequireDefault(_dom);
- var _icon = require("../core/utils/icon");
- var _data_helper = require("../data_helper");
- var _data_helper2 = _interopRequireDefault(_data_helper);
- var _data_source = require("../data/data_source/data_source");
- var _array_store = require("../data/array_store");
- var _array_store2 = _interopRequireDefault(_array_store);
- var _deferred = require("../core/utils/deferred");
- var _extend = require("../core/utils/extend");
- var _type = require("../core/utils/type");
- var _common = require("../core/utils/common");
- var _guid = require("../core/guid");
- var _guid2 = _interopRequireDefault(_guid);
- var _message = require("../localization/message");
- function _interopRequireDefault(obj) {
- return obj && obj.__esModule ? obj : {
- "default": obj
- }
- }
- var DROP_DOWN_BUTTON_CLASS = "dx-dropdownbutton";
- var DROP_DOWN_BUTTON_CONTENT = "dx-dropdownbutton-content";
- var DROP_DOWN_BUTTON_ACTION_CLASS = "dx-dropdownbutton-action";
- var DROP_DOWN_BUTTON_TOGGLE_CLASS = "dx-dropdownbutton-toggle";
- var DX_BUTTON_CONTENT_CLASS = "dx-button-content";
- var DX_ICON_RIGHT_CLASS = "dx-icon-right";
- var DROP_DOWN_BUTTON_POPUP_WRAPPER_CLASS = "dx-dropdownbutton-popup-wrapper";
- var DropDownButton = _ui2.default.inherit({
- _getDefaultOptions: function() {
- return (0, _extend.extend)(this.callBase(), {
- itemTemplate: "item",
- keyExpr: "this",
- displayExpr: "this",
- selectedItem: null,
- selectedItemKey: null,
- stylingMode: "outlined",
- deferRendering: true,
- noDataText: (0, _message.format)("dxCollectionWidget-noDataText"),
- useSelectMode: false,
- splitButton: false,
- showArrowIcon: true,
- text: "",
- icon: void 0,
- onButtonClick: null,
- onSelectionChanged: null,
- onItemClick: null,
- opened: false,
- items: null,
- dataSource: null,
- focusStateEnabled: true,
- hoverStateEnabled: true,
- dropDownOptions: {},
- dropDownContentTemplate: "content",
- grouped: false,
- groupTemplate: "group",
- buttonGroupOptions: {}
- })
- },
- _setOptionsByReference: function() {
- this.callBase();
- (0, _extend.extend)(this._optionsByReference, {
- selectedItem: true
- })
- },
- _init: function() {
- this.callBase();
- this._createItemClickAction();
- this._createActionClickAction();
- this._createSelectionChangedAction();
- this._compileKeyGetter();
- this._compileDisplayGetter();
- this._initDataSource();
- this._itemsToDataSource();
- this._initInnerOptionCache("buttonGroupOptions");
- this._initInnerOptionCache("dropDownOptions")
- },
- _initTemplates: function() {
- var _this = this;
- this.callBase();
- this._defaultTemplates.content = new _function_template2.default(function(options) {
- var $popupContent = (0, _renderer2.default)(options.container);
- var $listContainer = (0, _renderer2.default)("<div>").appendTo($popupContent);
- _this._list = _this._createComponent($listContainer, _list2.default, _this._listOptions());
- _this._list.registerKeyHandler("escape", _this._escHandler.bind(_this));
- _this._list.registerKeyHandler("tab", _this._escHandler.bind(_this));
- _this._list.registerKeyHandler("leftArrow", _this._escHandler.bind(_this));
- _this._list.registerKeyHandler("rightArrow", _this._escHandler.bind(_this))
- })
- },
- _itemsToDataSource: function() {
- if (!this._dataSource) {
- this._dataSource = new _data_source.DataSource({
- store: new _array_store2.default(this.option("items")),
- pageSize: 0
- })
- }
- },
- _compileKeyGetter: function() {
- this._keyGetter = (0, _data.compileGetter)(this.option("keyExpr"))
- },
- _compileDisplayGetter: function() {
- this._displayGetter = (0, _data.compileGetter)(this.option("displayExpr"))
- },
- _initMarkup: function() {
- this.callBase();
- this.$element().addClass(DROP_DOWN_BUTTON_CLASS);
- this._renderButtonGroup();
- this._loadSelectedItem().done(this._updateActionButton.bind(this));
- if (!this.option("deferRendering") || this.option("opened")) {
- this._renderPopup()
- }
- },
- _loadSelectedItem: function() {
- var d = new _deferred.Deferred;
- if (this._list) {
- var cachedResult = this.option("useSelectMode") ? this._list.option("selectedItem") : this._lastSelectedItemData;
- return d.resolve(cachedResult)
- }
- this._lastSelectedItemData = void 0;
- var selectedItemKey = this.option("selectedItemKey");
- this._loadSingle(this.option("keyExpr"), selectedItemKey).done(d.resolve).fail(function() {
- d.resolve(null)
- });
- return d.promise()
- },
- _createActionClickAction: function() {
- this._actionClickAction = this._createActionByOption("onButtonClick")
- },
- _createSelectionChangedAction: function() {
- this._selectionChangedAction = this._createActionByOption("onSelectionChanged")
- },
- _createItemClickAction: function() {
- this._itemClickAction = this._createActionByOption("onItemClick")
- },
- _fireSelectionChangedAction: function(_ref) {
- var previousValue = _ref.previousValue,
- value = _ref.value;
- this._selectionChangedAction({
- item: value,
- previousItem: previousValue
- })
- },
- _fireItemClickAction: function(_ref2) {
- var event = _ref2.event,
- itemElement = _ref2.itemElement,
- itemData = _ref2.itemData;
- return this._itemClickAction({
- event: event,
- itemElement: itemElement,
- itemData: this._actionItem || itemData
- })
- },
- _actionButtonConfig: function() {
- return {
- text: this.option("text"),
- icon: this.option("icon"),
- elementAttr: {
- "class": DROP_DOWN_BUTTON_ACTION_CLASS
- }
- }
- },
- _getButtonGroupItems: function() {
- var items = [];
- items.push(this._actionButtonConfig());
- if (this.option("splitButton")) {
- items.push({
- icon: "spindown",
- width: 26,
- elementAttr: {
- "class": DROP_DOWN_BUTTON_TOGGLE_CLASS
- }
- })
- }
- return items
- },
- _buttonGroupItemClick: function(_ref3) {
- var event = _ref3.event,
- itemData = _ref3.itemData;
- var isActionButton = itemData.elementAttr.class === DROP_DOWN_BUTTON_ACTION_CLASS;
- var isToggleButton = itemData.elementAttr.class === DROP_DOWN_BUTTON_TOGGLE_CLASS;
- if (isToggleButton) {
- this.toggle()
- } else {
- if (isActionButton) {
- this._actionClickAction({
- event: event,
- selectedItem: this.option("selectedItem")
- });
- if (!this.option("splitButton")) {
- this.toggle()
- }
- }
- }
- },
- _buttonGroupOptions: function() {
- return (0, _extend.extend)({
- items: this._getButtonGroupItems(),
- focusStateEnabled: this.option("focusStateEnabled"),
- hoverStateEnabled: this.option("hoverStateEnabled"),
- onItemClick: this._buttonGroupItemClick.bind(this),
- width: "100%",
- height: "100%",
- stylingMode: this.option("stylingMode"),
- selectionMode: "none"
- }, this._getInnerOptionsCache("buttonGroupOptions"))
- },
- _renderPopupContent: function() {
- var $content = this._popup.$content();
- var template = this._getTemplateByOption("dropDownContentTemplate");
- $content.empty();
- this._popupContentId = "dx-" + new _guid2.default;
- this.setAria("id", this._popupContentId, $content);
- return template.render({
- container: _dom2.default.getPublicElement($content),
- model: this.option("items") || this._dataSource
- })
- },
- _popupOptions: function() {
- var _this2 = this;
- return (0, _extend.extend)({
- dragEnabled: false,
- focusStateEnabled: false,
- deferRendering: this.option("deferRendering"),
- minWidth: function() {
- return _this2.$element().outerWidth()
- },
- closeOnOutsideClick: function(e) {
- var $element = _this2.$element();
- var $buttonClicked = (0, _renderer2.default)(e.target).closest(".".concat(DROP_DOWN_BUTTON_CLASS));
- return !$buttonClicked.is($element)
- },
- showTitle: false,
- animation: {
- show: {
- type: "fade",
- duration: 0,
- from: 0,
- to: 1
- },
- hide: {
- type: "fade",
- duration: 400,
- from: 1,
- to: 0
- }
- },
- width: function() {
- return this.$element().outerWidth()
- }.bind(this),
- height: "auto",
- shading: false,
- visible: this.option("opened"),
- position: {
- of: this.$element(),
- collision: "flipfit",
- my: "top left",
- at: "bottom left",
- offset: {
- y: -1
- }
- }
- }, this._getInnerOptionsCache("dropDownOptions"))
- },
- _listOptions: function() {
- var _this3 = this;
- var selectedItemKey = this.option("selectedItemKey");
- var useSelectMode = this.option("useSelectMode");
- return {
- selectionMode: useSelectMode ? "single" : "none",
- focusStateEnabled: this.option("focusStateEnabled"),
- hoverStateEnabled: this.option("hoverStateEnabled"),
- selectedItemKeys: selectedItemKey && useSelectMode ? [selectedItemKey] : [],
- grouped: this.option("grouped"),
- keyExpr: this.option("keyExpr"),
- noDataText: this.option("noDataText"),
- displayExpr: this.option("displayExpr"),
- itemTemplate: this.option("itemTemplate"),
- items: this.option("items"),
- dataSource: this._dataSource,
- onItemClick: function(e) {
- if (!_this3.option("useSelectMode")) {
- _this3._lastSelectedItemData = e.itemData
- }
- _this3.option("selectedItemKey", _this3._keyGetter(e.itemData));
- var actionResult = _this3._fireItemClickAction(e);
- if (false !== actionResult) {
- _this3.toggle(false);
- _this3._buttonGroup.focus()
- }
- }
- }
- },
- _upDownKeyHandler: function() {
- if (this._popup && this._popup.option("visible") && this._list) {
- this._list.focus()
- } else {
- this.open()
- }
- },
- _escHandler: function() {
- this.close();
- this._buttonGroup.focus()
- },
- _renderPopup: function() {
- var $popup = (0, _renderer2.default)("<div>");
- this.$element().append($popup);
- this._popup = this._createComponent($popup, _popup2.default, this._popupOptions());
- this._popup.$content().addClass(DROP_DOWN_BUTTON_CONTENT);
- this._popup._wrapper().addClass(DROP_DOWN_BUTTON_POPUP_WRAPPER_CLASS);
- this._popup.on("hiding", this._popupHidingHandler.bind(this));
- this._popup.on("showing", this._popupShowingHandler.bind(this));
- this._renderPopupContent();
- this._bindInnerWidgetOptions(this._popup, "dropDownOptions")
- },
- _popupHidingHandler: function() {
- this.option("opened", false);
- this.setAria({
- expanded: false,
- owns: void 0
- })
- },
- _popupShowingHandler: function() {
- this.option("opened", true);
- this.setAria({
- expanded: true,
- owns: this._popupContentId
- })
- },
- _renderAdditionalIcon: function() {
- if (this.option("splitButton") || !this.option("showArrowIcon")) {
- return
- }
- var $firstButtonContent = this._buttonGroup.$element().find(".".concat(DX_BUTTON_CONTENT_CLASS)).eq(0);
- var $iconElement = (0, _icon.getImageContainer)("spindown");
- $iconElement.addClass(DX_ICON_RIGHT_CLASS).appendTo($firstButtonContent)
- },
- _renderButtonGroup: function() {
- var $buttonGroup = this._buttonGroup && this._buttonGroup.$element() || (0, _renderer2.default)("<div>");
- if (!this._buttonGroup) {
- this.$element().append($buttonGroup)
- }
- this._buttonGroup = this._createComponent($buttonGroup, _button_group2.default, this._buttonGroupOptions());
- this._buttonGroup.registerKeyHandler("downArrow", this._upDownKeyHandler.bind(this));
- this._buttonGroup.registerKeyHandler("tab", this.close.bind(this));
- this._buttonGroup.registerKeyHandler("upArrow", this._upDownKeyHandler.bind(this));
- this._buttonGroup.registerKeyHandler("escape", this._escHandler.bind(this));
- this._renderAdditionalIcon();
- this._bindInnerWidgetOptions(this._buttonGroup, "buttonGroupOptions")
- },
- toggle: function(visible) {
- this._popup || this._renderPopup();
- return this._popup.toggle(visible)
- },
- open: function() {
- return this.toggle(true)
- },
- close: function() {
- return this.toggle(false)
- },
- _setListOption: function(name, value) {
- this._list && this._list.option(name, value)
- },
- _getDisplayValue: function(item) {
- var isPrimitiveItem = !(0, _type.isPlainObject)(item);
- var displayValue = isPrimitiveItem ? item : this._displayGetter(item);
- return !(0, _type.isPlainObject)(displayValue) ? String((0, _common.ensureDefined)(displayValue, "")) : ""
- },
- _updateActionButton: function(selectedItem) {
- if (this.option("useSelectMode")) {
- this.option({
- text: this._getDisplayValue(selectedItem),
- icon: (0, _type.isPlainObject)(selectedItem) ? selectedItem.icon : void 0
- })
- }
- this._setOptionSilent("selectedItem", selectedItem)
- },
- _clean: function() {
- this._list && this._list.$element().remove();
- this._popup && this._popup.$element().remove()
- },
- _selectedItemKeyChanged: function(value) {
- var _this4 = this;
- this._setListOption("selectedItemKeys", this.option("useSelectMode") && (0, _type.isDefined)(value) ? [value] : []);
- var previousItem = this.option("selectedItem");
- this._loadSelectedItem().done(function(selectedItem) {
- _this4._updateActionButton(selectedItem);
- if (_this4._displayGetter(previousItem) !== _this4._displayGetter(selectedItem)) {
- _this4._fireSelectionChangedAction({
- previousValue: previousItem,
- value: selectedItem
- })
- }
- })
- },
- _selectModeChanged: function(value) {
- if (value) {
- this._setListOption("selectionMode", "single");
- var selectedItemKey = this.option("selectedItemKey");
- this._setListOption("selectedItemKeys", selectedItemKey ? [selectedItemKey] : [])
- } else {
- this._setListOption("selectionMode", "none");
- this.option({
- selectedItemKey: void 0,
- selectedItem: void 0
- })
- }
- },
- _updateItemCollection: function(optionName) {
- this._setWidgetOption("_list", [optionName]);
- this._setListOption("selectedItemKeys", []);
- this._loadSelectedItem().done(this._updateActionButton.bind(this))
- },
- _optionChanged: function(args) {
- var name = args.name,
- value = args.value;
- switch (args.name) {
- case "useSelectMode":
- this._selectModeChanged(args.value);
- break;
- case "splitButton":
- this._renderButtonGroup();
- break;
- case "displayExpr":
- this._compileDisplayGetter();
- break;
- case "keyExpr":
- this._compileKeyGetter();
- break;
- case "buttonGroupOptions":
- this._innerOptionChanged(this._buttonGroup, args);
- break;
- case "dropDownOptions":
- this._innerOptionChanged(this._popup, args);
- break;
- case "opened":
- this.toggle(value);
- break;
- case "focusStateEnabled":
- case "hoverStateEnabled":
- this._setListOption(name, value);
- this._buttonGroup.option(name, value);
- break;
- case "items":
- this._dataSource = null;
- this._itemsToDataSource();
- this._updateItemCollection(name);
- break;
- case "dataSource":
- this._initDataSource();
- this._updateItemCollection(name);
- break;
- case "icon":
- this._buttonGroup.option("items[0]", (0, _extend.extend)({}, this._actionButtonConfig(), {
- icon: value
- }));
- this._renderAdditionalIcon();
- break;
- case "text":
- this._buttonGroup.option("items[0]", (0, _extend.extend)({}, this._actionButtonConfig(), {
- text: value
- }));
- this._renderAdditionalIcon();
- break;
- case "showArrowIcon":
- if (!value) {
- this._buttonGroup.$element().find(".".concat(DX_ICON_RIGHT_CLASS)).remove()
- }
- this._renderAdditionalIcon();
- break;
- case "width":
- case "height":
- this.callBase(args);
- this._popup && this._popup.repaint();
- break;
- case "stylingMode":
- this._buttonGroup.option(name, value);
- break;
- case "itemTemplate":
- case "grouped":
- case "noDataText":
- case "groupTemplate":
- this._setListOption(name, value);
- break;
- case "dropDownContentTemplate":
- this._popup && this._renderPopupContent();
- break;
- case "selectedItemKey":
- this._selectedItemKeyChanged(value);
- break;
- case "selectedItem":
- break;
- case "onItemClick":
- this._createItemClickAction();
- break;
- case "onButtonClick":
- this._createActionClickAction();
- break;
- case "onSelectionChanged":
- this._createSelectionChangedAction();
- break;
- case "deferRendering":
- if (!value && !this._popup) {
- this._renderPopup()
- }
- break;
- default:
- this.callBase(args)
- }
- }
- }).include(_data_helper2.default);
- (0, _component_registrator2.default)("dxDropDownButton", DropDownButton);
- module.exports = DropDownButton;
- module.exports.default = module.exports;
|