@aibsweb/faceted-search
Version:
A generalized faceted search application.
213 lines (179 loc) • 7.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _snarkdown = _interopRequireDefault(require("snarkdown"));
var _get = _interopRequireDefault(require("lodash/get"));
var _lodash = require("lodash");
var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));
var _uniqueId = _interopRequireDefault(require("lodash/uniqueId"));
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 _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; }
/**
* This class contains the static Data type handlers for the data processing class. This is used by the
* data-tables module to format data cells in tables.
*/
var DataHandlers =
/*#__PURE__*/
function () {
function DataHandlers() {
_classCallCheck(this, DataHandlers);
}
_createClass(DataHandlers, null, [{
key: "handleMarkdown",
/**
* Handles markdown cells, this simply runs the cell text through snarkdown and returns the result.
* @param {object} schema The schema for this cell
* @param {object} row The row this cell is a member of
*/
value: function handleMarkdown(schema, row) {
var result = (0, _snarkdown["default"])((0, _get["default"])(row, schema['key']) || '');
return decodeURIComponent(result);
}
/**
* This handles the string data type. Strings are returned pretty much as is.
* @param {object} schema The schema for this cell
* @param {object} row The row this cell is a member of
*/
}, {
key: "handleString",
value: function handleString(schema, row) {
var result = (0, _get["default"])(row, schema['key']) || '';
var decodedResult = decodeURIComponent(result);
return _react["default"].createElement("div", {
className: "data-table-cell__string",
title: decodedResult
}, decodedResult);
}
/**
* For multiple strings held in an array.
* @param {object} schema The schema for this cell
* @param {object} row The row this cell is a member of
*/
}, {
key: "handleMultiString",
value: function handleMultiString(schema, row) {
var data = (0, _get["default"])(row, schema['key']);
if ((0, _lodash.isNil)(data)) {
return [];
}
return _react["default"].createElement("p", {
key: (0, _uniqueId["default"])('string'),
className: "data-table-cell__list"
}, data.map(function (stringData) {
var stringText = (0, _get["default"])(stringData, schema['text-key']) || '';
return decodeURIComponent(stringText);
}).join(', '));
}
/**
* Handles links, this grabs link text from one of the row cells and the url from another row cell and returns the results
* @param {object} schema The schema for this cell
* @param {object} row The row this cell is a member of
*/
}, {
key: "handleLink",
value: function handleLink(schema, row) {
var link = decodeURIComponent((0, _get["default"])(row, schema['url-key'], ''));
var text = decodeURIComponent((0, _get["default"])(row, schema['text-key'], '')); // If either of the link properties are missing return an empty string instead of a broken anchor tag.
if ((0, _isEmpty["default"])(link) || (0, _isEmpty["default"])(text)) {
return '';
}
return _react["default"].createElement("a", {
href: link
}, text);
}
/**
* For multiple links in an array
* @param {object} schema The schema for this cell
* @param {object} row row data
*/
}, {
key: "handleMultiLink",
value: function handleMultiLink(schema, row) {
var data = (0, _get["default"])(row, schema['key']);
if ((0, _lodash.isNil)(data)) {
return [];
}
return data.map(function (linkData) {
var linkUrl = (0, _get["default"])(linkData, schema['url-key']) || '';
var linkText = (0, _get["default"])(linkData, schema['text-key']) || '';
return _react["default"].createElement("p", {
key: (0, _uniqueId["default"])('link')
}, _react["default"].createElement("a", {
href: decodeURIComponent(linkUrl)
}, decodeURIComponent(linkText)));
});
}
/**
* Handles multiple links that require an icon and some display text provided by the schema
* @param {object} schema the schema for this column
* @param {object} row the row data
*/
}, {
key: "handleIconLink",
value: function handleIconLink(schema, row) {
var data = (0, _get["default"])(row, schema['key']);
return data.map(function (iconData) {
var url = decodeURIComponent((0, _get["default"])(iconData, schema['url-key'], ''));
var icon = decodeURIComponent((0, _get["default"])(iconData, schema['icon-key'], ''));
var text = decodeURIComponent((0, _get["default"])(iconData, schema['text-key'], ''));
var alt = decodeURIComponent((0, _get["default"])(iconData, schema['alt-key'], ''));
if ((0, _isEmpty["default"])(icon) && (0, _isEmpty["default"])(text) || (0, _isEmpty["default"])(url)) {
return '';
}
return _react["default"].createElement("div", {
className: "icon-link",
key: (0, _uniqueId["default"])('icon')
}, _react["default"].createElement("a", {
key: url,
href: url,
className: "icon-link__a",
alt: alt,
target: "_blank"
}, icon && _react["default"].createElement("i", {
className: "fa ".concat(icon),
"aria-hidden": true
}), text));
});
}
/**
* Handles a relative path link. It will concat some key text to the path, OR it will replace the special characters "{}" in the relative path string.
* @param {object} schema the schema for this column
* @param {object} row the row data
*/
}, {
key: "handleRelativeLink",
value: function handleRelativeLink(schema, row) {
var text = (0, _get["default"])(row, schema['key']);
var relativePath = schema['relative-path'];
if (!text || !relativePath) {
return '';
}
var urlText = decodeURIComponent(text).trim().replace(/\s+/g, '-');
var url;
if (/{}/.test(relativePath)) {
url = relativePath.replace(/{}/, urlText);
} else if (/\/$/.test(relativePath)) {
url = "".concat(relativePath).concat(urlText, "/");
} else {
url = "".concat(relativePath, "/").concat(urlText, "/");
}
return _react["default"].createElement("a", {
href: url.toLowerCase()
}, text);
}
}, {
key: "handleNumber",
value: function handleNumber(schema, row) {
return _react["default"].createElement("div", {
className: "data-table-cell__number"
}, ((0, _get["default"])(row, schema['key']) || 'N/A').toString());
}
}]);
return DataHandlers;
}();
exports["default"] = DataHandlers;