Liferay.Loader.define("dynamic-data-mapping-form-builder@3.0.77/js/components/FormBuilder/withMoveableFields.es", ['module', 'exports', 'require', 'dynamic-data-mapping-form-renderer', 'frontend-js-metal-web$metal-dom', 'frontend-js-metal-web$metal-drag-drop', 'frontend-js-metal-web$metal-jsx', '../../util/dragAndDrop.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 _metalDragDrop = require("frontend-js-metal-web$metal-drag-drop");

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

    var _dragAndDrop = require("../../util/dragAndDrop.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 withMoveableFields = function withMoveableFields(ChildComponent) {
      var MoveableFields = /*#__PURE__*/function (_Component) {
        _inherits(MoveableFields, _Component);

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

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

        _createClass(MoveableFields, [{
          key: "createDragAndDrop",
          value: function createDragAndDrop() {
            this._dragAndDrop = new _metalDragDrop.DragDrop({
              container: this.element,
              sources: '.moveable .ddm-drag:not([data-drag-disabled="true"])',
              targets: '.moveable .ddm-target:not([data-drop-disabled="true"])',
              useShim: false
            });

            this._dragAndDrop.on(_metalDragDrop.DragDrop.Events.END, this._handleDragAndDropEnd.bind(this));

            this._dragAndDrop.on(_metalDragDrop.DragDrop.Events.DRAG, this._handleDragStarted.bind(this));
          }
        }, {
          key: "disposeDragAndDrop",
          value: function disposeDragAndDrop() {
            if (this._dragAndDrop) {
              this._dragAndDrop.dispose();
            }
          }
        }, {
          key: "disposeInternal",
          value: function disposeInternal() {
            _get(_getPrototypeOf(MoveableFields.prototype), "disposeInternal", this).call(this);

            this.disposeDragAndDrop();
          }
        }, {
          key: "render",
          value: function render() {
            IncrementalDOM.elementOpen("div", null, null, "class", "moveable");
            IncrementalDOM.elementOpenStart(ChildComponent);
            iDOMHelpers.forOwn(this.props, iDOMHelpers.attr);
            IncrementalDOM.elementOpenEnd(ChildComponent);
            IncrementalDOM.elementClose(ChildComponent);
            return IncrementalDOM.elementClose("div");
          }
        }, {
          key: "rendered",
          value: function rendered() {
            var _this$props = this.props,
                _this$props$allowNest = _this$props.allowNestedFields,
                allowNestedFields = _this$props$allowNest === void 0 ? true : _this$props$allowNest,
                pages = _this$props.pages;
            (0, _dragAndDrop.disableFieldSetDragSources)(this.element, pages);
            (0, _dragAndDrop.disableFieldSetDropTargets)(this.element, pages);

            if (!allowNestedFields) {
              (0, _dragAndDrop.disableFieldDropTargets)(this.element, pages);
            }

            if (!document.querySelector('.dragging')) {
              this._refreshDragAndDrop();
            }
          }
        }, {
          key: "_getClosestParent",
          value: function _getClosestParent(node) {
            return _metalDom.default.closest(node.parentElement, ".ddm-field-container");
          }
        }, {
          key: "_handleDragAndDropEnd",
          value: function _handleDragAndDropEnd(_ref) {
            var source = _ref.source,
                target = _ref.target;
            var lastParent = document.querySelector('.dragging');

            if (lastParent) {
              lastParent.classList.remove('dragging');
              lastParent.removeAttribute('style');
            }

            if (!target) {
              target = document.querySelector('.ddm-form-builder .ddm-target.targetOver');
            }

            if (target) {
              var sourceFieldNode = _metalDom.default.closest(source, '.ddm-field-container');

              var sourceFieldPage = parseInt(_metalDom.default.closest(source, '[data-ddm-page]').dataset.ddmPage, 10);
              var targetFieldName;

              if (target.classList.contains('ddm-field-container')) {
                targetFieldName = target.dataset.fieldName;
              }

              var sourceFieldName = sourceFieldNode.dataset.fieldName;

              if (sourceFieldName === targetFieldName) {
                return;
              }

              var targetParentFieldName;

              var targetParentFieldNode = this._getClosestParent(target);

              if (targetParentFieldNode) {
                targetParentFieldName = targetParentFieldNode.dataset.fieldName;
              }

              this._handleFieldMoved({
                sourceFieldName: sourceFieldName,
                sourceFieldPage: sourceFieldPage,
                targetFieldName: targetFieldName,
                targetIndexes: _dynamicDataMappingFormRenderer.FormSupport.getIndexes(target.parentElement),
                targetParentFieldName: targetParentFieldName
              });
            }
          }
        }, {
          key: "_handleDragStarted",
          value: function _handleDragStarted(_ref2) {
            var source = _ref2.source;

            var _source$getBoundingCl = source.getBoundingClientRect(),
                height = _source$getBoundingCl.height;

            var parentElement = source.parentElement;
            parentElement.setAttribute('style', "height: ".concat(height, "px !important;"));
            parentElement.classList.add('dragging');
          }
        }, {
          key: "_handleFieldMoved",
          value: function _handleFieldMoved(event) {
            var dispatch = this.context.dispatch;
            dispatch('fieldMoved', event);
          }
        }, {
          key: "_refreshDragAndDrop",
          value: function _refreshDragAndDrop() {
            this.disposeDragAndDrop();
            this.createDragAndDrop();
          }
        }]);

        return MoveableFields;
      }(_metalJsx.default);

      MoveableFields.PROPS = _objectSpread({}, _props.default);
      return MoveableFields;
    };

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