client/entity-editor/work-section/work-section.js

"use strict";

require("core-js/modules/es.array.slice.js");
require("core-js/modules/es.array.from.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.symbol.iterator.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.array.sort.js");
require("core-js/modules/web.dom-collections.for-each.js");
require("core-js/modules/es.symbol.js");
require("core-js/modules/es.symbol.description.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.function.name.js");
require("core-js/modules/es.array.find.js");
require("core-js/modules/es.regexp.to-string.js");
var React = _interopRequireWildcard(require("react"));
var _actions = require("./actions");
var _reactBootstrap = require("react-bootstrap");
var _reactSelect = _interopRequireWildcard(require("react-select"));
var _freeSolidSvgIcons = require("@fortawesome/free-solid-svg-icons");
var _reactFontawesome = require("@fortawesome/react-fontawesome");
var _languageField = _interopRequireDefault(require("../common/language-field"));
var _reactRedux = require("react-redux");
var _makeImmutable = _interopRequireDefault(require("../common/make-immutable"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } /*
                                                                                                                                                                                         * Copyright (C) 2016  Ben Ockmore
                                                                                                                                                                                         *
                                                                                                                                                                                         * This program is free software; you can redistribute it and/or modify
                                                                                                                                                                                         * it under the terms of the GNU General Public License as published by
                                                                                                                                                                                         * the Free Software Foundation; either version 2 of the License, or
                                                                                                                                                                                         * (at your option) any later version.
                                                                                                                                                                                         *
                                                                                                                                                                                         * This program is distributed in the hope that it will be useful,
                                                                                                                                                                                         * but WITHOUT ANY WARRANTY; without even the implied warranty of
                                                                                                                                                                                         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                                                                                                                                                                                         * GNU General Public License for more details.
                                                                                                                                                                                         *
                                                                                                                                                                                         * You should have received a copy of the GNU General Public License along
                                                                                                                                                                                         * with this program; if not, write to the Free Software Foundation, Inc.,
                                                                                                                                                                                         * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
                                                                                                                                                                                         */
var ImmutableLanguageField = (0, _makeImmutable.default)(_languageField.default);
function sortWorkTypes(workTypes) {
  var parentId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var sortedArray = [];

  // Filter the array to get all the items with the specified parentId
  var children = workTypes.filter(function (item) {
    return item.parentId === parentId;
  });

  // Sort the children based on the childOrder property
  children.sort(function (a, b) {
    return a.childOrder - b.childOrder;
  });

  // Apply the current depth so we can indent them in the select
  children.forEach(function (type) {
    return type.depth = depth;
  });

  // Recursively sort and append each child's descendants
  var _iterator = _createForOfIteratorHelper(children),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var child = _step.value;
      sortedArray.push(child);
      sortedArray.push.apply(sortedArray, (0, _toConsumableArray2.default)(sortWorkTypes(workTypes, child.id, depth + 1)));
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return sortedArray;
}
function workTypeSelectMenuOption(props) {
  var data = props.data,
    label = props.label;
  var depth = data.depth,
    id = data.id,
    description = data.description;
  var indentationClass;
  if (depth > 0) {
    indentationClass = "margin-left-d".concat(10 * depth);
  }
  return /*#__PURE__*/React.createElement(_reactSelect.components.Option, props, /*#__PURE__*/React.createElement(_reactBootstrap.OverlayTrigger, {
    flip: true,
    delay: 100,
    overlay: /*#__PURE__*/React.createElement(_reactBootstrap.Tooltip, {
      id: "tooltip-".concat(id)
    }, description),
    placement: "bottom"
  }, /*#__PURE__*/React.createElement("div", {
    "aria-label": label,
    className: indentationClass,
    key: id
  }, depth > 0 && /*#__PURE__*/React.createElement("div", {
    className: "hierarchy-arrow"
  }), label)));
}

/**
 * Container component. The WorkSection component contains input fields
 * specific to the work entity. The intention is that this component is
 * rendered as a modular section within the entity editor.
 *
 * @param {Object} props - The properties passed to the component.
 * @param {Array} props.workTypes - The list of possible types for a work.
 * @param {number} props.typeValue - The ID of the type currently selected for
 *        the work.
 * @param {Function} props.onTypeChange - A function to be called when
 *        a different work type is selected.
 * @returns {ReactElement} React element containing the rendered WorkSection.
 */
function WorkSection(_ref) {
  var languageOptions = _ref.languageOptions,
    languageValues = _ref.languageValues,
    typeValue = _ref.typeValue,
    workTypes = _ref.workTypes,
    isUnifiedForm = _ref.isUnifiedForm,
    onLanguagesChange = _ref.onLanguagesChange,
    onTypeChange = _ref.onTypeChange;
  var languageOptionsForDisplay = languageOptions.map(function (language) {
    return {
      frequency: language.frequency,
      label: language.name,
      value: language.id
    };
  });
  var validWorkTypes = workTypes.filter(function (type) {
    return !type.deprecated;
  });
  var workTypesForDisplay = sortWorkTypes(validWorkTypes);
  var selectedTypeOption = workTypesForDisplay.find(function (el) {
    return el.id === typeValue;
  });
  var tooltip = /*#__PURE__*/React.createElement(_reactBootstrap.Tooltip, {
    id: "work-type-tooltip"
  }, "Literary form or structure of the work");
  var heading = /*#__PURE__*/React.createElement("h2", null, " What else do you know about the Work?");
  var lgCol = {
    offset: 3,
    span: 6
  };
  if (isUnifiedForm) {
    lgCol.offset = 0;
  }
  return /*#__PURE__*/React.createElement("div", null, !isUnifiedForm && heading, /*#__PURE__*/React.createElement("p", {
    className: "text-muted"
  }, "All fields optional \u2014 leave something blank if you don\u2019t know it"), /*#__PURE__*/React.createElement(_reactBootstrap.Row, null, /*#__PURE__*/React.createElement(_reactBootstrap.Col, {
    lg: lgCol
  }, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Group, null, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Label, null, "Type", /*#__PURE__*/React.createElement(_reactBootstrap.OverlayTrigger, {
    delay: 50,
    overlay: tooltip
  }, /*#__PURE__*/React.createElement(_reactFontawesome.FontAwesomeIcon, {
    className: "margin-left-0-5",
    icon: _freeSolidSvgIcons.faQuestionCircle
  }))), /*#__PURE__*/React.createElement(_reactSelect.default, {
    isClearable: true,
    classNamePrefix: "react-select",
    components: {
      Option: workTypeSelectMenuOption
    }
    // eslint-disable-next-line react/jsx-no-bind
    ,
    getOptionValue: function getOptionValue(type) {
      return type.id.toString();
    },
    instanceId: "workType",
    options: workTypesForDisplay,
    value: selectedTypeOption,
    onChange: onTypeChange
  }), selectedTypeOption && /*#__PURE__*/React.createElement(_reactBootstrap.Form.Text, {
    className: "text-muted"
  }, /*#__PURE__*/React.createElement(_reactFontawesome.FontAwesomeIcon, {
    fixedWidth: true,
    className: "margin-right-0-3",
    icon: _freeSolidSvgIcons.faArrowTurnUp,
    rotation: 90
  }), selectedTypeOption.description)))), /*#__PURE__*/React.createElement(_reactBootstrap.Row, null, /*#__PURE__*/React.createElement(_reactBootstrap.Col, {
    lg: lgCol
  }, /*#__PURE__*/React.createElement(ImmutableLanguageField, {
    empty: true,
    isMulti: true,
    instanceId: "language",
    options: languageOptionsForDisplay,
    tooltipText: "Main language used for the content of the work",
    value: languageValues,
    onChange: onLanguagesChange
  }))));
}
WorkSection.displayName = 'WorkSection';
WorkSection.defaultProps = {
  isUnifiedForm: false
};
function mapStateToProps(rootState) {
  var state = rootState.get('workSection');
  return {
    languageValues: state.get('languages'),
    typeValue: state.get('type')
  };
}
function mapDispatchToProps(dispatch) {
  return {
    onLanguagesChange: function onLanguagesChange(values) {
      return dispatch((0, _actions.updateLanguages)(values));
    },
    onTypeChange: function onTypeChange(value) {
      return dispatch((0, _actions.updateType)(value && value.id));
    }
  };
}
var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(WorkSection);
exports.default = _default;
//# sourceMappingURL=work-section.js.map