@iiif/3d-manifesto-dev
Version:
IIIF Presentation API utility library for client and server with 3D extension
195 lines (191 loc) • 8.39 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { AnnotationBodyParser, AnnotationPage, ManifestResource, Resource, SpecificResource, TextualBody, } from "./internal";
import { Vector3 } from "threejs-math";
var Annotation = /** @class */ (function (_super) {
__extends(Annotation, _super);
function Annotation(jsonld, options) {
return _super.call(this, jsonld, options) || this;
}
/**
In spite of its name, this method returns an array of objects, each of which
represents a potential body annotations
@see{ https://iiif.io/api/cookbook/recipe/0033-choice/ }
**/
Annotation.prototype.getBody = function () {
var bodies = [];
/*
A bodyValue property in the annotation json will short circuit
the parsing process and be interpreted as a shorthand version of
a TextualBody resource defining as the body
This procedure is allowed, see Web Annotation Data Model section 3.2.5
https://www.w3.org/TR/annotation-model/#string-body
*/
var stringBody = this.getProperty("bodyValue");
//console.log("retrieved stringBody " + stringBody);
if (stringBody) {
return [
new TextualBody({
id: "https://example.com/TextualBody/1",
value: stringBody,
type: "TextualBody",
}, this.options),
];
}
var body = this.getProperty("body");
// the following is intended to handle the following cases for
/// the raw json of the body property of __jsonld
// -- body is an array, each element of which is parsed
// == body is an object with property items, each item is parsed
// -- body is parsed
if (body) {
for (var _i = 0, _a = [].concat(body); _i < _a.length; _i++) {
var bd = _a[_i];
var items = bd.items;
if (items)
bodies = bodies.concat(this.parseBodiesFromItemsList(items));
else
bodies.push(this.parseSingletonBody(bd));
}
}
return bodies;
};
Object.defineProperty(Annotation.prototype, "Body", {
get: function () {
return this.getBody();
},
enumerable: false,
configurable: true
});
/**
auxiliary function to getBody; intended to hande an object that has an element items
which is a array of annotation- body-like objects. This : https://iiif.io/api/cookbook/recipe/0033-choice/
seems to be the use case for this
**/
Annotation.prototype.parseBodiesFromItemsList = function (rawbodies) {
var retVal = [];
for (var _i = 0, _a = [].concat(rawbodies); _i < _a.length; _i++) {
var bd = _a[_i];
retVal.push(this.parseSingletonBody(bd));
}
return retVal;
};
/**
auxiliary function to parseBodiesFromItemsList and getBody, this is the last
step on recursively going through collections of bodies.
**/
Annotation.prototype.parseSingletonBody = function (rawbody) {
return AnnotationBodyParser.BuildFromJson(rawbody, this.options);
};
/**
Developer Note: 8 April 2024
getBody3D function was developed in the early stages of the 3D API Feb-March 2024
as alternative to the existing Annotation getBody function, but the signature for
getBody3D was chosen to be a single object instance, not an array.
At this stage, the merging of the 2D API anf the draft 3D API has been completed, so
3D applications can use the getBody() function to retrieve the body of an Annotation intended
to target a scene. For compatibily the return value of the function is still an
array.
3D clients using getBody are responsible for choosing the appropriate instance from the
returned array. In most cases this will be the sole 0th element.
**/
Annotation.prototype.getBody3D = function () {
console.warn("Annotation.getBody3D is deprecated: replace with getBody3D() with getBody()[0]");
return this.getBody()[0];
};
Annotation.prototype.getMotivation = function () {
var motivation = this.getProperty("motivation");
if (motivation) {
//const key: string | undefined = Object.keys(AnnotationMotivationEnum).find(k => AnnotationMotivationEnum[k] === motivation);
return motivation;
}
return null;
};
// open annotation
Annotation.prototype.getOn = function () {
return this.getProperty("on");
};
Annotation.prototype.getTarget = function () {
var rawTarget = this.getPropertyAsObject("target");
if (rawTarget.isIRI)
return rawTarget;
if (rawTarget.type && rawTarget.type == "SpecificResource") {
return new SpecificResource(rawTarget, this.options);
}
else if (["Scene", "Canvas"].includes(rawTarget.type)) {
return rawTarget;
}
else {
throw new Error("unknown target specified");
}
};
Object.defineProperty(Annotation.prototype, "Target", {
get: function () {
return this.getTarget();
},
enumerable: false,
configurable: true
});
// Retrieves target scope content state annotations
Annotation.prototype.getScopeContent = function () {
var _this = this;
var _a, _b, _c;
var items = (_c = (_b = (_a = this.getTarget()) === null || _a === void 0 ? void 0 : _a.getScope()) === null || _b === void 0 ? void 0 : _b.getTarget()) === null || _c === void 0 ? void 0 : _c.items;
if (!items)
return [];
return items
.filter(function (item) { return item && item.type === "AnnotationPage"; })
.map(function (item) { return new AnnotationPage(item, _this.options).getItems(); })
.flat()
.filter(function (item) { return item && item.type === "Annotation"; })
.map(function (annotation) { return new Annotation(annotation, _this.options); });
};
Object.defineProperty(Annotation.prototype, "ScopeContent", {
get: function () {
return this.getScopeContent();
},
enumerable: false,
configurable: true
});
Annotation.prototype.getResource = function () {
return new Resource(this.getProperty("resource"), this.options);
};
Object.defineProperty(Annotation.prototype, "LookAtLocation", {
/**
* A 3D point coordinate object for the location of an Annotation
* to satisfy the requirements of the lookAt property of camera and
* spotlight resources, according to the draft v4 API as of April 1 2024
*
* Is the position of the point for a target which is a SpecificResource with
* a PointSelector
* Otherwise, for example when the annotation target is an entire Scene, the
* location for lookAt is the origin (0,0,0)
**/
get: function () {
var _a;
var target = this.getTarget();
if (target.isSpecificResource && ((_a = target.getSelector()) === null || _a === void 0 ? void 0 : _a.isPointSelector))
return target.getSelector().getLocation();
else
return new Vector3(0.0, 0.0, 0.0);
},
enumerable: false,
configurable: true
});
return Annotation;
}(ManifestResource));
export { Annotation };
//# sourceMappingURL=Annotation.js.map