Liferay.Loader.define("dynamic-data-mapping-form-builder@3.0.77/js/components/FormBuilder/withActionableFields.es", ['module', 'exports', 'require', 'dynamic-data-mapping-form-renderer', 'frontend-js-metal-web$metal-dom', 'frontend-js-metal-web$metal-events', 'frontend-js-metal-web$metal-jsx', 'frontend-js-metal-web$metal-state', '../../util/fieldSupport.es', './FieldActionsDropDown.es', './props.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = void 0;

    var _dynamicDataMappingFormRenderer = require("dynamic-data-mapping-form-renderer");

    var _metalDom = _interopRequireDefault(require("frontend-js-metal-web$metal-dom"));

    var _metalEvents = require("frontend-js-metal-web$metal-events");

    var _metalJsx = _interopRequireDefault(require("frontend-js-metal-web$metal-jsx"));

    var _metalState = require("frontend-js-metal-web$metal-state");

    var _fieldSupport = require("../../util/fieldSupport.es");

    var _FieldActionsDropDown = _interopRequireDefault(require("./FieldActionsDropDown.es"));

    var _props = _interopRequireDefault(require("./props.es"));

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

    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);
    }

    function ownKeys(object, enumerableOnly) {
      var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {
          return Object.getOwnPropertyDescriptor(object, sym).enumerable;
        });keys.push.apply(keys, symbols);
      }return keys;
    }

    function _objectSpread(target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {
          ownKeys(Object(source), true).forEach(function (key) {
            _defineProperty(target, key, source[key]);
          });
        } else if (Object.getOwnPropertyDescriptors) {
          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
        } else {
          ownKeys(Object(source)).forEach(function (key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
          });
        }
      }return target;
    }

    function _defineProperty(obj, key, value) {
      if (key in obj) {
        Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
      } else {
        obj[key] = value;
      }return obj;
    }

    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, descriptor.key, descriptor);
      }
    }

    function _createClass(Constructor, protoProps, staticProps) {
      if (protoProps) _defineProperties(Constructor.prototype, protoProps);if (staticProps) _defineProperties(Constructor, staticProps);return Constructor;
    }

    function _possibleConstructorReturn(self, call) {
      if (call && (_typeof(call) === "object" || typeof call === "function")) {
        return call;
      }return _assertThisInitialized(self);
    }

    function _assertThisInitialized(self) {
      if (self === void 0) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
      }return self;
    }

    function _get(target, property, receiver) {
      if (typeof Reflect !== "undefined" && Reflect.get) {
        _get = Reflect.get;
      } else {
        _get = function _get(target, property, receiver) {
          var base = _superPropBase(target, property);if (!base) return;var desc = Object.getOwnPropertyDescriptor(base, property);if (desc.get) {
            return desc.get.call(receiver);
          }return desc.value;
        };
      }return _get(target, property, receiver || target);
    }

    function _superPropBase(object, property) {
      while (!Object.prototype.hasOwnProperty.call(object, property)) {
        object = _getPrototypeOf(object);if (object === null) break;
      }return object;
    }

    function _getPrototypeOf(o) {
      _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
        return o.__proto__ || Object.getPrototypeOf(o);
      };return _getPrototypeOf(o);
    }

    function _inherits(subClass, superClass) {
      if (typeof superClass !== "function" && superClass !== null) {
        throw new TypeError("Super expression must either be null or a function");
      }subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } });if (superClass) _setPrototypeOf(subClass, superClass);
    }

    function _setPrototypeOf(o, p) {
      _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
        o.__proto__ = p;return o;
      };return _setPrototypeOf(o, p);
    }

    var _CSS_HOVERED = 'hovered';
    var ACTIONABLE_FIELDS_CONTAINER = 'ddm-actionable-fields-container';

    var withActionableFields = function withActionableFields(ChildComponent) {
      var ActionableFields = /*#__PURE__*/function (_Component) {
        _inherits(ActionableFields, _Component);

        function ActionableFields() {
          _classCallCheck(this, ActionableFields);

          return _possibleConstructorReturn(this, _getPrototypeOf(ActionableFields).apply(this, arguments));
        }

        _createClass(ActionableFields, [{
          key: "attached",
          value: function attached() {
            this._eventHandler = new _metalEvents.EventHandler();

            this._eventHandler.add(this.delegate('mouseenter', '.ddm-field-container', this._handleMouseEnterField.bind(this)));

            this._eventHandler.add(this.delegate('mouseleave', '.ddm-field-container', this._handleMouseLeaveField.bind(this)));
          }
        }, {
          key: "created",
          value: function created() {
            if (!this._actionableFieldsContainer) {
              var container = document.createElement('div');
              container.id = ACTIONABLE_FIELDS_CONTAINER;
              document.body.appendChild(container);
              this._actionableFieldsContainer = container;
            }
          }
        }, {
          key: "disposeInternal",
          value: function disposeInternal() {
            _get(_getPrototypeOf(ActionableFields.prototype), "disposeInternal", this).call(this);

            this._eventHandler.removeAllListeners();

            if (this._actionableFieldsContainer) {
              _metalDom.default.exitDocument(this._actionableFieldsContainer);
            }
          }
        }, {
          key: "hasFocusedField",
          value: function hasFocusedField() {
            var focusedField = this.props.focusedField;
            return Object.keys(focusedField).length > 0;
          }
        }, {
          key: "hideActions",
          value: function hideActions(actions) {
            actions.close();
            actions.setState({
              fieldName: null
            });

            if (actions === this.refs.hoveredFieldActions) {
              this.setState({
                hoveredFieldActionsVisible: false
              });
            }
          }
        }, {
          key: "render",
          value: function render() {
            var _this$state = this.state,
                activePage = _this$state.activePage,
                hoveredFieldActionsVisible = _this$state.hoveredFieldActionsVisible;
            var _this$props = this.props,
                fieldActions = _this$props.fieldActions,
                fieldTypes = _this$props.fieldTypes,
                pages = _this$props.pages,
                spritemap = _this$props.spritemap;
            IncrementalDOM.elementOpen("div");
            IncrementalDOM.elementOpenStart(ChildComponent);
            iDOMHelpers.forOwn(this.props, iDOMHelpers.attr);
            IncrementalDOM.elementOpenEnd(ChildComponent);
            IncrementalDOM.elementClose(ChildComponent);
            IncrementalDOM.elementVoid(_FieldActionsDropDown.default, null, null, "activePage", activePage, "events", {
              mouseLeave: this._handleMouseLeaveActions.bind(this)
            }, "fieldTypes", fieldTypes, "items", fieldActions, "pages", pages, "portalElement", this._actionableFieldsContainer, "spritemap", spritemap, "visible", this.hasFocusedField(), "ref", "selectedFieldActions");
            IncrementalDOM.elementVoid(_FieldActionsDropDown.default, null, null, "activePage", activePage, "events", {
              mouseLeave: this._handleMouseLeaveActions.bind(this)
            }, "fieldTypes", fieldTypes, "items", fieldActions, "pages", pages, "portalElement", this._actionableFieldsContainer, "spritemap", spritemap, "visible", hoveredFieldActionsVisible, "ref", "hoveredFieldActions");
            return IncrementalDOM.elementClose("div");
          }
        }, {
          key: "rendered",
          value: function rendered() {
            var _this$props2 = this.props,
                focusedField = _this$props2.focusedField,
                pages = _this$props2.pages;
            var _this$refs = this.refs,
                hoveredFieldActions = _this$refs.hoveredFieldActions,
                selectedFieldActions = _this$refs.selectedFieldActions;

            if (this.hasFocusedField()) {
              var fieldName = focusedField.fieldName;
              this.showActions(selectedFieldActions, fieldName);
            } else {
              this.hideActions(selectedFieldActions);
            }

            var hoveredFieldName = hoveredFieldActions.state.fieldName;

            if (!_dynamicDataMappingFormRenderer.FormSupport.findFieldByFieldName(pages, hoveredFieldName)) {
              this.hideActions(hoveredFieldActions);

              var hoveredNode = this._getHoveredNode();

              if (hoveredNode) {
                hoveredNode.classList.remove(_CSS_HOVERED);
              }
            }
          }
        }, {
          key: "showActions",
          value: function showActions(actions, fieldName) {
            if (actions.state.fieldName !== fieldName) {
              actions.close();
            }

            actions.setState({
              fieldName: fieldName
            });

            if (actions === this.refs.hoveredFieldActions) {
              this.setState({
                hoveredFieldActionsVisible: true
              });
            }
          }
        }, {
          key: "_getClosestParent",
          value: function _getClosestParent(node) {
            return _metalDom.default.closest(node.parentElement, ".ddm-field-container");
          }
        }, {
          key: "_getHoveredNode",
          value: function _getHoveredNode() {
            return this.element.querySelector(".ddm-field-container.".concat(_CSS_HOVERED));
          }
        }, {
          key: "_handleMouseEnterField",
          value: function _handleMouseEnterField(event) {
            var _this$props3 = this.props,
                contentType = _this$props3.contentType,
                pages = _this$props3.pages;
            var delegateTarget = event.delegateTarget;
            var dispatch = this.context.dispatch;
            var fieldName = delegateTarget.dataset.fieldName;
            var _this$refs2 = this.refs,
                hoveredFieldActions = _this$refs2.hoveredFieldActions,
                selectedFieldActions = _this$refs2.selectedFieldActions;
            var activePage = parseInt(_metalDom.default.closest(event.delegateTarget, '[data-ddm-page]').dataset.ddmPage, 10);
            this.setState({
              activePage: activePage
            });

            if (selectedFieldActions.state.fieldName === fieldName || (0, _fieldSupport.isFieldSetChild)(pages, fieldName)) {
              this._handleMouseLeaveField(event);

              return;
            }

            if (contentType && contentType === 'app-builder' && fieldName !== selectedFieldActions.state.fieldName) {
              selectedFieldActions.close();
            }

            var hoveredNode = this._getHoveredNode();

            if (hoveredNode) {
              hoveredNode.classList.remove(_CSS_HOVERED);
              dispatch('fieldBlurred', {});
            }

            delegateTarget.classList.add(_CSS_HOVERED);
            dispatch('fieldHovered', {
              fieldName: fieldName
            });
            this.showActions(hoveredFieldActions, fieldName);
            event.stopPropagation();
          }
        }, {
          key: "_handleMouseLeaveActions",
          value: function _handleMouseLeaveActions(event) {
            var delegateTarget = event.delegateTarget,
                relatedTarget = event.relatedTarget;
            var fieldName = delegateTarget.dataset.fieldName;
            var hoveredFieldActions = this.refs.hoveredFieldActions;
            var pages = this.props.pages;

            if ((0, _fieldSupport.isFieldSetChild)(pages, fieldName)) {
              return;
            }

            var closestRelatedParent = this._getClosestParent(relatedTarget);

            if (closestRelatedParent && closestRelatedParent.dataset.fieldName === hoveredFieldActions.state.fieldName) {
              return;
            }

            this._handleMouseLeaveField({
              delegateTarget: event.container,
              relatedTarget: relatedTarget
            });
          }
        }, {
          key: "_handleMouseLeaveField",
          value: function _handleMouseLeaveField(event) {
            var delegateTarget = event.delegateTarget;
            var _this$refs3 = this.refs,
                hoveredFieldActions = _this$refs3.hoveredFieldActions,
                selectedFieldActions = _this$refs3.selectedFieldActions;

            if (hoveredFieldActions.expanded || !this._hasLeftField(event.relatedTarget)) {
              return;
            }

            delegateTarget.classList.remove(_CSS_HOVERED);
            this.hideActions(hoveredFieldActions);

            this._handleClosestParent({
              delegateTarget: delegateTarget,
              hoveredFieldActions: hoveredFieldActions,
              selectedFieldActions: selectedFieldActions
            });

            if (event.stopPropagation) {
              event.stopPropagation();
            }
          }
        }, {
          key: "_handleClosestParent",
          value: function _handleClosestParent(_ref) {
            var delegateTarget = _ref.delegateTarget,
                hoveredFieldActions = _ref.hoveredFieldActions,
                selectedFieldActions = _ref.selectedFieldActions;
            var pages = this.props.pages;

            var closestParent = this._getClosestParent(delegateTarget);

            if (closestParent) {
              var fieldName = closestParent.dataset.fieldName;

              if (selectedFieldActions.state.fieldName !== fieldName && !(0, _fieldSupport.isFieldSetChild)(pages, fieldName)) {
                closestParent.classList.add(_CSS_HOVERED);
                this.showActions(hoveredFieldActions, fieldName);
              }
            }
          }
        }, {
          key: "_hasLeftField",
          value: function _hasLeftField(relatedTarget) {
            return !_metalDom.default.closest(relatedTarget, '.dropdown-menu,.ddm-field-actions-container');
          }
        }]);

        return ActionableFields;
      }(_metalJsx.default);

      ActionableFields.PROPS = _objectSpread({}, _props.default);
      ActionableFields.STATE = {
        activePage: _metalState.Config.number(),
        hoveredFieldActionsVisible: _metalState.Config.bool().value(false)
      };
      return ActionableFields;
    };

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