UNPKG

box-ui-elements

Version:
157 lines (155 loc) 6.95 kB
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import cloneDeep from 'lodash/cloneDeep'; import find from 'lodash/find'; import getProp from 'lodash/get'; import includes from 'lodash/includes'; import isArray from 'lodash/isArray'; import isNil from 'lodash/isNil'; import { JSON_PATCH_OP_ADD, JSON_PATCH_OP_REMOVE, JSON_PATCH_OP_REPLACE, JSON_PATCH_OP_TEST, METADATA_FIELD_TYPE_ENUM, METADATA_FIELD_TYPE_MULTISELECT } from '../../common/constants'; import { FIELD_NAME, FIELD_METADATA, FIELD_EXTENSION } from '../../constants'; const SELECT_TYPES = [METADATA_FIELD_TYPE_ENUM, METADATA_FIELD_TYPE_MULTISELECT]; export default class MetadataQueryAPIHelper { constructor(api) { _defineProperty(this, "createJSONPatchOperations", (field, oldValue, newValue) => { let operation = JSON_PATCH_OP_REPLACE; if (isNil(oldValue) && newValue) { operation = JSON_PATCH_OP_ADD; } if (oldValue && isNil(newValue)) { operation = JSON_PATCH_OP_REMOVE; } const testOp = { op: JSON_PATCH_OP_TEST, path: `/${field}`, value: oldValue }; const patchOp = { op: operation, path: `/${field}`, value: newValue }; if (operation === JSON_PATCH_OP_REMOVE) { delete patchOp.value; } return operation === JSON_PATCH_OP_ADD ? [patchOp] : [testOp, patchOp]; }); _defineProperty(this, "getMetadataQueryFields", () => { /* Example metadata query: const query = { from: 'enterprise_12345.myAwesomeTemplateKey', fields: [ 'name', // base representation field for an item (name, size, etag etc.) 'metadata.enterprise_12345.myAwesomeTemplateKey.field_1', // metadata instance field 'metadata.enterprise_12345.myAwesomeTemplateKey.field_2', // metadata instance field 'metadata.enterprise_12345.myAwesomeTemplateKey.field_3' // metadata instance field ], ancestor_folder_id: 0, }; This function will return ['field_1', 'field_2', 'field_3'] */ const { fields = [], from } = this.metadataQuery; return fields.filter(field => field.includes(from)).map(field => field.split('.').pop()); }); _defineProperty(this, "flattenMetadata", metadata => { const templateFields = getProp(this.metadataTemplate, 'fields', []); const instance = getProp(metadata, `${this.templateScope}.${this.templateKey}`); if (!instance) { return {}; } const queryFields = this.getMetadataQueryFields(); const fields = queryFields.map(queryField => { const templateField = find(templateFields, ['key', queryField]); const type = getProp(templateField, 'type'); // get data type const displayName = getProp(templateField, 'displayName', queryField); // get displayName, defaults to key const field = { key: `${FIELD_METADATA}.${this.templateScope}.${this.templateKey}.${queryField}`, value: instance[queryField], type, displayName }; if (includes(SELECT_TYPES, type)) { // get "options" for enums or multiselects field.options = getProp(templateField, 'options'); } return field; }); return { enterprise: { fields, id: instance.$id } }; }); _defineProperty(this, "getDataWithTypes", templateSchemaResponse => { this.metadataTemplate = getProp(templateSchemaResponse, 'data'); const { entries: items, next_marker: nextMarker } = this.metadataQueryResponseData; return { items, nextMarker }; }); _defineProperty(this, "getTemplateSchemaInfo", data => { const { entries } = data; this.metadataQueryResponseData = data; if (!entries || entries.length === 0) { // Don't make metadata API call to get template info return Promise.resolve(); } const metadata = getProp(entries, '[0].metadata'); this.templateScope = Object.keys(metadata)[0]; const instance = metadata[this.templateScope]; this.templateKey = Object.keys(instance)[0]; return this.api.getMetadataAPI(true).getSchemaByTemplateKey(this.templateKey); }); _defineProperty(this, "queryMetadata", () => { return new Promise((resolve, reject) => { this.api.getMetadataQueryAPI().queryMetadata(this.metadataQuery, resolve, reject, { forceFetch: true }); }); }); _defineProperty(this, "fetchMetadataQueryResults", (metadataQuery, successCallback, errorCallback) => { this.metadataQuery = this.verifyQueryFields(metadataQuery); return this.queryMetadata().then(this.getTemplateSchemaInfo).then(this.getDataWithTypes).then(collection => { return successCallback(collection, this.metadataTemplate); }).catch(errorCallback); }); _defineProperty(this, "updateMetadata", (file, field, oldValue, newValue, successCallback, errorCallback) => { const operations = this.createJSONPatchOperations(field, oldValue, newValue); return this.api.getMetadataAPI(true).updateMetadata(file, this.metadataTemplate, operations, successCallback, errorCallback); }); /** * Verify that the metadata query has required fields and update it if necessary * For a file item, default fields included in the response are "type", "id", "etag" * * @param {MetadataQueryType} metadataQuery metadata query object * @return {MetadataQueryType} updated metadata query object with required fields */ _defineProperty(this, "verifyQueryFields", metadataQuery => { const clonedQuery = cloneDeep(metadataQuery); const clonedFields = isArray(clonedQuery.fields) ? clonedQuery.fields : []; // Make sure the query fields array has "name" field which is necessary to display info. if (!clonedFields.includes(FIELD_NAME)) { clonedFields.push(FIELD_NAME); } if (!clonedFields.includes(FIELD_EXTENSION)) { clonedFields.push(FIELD_EXTENSION); } clonedQuery.fields = clonedFields; return clonedQuery; }); this.api = api; } } //# sourceMappingURL=MetadataQueryAPIHelper.js.map