UNPKG

@mason-api/javascript-sdk

Version:

Mason component rendering library

188 lines (146 loc) 7.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _lodash = _interopRequireDefault(require("lodash")); var _immutabilityHelper = _interopRequireDefault(require("immutability-helper")); var _utils = require("@mason-api/utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var applyData = function applyData(config, configSubpath, datasources) { return (0, _immutabilityHelper.default)(config, { data: _defineProperty({}, configSubpath, { tree: { $set: _utils.TREE.interpolate(_lodash.default.get(config, "data.".concat(configSubpath, ".tree")), datasources, function (collection) { return collection || []; }, function (value) { return _lodash.default.isNil(value) ? '' : value; }) } }) }); }; var fetchDatasource = function fetchDatasource(datasource) { return _utils.HTTP.makeCall({ data: _utils.OBJECT.keyValueArrayToObject(datasource.queries), headers: _utils.OBJECT.keyValueArrayToObject(datasource.headers), url: datasource.url, verb: _utils.HTTP.GET }); }; var store = { datasources: {}, data: {} }; var processDatasourceEvents = _lodash.default.noop; var _initDatasourceEventListener = function initDatasourceEventListener(getContext) { processDatasourceEvents = function processDatasourceEvents(e) { var form = e.target; var _getContext = getContext(form), callback = _getContext.callback, component = _getContext.component, components = _getContext.components, instance = _getContext.instance, render = _getContext.render; var configSubpath = instance.target.getAttribute('data-config-subpath') || 'default'; var tree = instance.config.data[configSubpath].tree; var node = _utils.TREE.findNodeAtPath(tree, form.getAttribute('data-path')); var datasourceEvents = _lodash.default.reduce(_lodash.default.get(node.p, '_events.success'), function (result, event) { if (event.type === 'datasource') { return _lodash.default.concat(result, { action: event.action, data: e.detail.data, datasourceId: event.id, path: event.path }); } return result; }, []); if (!_lodash.default.isEmpty(datasourceEvents)) { var updatedDatasources = {}; var didFetchData = callback('didFetchData', component.id, instance.target); _lodash.default.forEach(datasourceEvents, function (op) { updatedDatasources[op.datasourceId] = true; var datasource = _lodash.default.get(store.datasources, "".concat(component.projectId, ".").concat(op.datasourceId)); var data = didFetchData(op.data, datasource, component.id); receiveDatasource(op.datasourceId, component.projectId, data, op.path, op.action); _lodash.default.forEach(document.querySelector('mason-canvas'), function (canvas) { var id = canvas.getAttribute('data-id'); var c = _lodash.default.get(components, id); if (c && _utils.COMPONENT.usesDatasource(c, op.datasourceId)) { canvas.setAttribute('data-render', !!component.getAttribute('data-render')); // trigger a re-render using the observer } }); }); } }; _initDatasourceEventListener = _lodash.default.noop; }; var receiveDatasource = function receiveDatasource(datasourceId, projectId, data) { var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ''; var operation = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 'set'; var datasource = _lodash.default.get(store.datasources, "".concat(projectId, ".").concat(datasourceId)); var nextDatasource; switch (operation) { case 'replace': case 'set': nextDatasource = _utils.DATASOURCE.setData(datasource, data, path); break; case 'merge': nextDatasource = _utils.DATASOURCE.mergeData(datasource, data, path); break; case 'remove': nextDatasource = _utils.DATASOURCE.removeData(datasource, data, path); break; default: nextDatasource = _utils.DATASOURCE.setData(datasource, data, path); } store.datasources[projectId][datasourceId] = nextDatasource; }; var _default = { has: _lodash.default.stubTrue, init: function init(getContext) { return function (next) { return function (config) { var _getContext2 = getContext(), components = _getContext2.components, projects = _getContext2.projects; var component = _lodash.default.get(components, config.componentId); var project = _lodash.default.get(projects, component.projectId); if (!_lodash.default.has(store.datasources, project.id)) { store.datasources[project.id] = _objectSpread({}, project.datasources); } _initDatasourceEventListener(getContext); return next(config); }; }; }, render: function render(getContext) { return function (next) { return function (config, configSubpath, target, props) { var _getContext3 = getContext(), callback = _getContext3.callback, components = _getContext3.components; var component = _lodash.default.get(components, config.componentId); target.addEventListener('didReceiveData', processDatasourceEvents); if (!_lodash.default.isEmpty(config.data.default.datasources)) { var datasourcesToFetch = _lodash.default.pick(store.datasources[component.projectId], _lodash.default.map(config.data.default.datasources, 'id')); var before = callback('willFetchData', config.componentId, target); var after = callback('didFetchData', config.componentId, target); Promise.all(_lodash.default.map(datasourcesToFetch, function (datasource) { return fetchDatasource(before(datasource, config.componentId)).then(function (d) { return receiveDatasource(datasource.id, component.projectId, after(d, datasource, config.componentId)); }); })).finally(function () { return next(applyData(config, configSubpath, store.datasources[component.projectId]), configSubpath, target, props); }); } else { next(applyData(config, configSubpath, store.datasources[component.projectId]), configSubpath, target, props); } }; }; } }; exports.default = _default;