'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _RecordButtonBar = require('./RecordButtonBar');

var _RecordButtonBar2 = _interopRequireDefault(_RecordButtonBar);

var _RecordFormSelector = require('./RecordFormSelector');

var _RecordFormSelector2 = _interopRequireDefault(_RecordFormSelector);

var _RecordHistory = require('./RecordHistory');

var _RecordHistory2 = _interopRequireDefault(_RecordHistory);

var _RecordHeader = require('../../../styles/cspace-ui/RecordHeader.css');

var _RecordHeader2 = _interopRequireDefault(_RecordHeader);

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* global window */

var propTypes = {
  config: _propTypes2.default.object,
  data: _propTypes2.default.instanceOf(_immutable2.default.Map),
  dockTop: _propTypes2.default.number,
  formName: _propTypes2.default.string,
  isCloneable: _propTypes2.default.bool,
  isDeletable: _propTypes2.default.bool,
  isDeprecated: _propTypes2.default.bool,
  isModified: _propTypes2.default.bool,
  isReadPending: _propTypes2.default.bool,
  isSavePending: _propTypes2.default.bool,
  readOnly: _propTypes2.default.bool,
  recordType: _propTypes2.default.string.isRequired,
  showDeprecationButtons: _propTypes2.default.bool,
  validationErrors: _propTypes2.default.instanceOf(_immutable2.default.Map),
  onCloneButtonClick: _propTypes2.default.func,
  onCommit: _propTypes2.default.func,
  onDeprecateButtonClick: _propTypes2.default.func,
  onDeleteButtonClick: _propTypes2.default.func,
  onRevertButtonClick: _propTypes2.default.func,
  onSaveButtonClick: _propTypes2.default.func,
  onSaveButtonErrorBadgeClick: _propTypes2.default.func,
  onUndeprecateButtonClick: _propTypes2.default.func
};

var RecordHeader = function (_Component) {
  _inherits(RecordHeader, _Component);

  function RecordHeader(props) {
    _classCallCheck(this, RecordHeader);

    var _this = _possibleConstructorReturn(this, (RecordHeader.__proto__ || Object.getPrototypeOf(RecordHeader)).call(this, props));

    _this.setDomNode = _this.setDomNode.bind(_this);
    _this.handleScroll = _this.handleScroll.bind(_this);

    _this.state = {
      docked: false
    };
    return _this;
  }

  _createClass(RecordHeader, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      window.addEventListener('scroll', this.handleScroll, false);
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      window.removeEventListener('scroll', this.handleScroll, false);
    }
  }, {
    key: 'setDomNode',
    value: function setDomNode(ref) {
      this.domNode = ref;
    }
  }, {
    key: 'handleScroll',
    value: function handleScroll() {
      var node = this.domNode;

      if (!node) return;

      var dockTop = this.props.dockTop;


      if (this.state.docked) {
        if (window.scrollY + dockTop < node.offsetTop) {
          this.setState({
            docked: false
          });
        }
      } else if (window.scrollY + dockTop >= node.offsetTop) {
        this.setState({
          docked: true
        });
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          config = _props.config,
          data = _props.data,
          dockTop = _props.dockTop,
          formName = _props.formName,
          isCloneable = _props.isCloneable,
          isDeletable = _props.isDeletable,
          isDeprecated = _props.isDeprecated,
          isModified = _props.isModified,
          isReadPending = _props.isReadPending,
          isSavePending = _props.isSavePending,
          readOnly = _props.readOnly,
          recordType = _props.recordType,
          showDeprecationButtons = _props.showDeprecationButtons,
          validationErrors = _props.validationErrors,
          onCloneButtonClick = _props.onCloneButtonClick,
          onCommit = _props.onCommit,
          onDeprecateButtonClick = _props.onDeprecateButtonClick,
          onDeleteButtonClick = _props.onDeleteButtonClick,
          onRevertButtonClick = _props.onRevertButtonClick,
          onSaveButtonClick = _props.onSaveButtonClick,
          onSaveButtonErrorBadgeClick = _props.onSaveButtonErrorBadgeClick,
          onUndeprecateButtonClick = _props.onUndeprecateButtonClick;
      var docked = this.state.docked;


      var className = docked ? _RecordHeader2.default.docked : _RecordHeader2.default.common;

      var inlineStyle = docked ? { height: this.domNode.offsetHeight } : {};

      var innerInlineStyle = docked ? { top: dockTop || 0 } : {};

      return _react2.default.createElement(
        'header',
        {
          className: className,
          style: inlineStyle,
          ref: this.setDomNode
        },
        _react2.default.createElement(
          'div',
          { style: innerInlineStyle },
          _react2.default.createElement(_RecordButtonBar2.default, {
            isCloneable: isCloneable,
            isDeletable: isDeletable,
            isDeprecated: isDeprecated,
            isModified: isModified,
            isReadPending: isReadPending,
            isSavePending: isSavePending,
            readOnly: readOnly,
            showDeprecationButtons: showDeprecationButtons,
            validationErrors: validationErrors,
            onCloneButtonClick: onCloneButtonClick,
            onDeprecateButtonClick: onDeprecateButtonClick,
            onDeleteButtonClick: onDeleteButtonClick,
            onRevertButtonClick: onRevertButtonClick,
            onSaveButtonClick: onSaveButtonClick,
            onSaveButtonErrorBadgeClick: onSaveButtonErrorBadgeClick,
            onUndeprecateButtonClick: onUndeprecateButtonClick
          }),
          _react2.default.createElement(_RecordFormSelector2.default, {
            config: config,
            formName: formName,
            recordType: recordType,
            onCommit: onCommit
          }),
          _react2.default.createElement(_RecordHistory2.default, {
            data: data,
            isModified: isModified,
            isSavePending: isSavePending
          })
        )
      );
    }
  }]);

  return RecordHeader;
}(_react.Component);

exports.default = RecordHeader;


RecordHeader.propTypes = propTypes;