mirador
Version:
An open-source, web-based 'multi-up' viewer that supports zoom-pan-rotate functionality, ability to display/compare simple images, and images with annotations.
182 lines (146 loc) • 4.36 kB
JavaScript
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
import compact from 'lodash/compact';
import flatten from 'lodash/flatten';
import { v4 as uuid } from 'uuid';
/** */
var AnnotationResource = /*#__PURE__*/function () {
/** */
function AnnotationResource() {
var resource = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, AnnotationResource);
this.resource = resource;
}
/** */
_createClass(AnnotationResource, [{
key: "isOnlyTag",
value: function isOnlyTag() {
return this.motivations.length === 1 && this.motivations[0] === 'oa:tagging';
}
/** */
}, {
key: "id",
get: function get() {
this._id = this._id || this.resource['@id'] || this.resources[0] && this.resources[0]['@id'] || uuid();
return this._id; // eslint-disable-line no-underscore-dangle
}
/** */
}, {
key: "targetId",
get: function get() {
var on = this.on[0];
switch (typeof on) {
case 'string':
return on.replace(/#?xywh=(.*)$/, '');
case 'object':
return on.full.replace(/#?xywh=(.*)$/, '');
default:
return null;
}
}
/**
* @return {[Array]}
*/
}, {
key: "motivations",
get: function get() {
return flatten(compact(new Array(this.resource.motivation)));
}
/** */
}, {
key: "resources",
get: function get() {
return flatten(compact(new Array(this.resource.resource)));
}
/** */
}, {
key: "on",
get: function get() {
return flatten(compact(new Array(this.resource.on)));
}
/** */
}, {
key: "tags",
get: function get() {
if (this.isOnlyTag()) {
return this.resources.map(function (r) {
return r.chars;
});
}
return this.resources.filter(function (r) {
return r['@type'] === 'oa:Tag';
}).map(function (r) {
return r.chars;
});
}
/** */
}, {
key: "chars",
get: function get() {
return this.resources.filter(function (r) {
return r['@type'] !== 'oa:Tag';
}).map(function (r) {
return r.chars;
}).join(' ');
}
/** */
}, {
key: "selector",
get: function get() {
var on = this.on[0];
switch (typeof on) {
case 'string':
return on;
case 'object':
// For choices, just return the default for now. FIXME: enhance for SVG
// selectors
if (on.selector['@type'] === 'oa:Choice') {
return on.selector["default"];
}
return on.selector;
default:
return null;
}
}
/** */
}, {
key: "svgSelector",
get: function get() {
var on = this.on[0];
switch (typeof on) {
case 'string':
return null;
case 'object':
if (on.selector && on.selector.item && on.selector.item['@type'] === 'oa:SvgSelector') {
return on.selector.item;
}
return null;
default:
return null;
}
}
/** */
}, {
key: "fragmentSelector",
get: function get() {
var selector = this.selector;
var match;
switch (typeof selector) {
case 'string':
match = selector.match(/xywh=(.*)$/);
break;
case 'object':
match = selector.value.match(/xywh=(.*)$/);
break;
default:
return null;
}
return match && match[1].split(',').map(function (str) {
return parseInt(str, 10);
});
}
}]);
return AnnotationResource;
}();
export { AnnotationResource as default };