2024-09-14 08:31:32 +00:00
|
|
|
import metadataAPI from './api';
|
|
|
|
import {
|
2024-09-18 03:08:14 +00:00
|
|
|
PRIVATE_COLUMN_KEYS, EDITABLE_DATA_PRIVATE_COLUMN_KEYS, EDITABLE_PRIVATE_COLUMN_KEYS, DELETABLE_PRIVATE_COLUMN_KEY,
|
2024-11-05 09:37:24 +00:00
|
|
|
FACE_RECOGNITION_VIEW_ID,
|
|
|
|
VIEW_TYPE,
|
2024-09-14 08:31:32 +00:00
|
|
|
} from './constants';
|
|
|
|
import LocalStorage from './utils/local-storage';
|
|
|
|
import EventBus from '../components/common/event-bus';
|
|
|
|
import { username, lang } from '../utils/constants';
|
2024-06-29 09:58:27 +00:00
|
|
|
|
|
|
|
class Context {
|
|
|
|
|
|
|
|
constructor() {
|
2024-08-30 06:36:52 +00:00
|
|
|
this.settings = { lang };
|
2024-06-29 09:58:27 +00:00
|
|
|
this.metadataAPI = null;
|
|
|
|
this.localStorage = null;
|
|
|
|
this.eventBus = null;
|
|
|
|
this.hasInit = false;
|
2024-07-26 09:15:52 +00:00
|
|
|
this.permission = 'r';
|
2024-08-02 08:15:38 +00:00
|
|
|
this.collaboratorsCache = {};
|
2024-06-29 09:58:27 +00:00
|
|
|
}
|
|
|
|
|
2024-08-15 09:38:42 +00:00
|
|
|
async init(settings) {
|
2024-06-29 09:58:27 +00:00
|
|
|
if (this.hasInit) return;
|
|
|
|
|
|
|
|
// init settings
|
2024-08-30 06:36:52 +00:00
|
|
|
this.settings = { ...this.settings, ...settings };
|
2024-06-29 09:58:27 +00:00
|
|
|
|
|
|
|
// init metadataAPI
|
2024-08-15 09:38:42 +00:00
|
|
|
const { repoInfo } = this.settings;
|
2024-06-29 09:58:27 +00:00
|
|
|
this.metadataAPI = metadataAPI;
|
|
|
|
|
|
|
|
// init localStorage
|
2024-07-22 01:45:08 +00:00
|
|
|
const { repoID, viewID } = this.settings;
|
2024-08-15 09:38:42 +00:00
|
|
|
const localStorageName = viewID ? `sf-metadata-${repoID}-${viewID}` : `sf-metadata-${repoID}`;
|
|
|
|
this.localStorage = new LocalStorage(localStorageName);
|
2024-06-29 09:58:27 +00:00
|
|
|
|
|
|
|
const eventBus = new EventBus();
|
|
|
|
this.eventBus = eventBus;
|
|
|
|
|
2024-07-26 09:15:52 +00:00
|
|
|
this.permission = repoInfo.permission !== 'admin' && repoInfo.permission !== 'rw' ? 'r' : 'rw';
|
|
|
|
|
2024-06-29 09:58:27 +00:00
|
|
|
this.hasInit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy = () => {
|
|
|
|
this.settings = {};
|
|
|
|
this.metadataAPI = null;
|
|
|
|
this.localStorage = null;
|
|
|
|
this.eventBus = null;
|
|
|
|
this.hasInit = false;
|
2024-07-26 09:15:52 +00:00
|
|
|
this.permission = 'r';
|
2024-06-29 09:58:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
getSetting = (key) => {
|
|
|
|
if (this.settings[key] === false) return this.settings[key];
|
|
|
|
return this.settings[key] || '';
|
|
|
|
};
|
|
|
|
|
|
|
|
setSetting = (key, value) => {
|
|
|
|
this.settings[key] = value;
|
|
|
|
};
|
|
|
|
|
2024-07-03 09:04:30 +00:00
|
|
|
getUsername = () => {
|
|
|
|
return username;
|
|
|
|
};
|
|
|
|
|
|
|
|
// collaborators
|
|
|
|
getCollaborators = () => {
|
|
|
|
const repoID = this.settings['repoID'];
|
|
|
|
return this.metadataAPI.getCollaborators(repoID);
|
|
|
|
};
|
|
|
|
|
2024-06-29 09:58:27 +00:00
|
|
|
// metadata
|
2024-07-03 09:04:30 +00:00
|
|
|
getMetadata = (params) => {
|
2024-11-05 09:37:24 +00:00
|
|
|
if (!this.metadataAPI) return null;
|
2024-07-03 09:04:30 +00:00
|
|
|
const repoID = this.settings['repoID'];
|
2024-11-05 09:37:24 +00:00
|
|
|
const { view_id, start, limit } = params;
|
|
|
|
if (view_id === FACE_RECOGNITION_VIEW_ID) {
|
|
|
|
return this.metadataAPI.getFaceData(repoID, start, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.metadataAPI.getMetadata(repoID, params);
|
2024-06-29 09:58:27 +00:00
|
|
|
};
|
|
|
|
|
2024-08-23 09:51:52 +00:00
|
|
|
getRecord = (parentDir, fileName) => {
|
|
|
|
const repoID = this.settings['repoID'];
|
|
|
|
return this.metadataAPI.getMetadataRecordInfo(repoID, parentDir, fileName);
|
|
|
|
};
|
|
|
|
|
2024-07-22 01:45:08 +00:00
|
|
|
getViews = () => {
|
|
|
|
const repoID = this.settings['repoID'];
|
|
|
|
return this.metadataAPI.listViews(repoID);
|
|
|
|
};
|
|
|
|
|
2024-07-22 02:52:21 +00:00
|
|
|
getView = (viewId) => {
|
2024-11-05 09:37:24 +00:00
|
|
|
if (viewId === FACE_RECOGNITION_VIEW_ID) {
|
|
|
|
return {
|
|
|
|
data: {
|
|
|
|
view: {
|
|
|
|
_id: FACE_RECOGNITION_VIEW_ID,
|
|
|
|
type: VIEW_TYPE.FACE_RECOGNITION,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-07-22 02:52:21 +00:00
|
|
|
const repoID = this.settings['repoID'];
|
|
|
|
return this.metadataAPI.getView(repoID, viewId);
|
|
|
|
};
|
|
|
|
|
2024-07-26 09:15:52 +00:00
|
|
|
getPermission = () => {
|
|
|
|
return this.permission;
|
|
|
|
};
|
|
|
|
|
2024-09-11 09:38:22 +00:00
|
|
|
canModify = () => {
|
|
|
|
if (this.permission === 'r') return false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2024-08-12 06:24:48 +00:00
|
|
|
canModifyRow = (row) => {
|
2024-07-26 09:15:52 +00:00
|
|
|
if (this.permission === 'r') return false;
|
2024-07-18 09:40:53 +00:00
|
|
|
return true;
|
2024-06-29 09:58:27 +00:00
|
|
|
};
|
|
|
|
|
2024-09-29 09:52:25 +00:00
|
|
|
checkCanDeleteRow = () => {
|
|
|
|
if (this.permission === 'r') return false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2024-08-12 06:24:48 +00:00
|
|
|
canModifyRows = () => {
|
2024-07-26 09:15:52 +00:00
|
|
|
if (this.permission === 'r') return false;
|
2024-07-05 09:41:34 +00:00
|
|
|
return true;
|
2024-06-29 09:58:27 +00:00
|
|
|
};
|
|
|
|
|
2024-07-25 08:42:18 +00:00
|
|
|
canModifyColumn = (column) => {
|
2024-07-26 09:15:52 +00:00
|
|
|
if (this.permission === 'r') return false;
|
2024-08-12 06:24:48 +00:00
|
|
|
const { editable } = column;
|
|
|
|
if (!editable) return false;
|
2024-07-26 09:15:52 +00:00
|
|
|
if (PRIVATE_COLUMN_KEYS.includes(column.key) && !EDITABLE_PRIVATE_COLUMN_KEYS.includes(column.key)) return false;
|
2024-07-25 08:42:18 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2024-07-26 09:15:52 +00:00
|
|
|
canRenameColumn = (column) => {
|
|
|
|
if (this.permission === 'r') return false;
|
|
|
|
if (PRIVATE_COLUMN_KEYS.includes(column.key)) return false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
canModifyColumnData = (column) => {
|
|
|
|
if (this.permission === 'r') return false;
|
|
|
|
const { key } = column;
|
|
|
|
if (PRIVATE_COLUMN_KEYS.includes(key)) return EDITABLE_DATA_PRIVATE_COLUMN_KEYS.includes(key);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
canDeleteColumn = (column) => {
|
|
|
|
if (this.permission === 'r') return false;
|
|
|
|
const { key } = column;
|
2024-09-18 03:08:14 +00:00
|
|
|
if (PRIVATE_COLUMN_KEYS.includes(key)) return DELETABLE_PRIVATE_COLUMN_KEY.includes(key);
|
2024-07-26 09:15:52 +00:00
|
|
|
return true;
|
2024-06-29 09:58:27 +00:00
|
|
|
};
|
|
|
|
|
2024-08-23 09:11:24 +00:00
|
|
|
canModifyColumnOrder = () => {
|
|
|
|
if (this.permission === 'r') return false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2024-08-06 06:32:15 +00:00
|
|
|
canModifyView = (view) => {
|
|
|
|
if (this.permission === 'r') return false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2024-07-03 09:04:30 +00:00
|
|
|
restoreRows = () => {
|
|
|
|
// todo
|
|
|
|
};
|
|
|
|
|
|
|
|
updateRows = () => {
|
|
|
|
// todo
|
|
|
|
};
|
|
|
|
|
|
|
|
lockRowViaButton = () => {
|
|
|
|
// todo
|
|
|
|
};
|
|
|
|
|
|
|
|
updateRowViaButton = () => {
|
|
|
|
// todo
|
|
|
|
};
|
|
|
|
|
2024-07-25 08:42:18 +00:00
|
|
|
// column
|
2024-07-18 09:40:53 +00:00
|
|
|
insertColumn = (repoId, name, type, { key, data }) => {
|
|
|
|
return this.metadataAPI.insertColumn(repoId, name, type, { key, data });
|
|
|
|
};
|
|
|
|
|
2024-07-25 08:42:18 +00:00
|
|
|
deleteColumn = (repoId, columnKey) => {
|
|
|
|
return this.metadataAPI.deleteColumn(repoId, columnKey);
|
|
|
|
};
|
|
|
|
|
|
|
|
renameColumn = (repoId, columnKey, name) => {
|
|
|
|
return this.metadataAPI.renameColumn(repoId, columnKey, name);
|
|
|
|
};
|
|
|
|
|
|
|
|
modifyColumnData = (repoId, columnKey, data) => {
|
|
|
|
return this.metadataAPI.modifyColumnData(repoId, columnKey, data);
|
|
|
|
};
|
|
|
|
|
|
|
|
// record
|
2024-08-15 12:34:33 +00:00
|
|
|
modifyRecord = (repoId, recordId, objID, update) => {
|
|
|
|
return this.metadataAPI.modifyRecord(repoId, recordId, objID, update);
|
2024-07-18 09:40:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
modifyRecords = (repoId, recordsData, isCopyPaste) => {
|
|
|
|
return this.metadataAPI.modifyRecords(repoId, recordsData, isCopyPaste);
|
|
|
|
};
|
|
|
|
|
2024-09-29 09:52:25 +00:00
|
|
|
batchDeleteFiles = (repoId, fileNames) => {
|
|
|
|
return this.metadataAPI.batchDeleteFiles(repoId, fileNames);
|
|
|
|
};
|
|
|
|
|
2024-07-25 08:42:18 +00:00
|
|
|
// view
|
2024-07-22 01:45:08 +00:00
|
|
|
modifyView = (repoId, viewId, viewData) => {
|
|
|
|
return this.metadataAPI.modifyView(repoId, viewId, viewData);
|
|
|
|
};
|
|
|
|
|
2024-07-03 09:04:30 +00:00
|
|
|
getRowsByIds = () => {
|
|
|
|
// todo
|
|
|
|
};
|
|
|
|
|
2024-08-23 09:51:52 +00:00
|
|
|
// ai
|
2024-09-10 07:18:07 +00:00
|
|
|
generateDescription = (filePath) => {
|
2024-08-30 06:36:52 +00:00
|
|
|
const repoID = this.settings['repoID'];
|
2024-09-10 07:18:07 +00:00
|
|
|
return this.metadataAPI.generateDescription(repoID, filePath);
|
2024-08-30 06:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
imageCaption = (filePath) => {
|
2024-08-23 09:51:52 +00:00
|
|
|
const repoID = this.settings['repoID'];
|
2024-08-30 06:36:52 +00:00
|
|
|
const lang = this.settings['lang'];
|
|
|
|
return this.metadataAPI.imageCaption(repoID, filePath, lang);
|
2024-08-23 09:51:52 +00:00
|
|
|
};
|
2024-10-18 05:25:10 +00:00
|
|
|
|
|
|
|
extractFileDetails = (objIds) => {
|
|
|
|
const repoID = this.settings['repoID'];
|
|
|
|
return this.metadataAPI.extractFileDetails(repoID, objIds);
|
|
|
|
};
|
2024-11-05 09:37:24 +00:00
|
|
|
|
|
|
|
// face api
|
|
|
|
renamePeople = (recordId, name) => {
|
|
|
|
const repoID = this.settings['repoID'];
|
|
|
|
return this.metadataAPI.renamePeople(repoID, recordId, name);
|
|
|
|
};
|
|
|
|
|
|
|
|
getPeoplePhotos = (recordId, start, limit) => {
|
|
|
|
const repoID = this.settings['repoID'];
|
|
|
|
return this.metadataAPI.getPeoplePhotos(repoID, recordId, start, limit);
|
|
|
|
};
|
|
|
|
|
2024-06-29 09:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Context;
|