Liferay.Loader.define("app-builder-web@1.0.73/js/pages/form-view/CustomObjectSidebar.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/drop-down', '@frontend-taglib-clay$clayui/form', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/layout', 'frontend-js-react-web$classnames', 'data-engine-taglib', 'frontend-js-react-web$react', '../../hooks/index.es', '../../utils/clickOutside.es', './CustomObjectFieldsList.es', './DataLayoutBuilderInstanceContext.es', './FormViewContext.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }return _typeof(obj);
    }

    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = void 0;

    var _button = require("@frontend-taglib-clay$clayui/button");

    var _dropDown = _interopRequireWildcard(require("@frontend-taglib-clay$clayui/drop-down"));

    var _form = _interopRequireDefault(require("@frontend-taglib-clay$clayui/form"));

    var _icon = _interopRequireDefault(require("@frontend-taglib-clay$clayui/icon"));

    var _layout = _interopRequireDefault(require("@frontend-taglib-clay$clayui/layout"));

    var _classnames = _interopRequireDefault(require("frontend-js-react-web$classnames"));

    var _dataEngineTaglib = require("data-engine-taglib");

    var _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    var _index = require("../../hooks/index.es");

    var _clickOutside = _interopRequireDefault(require("../../utils/clickOutside.es"));

    var _CustomObjectFieldsList = _interopRequireDefault(require("./CustomObjectFieldsList.es"));

    var _DataLayoutBuilderInstanceContext = _interopRequireDefault(require("./DataLayoutBuilderInstanceContext.es"));

    var _FormViewContext = _interopRequireDefault(require("./FormViewContext.es"));

    function _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { default: obj };
    }

    function _getRequireWildcardCache() {
      if (typeof WeakMap !== "function") return null;var cache = new WeakMap();_getRequireWildcardCache = function _getRequireWildcardCache() {
        return cache;
      };return cache;
    }

    function _interopRequireWildcard(obj) {
      if (obj && obj.__esModule) {
        return obj;
      }if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
        return { default: obj };
      }var cache = _getRequireWildcardCache();if (cache && cache.has(obj)) {
        return cache.get(obj);
      }var newObj = {};var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;if (desc && (desc.get || desc.set)) {
            Object.defineProperty(newObj, key, desc);
          } else {
            newObj[key] = obj[key];
          }
        }
      }newObj.default = obj;if (cache) {
        cache.set(obj, newObj);
      }return newObj;
    }

    function _slicedToArray(arr, i) {
      return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
    }

    function _nonIterableRest() {
      throw new TypeError("Invalid attempt to destructure non-iterable instance");
    }

    function _iterableToArrayLimit(arr, i) {
      if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
        return;
      }var _arr = [];var _n = true;var _d = false;var _e = undefined;try {
        for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
          _arr.push(_s.value);if (i && _arr.length === i) break;
        }
      } catch (err) {
        _d = true;_e = err;
      } finally {
        try {
          if (!_n && _i["return"] != null) _i["return"]();
        } finally {
          if (_d) throw _e;
        }
      }return _arr;
    }

    function _arrayWithHoles(arr) {
      if (Array.isArray(arr)) return arr;
    }

    var DropDown = function DropDown() {
      var _useContext = (0, _react.useContext)(_FormViewContext.default),
          _useContext2 = _slicedToArray(_useContext, 2),
          _useContext2$ = _useContext2[0],
          allowNestedFields = _useContext2$.config.allowNestedFields,
          fieldTypes = _useContext2$.fieldTypes,
          dispatch = _useContext2[1];

      var _useState = (0, _react.useState)(false),
          _useState2 = _slicedToArray(_useState, 2),
          active = _useState2[0],
          setActive = _useState2[1];

      var onClickFieldType = function onClickFieldType(fieldTypeName) {
        setActive(false);
        dispatch({
          payload: {
            fieldTypeName: fieldTypeName
          },
          type: _dataEngineTaglib.DataLayoutBuilderActions.ADD_CUSTOM_OBJECT_FIELD
        });
      };

      (0, _react.useLayoutEffect)(function () {
        if (active) {
          var _document$querySelect = document.querySelector('.custom-object-dropdown-list'),
              parentElement = _document$querySelect.parentElement;

          parentElement.classList.add('custom-object-dropdown-menu');
        }
      }, [active]);
      var filteredFieldTypes = fieldTypes.filter(function (_ref) {
        var name = _ref.name,
            scope = _ref.scope;

        if (!scope.includes('app-builder')) {
          return false;
        } // Remove fields group field from left sidebar


        if (name === 'fieldset' && !allowNestedFields) {
          return false;
        }

        return true;
      });
      filteredFieldTypes.sort(function (_ref2, _ref3) {
        var a = _ref2.displayOrder;
        var b = _ref3.displayOrder;
        return a - b;
      });
      return (/*#__PURE__*/_react.default.createElement(_dropDown.default, {
          active: active,
          alignmentPosition: _dropDown.Align.BottomRight,
          className: "custom-object-dropdown",
          hasLeftSymbols: true,
          onActiveChange: function onActiveChange(newVal) {
            return setActive(newVal);
          },
          trigger: /*#__PURE__*/_react.default.createElement(_button.ClayButtonWithIcon, {
            displayType: "unstyled",
            symbol: "plus"
          })
        }, /*#__PURE__*/_react.default.createElement(_dropDown.default.ItemList, {
          className: "custom-object-dropdown-list"
        }, filteredFieldTypes.map(function (_ref4) {
          var icon = _ref4.icon,
              label = _ref4.label,
              name = _ref4.name;
          return (/*#__PURE__*/_react.default.createElement(_dropDown.default.Item, {
              key: name,
              onClick: function onClick() {
                return onClickFieldType(name);
              },
              symbolLeft: icon
            }, label)
          );
        })))
      );
    };

    var EmptyState = function EmptyState() {
      return (/*#__PURE__*/_react.default.createElement("div", {
          className: "custom-object-sidebar-empty"
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          symbol: "custom-field"
        }), /*#__PURE__*/_react.default.createElement("h3", null, Liferay.Language.get('there-are-no-fields-yet')), /*#__PURE__*/_react.default.createElement("p", null, Liferay.Language.get('any-field-added-to-the-object-or-to-a-form-view-appears-here')))
      );
    };

    var Header = function Header(_ref5) {
      var onCloseSearch = _ref5.onCloseSearch,
          onSearch = _ref5.onSearch,
          searchText = _ref5.searchText;

      var _useState3 = (0, _react.useState)(false),
          _useState4 = _slicedToArray(_useState3, 2),
          searchMode = _useState4[0],
          setSearchMode = _useState4[1];

      var closeSearch = (0, _react.useCallback)(function () {
        setSearchMode(false);
        onCloseSearch();
      }, [onCloseSearch, setSearchMode]);

      (0, _index.useKeyDown)(function () {
        if (searchMode) {
          closeSearch();
        }
      }, 27);
      var searchInputRef = (0, _react.useRef)();
      (0, _react.useEffect)(function () {
        if (searchMode && searchInputRef.current) {
          searchInputRef.current.focus();
        }
      }, [searchInputRef, searchMode]);

      var _useContext3 = (0, _react.useContext)(_FormViewContext.default),
          _useContext4 = _slicedToArray(_useContext3, 1),
          dataDefinition = _useContext4[0].dataDefinition;

      var _dataDefinition$name$ = dataDefinition.name[dataDefinition.defaultLanguageId],
          dataDefinitionName = _dataDefinition$name$ === void 0 ? '' : _dataDefinition$name$;
      return (/*#__PURE__*/_react.default.createElement(_form.default, {
          onSubmit: function onSubmit(event) {
            return event.preventDefault();
          }
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)('custom-object-sidebar-header', 'ml-4 mr-4 mt-4 pt-2 pb-2')
        }, /*#__PURE__*/_react.default.createElement(_layout.default.ContentRow, {
          verticalAlign: "center"
        }, searchMode ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_layout.default.ContentCol, {
          expand: true
        }, /*#__PURE__*/_react.default.createElement(_dataEngineTaglib.SearchInput, {
          clearButton: false,
          onChange: function onChange(searchText) {
            return onSearch(searchText);
          },
          ref: searchInputRef,
          searchText: searchText
        })), /*#__PURE__*/_react.default.createElement(_layout.default.ContentCol, {
          className: "ml-2",
          key: "closeButton"
        }, /*#__PURE__*/_react.default.createElement(_button.ClayButtonWithIcon, {
          displayType: "unstyled",
          onClick: function onClickClose() {
            return closeSearch();
          },
          symbol: "times"
        }))) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_layout.default.ContentCol, {
          expand: true
        }, /*#__PURE__*/_react.default.createElement("h3", null, dataDefinitionName)), /*#__PURE__*/_react.default.createElement(_layout.default.ContentCol, {
          key: "searchButton"
        }, /*#__PURE__*/_react.default.createElement(_button.ClayButtonWithIcon, {
          displayType: "unstyled",
          onClick: function onClickSearch() {
            return setSearchMode(true);
          },
          symbol: "search"
        })), /*#__PURE__*/_react.default.createElement(_layout.default.ContentCol, {
          key: "dropdown"
        }, /*#__PURE__*/_react.default.createElement(DropDown, null))))))
      );
    };

    var _default = function _default() {
      var _useContext5 = (0, _react.useContext)(_FormViewContext.default),
          _useContext6 = _slicedToArray(_useContext5, 2),
          _useContext6$ = _useContext6[0],
          _useContext6$$dataDef = _useContext6$.dataDefinition,
          dataDefinitionFields = _useContext6$$dataDef.dataDefinitionFields,
          id = _useContext6$$dataDef.id,
          focusedCustomObjectField = _useContext6$.focusedCustomObjectField,
          dispatch = _useContext6[1];

      var _useState5 = (0, _react.useState)(''),
          _useState6 = _slicedToArray(_useState5, 2),
          searchText = _useState6[0],
          setSearchText = _useState6[1];

      var _useContext7 = (0, _react.useContext)(_DataLayoutBuilderInstanceContext.default),
          _useContext8 = _slicedToArray(_useContext7, 1),
          dataLayoutBuilder = _useContext8[0];

      var sidebarRef = (0, _react.useRef)();
      (0, _index.useKeyDown)(function () {
        if (Object.keys(focusedCustomObjectField).length > 0) {
          dispatch({
            payload: {
              dataDefinitionField: {}
            },
            type: _dataEngineTaglib.DataLayoutBuilderActions.UPDATE_FOCUSED_CUSTOM_OBJECT_FIELD
          });
        }
      }, 27);
      (0, _react.useEffect)(function () {
        var eventHandler = function eventHandler(_ref6) {
          var target = _ref6.target;

          if ((0, _clickOutside.default)(target, '.app-builder-upper-toolbar', 'button.close', '.display-settings', '.dropdown-menu', '.modal.show', '.nav-underline', '#ddm-actionable-fields-container')) {
            dispatch({
              payload: {
                dataDefinitionField: {}
              },
              type: _dataEngineTaglib.DataLayoutBuilderActions.UPDATE_FOCUSED_CUSTOM_OBJECT_FIELD
            });
            dataLayoutBuilder.dispatch('sidebarFieldBlurred');
          }
        };

        window.addEventListener('click', eventHandler);
        return function () {
          return window.removeEventListener('click', eventHandler);
        };
      }, [dataLayoutBuilder, dispatch]);
      var empty = dataDefinitionFields.length === 0;
      return (/*#__PURE__*/_react.default.createElement(_dataEngineTaglib.Sidebar, {
          closeable: false,
          ref: sidebarRef
        }, /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(Header, {
          onCloseSearch: function onCloseSearch() {
            return setSearchText('');
          },
          onSearch: function onSearch(searchText) {
            return setSearchText(searchText);
          },
          searchText: searchText
        }), /*#__PURE__*/_react.default.createElement(_dataEngineTaglib.Sidebar.Body, {
          className: (0, _classnames.default)({
            empty: empty
          })
        }, !!id && empty ? /*#__PURE__*/_react.default.createElement(EmptyState, null) : /*#__PURE__*/_react.default.createElement(_CustomObjectFieldsList.default, {
          keywords: searchText
        }))))
      );
    };

    exports.default = _default;
    //# sourceMappingURL=CustomObjectSidebar.es.js.map
  }
});
//# sourceMappingURL=CustomObjectSidebar.es.js.map