feat: add core

This commit is contained in:
Karl-Philipp Wulfert
2018-11-29 16:22:57 +01:00
commit 2d770dde44
131 changed files with 41268 additions and 0 deletions

273
src/core/Thing.ts Normal file
View File

@@ -0,0 +1,273 @@
/*
* Copyright (C) 2018 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SCThingsField} from './Classes';
import {SCOrganization} from './things/Organization';
import {SCPerson} from './things/Person';
import {isThingWithTranslations} from './types/Guards';
import {SCTranslations} from './types/i18n';
import {SCISO8601Date} from './types/Time';
import {SCUuid} from './types/UUID';
/**
* Types a thing can be
*/
export type SCThingTypes = 'article'
| 'academic event'
| 'book'
| 'building'
| 'catalog'
| 'date series'
| 'diff'
| 'dish'
| 'favorite'
| 'floor'
| 'message'
| 'offer'
| 'organization'
| 'person'
| 'point of interest'
| 'room'
| 'semester'
| 'setting'
| 'sport course'
| 'ticket'
| 'tour'
| 'video';
/**
* A thing
*/
export interface SCThing {
/**
* Alternate names of the thing
*/
alternateNames?: string[];
/**
* Description of the thing
*
* @minLength 1
*/
description?: string;
/**
* Image of the thing
*/
image?: string;
/**
* Name of the thing
*
* @minLength 1
*/
name: string;
/**
* Origin of the thing
*/
origin: SCThingOrigin;
/**
* Translations of specific values of the object
*
* Take precedence over "main" value for selected languages.
*/
translations?: SCTranslations<SCThingTranslatableProperties>;
/**
* Type of the thing
*/
type: SCThingTypes;
/**
* Universally unique identifier of the thing
*/
uid: SCUuid;
/**
* URL of the thing
*/
url?: string;
}
/**
* Origin of a thing
*/
export interface SCThingOrigin {
/**
* When the thing was indexed last from the origin
*/
indexed: SCISO8601Date;
/**
* Maintainer of the origin
*
* e.g. restaurant of a dish
*/
maintainer?: SCPerson | SCOrganization;
/**
* When the thing was modified last in the origin
*/
modified?: SCISO8601Date;
/**
* Name of the origin
*/
name: string;
/**
* Original ID of the thing in the origin
*/
originalId?: string;
/**
* Entity that is responsible for the entity
*
* e.g. an organizer for an event
*/
responsibleEntity?: SCPerson | SCOrganization;
/**
* Main URL of the origin
*/
url?: string;
}
/**
* Meta information about things
*/
export class SCThingMeta {
/**
* Translations of fields
*/
static fieldTranslations: any = {
de: {
alternateNames: 'alternative Namen',
description: 'Beschreibung',
image: 'Bild',
name: 'Name',
translations: 'Übersetzungen',
type: 'Typ',
uid: 'Identifikation',
url: 'URL',
},
en: {
alternateNames: 'alternate names',
description: 'description',
uid: 'identification',
},
};
/**
* Translations of values of fields
*/
static fieldValueTranslations: any = {
de: {
type: {
AcademicTerm: 'Studienabschnitt',
Article: 'Artikel',
Book: 'Buch',
Catalog: 'Katalog',
Date: 'Termin',
Diff: 'Unterschied',
Dish: 'Essen',
Event: 'Veranstaltung',
Favorite: 'Favorit',
FloorPlan: 'Etagenplan',
Message: 'Nachricht',
Offer: 'Angebot',
Organization: 'Organisation',
Person: 'Person',
Place: 'Ort',
Setting: 'Einstellung',
Thing: 'Ding',
Ticket: 'Ticket',
Tour: 'Tour',
Video: 'Video',
},
},
};
/**
* Get field translation
*
* @param {keyof SCTranslations<T extends SCThing>} language Language to get field translation for
* @param {keyof T} field Field to get translation for
* @returns {string} Translated field or field itself
*/
static getFieldTranslation<T extends SCThing>(language: keyof SCTranslations<T>,
field: SCThingsField): string {
if (typeof this.fieldTranslations[language] !== 'undefined'
&& typeof this.fieldTranslations[language][field] !== 'undefined') {
return this.fieldTranslations[language][field];
}
return field as string;
}
/**
* Get field value translation
*
* @param {keyof SCTranslations<T>} language Language to get value translation for
* @param {string} field Field to get value translation for
* @param {T} thing SCThing to get value translation for
* @returns {string} Translated value or value itself
*/
static getFieldValueTranslation<T extends SCThing>(language: keyof SCTranslations<T>,
field: SCThingsField,
thing: T): string {
let translations: SCTranslations<SCThingTranslatableProperties>;
if (isThingWithTranslations(thing)) {
translations = thing.translations;
const languageTranslations: SCThingTranslatableProperties | undefined = translations[language];
if (typeof languageTranslations !== 'undefined') {
if (typeof (languageTranslations as any)[field] !== 'undefined') {
return (languageTranslations as any)[field];
}
}
}
// get translation from meta object
if (typeof this.fieldValueTranslations[language] !== 'undefined'
&& typeof this.fieldValueTranslations[language][field] !== 'undefined'
&& typeof (thing as any)[field] !== 'undefined'
&& typeof this.fieldValueTranslations[language][field][(thing as any)[field]]) {
return this.fieldValueTranslations[language][field][(thing as any)[field]];
}
// fallback to value itself
return (thing as any)[field];
}
}
/**
* Translatable properties of things
*/
export interface SCThingTranslatableProperties {
/**
* Translation of the description of the thing
*/
description?: string;
/**
* Translation of the name of the thing
*/
name?: string;
/**
* Origin of the thing
*/
origin?: {
/**
* Translation of the name of the origin of the thing
*/
name: string;
};
}