Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
"use strict";

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 _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

var _Icon = _interopRequireDefault(require("../../visuals/Icon/Icon"));

var _Button = _interopRequireDefault(require("../../controls/Button/Button"));

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

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

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

var closeIcon = function closeIcon(props) {
  return /*#__PURE__*/_react["default"].createElement("svg", props, /*#__PURE__*/_react["default"].createElement("path", {
    d: "M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z",
    fill: "currentColor"
  }));
};

closeIcon.defaultProps = {
  xmlns: "http://www.w3.org/2000/svg",
  viewBox: "0 0 24 24"
};
var escButtonCode = 27;

var Dialog = /*#__PURE__*/function (_PureComponent) {
  _inherits(Dialog, _PureComponent);

  var _super = _createSuper(Dialog);

  function Dialog(props) {
    var _this;

    _classCallCheck(this, Dialog);

    _this = _super.call(this, props);
    _this.dialogRef = null;
    _this.parentNode = null;
    _this.recalculatePosition = _this.recalculatePosition.bind(_assertThisInitialized(_this));
    _this.recalculateTopPosition = _this.recalculateTopPosition.bind(_assertThisInitialized(_this));
    _this.recalculateLeftPosition = _this.recalculateLeftPosition.bind(_assertThisInitialized(_this));
    _this.handleKeyDownDismiss = _this.handleKeyDownDismiss.bind(_assertThisInitialized(_this));
    _this.handleDismiss = _this.handleDismiss.bind(_assertThisInitialized(_this));
    _this.addListeners = _this.addListeners.bind(_assertThisInitialized(_this));
    _this.removeListeners = _this.removeListeners.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(Dialog, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props = this.props,
          id = _this$props.id,
          isVisible = _this$props.isVisible;
      this.parentNode = document.querySelector("[data-source=\"".concat(id, "\"]"));
      this.recalculatePosition();

      if (isVisible) {
        this.addListeners();
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(previousProps) {
      var _this$props2 = this.props,
          isVisible = _this$props2.isVisible,
          offset = _this$props2.offset,
          horizontalAlign = _this$props2.horizontalAlign,
          verticalAlign = _this$props2.verticalAlign;
      var prevIsVisible = previousProps.isVisible,
          prevOffset = previousProps.offset,
          prevHorizontalAlign = previousProps.horizontalAlign,
          prevVerticalAlign = previousProps.verticalAlign;

      if (isVisible && !prevIsVisible || horizontalAlign !== prevHorizontalAlign || verticalAlign !== prevVerticalAlign || !(0, _isEqual["default"])(offset, prevOffset)) {
        this.recalculatePosition();
      }

      if (isVisible && !prevIsVisible) {
        this.addListeners();
      }

      if (!isVisible && prevIsVisible) {
        this.removeListeners();
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.removeListeners();
    }
  }, {
    key: "addListeners",
    value: function addListeners() {
      var _this2 = this;

      var disableCloseOnScroll = this.props.disableCloseOnScroll; // need for skipping opening click

      setTimeout(function () {
        if (!disableCloseOnScroll) {
          window.addEventListener('scroll', _this2.handleDismiss, true);
        }

        window.addEventListener('click', _this2.handleDismiss);
        window.addEventListener('resize', _this2.handleDismiss);
        window.addEventListener('keydown', _this2.handleKeyDownDismiss);
      }, 0);
    }
  }, {
    key: "removeListeners",
    value: function removeListeners() {
      var disableCloseOnScroll = this.props.disableCloseOnScroll;

      if (!disableCloseOnScroll) {
        window.removeEventListener('scroll', this.handleDismiss, true);
      }

      window.removeEventListener('resize', this.handleDismiss);
      window.removeEventListener('click', this.handleDismiss);
      window.removeEventListener('keydown', this.handleKeyDownDismiss);
    }
  }, {
    key: "recalculateTopPosition",
    value: function recalculateTopPosition(parentRect, dialogRect, currentOffset) {
      var verticalAlign = this.props.verticalAlign;

      switch (verticalAlign) {
        case Dialog.alignType.bottom:
          {
            this.dialogRef.style.top = "".concat(parentRect.bottom - dialogRect.height + currentOffset.bottom, "px");
            break;
          }

        case Dialog.alignType.middle:
          {
            this.dialogRef.style.top = "".concat(parentRect.top + parentRect.height / 2 - dialogRect.height / 2 + currentOffset.middle, "px");
            break;
          }

        default:
          {
            this.dialogRef.style.top = "".concat(parentRect.top + currentOffset.top, "px");
          }
      }
    }
  }, {
    key: "recalculateLeftPosition",
    value: function recalculateLeftPosition(parentRect, dialogRect, currentOffset) {
      var horizontalAlign = this.props.horizontalAlign;

      switch (horizontalAlign) {
        case Dialog.alignType.left:
          {
            this.dialogRef.style.left = "".concat(parentRect.left - dialogRect.width + currentOffset.left, "px");
            break;
          }

        case Dialog.alignType.center:
          {
            this.dialogRef.style.left = "".concat(parentRect.left + parentRect.width / 2 - dialogRect.width / 2 + currentOffset.center, "px");
            break;
          }

        default:
          {
            this.dialogRef.style.left = "".concat(parentRect.right + currentOffset.right, "px");
          }
      }
    }
  }, {
    key: "recalculatePosition",
    value: function recalculatePosition() {
      var _this$props3 = this.props,
          isVisible = _this$props3.isVisible,
          offset = _this$props3.offset,
          onRecalculate = _this$props3.onRecalculate;

      var currentOffset = _objectSpread(_objectSpread({}, Dialog.defaultOffset), offset);

      if (!isVisible || !this.parentNode || !this.dialogRef) return;

      if (onRecalculate) {
        onRecalculate(this.dialogRef, this.parentNode);
      } else {
        var parentRect = this.parentNode.getBoundingClientRect();
        var dialogRect = this.dialogRef.getBoundingClientRect();
        this.recalculateTopPosition(parentRect, dialogRect, currentOffset);
        this.recalculateLeftPosition(parentRect, dialogRect, currentOffset);
      }
    }
  }, {
    key: "handleKeyDownDismiss",
    value: function handleKeyDownDismiss(e) {
      if (e.keyCode === escButtonCode) {
        this.handleDismiss();
      }
    }
  }, {
    key: "handleDismiss",
    value: function handleDismiss() {
      var onDismiss = this.props.onDismiss;
      onDismiss();
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var _this$props4 = this.props,
          children = _this$props4.children,
          isVisible = _this$props4.isVisible,
          isDismissible = _this$props4.isDismissible,
          onDismiss = _this$props4.onDismiss,
          className = _this$props4.className,
          title = _this$props4.title;
      return isVisible ?
      /*#__PURE__*/
      // need for implementation outside click
      // eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-noninteractive-element-interactions
      _react["default"].createElement("div", {
        role: "dialog",
        className: "Dialog ".concat(className),
        "aria-label": title,
        ref: function ref(_ref) {
          _this3.dialogRef = _ref;
        },
        onClick: function onClick(e) {
          e.stopPropagation();
        }
      }, isDismissible && /*#__PURE__*/_react["default"].createElement(_Button["default"], {
        variant: "linkDark",
        className: "Dialog__close-button",
        onClick: onDismiss,
        "aria-label": "Close button"
      }, /*#__PURE__*/_react["default"].createElement(_Icon["default"], {
        className: "Dialog__close-icon",
        icon: closeIcon
      })), children) : null;
    }
  }]);

  return Dialog;
}(_react.PureComponent);

Dialog.defaultOffset = {
  left: 0,
  right: 0,
  top: 0,
  bottom: 0,
  center: 0,
  middle: 0
};
Dialog.alignType = {
  left: 'left',
  right: 'right',
  top: 'top',
  bottom: 'bottom',
  center: 'center',
  middle: 'middle'
};
Dialog.propTypes = {
  /** Dialog content */
  children: _propTypes["default"].node.isRequired,

  /** Control flag for displaying dialog component */
  isVisible: _propTypes["default"].bool.isRequired,

  /** Callback was fired if user clicks on close button or scroll/resize page */
  onDismiss: _propTypes["default"].func,

  /** Control flag for displaying close button */
  isDismissible: _propTypes["default"].bool,

  /** An id that was placed in data-source parent attribute */
  id: _propTypes["default"].string.isRequired,

  /**  A11y-enabled modal title */
  title: _propTypes["default"].string.isRequired,

  /** Align value */
  horizontalAlign: _propTypes["default"].oneOf([Dialog.alignType.left, Dialog.alignType.right, Dialog.alignType.center]),

  /** Align value */
  verticalAlign: _propTypes["default"].oneOf([Dialog.alignType.top, Dialog.alignType.bottom, Dialog.alignType.middle]),

  /** Offset values for aligning */
  offset: _propTypes["default"].shape({
    left: _propTypes["default"].number,
    right: _propTypes["default"].number,
    top: _propTypes["default"].number,
    bottom: _propTypes["default"].number,
    center: _propTypes["default"].number,
    middle: _propTypes["default"].number
  }),

  /** Wrapper class for customization */
  className: _propTypes["default"].string,
  onRecalculate: _propTypes["default"].func,

  /** Disables dialog closing on scroll so we can scroll on dialog if necessary */
  disableCloseOnScroll: _propTypes["default"].bool
};
Dialog.defaultProps = {
  isDismissible: false,
  onDismiss: function onDismiss() {},
  className: '',
  offset: Dialog.defaultOffset,
  horizontalAlign: Dialog.alignType.right,
  verticalAlign: Dialog.alignType.top,
  onRecalculate: null,
  disableCloseOnScroll: false
};
var _default = Dialog;
exports["default"] = _default;