Repository URL to install this package:
|
Version:
3.12.16 ▾
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { MODE_CHAR, MODE_ID } from './constants';
import SuggestionModeAttribute from './SuggestionModeAttribute';
import SuggestionModeExpression from './SuggestionModeExpression';
import SuggestionModeTag from './SuggestionModeTag';
var SuggestionsManager = /*#__PURE__*/function () {
function SuggestionsManager(_ref) {
var i18n = _ref.i18n,
_ref$modeId = _ref.modeId,
modeId = _ref$modeId === void 0 ? null : _ref$modeId,
searchInputRef = _ref.searchInputRef,
setModeState = _ref.setModeState,
_ref$attributesKeys = _ref.attributesKeys,
attributesKeys = _ref$attributesKeys === void 0 ? [] : _ref$attributesKeys,
_ref$attributesTags = _ref.attributesTags,
attributesTags = _ref$attributesTags === void 0 ? [] : _ref$attributesTags,
selectFilter = _ref.selectFilter,
selectTag = _ref.selectTag,
_ref$filters = _ref.filters,
filters = _ref$filters === void 0 ? [] : _ref$filters,
_ref$tags = _ref.tags,
tags = _ref$tags === void 0 ? [] : _ref$tags,
setItems = _ref.setItems,
setEmptyItemsPlaceholder = _ref.setEmptyItemsPlaceholder;
_classCallCheck(this, SuggestionsManager);
_defineProperty(this, "i18n", function () {});
_defineProperty(this, "activeMode", null);
_defineProperty(this, "activeAttribute", null);
_defineProperty(this, "searchInputRef", null);
_defineProperty(this, "setModeState", function () {});
_defineProperty(this, "attributesKeys", []);
_defineProperty(this, "attributesTags", []);
_defineProperty(this, "selectFilter", function () {});
_defineProperty(this, "selectTag", function () {});
_defineProperty(this, "setItems", function () {});
_defineProperty(this, "setEmptyItemsPlaceholder", function () {});
_defineProperty(this, "filters", []);
// selected filters
_defineProperty(this, "tags", []);
// selected tags
_defineProperty(this, "modeAttribute", new SuggestionModeAttribute(this));
_defineProperty(this, "modeExpression", new SuggestionModeExpression(this));
_defineProperty(this, "modeTag", new SuggestionModeTag(this));
_defineProperty(this, "items", []);
_defineProperty(this, "emptyItemsPlaceholder", '');
this.setI18n(i18n);
this.setModeState = setModeState;
this.setMode(modeId);
this.setSearchInputRef(searchInputRef);
this.setAttributesKeys(attributesKeys);
this.setAttributesTags(attributesTags);
this.setSelectFilter(selectFilter);
this.setSelectTag(selectTag);
this.setFilters(filters);
this.setTags(tags);
this.setItems = setItems;
this.setEmptyItemsPlaceholder = setEmptyItemsPlaceholder;
}
_createClass(SuggestionsManager, [{
key: "setMode",
value: function setMode() {
var modeId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
switch (modeId) {
case MODE_ID.ATTRIBUTE:
this.activeMode = this.modeAttribute;
break;
case MODE_ID.EXPRESSION:
this.activeMode = this.modeExpression;
break;
case MODE_ID.TAG:
this.activeMode = this.modeTag;
break;
default:
this.activeMode = null;
}
this.setModeState(modeId);
this.render();
}
}, {
key: "setActiveAttribute",
value: function setActiveAttribute() {
var activeAttribute = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
this.activeAttribute = activeAttribute || null;
this.render();
}
}, {
key: "setSearchInputRef",
value: function setSearchInputRef() {
var searchInputRef = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
this.searchInputRef = searchInputRef || null;
}
}, {
key: "setAttributesKeys",
value: function setAttributesKeys() {
var attributesKeys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
this.attributesKeys = attributesKeys || [];
}
}, {
key: "setAttributesTags",
value: function setAttributesTags() {
var attributesTags = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
this.attributesTags = attributesTags || [];
}
}, {
key: "setSelectFilter",
value: function setSelectFilter(selectFilter) {
this.selectFilter = selectFilter;
}
}, {
key: "setSelectTag",
value: function setSelectTag(selectTag) {
this.selectTag = selectTag;
}
}, {
key: "setFilters",
value: function setFilters() {
var filters = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
this.filters = filters || [];
}
}, {
key: "setTags",
value: function setTags() {
var tags = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
this.tags = tags || [];
}
}, {
key: "setI18n",
value: function setI18n(i18n) {
this.i18n = i18n;
}
}, {
key: "activateAttribute",
value: function activateAttribute() {
var attribute = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
this.setMode(attribute ? MODE_ID.EXPRESSION : null);
this.setActiveAttribute(attribute);
}
}, {
key: "changeSearchInputValue",
value: function changeSearchInputValue(newValue) {
this.searchInputRef.value = newValue;
this.searchInputRef.focus();
}
}, {
key: "onSearchButtonClick",
value: function onSearchButtonClick(event) {
this.activeMode.onEnterPressed(event);
}
}, {
key: "onSearchInputKeyDown",
value: function onSearchInputKeyDown(event) {
if (this.activeMode) {
switch (event.key) {
case 'Enter':
this.activeMode.onEnterPressed(event);
break;
case 'Escape':
this.activeMode.onEscapePressed(event);
break;
default:
{
var shouldPreventDefault = false;
if (this.activeMode) {
shouldPreventDefault = this.activeMode["char"] === event.key ? true : this.activeMode.shouldPreventDefaultKeyDown(event);
}
if (shouldPreventDefault) {
event.preventDefault();
event.stopPropagation();
} else if (this.activeMode) {
this.activeMode.onKeyDown(event);
}
}
}
} else {
// No active mode
if (event.key === MODE_CHAR[MODE_ID.ATTRIBUTE]) {
this.setMode(MODE_ID.ATTRIBUTE);
} else if (event.key === MODE_CHAR[MODE_ID.TAG]) {
this.setMode(MODE_ID.TAG);
}
}
}
}, {
key: "onSearchInputKeyUp",
value: function onSearchInputKeyUp(event) {
var _event$currentTarget;
var q = (_event$currentTarget = event.currentTarget) === null || _event$currentTarget === void 0 ? void 0 : _event$currentTarget.value;
if (this.activeMode) {
var _this$activeMode;
var isAttrModeCharIncluded = this.activeMode.id !== MODE_ID.TAG && q.includes(MODE_CHAR[MODE_ID.ATTRIBUTE]);
var isTagModeCharIncluded = this.activeMode.id === MODE_ID.TAG && q.includes(MODE_CHAR[MODE_ID.TAG]);
if (!q || !(isAttrModeCharIncluded || isTagModeCharIncluded)) {
this.setMode(null);
this.setActiveAttribute(null);
}
if (((_this$activeMode = this.activeMode) === null || _this$activeMode === void 0 ? void 0 : _this$activeMode.id) === MODE_ID.EXPRESSION && this.activeAttribute && !new RegExp("".concat(this.activeAttribute.slug).concat(MODE_CHAR[MODE_ID.EXPRESSION])).test(q)) {
this.setMode(MODE_ID.ATTRIBUTE);
}
}
}
}, {
key: "reset",
value: function reset() {
if (this.activeMode) {
this.activeMode.reset();
}
}
}, {
key: "getItems",
value: function getItems() {
return this.activeMode ? this.activeMode.getItems() : [];
}
}, {
key: "getEmptyItemsPlaceholder",
value: function getEmptyItemsPlaceholder(items) {
return this.activeMode ? this.activeMode.getEmptyItemsPlaceholder(items) : '';
}
}, {
key: "render",
value: function render() {
var newItems = this.getItems();
var newEmptyItemsPlaceholder = this.getEmptyItemsPlaceholder(this.items);
var newItemsEmpty = Array.isArray(newItems) && newItems.length === 0;
var oldItemsEmpty = Array.isArray(this.items) && this.items.length === 0;
if (newItems !== this.items && !(newItemsEmpty && oldItemsEmpty)) {
this.items = newItems;
this.setItems(this.items);
}
if (newEmptyItemsPlaceholder !== this.emptyItemsPlaceholder) {
this.emptyItemsPlaceholder = newEmptyItemsPlaceholder;
this.setEmptyItemsPlaceholder(this.emptyItemsPlaceholder);
}
}
}]);
return SuggestionsManager;
}();
export default SuggestionsManager;