@kineticdata/react
Version:
A React library for the Kinetic Platform
417 lines (409 loc) • 16.2 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
K: true,
bundle: true,
splitTeamName: true,
partitionListBy: true,
generateKey: true,
slugify: true,
buildDefinitionId: true,
buildCodeEditorBindings: true,
buildAgentPath: true,
handleFormErrors: true,
INDEX_STATIC_PARTS: true,
TIMELINES: true,
MAX_PART_LENGTH: true
};
exports.splitTeamName = exports.slugify = exports.partitionListBy = exports.handleFormErrors = exports.generateKey = exports.bundle = exports.buildDefinitionId = exports.buildCodeEditorBindings = exports.buildAgentPath = exports.TIMELINES = exports.MAX_PART_LENGTH = exports.K = exports.INDEX_STATIC_PARTS = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/toConsumableArray"));
var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/toArray"));
var _immutable = require("immutable");
var _lodashEs = require("lodash-es");
var _SearchBuilder = require("./SearchBuilder");
Object.keys(_SearchBuilder).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _SearchBuilder[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _SearchBuilder[key];
}
});
});
var K = exports.K = typeof window !== "undefined" ? window.K : function () {};
// platform consoles and other apps may not load app head content that
// instantiates the bundle object used by some of these helpers so we create a
// mock version here
var bundle = exports.bundle = typeof window !== "undefined" && window.bundle ? window.bundle : {
apiLocation: function apiLocation() {
return "".concat(process.env.REACT_APP_API_HOST || '', "/app/api/v1");
},
spaceLocation: function spaceLocation() {
return process.env.REACT_APP_API_HOST || '';
},
kappSlug: function kappSlug() {
return '';
}
};
// when running the bundle in dev mode there will already be a bundle object but
// we want to prefix the locations with the REACT_APP_API_HOST value
if (typeof window !== "undefined" && window.bundle && process.env.REACT_APP_API_HOST) {
var spaceLocation = window.bundle.spaceLocation();
var apiLocation = window.bundle.apiLocation();
window.bundle.spaceLocation = function () {
return process.env.REACT_APP_API_HOST + spaceLocation;
};
window.bundle.apiLocation = function () {
return process.env.REACT_APP_API_HOST + apiLocation;
};
}
var splitTeamName = exports.splitTeamName = function splitTeamName(team) {
var _team$get$split$rever = team.get('name').split('::').reverse(),
_team$get$split$rever2 = (0, _toArray2["default"])(_team$get$split$rever),
local = _team$get$split$rever2[0],
parents = _team$get$split$rever2.slice(1);
return [parents.reverse().join('::'), local];
};
// Applies fn to each value in list, splitting it into a new list each time fn
// returns a different value.
var partitionListBy = exports.partitionListBy = function partitionListBy(fn, list) {
return list.isEmpty() ? (0, _immutable.List)() : list.rest().reduce(function (reduction, current) {
return fn(reduction.last().last(), current) ? reduction.push((0, _immutable.List)([current])) : reduction.update(reduction.size - 1, function (list) {
return list.push(current);
});
}, (0, _immutable.List)([(0, _immutable.List)([list.first()])]));
};
var generateKey = exports.generateKey = function generateKey() {
var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 6;
var result = '';
while (result.length < length) {
result = result + Math.floor(Math.random() * 16).toString(16).toUpperCase();
}
return result;
};
var slugify = exports.slugify = function slugify(text) {
return text.trim()
// Convert uppercase to lowercase
.toLowerCase()
// Replace spaces with -
.replace(/\s+/g, '-')
// Remove all non-word chars
.replace(/[^A-Za-z0-9\u0080-\u00FF-]+/g, '');
};
var buildDefinitionId = exports.buildDefinitionId = function buildDefinitionId(text) {
return text.trim()
// Convert uppercase to lowercase
.toLowerCase()
// Replace spaces with _
.replace(/\s+/g, '_')
// Remove unwanted chars
.replace(/[^A-Za-z0-9_]+/g, '');
};
var STATIC_FORM_BINDINGS = [{
label: 'name',
boost: 20
}, {
label: 'slug',
boost: 20
}];
var STATIC_FORM_BINDINGS_DETAILED = [].concat(STATIC_FORM_BINDINGS, [{
label: 'anonymous'
}, {
label: 'description'
}, {
label: 'status'
}, {
label: 'type'
}, {
label: 'createdAt',
boost: -20
}, {
label: 'createdBy',
boost: -20
}, {
label: 'updatedAt',
boost: -20
}, {
label: 'updatedBy',
boost: -20
}]);
var STATIC_KAPP_BINDINGS = [{
label: 'name',
boost: 20
}, {
label: 'slug',
boost: 20
}];
var STATIC_SPACE_BINDINGS = [{
label: 'name',
boost: 20
}, {
label: 'slug',
boost: 20
}];
var STATIC_SUBMISSION_BINDINGS = [{
label: 'id',
boost: 20
}, {
label: 'coreState'
}, {
label: 'currentPage'
}, {
label: 'handle'
}, {
label: 'type'
}, {
label: 'closedAt',
boost: -20
}, {
label: 'closedBy',
boost: -20
}, {
label: 'createdAt',
boost: -20
}, {
label: 'createdBy',
boost: -20
}, {
label: 'submittedAt',
boost: -20
}, {
label: 'submittedBy',
boost: -20
}, {
label: 'updatedAt',
boost: -20
}, {
label: 'updatedBy',
boost: -20
}];
var STATIC_SUBMISSION_BINDINGS_DETAILED = [].concat(STATIC_SUBMISSION_BINDINGS, [{
label: 'sessionToken'
}]);
var STATIC_IDENTITY_BINDINGS = [{
label: 'username',
boost: 20
}, {
label: 'displayName',
boost: 10
}, {
label: 'email',
boost: 10
}, {
label: 'anonymous'
}, {
label: 'authenticated'
}, {
label: 'sessionToken'
}, {
label: 'spaceAdmin'
}];
var STATIC_FILE_BINDINGS = [{
label: 'folder'
}, {
label: 'name'
}, {
label: 'path'
}];
var STATIC_TEAM_BINDINGS = [{
label: 'name'
}, {
label: 'slug'
}];
var STATIC_USER_BINDINGS = [{
label: 'username',
boost: 20
}, {
label: 'displayName',
boost: 10
}, {
label: 'email',
boost: 10
}, {
label: 'spaceAdmin'
}];
/**
* Builds code editor bindings from the provided options
*
* @param {object} options
*
* @param {object} [options.values] Options for values bindings
* @param {string} [options.values.label=values]
* @param {object[]} options.values.data
* @param {object} options.values.data[].name
* @param {object} options.values.data[].renderType
*
* @param {object} [options.form] Options for form bindings
* @param {string} [options.form.label=form]
* @param {boolean} [options.form.detailed]
* @param {object[]} [options.form.staticBindings]
* @param {object[]} [options.form.attributeDefinitions]
*
* @param {object} [options.kapp] Options for kapp bindings
* @param {string} [options.kapp.label=kapp]
* @param {object[]} [options.kapp.staticBindings]
* @param {object[]} [options.kapp.attributeDefinitions]
*
* @param {object} [options.space] Options for space bindings
* @param {string} [options.space.label=space]
* @param {object[]} [options.space.staticBindings]
* @param {object[]} [options.space.attributeDefinitions]
*
* @param {object} [options.submission] Options for submission bindings
* @param {string} [options.submission.label=submission]
* @param {boolean} [options.submission.detailed]
* @param {object[]} [options.submission.staticBindings]
*
* @param {object} [options.identity] Options for identity bindings
* @param {string} [options.identity.label=identity]
* @param {object[]} [options.identity.staticBindings]
* @param {object[]} [options.identity.attributeDefinitions]
* @param {object[]} [options.identity.profileAttributeDefinitions]
*
* @param {object} [options.user] Options for user bindings
* @param {string} [options.user.label=user]
* @param {object[]} [options.user.staticBindings]
* @param {object[]} [options.user.attributeDefinitions]
* @param {object[]} [options.user.profileAttributeDefinitions]
*
* @param {object} [options.team] Options for team bindings
* @param {string} [options.team.label=team]
* @param {object[]} [options.team.staticBindings]
* @param {object[]} [options.team.attributeDefinitions]
*
* @param {object} [options.file] Options for file resource bindings
* @param {string} [options.file.label=file]
* @param {object[]} [options.file.staticBindings]
*
* @param {object} [options.resources] Options for bridged resource bindings
* @param {string} [options.resources.label=resources]
* @param {string} [options.resources.bridgedResourceName]
* @param {string[]} [options.resources.attributes]
*
* @param {object} [options.integration] Options for integration bindings
* @param {string} [options.integration.label=integration]
* @param {string} [options.integration.integrationName]
* @param {string[]} [options.integration.outputs]
*
* @param {object[]} [options.staticBindings] Additional bindings to include
*
* @returns The bindings object to be used by the CodeEditor component.
*/
var buildCodeEditorBindings = exports.buildCodeEditorBindings = function buildCodeEditorBindings() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
values = _ref.values,
form = _ref.form,
kapp = _ref.kapp,
space = _ref.space,
submission = _ref.submission,
identity = _ref.identity,
user = _ref.user,
team = _ref.team,
file = _ref.file,
resources = _ref.resources,
integration = _ref.integration,
staticBindings = _ref.staticBindings;
return (0, _immutable.List)([values && buildValuesBindings(values.label || 'values', (0, _immutable.List)(values.data)), form && buildObjectWithAttributesBindings(form.label || 'form', (0, _immutable.List)(form.staticBindings).concat(form.detailed ? STATIC_FORM_BINDINGS_DETAILED : STATIC_FORM_BINDINGS), (0, _immutable.List)(form.attributeDefinitions)), kapp && buildObjectWithAttributesBindings(kapp.label || 'kapp', (0, _immutable.List)(kapp.staticBindings).concat(STATIC_KAPP_BINDINGS), (0, _immutable.List)(kapp.attributeDefinitions)), space && buildObjectWithAttributesBindings(space.label || 'space', (0, _immutable.List)(space.staticBindings).concat(STATIC_SPACE_BINDINGS), (0, _immutable.List)(space.attributeDefinitions)), submission && buildObjectWithAttributesBindings(submission.label || 'submission', (0, _immutable.List)(submission.staticBindings).concat(submission.detailed ? STATIC_SUBMISSION_BINDINGS_DETAILED : STATIC_SUBMISSION_BINDINGS)), identity && buildObjectWithAttributesBindings(identity.label || 'identity', (0, _immutable.List)(identity.staticBindings).concat(STATIC_IDENTITY_BINDINGS), (0, _immutable.List)(identity.attributeDefinitions), (0, _immutable.List)(identity.profileAttributeDefinitions)), user && buildObjectWithAttributesBindings(user.label || 'user', (0, _immutable.List)(user.staticBindings).concat(STATIC_USER_BINDINGS), (0, _immutable.List)(user.attributeDefinitions), (0, _immutable.List)(user.profileAttributeDefinitions)), team && buildObjectWithAttributesBindings(team.label || 'team', (0, _immutable.List)(team.staticBindings).concat(STATIC_TEAM_BINDINGS), (0, _immutable.List)(team.attributeDefinitions)), file && buildObjectWithAttributesBindings(file.label || 'file', (0, _immutable.List)(file.staticBindings).concat(STATIC_FILE_BINDINGS), (0, _immutable.List)(file.attributeDefinitions)), resources && buildResourcesBindings(resources.label || 'resources', resources.bridgedResourceName, resources.attributes), integration && buildIntegrationBindings(integration.label || 'integration', integration.integrationName, integration.integrationProperty, integration.outputs)].concat((0, _toConsumableArray2["default"])((staticBindings || []).map(function (binding) {
return (0, _immutable.fromJS)(binding);
}))).filter(Boolean));
};
// Builds code editor binding for kinetic objects with optional attribute
// definitions
var buildObjectWithAttributesBindings = function buildObjectWithAttributesBindings(label) {
var staticBindings = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (0, _immutable.List)();
var attributeDefinitions = arguments.length > 2 ? arguments[2] : undefined;
var profileAttributeDefinitions = arguments.length > 3 ? arguments[3] : undefined;
return (0, _immutable.Map)({
label: label,
type: 'function',
children: staticBindings.concat([attributeDefinitions && (0, _immutable.Map)({
label: 'attribute',
siblings: buildAttributeBindings(attributeDefinitions),
detail: 'Attribute Selector',
collapseSiblings: true
}), profileAttributeDefinitions && (0, _immutable.Map)({
label: 'profileAttribute',
siblings: buildAttributeBindings(profileAttributeDefinitions, true),
detail: 'Profile Attribute Selector',
collapseSiblings: true
})].filter(Boolean))
});
};
// Converts a list of attribute definitions to code editor bindings
var buildAttributeBindings = function buildAttributeBindings() {
var definitions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _immutable.List)();
var isProfile = arguments.length > 1 ? arguments[1] : undefined;
return definitions.map(function (def) {
return (0, _immutable.Map)({
label: (0, _immutable.get)(def, 'name'),
detail: "".concat(isProfile ? 'Profile ' : '', "Attribute").concat((0, _immutable.get)(def, 'allowsMultiple') ? ' (Multiple)' : ''),
section: "".concat(isProfile ? 'Profile ' : '', "Attributes")
});
});
};
// Converts a list of field data objects to code editor bindings
var buildValuesBindings = function buildValuesBindings(label) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (0, _immutable.List)();
return (0, _immutable.Map)({
label: label,
type: 'function',
children: data.map(function (field) {
return (0, _immutable.Map)({
label: (0, _immutable.get)(field, 'name'),
detail: (0, _lodashEs.capitalize)((0, _immutable.get)(field, 'renderType'))
});
})
});
};
// Converts bridged resources data to code editor bindings
var buildResourcesBindings = function buildResourcesBindings(label, bridgedResourceName, attributes) {
return (0, _immutable.Map)({
label: label,
type: 'function',
children: [(0, _immutable.Map)({
label: bridgedResourceName,
siblings: attributes.map(function (label) {
return (0, _immutable.Map)({
label: label,
section: "".concat(bridgedResourceName, " Bridged Resource")
});
}),
detail: 'Bridged Resource',
collapseSiblings: true
})]
});
};
// Converts integration data to code editor bindings
var buildIntegrationBindings = function buildIntegrationBindings(label, integrationName, integrationProperty, outputs) {
return (0, _immutable.Map)({
label: label,
type: 'function',
detail: 'Integration Result',
children: outputs.map(function (label) {
return (0, _immutable.Map)({
label: label,
section: "".concat(integrationName, " Integration") + (integrationProperty ? "\n\xA0\u2BA1 Properties of: ".concat(integrationProperty) : '')
});
})
});
};
var buildAgentPath = exports.buildAgentPath = function buildAgentPath(options) {
return "".concat(bundle.spaceLocation(), "/app/components/agents/").concat(options.agentSlug ? options.agentSlug : 'system');
};
var handleFormErrors = exports.handleFormErrors = function handleFormErrors(key) {
return function (result) {
var error = result.error;
if (error) {
throw error.statusCode === 400 && error.message || 'There was an error while saving.';
}
return key ? result[key] : result;
};
};
var INDEX_STATIC_PARTS = exports.INDEX_STATIC_PARTS = ['closedBy', 'coreState', 'createdBy', 'handle', 'submittedBy', 'type', 'updatedBy'].sort();
var TIMELINES = exports.TIMELINES = ['createdAt', 'updatedAt', 'submittedAt', 'closedAt'].sort();
var MAX_PART_LENGTH = exports.MAX_PART_LENGTH = 10;