Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-Implement property value handling #75

Merged
merged 8 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ViewingDirection } from "@iiif/vocabulary/dist-commonjs";
import {
IIIFResource,
IManifestoOptions,
LanguageMap,
Manifest,
TreeNode,
TreeNodeType,
Expand Down Expand Up @@ -126,7 +125,7 @@ export class Collection extends IIIFResource {
var tree: TreeNode = manifest.getDefaultTree();
tree.label =
manifest.parentLabel ||
LanguageMap.getValue(manifest.getLabel(), this.options.locale) ||
manifest.getLabel().getValue(this.options.locale) ||
"manifest " + (i + 1);
tree.navDate = manifest.getNavDate();
tree.data.id = manifest.id;
Expand All @@ -147,7 +146,7 @@ export class Collection extends IIIFResource {
var tree: TreeNode = collection.getDefaultTree();
tree.label =
collection.parentLabel ||
LanguageMap.getValue(collection.getLabel(), this.options.locale) ||
collection.getLabel().getValue(this.options.locale) ||
"collection " + (i + 1);
tree.navDate = collection.getNavDate();
tree.data.id = collection.id;
Expand Down
20 changes: 9 additions & 11 deletions src/IIIFResource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ManifestResource,
Utils,
LanguageMap,
PropertyValue,
Deserialiser,
LabelValuePair,
TreeNode,
Expand Down Expand Up @@ -34,26 +34,26 @@ export class IIIFResource extends ManifestResource {
this.options = Object.assign(defaultOptions, options);
}

getAttribution(): LanguageMap {
getAttribution(): PropertyValue {
//console.warn('getAttribution will be deprecated, use getRequiredStatement instead.');

const attribution: any = this.getProperty("attribution");

if (attribution) {
return LanguageMap.parse(attribution, this.options.locale);
return PropertyValue.parse(attribution, this.options.locale);
}

return [];
return new PropertyValue([], this.options.locale);
}

getDescription(): LanguageMap {
getDescription(): PropertyValue {
const description: any = this.getProperty("description");

if (description) {
return LanguageMap.parse(description, this.options.locale);
return PropertyValue.parse(description, this.options.locale);
}

return [];
return new PropertyValue([], this.options.locale);
}

getIIIFResourceType(): IIIFResourceType {
Expand Down Expand Up @@ -115,7 +115,7 @@ export class IIIFResource extends ManifestResource {
requiredStatement.parse(_requiredStatement);
} else {
// fall back to attribution (if it exists)
const attribution: LanguageMap = this.getAttribution();
const attribution: PropertyValue = this.getAttribution();

if (attribution) {
requiredStatement = new LabelValuePair(this.options.locale);
Expand Down Expand Up @@ -156,9 +156,7 @@ export class IIIFResource extends ManifestResource {
}

Utils.loadManifest(id).then(function(data) {
that.parentLabel = <string>(
LanguageMap.getValue(that.getLabel(), options.locale)
);
that.parentLabel = <string>that.getLabel().getValue(options.locale);
const parsed = Deserialiser.parse(data, options);
that = Object.assign(that, parsed);
//that.parentCollection = options.resource.parentCollection;
Expand Down
85 changes: 36 additions & 49 deletions src/LabelValuePair.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Language, LanguageMap, Utils } from "./internal";
import { PropertyValue } from "./internal";

export class LabelValuePair {
public label: LanguageMap;
public value: LanguageMap;
public label: PropertyValue | null;
public value: PropertyValue | null;
public defaultLocale: string;
public resource: any;

Expand All @@ -12,69 +12,56 @@ export class LabelValuePair {

public parse(resource: any): void {
this.resource = resource;
this.label = LanguageMap.parse(this.resource.label, this.defaultLocale);
this.value = LanguageMap.parse(this.resource.value, this.defaultLocale);
this.label = PropertyValue.parse(this.resource.label, this.defaultLocale);
this.value = PropertyValue.parse(this.resource.value, this.defaultLocale);
}

// shortcuts to get/set values based on default locale
// shortcuts to get/set values based on user or default locale

public getLabel(): string | null {
if (this.label) {
return LanguageMap.getValue(this.label, this.defaultLocale);
public getLabel(locale?: string | string[]): string | null {
if (this.label === null) {
return null;
}

return null;
if (Array.isArray(locale) && !locale.length) {
locale = undefined;
}
return this.label.getValue(locale || this.defaultLocale);
}

public setLabel(value: string): void {
if (this.label && this.label.length) {
var t: Language = this.label.filter(
x =>
x.locale === this.defaultLocale ||
x.locale === Utils.getInexactLocale(this.defaultLocale)
)[0];
if (t) t.value = value;
if (this.label === null) {
this.label = new PropertyValue([]);
}
this.label.setValue(value, this.defaultLocale);
}

public getValue(): string | null {
if (this.value) {
var locale: string = this.defaultLocale;

// if the label has a locale, prefer that to the default locale
if (this.label && this.label.length && this.label[0].locale) {
locale = this.label[0].locale;
}

return LanguageMap.getValue(this.value, locale);
public getValue(
locale?: string | string[],
joinWith: string = "<br/>"
): string | null {
if (this.value === null) {
return null;
}

return null;
if (Array.isArray(locale) && !locale.length) {
locale = undefined;
}
return this.value.getValue(locale || this.defaultLocale, joinWith);
}

public getValues(): Array<string | null> {
if (this.value) {
var locale: string = this.defaultLocale;

// if the label has a locale, prefer that to the default locale
if (this.label && this.label.length && this.label[0].locale) {
locale = this.label[0].locale;
}

return LanguageMap.getValues(this.value, locale);
public getValues(locale?: string | string[]): Array<string | null> {
if (this.value === null) {
return [];
}

return [];
if (Array.isArray(locale) && !locale.length) {
locale = undefined;
}
return this.value.getValues(locale || this.defaultLocale);
}

public setValue(value: string): void {
if (this.value && this.value.length) {
var t: Language = this.value.filter(
x =>
x.locale === this.defaultLocale ||
x.locale === Utils.getInexactLocale(this.defaultLocale)
)[0];
if (t) t.value = value;
if (this.value === null) {
this.value = new PropertyValue([]);
}
this.value.setValue(value, this.defaultLocale);
}
}
18 changes: 2 additions & 16 deletions src/Language.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
export class Language {
/** @deprecated Use LocalizedValue instead */
export default interface Language {
value: string;
locale: string;

constructor(value: string | string[], locale: string) {
if (Array.isArray(value)) {
if (value.length === 1) {
this.value = value[0];
} else {
// concatenate all of the values
this.value = value.join("<br/>");
}
} else {
this.value = value;
}

this.locale = locale;
}
}
94 changes: 7 additions & 87 deletions src/LanguageMap.ts
Original file line number Diff line number Diff line change
@@ -1,101 +1,21 @@
import { Language, Utils } from "./internal";
import { PropertyValue } from "./PropertyValue";
import Language from "./Language";

/** @deprecated Use PropertyValue instead */
export class LanguageMap extends Array<Language> {
static parse(language: any, defaultLocale: string): LanguageMap {
const tc: LanguageMap = <LanguageMap>[];
let t: Language;

if (!language) {
return tc;
} else if (Array.isArray(language)) {
for (let i = 0; i < language.length; i++) {
var value: any = language[i];

if (typeof value === "string") {
t = new Language(value, defaultLocale);
} else {
t = new Language(
value["@value"],
value["@language"] || defaultLocale
);
}

tc.push(t);
}
} else if (typeof language === "string") {
// if it's just a single string value, create one language in the configured locale
t = new Language(language, defaultLocale);
tc.push(t);
return tc;
} else {
// it's an object
if (language["@value"]) {
// presentation 2
t = new Language(
language["@value"],
language["@language"] || defaultLocale
);
tc.push(t);
} else {
// presentation 3
Object.keys(language).forEach(key => {
// todo: support multiple values in array
if (language[key].length) {
t = new Language(language[key], key);
tc.push(t);
} else {
throw new Error("language must have a value");
}
});
}
}

return tc;
}

/** @deprecated Use the `PropertyValue#getValue` instance method instead */
static getValue(
languageCollection: LanguageMap,
locale?: string
): string | null {
if (languageCollection.length) {
if (locale) {
const language: Language = languageCollection.filter(
t =>
t.locale === locale ||
Utils.getInexactLocale(t.locale) === Utils.getInexactLocale(locale)
)[0];
if (language) {
return language.value;
}
}

// return the first value
return languageCollection[0].value;
}

return null;
return (languageCollection as PropertyValue).getValue(locale, "<br/>");
}

/** @deprecated Use the `PropertyValue#getValues` instance method instead */
static getValues(
languageCollection: LanguageMap,
locale?: string
): Array<string | null> {
if (languageCollection.length) {
if (locale) {
return languageCollection
.filter(
t =>
t.locale === locale ||
Utils.getInexactLocale(t.locale) ===
Utils.getInexactLocale(locale)
)
.map(language => language.value);
}

// returns all of the values
return languageCollection.map(language => language.value);
}

return [];
return (languageCollection as PropertyValue).getValues(locale);
}
}
10 changes: 5 additions & 5 deletions src/ManifestResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Utils,
Rendering,
LabelValuePair,
LanguageMap,
PropertyValue,
IManifestoOptions,
IExternalResource
} from "./internal";
Expand All @@ -28,18 +28,18 @@ export class ManifestResource extends JSONLDResource {
return <IIIFResourceType>Utils.normaliseType(this.getProperty("type"));
}

getLabel(): LanguageMap {
getLabel(): PropertyValue {
const label: any = this.getProperty("label");

if (label) {
return LanguageMap.parse(label, this.options.locale);
return PropertyValue.parse(label, this.options.locale);
}

return [];
return new PropertyValue([], this.options.locale);
}

getDefaultLabel(): string | null {
return LanguageMap.getValue(this.getLabel());
return this.getLabel().getValue(this.options.locale);
}

getMetadata(): LabelValuePair[] {
Expand Down
Loading