server/helpers/render.js

"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.array.concat.js");
var _isString2 = _interopRequireDefault(require("lodash/isString"));
var _get2 = _interopRequireDefault(require("lodash/get"));
var _entity = require("../../client/helpers/entity");
var _utils = require("../../common/helpers/utils");
/*
 * Copyright (C) 2015  Ben Ockmore
 *               2015  Sean Burke
 *               2018  Eshan Singh
 *
 * 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.
 */

/**
 * @typedef {Object} EntityInRelationship
 * @property {string} bbid
 * @property {?Object} defaultAlias
 * @property {string} defaultAlias.name
 * @property {string} type
 */

/**
 * @typedef {Object} Relationship
 * @property {EntityInRelationship} source
 * @property {EntityInRelationship} target
 * @property {Object} type
 * @property {string} type.displayTemplate
 */
/**
 * Returns the markup corresponding to the given entity relationship.
 * This markup is defined by a Handlebars template in
 * relationship.type.displayTemplate.
 * This function is used, for example, to render
 * relationships on entity display pages.
 * @param {Relationship} relationship - Relationship object.
 * @returns {string} - Rendered HTML string.
 */
function renderRelationship(relationship) {
  var inputsInvalid = !relationship.source || !relationship.target || !(0, _isString2.default)((0, _get2.default)(relationship, 'type.linkPhrase'));
  if (inputsInvalid) {
    throw new TypeError("Invalid inputs to renderRelationship:\n".concat(JSON.stringify(relationship, null, 2)));
  }
  function template(data) {
    return "".concat(data.entities[0], " ") + "".concat(relationship.type.linkPhrase, " ") + "".concat(data.entities[1]);
  }
  var data = {
    entities: [relationship.source, relationship.target].map(function (entity) {
      // Linkify source and target based on default alias
      var name = (0, _get2.default)(entity, 'defaultAlias.name', '(unnamed)');
      var entityIcon = "<i class=\"fa fa-".concat(_entity.ENTITY_TYPE_ICONS[entity.type], " margin-right-0-5\"></i>");
      return "".concat(entityIcon, "<a href=\"").concat((0, _utils.getEntityLink)(entity), "\">").concat(name, "</a>");
    })
  };
  return template(data);
}
var _default = renderRelationship;
exports.default = _default;
//# sourceMappingURL=render.js.map