client/entity-editor/helpers.js

"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.attachAttribToRelForDisplay = attachAttribToRelForDisplay;
exports.createRootReducer = createRootReducer;
exports.getEntitySection = getEntitySection;
exports.getEntitySectionMerge = getEntitySectionMerge;
exports.getValidator = getValidator;
exports.isAliasEmpty = isAliasEmpty;
exports.isRequiredDisambiguationEmpty = isRequiredDisambiguationEmpty;
exports.shouldDevToolsBeInjected = shouldDevToolsBeInjected;
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/web.dom-collections.for-each.js");
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _camelCase2 = _interopRequireDefault(require("lodash/camelCase"));
var _authorSection = _interopRequireDefault(require("./author-section/author-section"));
var _authorSectionMerge = _interopRequireDefault(require("./author-section/author-section-merge"));
var _editionGroupSection = _interopRequireDefault(require("./edition-group-section/edition-group-section"));
var _editionGroupSectionMerge = _interopRequireDefault(require("./edition-group-section/edition-group-section-merge"));
var _editionSection = _interopRequireDefault(require("./edition-section/edition-section"));
var _editionSectionMerge = _interopRequireDefault(require("./edition-section/edition-section-merge"));
var _publisherSection = _interopRequireDefault(require("./publisher-section/publisher-section"));
var _publisherSectionMerge = _interopRequireDefault(require("./publisher-section/publisher-section-merge"));
var _seriesSection = _interopRequireDefault(require("./series-section/series-section"));
var _seriesSectionMerge = _interopRequireDefault(require("./series-section/series-section-merge"));
var _workSection = _interopRequireDefault(require("./work-section/work-section"));
var _workSectionMerge = _interopRequireDefault(require("./work-section/work-section-merge"));
var _reducer = _interopRequireDefault(require("./alias-editor/reducer"));
var _reducer2 = _interopRequireDefault(require("./annotation-section/reducer"));
var _reducer3 = _interopRequireDefault(require("./author-credit-editor/reducer"));
var _mergeReducer = _interopRequireDefault(require("./author-credit-editor/merge-reducer"));
var _reducer4 = _interopRequireDefault(require("./author-section/reducer"));
var _reducer5 = _interopRequireDefault(require("./button-bar/reducer"));
var _reduxImmutable = require("redux-immutable");
var _reducer6 = _interopRequireDefault(require("./edition-group-section/reducer"));
var _reducer7 = _interopRequireDefault(require("./edition-section/reducer"));
var _helper = require("./relationship-editor/helper");
var _reducer8 = _interopRequireDefault(require("./identifier-editor/reducer"));
var _reducer9 = _interopRequireDefault(require("./name-section/reducer"));
var _reducer10 = _interopRequireDefault(require("./publisher-section/reducer"));
var _reducer11 = _interopRequireDefault(require("./relationship-editor/reducer"));
var _reducer12 = _interopRequireDefault(require("./series-section/reducer"));
var _reducer13 = _interopRequireDefault(require("./submission-section/reducer"));
var _author = require("./validators/author");
var _edition = require("./validators/edition");
var _editionGroup = require("./validators/edition-group");
var _publisher = require("./validators/publisher");
var _series = require("./validators/series");
var _work = require("./validators/work");
var _reducer14 = _interopRequireDefault(require("./work-section/reducer"));
/*
 * 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.
 */

function isAliasEmpty(nameValue, sortNameValue, languageValue) {
  return !(nameValue.length || sortNameValue.length || languageValue);
}
function isRequiredDisambiguationEmpty(required, disambiguation) {
  return required ? !disambiguation : false;
}
function getEntitySection(entityType) {
  var SECTION_MAP = {
    author: _authorSection.default,
    edition: _editionSection.default,
    editionGroup: _editionGroupSection.default,
    publisher: _publisherSection.default,
    series: _seriesSection.default,
    work: _workSection.default
  };
  return SECTION_MAP[(0, _camelCase2.default)(entityType)];
}
function getEntitySectionMerge(entityType) {
  var SECTION_MAP = {
    author: _authorSectionMerge.default,
    edition: _editionSectionMerge.default,
    editionGroup: _editionGroupSectionMerge.default,
    publisher: _publisherSectionMerge.default,
    series: _seriesSectionMerge.default,
    work: _workSectionMerge.default
  };
  return SECTION_MAP[entityType];
}
function getEntitySectionReducer(entityType) {
  var SECTION_REDUCER_MAP = {
    author: _reducer4.default,
    edition: _reducer7.default,
    editionGroup: _reducer6.default,
    publisher: _reducer10.default,
    series: _reducer12.default,
    work: _reducer14.default
  };
  return SECTION_REDUCER_MAP[entityType];
}
function getValidator(entityType) {
  var VALIDATOR_MAP = {
    author: _author.validateForm,
    edition: _edition.validateForm,
    editionGroup: _editionGroup.validateForm,
    publisher: _publisher.validateForm,
    series: _series.validateForm,
    work: _work.validateForm
  };
  return VALIDATOR_MAP[(0, _camelCase2.default)(entityType)];
}
function getEntitySectionReducerName(entityType) {
  return "".concat(entityType, "Section");
}
function entityHasAuthorCredits(entityType) {
  return entityType === 'edition' || entityType === 'editionGroup';
}
function createRootReducer(entityType) {
  var _reducers;
  var isMerge = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  var entityReducerKey = getEntitySectionReducerName(entityType);
  var entityReducer = getEntitySectionReducer(entityType);
  var reducers = (_reducers = {
    aliasEditor: _reducer.default,
    annotationSection: _reducer2.default,
    buttonBar: _reducer5.default
  }, (0, _defineProperty2.default)(_reducers, entityReducerKey, entityReducer), (0, _defineProperty2.default)(_reducers, "identifierEditor", _reducer8.default), (0, _defineProperty2.default)(_reducers, "nameSection", _reducer9.default), (0, _defineProperty2.default)(_reducers, "relationshipSection", _reducer11.default), (0, _defineProperty2.default)(_reducers, "submissionSection", _reducer13.default), _reducers);
  if (isMerge) {
    reducers.authorCredit = _mergeReducer.default;
  } else if (entityHasAuthorCredits(entityType)) {
    reducers.authorCreditEditor = _reducer3.default;
  }
  return (0, _reduxImmutable.combineReducers)(reducers);
}
function shouldDevToolsBeInjected() {
  return Boolean((typeof window === "undefined" ? "undefined" : (0, _typeof2.default)(window)) === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__);
}

/**
 * Takes an array of relationships and attach the deeply nested
 * relationship attributes to the first level of the relationship object.
 *
 * @param {Array} relationships the array of relationships
 */
function attachAttribToRelForDisplay(relationships) {
  relationships.forEach(function (relationship) {
    relationship.attributes.forEach(function (attribute) {
      var attributeName = (0, _helper.getAttributeName)(attribute.attributeType);
      relationship[attributeName] = attribute.value.textValue;
    });
  });
}
//# sourceMappingURL=helpers.js.map