From e737f6c69c1d4d5aa4cd942af44890138cfc0143 Mon Sep 17 00:00:00 2001 From: edsilv Date: Tue, 8 Sep 2015 19:20:54 +0100 Subject: [PATCH] reloading works --- bower.json | 2 +- src/BootstrapParams.ts | 37 ++++---- src/Bootstrapper.ts | 7 ++ .../uv-seadragon-extension/Extension.ts | 2 +- src/lib/extensions.js | 11 +++ src/lib/manifesto.js | 84 +++++++++++-------- src/modules/uv-shared-module/BaseExtension.ts | 20 ++--- src/modules/uv-shared-module/BaseProvider.ts | 9 +- src/modules/uv-shared-module/IExtension.ts | 2 +- src/modules/uv-shared-module/IProvider.ts | 3 + src/typings/extensions.d.ts | 1 + src/typings/manifesto.d.ts | 37 +++++--- 12 files changed, 136 insertions(+), 79 deletions(-) diff --git a/bower.json b/bower.json index 82f02f618..3bde0be22 100644 --- a/bower.json +++ b/bower.json @@ -24,7 +24,7 @@ "utils": "edsilv/utils#~0.0.23", "uv-cy-GB-theme": "universalviewer/uv-cy-GB-theme", "uv-en-GB-theme": "universalviewer/uv-en-GB-theme", - "manifesto": "universalviewer/manifesto#dev", + "manifesto": "universalviewer/manifesto#tree", "lodash-compat": "~3.10.0" }, "devDependencies": { diff --git a/src/BootstrapParams.ts b/src/BootstrapParams.ts index 72a1705ae..de45c829a 100644 --- a/src/BootstrapParams.ts +++ b/src/BootstrapParams.ts @@ -17,7 +17,7 @@ class BootstrapParams { locales: any[]; manifestIndex: number; manifestUri: string; - paramMap: string[] = ['c', 'm', 's', 'cv', 'z', 'r']; + paramMap: string[] = ['c', 'm', 's', 'cv', 'z', 'r']; // todo: remove z, r sequenceIndex: number; constructor() { @@ -40,23 +40,6 @@ class BootstrapParams { this.canvasIndex = this.getParam(Params.canvasIndex); } - // parse string 'en-GB' or 'en-GB:English,cy-GB:Welsh' into array - setLocale(locale: string): void { - this.locale = locale; - this.locales = []; - var l = this.locale.split(','); - - for (var i = 0; i < l.length; i++) { - var v = l[i].split(':'); - this.locales.push({ - name: v[0].trim(), - label: (v[1]) ? v[1].trim() : "" - }); - } - - this.localeName = this.locales[0].name; - } - getLocaleName(): string { return this.localeName; } @@ -72,8 +55,26 @@ class BootstrapParams { } hashParamsAvailable(): boolean { + // if reloading, return (this.isHomeDomain && !this.isReload); } + + // parse string 'en-GB' or 'en-GB:English,cy-GB:Welsh' into array + setLocale(locale: string): void { + this.locale = locale; + this.locales = []; + var l = this.locale.split(','); + + for (var i = 0; i < l.length; i++) { + var v = l[i].split(':'); + this.locales.push({ + name: v[0].trim(), + label: (v[1]) ? v[1].trim() : "" + }); + } + + this.localeName = this.locales[0].name; + } } export = BootstrapParams; \ No newline at end of file diff --git a/src/Bootstrapper.ts b/src/Bootstrapper.ts index 787e10ac6..397462e72 100644 --- a/src/Bootstrapper.ts +++ b/src/Bootstrapper.ts @@ -86,8 +86,15 @@ class Bootstrapper{ var manifest: Manifesto.IManifest; if (this.iiifResource.getIIIFResourceType().toString() === manifesto.IIIFResourceType.collection().toString()){ + // if it's a collection and has child collections, get the collection by index if ((this.iiifResource).collections && (this.iiifResource).collections.length){ var collection = (this.iiifResource).collections[this.params.collectionIndex]; + + if (!collection){ + this.notFound(); + return; + } + manifest = collection.getManifestByIndex(this.params.manifestIndex); } else { manifest = (this.iiifResource).getManifestByIndex(this.params.manifestIndex); diff --git a/src/extensions/uv-seadragon-extension/Extension.ts b/src/extensions/uv-seadragon-extension/Extension.ts index add00dc7a..5e8c8b7dc 100644 --- a/src/extensions/uv-seadragon-extension/Extension.ts +++ b/src/extensions/uv-seadragon-extension/Extension.ts @@ -328,7 +328,7 @@ class Extension extends BaseExtension { if (!data.type) return; if (data.type === 'manifest') { - //this.viewManifest(data); + this.viewManifest(data); } else { this.viewRange(data.path); } diff --git a/src/lib/extensions.js b/src/lib/extensions.js index 3db1e8108..af24f4b42 100644 --- a/src/lib/extensions.js +++ b/src/lib/extensions.js @@ -169,6 +169,17 @@ String.format = function () { } return s; }; +String.prototype.hashCode = function () { + var hash = 0, i, chr, len; + if (this.length == 0) + return hash; + for (i = 0, len = this.length; i < len; i++) { + chr = this.charCodeAt(i); + hash = ((hash << 5) - hash) + chr; + hash |= 0; // Convert to 32bit integer + } + return hash.toString(); +}; String.prototype.ltrim = function () { return this.replace(/^\s+/, ''); }; diff --git a/src/lib/manifesto.js b/src/lib/manifesto.js index ad8aa4f93..f3860b1f8 100644 --- a/src/lib/manifesto.js +++ b/src/lib/manifesto.js @@ -284,9 +284,11 @@ var Manifesto; } return metadata; }; + // todo: once UV download menu uses manifesto parsed objects, this can be moved back from Utils ManifestResource.prototype.getRendering = function (format) { return Manifesto.Utils.getRendering(this, format); }; + // todo: once UV download menu uses manifesto parsed objects, this can be moved back from Utils ManifestResource.prototype.getRenderings = function () { return Manifesto.Utils.getRenderings(this); }; @@ -394,6 +396,7 @@ var Manifesto; __extends(IIIFResource, _super); function IIIFResource(jsonld, options) { _super.call(this, jsonld, options); + this.index = 0; this.isLoaded = false; var defaultOptions = { defaultLabel: '-', @@ -423,6 +426,11 @@ var Manifesto; IIIFResource.prototype.getTitle = function () { return Manifesto.Utils.getLocalisedValue(this.getProperty('label'), this.options.locale); }; + IIIFResource.prototype.getTree = function () { + this.treeRoot = new Manifesto.TreeNode('root'); + this.treeRoot.data = this; + return this.treeRoot; + }; IIIFResource.prototype.load = function () { var that = this; return new Promise(function (resolve, reject) { @@ -450,39 +458,10 @@ var Manifesto; __extends(Manifest, _super); function Manifest(jsonld, options) { _super.call(this, jsonld, options); + this.index = 0; this.sequences = []; jsonld.__manifest = this; } - //getMetadata(): any { - // var metadata = this.getMetadata(); - // - // if (this.getLicense()){ - // metadata.unshift({ - // "label": "license", - // "value": this.getLicense() - // }); - // } - // - // if (this.getAttribution()){ - // metadata.unshift({ - // "label": "attribution", - // "value": this.getAttribution() - // }); - // } - // - // if (this.getDescription()){ - // metadata.unshift({ - // "label": "description", - // "value": this.getDescription() - // }); - // } - // - // if (this.getLogo()){ - // metadata.pop({ - // "label": "logo", - // "value": ''}); - // } - //} // todo: use jmespath to flatten tree? // https://github.com/jmespath/jmespath.js/issues/6 // using r.__parsed in the meantime @@ -524,12 +503,14 @@ var Manifesto; return this.sequences.length; }; Manifest.prototype.getTree = function () { - this.treeRoot = new Manifesto.TreeNode('root'); - this.treeRoot.label = 'root'; + _super.prototype.getTree.call(this); + this.treeRoot.data.type = 'manifest'; + if (!this.isLoaded) { + return this.treeRoot; + } if (!this.rootRange) return this.treeRoot; this.treeRoot.data = this.rootRange; - this.treeRoot.data.type = 'manifest'; this.rootRange.treeNode = this.treeRoot; if (this.rootRange.ranges) { for (var i = 0; i < this.rootRange.ranges.length; i++) { @@ -587,6 +568,39 @@ var Manifesto; Collection.prototype.getTotalManifests = function () { return this.manifests.length; }; + Collection.prototype.getTree = function () { + _super.prototype.getTree.call(this); + this.treeRoot.data.type = 'collection'; + this._parseManifests(this); + this._parseCollections(this); + return this.treeRoot; + }; + Collection.prototype._parseManifests = function (parentCollection) { + if (parentCollection.manifests && parentCollection.manifests.length) { + for (var i = 0; i < parentCollection.manifests.length; i++) { + var manifest = parentCollection.manifests[i]; + //manifest.parentCollection = parentCollection; + //manifest.index = i; + var tree = manifest.getTree(); + tree.label = manifest.getTitle() || 'manifest ' + (i + 1); + parentCollection.treeRoot.addNode(tree); + } + } + }; + Collection.prototype._parseCollections = function (parentCollection) { + if (parentCollection.collections && parentCollection.collections.length) { + for (var i = 0; i < parentCollection.collections.length; i++) { + var collection = parentCollection.collections[i]; + //collection.parentCollection = parentCollection; + //collection.index = i; + var tree = collection.getTree(); + tree.label = collection.getTitle() || 'collection ' + (i + 1); + parentCollection.treeRoot.addNode(tree); + this._parseManifests(collection); + this._parseCollections(collection); + } + } + }; return Collection; })(Manifesto.IIIFResource); Manifesto.Collection = Collection; @@ -873,6 +887,8 @@ var Manifesto; if (children) { for (var i = 0; i < children.length; i++) { var child = this.parseCollection(children[i], options); + child.index = i; + child.parentCollection = collection; collection.collections.push(child); } } @@ -890,6 +906,8 @@ var Manifesto; if (children) { for (var i = 0; i < children.length; i++) { var child = this.parseManifest(children[i], options); + child.index = i; + child.parentCollection = collection; collection.manifests.push(child); } } diff --git a/src/modules/uv-shared-module/BaseExtension.ts b/src/modules/uv-shared-module/BaseExtension.ts index e22cc2124..108ef596f 100644 --- a/src/modules/uv-shared-module/BaseExtension.ts +++ b/src/modules/uv-shared-module/BaseExtension.ts @@ -1,6 +1,7 @@ import BaseCommands = require("./BaseCommands"); import BaseProvider = require("./BaseProvider"); import BootStrapper = require("../../Bootstrapper"); +import BootstrapParams = require("../../BootstrapParams"); import ClickThroughDialogue = require("../../modules/uv-dialogues-module/ClickThroughDialogue"); import ExternalResource = require("./ExternalResource"); import IExtension = require("./IExtension"); @@ -226,8 +227,10 @@ class BaseExtension implements IExtension { setParams(): void{ if (!this.provider.isHomeDomain) return; - // set sequenceIndex hash param. + this.setParam(Params.collectionIndex, this.provider.collectionIndex); + this.setParam(Params.manifestIndex, this.provider.manifestIndex); this.setParam(Params.sequenceIndex, this.provider.sequenceIndex); + this.setParam(Params.canvasIndex, this.provider.canvasIndex); } setDefaultFocus(): void { @@ -369,17 +372,12 @@ class BaseExtension implements IExtension { return Shell.$overlays.is(':visible'); } - viewManifest(manifestIndex: number): void{ - //var seeAlsoUri = this.provider.getManifestSeeAlsoUri(manifest); - //if (seeAlsoUri){ - // window.open(seeAlsoUri, '_blank'); - //} else { - if (this.isFullScreen()) { - $.publish(BaseCommands.TOGGLE_FULLSCREEN); - } + viewManifest(manifest: Manifesto.IManifest): void{ + var p = new BootstrapParams(); + p.collectionIndex = this.provider.getCollectionIndex(manifest); + p.manifestIndex = manifest.index; - //this.triggerSocket(BaseCommands.SEQUENCE_INDEX_CHANGED, manifest.assetSequence); - //} + this.provider.reload(p); } inIframe(): boolean { diff --git a/src/modules/uv-shared-module/BaseProvider.ts b/src/modules/uv-shared-module/BaseProvider.ts index 9713177f0..50c99f835 100644 --- a/src/modules/uv-shared-module/BaseProvider.ts +++ b/src/modules/uv-shared-module/BaseProvider.ts @@ -13,6 +13,7 @@ class BaseProvider implements IProvider{ bootstrapper: BootStrapper; canvasIndex: number; + collectionIndex: number; config: any; domain: string; embedDomain: string; @@ -58,6 +59,7 @@ class BaseProvider implements IProvider{ this.domain = this.bootstrapper.params.domain; this.isLightbox = this.bootstrapper.params.isLightbox; + this.collectionIndex = this.bootstrapper.params.collectionIndex; this.manifestIndex = this.bootstrapper.params.manifestIndex; this.sequenceIndex = this.bootstrapper.params.sequenceIndex; this.canvasIndex = this.bootstrapper.params.canvasIndex; @@ -78,6 +80,11 @@ class BaseProvider implements IProvider{ this.bootstrapper.bootStrap(p); } + getCollectionIndex(iiifResource: Manifesto.IIIIFResource): number { + // todo: support nested collections. walk up parents adding to array and return csv string. + return iiifResource.parentCollection.index; + } + getManifestType(): Manifesto.ManifestType{ var manifestType = this.manifest.getManifestType(); @@ -293,7 +300,7 @@ class BaseProvider implements IProvider{ } getTree(): Manifesto.TreeNode{ - return this.manifest.getTree(); + return this.iiifResource.getTree(); } getDomain(): string{ diff --git a/src/modules/uv-shared-module/IExtension.ts b/src/modules/uv-shared-module/IExtension.ts index c63c81f88..56711b6a6 100644 --- a/src/modules/uv-shared-module/IExtension.ts +++ b/src/modules/uv-shared-module/IExtension.ts @@ -26,7 +26,7 @@ interface IExtension{ tabbing: boolean; triggerSocket(eventName: string, eventObject: any): void; viewCanvas(canvasIndex): void; - viewManifest(manifestIndex: number): void + viewManifest(manifest: Manifesto.IManifest): void; width(): number; } diff --git a/src/modules/uv-shared-module/IProvider.ts b/src/modules/uv-shared-module/IProvider.ts index 72e2cccb3..b7979d0df 100644 --- a/src/modules/uv-shared-module/IProvider.ts +++ b/src/modules/uv-shared-module/IProvider.ts @@ -6,7 +6,9 @@ import ExternalResource = require("./ExternalResource"); // interacting with the IIIF data model. interface IProvider{ canvasIndex: number; + collectionIndex: number; manifest: Manifesto.IIIIFResource; + manifestIndex: number; resources: Manifesto.IExternalResource[]; sequenceIndex: number; @@ -17,6 +19,7 @@ interface IProvider{ getCanvasIndexByLabel(label: string): number; getCanvasIndexParam(): number; getCanvasType(canvas?: Manifesto.ICanvas): Manifesto.CanvasType; + getCollectionIndex(iiifResource: Manifesto.IIIIFResource): number; getCurrentCanvas(): Manifesto.ICanvas; getCurrentSequence(): Manifesto.ISequence; getFirstPageIndex(): number; diff --git a/src/typings/extensions.d.ts b/src/typings/extensions.d.ts index 2714544f1..3e5a3cb31 100644 --- a/src/typings/extensions.d.ts +++ b/src/typings/extensions.d.ts @@ -38,6 +38,7 @@ interface String { b64_to_utf8(str: string): string; contains(str: string): boolean; endsWith(text: string): boolean; + hashCode(): string; ltrim(): string; rtrim(): string; startsWith(text: string): boolean; diff --git a/src/typings/manifesto.d.ts b/src/typings/manifesto.d.ts index 38d05d45b..5e06172ac 100644 --- a/src/typings/manifesto.d.ts +++ b/src/typings/manifesto.d.ts @@ -146,7 +146,10 @@ declare module Manifesto { declare var _assign: any; declare module Manifesto { class IIIFResource extends ManifestResource implements IIIIFResource { + index: number; isLoaded: boolean; + parentCollection: ICollection; + treeRoot: TreeNode; constructor(jsonld: any, options?: IManifestoOptions); getAttribution(): string; getDescription(): string; @@ -155,6 +158,7 @@ declare module Manifesto { getLicense(): string; getSeeAlso(): any; getTitle(): string; + getTree(): TreeNode; load(): Promise; } } @@ -162,9 +166,9 @@ declare var _isArray: any; declare var _map: any; declare module Manifesto { class Manifest extends IIIFResource implements IManifest { + index: number; rootRange: IRange; sequences: ISequence[]; - treeRoot: TreeNode; constructor(jsonld: any, options?: IManifestoOptions); getRanges(): IRange[]; getRangeById(id: string): IRange; @@ -186,6 +190,9 @@ declare module Manifesto { getManifestByIndex(manifestIndex: number): IManifest; getTotalCollections(): number; getTotalManifests(): number; + getTree(): TreeNode; + private _parseManifests(parentCollection); + private _parseCollections(parentCollection); } } declare module Manifesto { @@ -239,17 +246,17 @@ declare module Manifesto { class Deserialiser { static parse(manifest: string, options?: IManifestoOptions): IIIIFResource; static parseJson(json: any, options?: IManifestoOptions): IIIIFResource; - static parseCollection(json: any, options?: IManifestoOptions): Collection; - static parseCollections(collection: Collection, options?: IManifestoOptions): void; - static parseManifest(json: any, options?: IManifestoOptions): Manifest; - static parseManifests(collection: Collection, options?: IManifestoOptions): void; - static parseSequences(manifest: Manifest, options: IManifestoOptions): void; + static parseCollection(json: any, options?: IManifestoOptions): ICollection; + static parseCollections(collection: ICollection, options?: IManifestoOptions): void; + static parseManifest(json: any, options?: IManifestoOptions): IManifest; + static parseManifests(collection: ICollection, options?: IManifestoOptions): void; + static parseSequences(manifest: IManifest, options: IManifestoOptions): void; static parseCanvases(sequence: any, options: IManifestoOptions): ICanvas[]; - static parseRanges(manifest: Manifest, r: any, path: string, parentRange?: IRange): void; - static getCanvasById(manifest: Manifest, id: string): ICanvas; + static parseRanges(manifest: IManifest, r: any, path: string, parentRange?: IRange): void; + static getCanvasById(manifest: IManifest, id: string): ICanvas; } class Serialiser { - static serialise(manifest: Manifest): string; + static serialise(manifest: IManifest): string; } } declare var _endsWith: any; @@ -324,11 +331,12 @@ declare module Manifesto { } declare module Manifesto { interface ICollection extends IIIIFResource { + collections: ICollection[]; getCollectionByIndex(index: number): ICollection; getManifestByIndex(index: number): IManifest; getTotalCollections(): number; getTotalManifests(): number; - collections: ICollection[]; + getTree(): TreeNode; manifests: IManifest[]; } } @@ -354,15 +362,19 @@ declare module Manifesto { } declare module Manifesto { interface IIIIFResource extends IManifestResource { - isLoaded: boolean; getAttribution(): string; getDescription(): string; + getIIIFResourceType(): IIIFResourceType; getLicense(): string; getLogo(): string; getSeeAlso(): any; getTitle(): string; - getIIIFResourceType(): IIIFResourceType; + getTree(): TreeNode; + index: number; + isLoaded: boolean; load(): Promise; + parentCollection: ICollection; + treeRoot: TreeNode; } } declare module Manifesto { @@ -384,7 +396,6 @@ declare module Manifesto { isMultiSequence(): boolean; rootRange: IRange; sequences: ISequence[]; - treeRoot: TreeNode; } } declare module Manifesto {