2021-08-16 16:20:14 +02:00
|
|
|
export interface SyncTargetInfo {
|
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
label: string;
|
|
|
|
supportsSelfHosted: boolean;
|
|
|
|
supportsConfigCheck: boolean;
|
|
|
|
description: string;
|
|
|
|
classRef: any;
|
|
|
|
}
|
|
|
|
|
2021-08-16 17:18:32 +02:00
|
|
|
// const syncTargetOrder = [
|
|
|
|
// 'joplinCloud',
|
|
|
|
// 'dropbox',
|
|
|
|
// 'onedrive',
|
|
|
|
// ];
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
export default class SyncTargetRegistry {
|
|
|
|
|
|
|
|
private static reg_: Record<number, SyncTargetInfo> = {};
|
|
|
|
|
2021-08-16 17:18:32 +02:00
|
|
|
private static get reg() {
|
2021-08-16 19:05:22 +02:00
|
|
|
// if (!this.reg_[0]) {
|
|
|
|
// this.reg_[0] = {
|
|
|
|
// id: 0,
|
|
|
|
// name: SyncTargetNone.targetName(),
|
|
|
|
// label: SyncTargetNone.label(),
|
|
|
|
// classRef: SyncTargetNone,
|
|
|
|
// description: SyncTargetNone.description(),
|
|
|
|
// supportsSelfHosted: false,
|
|
|
|
// supportsConfigCheck: false,
|
|
|
|
// };
|
|
|
|
// }
|
2021-08-16 17:18:32 +02:00
|
|
|
|
|
|
|
return this.reg_;
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static classById(syncTargetId: number) {
|
2021-08-16 17:18:32 +02:00
|
|
|
const info = SyncTargetRegistry.reg[syncTargetId];
|
2019-09-19 23:51:18 +02:00
|
|
|
if (!info) throw new Error(`Invalid id: ${syncTargetId}`);
|
2017-11-24 20:59:16 +02:00
|
|
|
return info.classRef;
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static infoByName(name: string): SyncTargetInfo {
|
2021-08-16 17:18:32 +02:00
|
|
|
for (const [, info] of Object.entries(this.reg)) {
|
2021-08-16 16:20:14 +02:00
|
|
|
if (info.name === name) return info;
|
|
|
|
}
|
|
|
|
throw new Error(`Unknown name: ${name}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static addClass(SyncTargetClass: any) {
|
2021-08-16 17:18:32 +02:00
|
|
|
this.reg[SyncTargetClass.id()] = {
|
2017-11-24 20:59:16 +02:00
|
|
|
id: SyncTargetClass.id(),
|
2017-11-24 21:21:30 +02:00
|
|
|
name: SyncTargetClass.targetName(),
|
2017-11-24 20:59:16 +02:00
|
|
|
label: SyncTargetClass.label(),
|
|
|
|
classRef: SyncTargetClass,
|
2021-08-16 16:20:14 +02:00
|
|
|
description: SyncTargetClass.description(),
|
|
|
|
supportsSelfHosted: SyncTargetClass.supportsSelfHosted(),
|
2018-02-06 20:59:36 +02:00
|
|
|
supportsConfigCheck: SyncTargetClass.supportsConfigCheck(),
|
2017-11-24 20:59:16 +02:00
|
|
|
};
|
2017-11-24 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static allIds() {
|
2021-08-16 17:18:32 +02:00
|
|
|
return Object.keys(this.reg);
|
2021-05-03 12:55:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static nameToId(name: string) {
|
2021-08-16 17:18:32 +02:00
|
|
|
for (const n in this.reg) {
|
|
|
|
if (!this.reg.hasOwnProperty(n)) continue;
|
|
|
|
if (this.reg[n].name === name) return this.reg[n].id;
|
2017-11-24 21:21:30 +02:00
|
|
|
}
|
2021-06-03 17:12:07 +02:00
|
|
|
throw new Error(`Name not found: ${name}. Was the sync target registered?`);
|
2017-11-24 21:21:30 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static idToMetadata(id: number) {
|
2021-08-16 17:18:32 +02:00
|
|
|
for (const n in this.reg) {
|
|
|
|
if (!this.reg.hasOwnProperty(n)) continue;
|
|
|
|
if (this.reg[n].id === id) return this.reg[n];
|
2018-01-25 21:01:14 +02:00
|
|
|
}
|
2019-09-19 23:51:18 +02:00
|
|
|
throw new Error(`ID not found: ${id}`);
|
2018-01-25 21:01:14 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static idToName(id: number) {
|
2018-01-25 23:15:58 +02:00
|
|
|
return this.idToMetadata(id).name;
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static idAndLabelPlainObject(os: string) {
|
|
|
|
const output: Record<string, string> = {};
|
2021-08-16 17:18:32 +02:00
|
|
|
for (const n in this.reg) {
|
|
|
|
if (!this.reg.hasOwnProperty(n)) continue;
|
|
|
|
const info = this.reg[n];
|
2020-02-08 13:59:19 +02:00
|
|
|
if (info.classRef.unsupportedPlatforms().indexOf(os) >= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
output[n] = info.label;
|
2017-11-24 21:21:30 +02:00
|
|
|
}
|
2021-08-16 17:18:32 +02:00
|
|
|
|
2017-11-24 21:21:30 +02:00
|
|
|
return output;
|
2021-08-16 17:18:32 +02:00
|
|
|
|
|
|
|
// const sorted: Record<string, string> = {};
|
|
|
|
// for (const o of syncTargetOrder) {
|
|
|
|
// sorted[o] = output[o];
|
|
|
|
// }
|
|
|
|
|
|
|
|
// for (const [name, value] of Object.entries(output)) {
|
|
|
|
// if (!sorted[name]) sorted[name] = value;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return sorted;
|
2017-11-24 21:21:30 +02:00
|
|
|
}
|
2017-11-24 20:37:40 +02:00
|
|
|
}
|