forked from ProjectMirador/mirador
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
COLUMBIA: Core mirador behaviors to provide a plugin target for text …
…resources - refactor type-based filters into a module - MiradorCanvas.imagesResources does not assume any service is an image service - stub TextViewer shows empty div, source elements for text resources, and canvas navigation - fixes ProjectMirador#4085
- Loading branch information
Showing
5 changed files
with
156 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Utils } from 'manifesto.js'; | ||
import flattenDeep from 'lodash/flattenDeep'; | ||
import manifestFixture019 from '../../fixtures/version-2/019.json'; | ||
import { | ||
filterByProfiles, filterByTypes, | ||
} from '../../../src/lib/resourceFilters'; | ||
|
||
describe('resourceFilters', () => { | ||
let canvas; | ||
beforeEach(() => { | ||
[canvas] = Utils.parseManifest(manifestFixture019).getSequences()[0].getCanvases(); | ||
}); | ||
describe('filterByProfiles', () => { | ||
it('filters resources', () => { | ||
const services = flattenDeep(canvas.resourceAnnotations.map((a) => a.getResource().getServices())); | ||
expect(filterByProfiles(services, 'http://iiif.io/api/image/2/level2.json').map((s) => s.id)).toEqual([ | ||
'https://stacks.stanford.edu/image/iiif/hg676jb4964%2F0380_796-44', | ||
]); | ||
expect(filterByProfiles(services, 'http://nonexistent.io/api/service.json').map((s) => s.id)).toEqual([]); | ||
}); | ||
}); | ||
describe('filterByTypes', () => { | ||
it('filters resources', () => { | ||
const resources = flattenDeep(canvas.resourceAnnotations.map((a) => a.getResource())); | ||
expect(filterByTypes(resources, 'dctypes:Image').map((r) => r.id)).toEqual([ | ||
'https://stacks.stanford.edu/image/iiif/hg676jb4964%2F0380_796-44/full/full/0/default.jpg', | ||
]); | ||
expect(filterByTypes(resources, 'Nonexistent').map((r) => r.id)).toEqual([]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** values for type/@type that indicate an image content resource */ | ||
const imageTypes = ['Image', 'StillImage', 'dctypes:Image', 'dctypes:StillImage']; | ||
|
||
/** values for type/@type that indicate a sound content resource */ | ||
const audioTypes = ['Audio', 'Sound', 'dctypes:Audio', 'dctypes:Sound']; | ||
|
||
/** values for type/@type that indicate a choice resource */ | ||
const choiceTypes = ['oa:Choice']; | ||
|
||
/** values for type/@type that indicate a text content resource */ | ||
const textTypes = ['Document', 'Text', 'dctypes:Document', 'dctypes:Text']; | ||
|
||
/** values for type/@type that indicate a video content resource */ | ||
const videoTypes = ['Video', 'MovingImage', 'dctypes:Video', 'dctypes:MovingImage']; | ||
|
||
export default { | ||
audioTypes, | ||
choiceTypes, | ||
imageTypes, | ||
textTypes, | ||
videoTypes, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import canvasTypes from './canvasTypes'; | ||
import serviceProfiles from './serviceProfiles'; | ||
|
||
/** | ||
* Filter resources by profile property in given profiles | ||
*/ | ||
export function filterByProfiles(resources, profiles) { | ||
if (profiles === undefined || resources === undefined) return []; | ||
|
||
if (!Array.isArray(profiles)) { | ||
return resources.filter((resource) => profiles === resource.getProperty('profile')); | ||
} | ||
|
||
return resources.filter((resource) => profiles.includes(resource.getProperty('profile'))); | ||
} | ||
|
||
/** | ||
* Filter resources by type property in given types | ||
*/ | ||
export function filterByTypes(resources, types) { | ||
if (types === undefined || resources === undefined) return []; | ||
|
||
if (!Array.isArray(types)) { | ||
return resources.filter((resource) => types === resource.getProperty('type')); | ||
} | ||
|
||
return resources.filter((resource) => types.includes(resource.getProperty('type'))); | ||
} | ||
|
||
/** */ | ||
export function audioResourcesFrom(resources) { | ||
return filterByTypes(resources, canvasTypes.audioTypes); | ||
} | ||
|
||
/** */ | ||
export function choiceResourcesFrom(resources) { | ||
return filterByTypes(resources, canvasTypes.choiceTypes); | ||
} | ||
|
||
/** | ||
*/ | ||
export function imageServicesFrom(services) { | ||
return filterByProfiles(services, serviceProfiles.iiifImageProfiles); | ||
} | ||
|
||
/** */ | ||
export function hasImageService(resource) { | ||
const imageServices = imageServicesFrom(resource ? resource.getServices() : []); | ||
return imageServices[0] && imageServices[0].id; | ||
} | ||
|
||
/** */ | ||
export function iiifImageResourcesFrom(resources) { | ||
return imageResourcesFrom(resources).filter((r) => hasImageService(r)); | ||
} | ||
|
||
/** */ | ||
export function imageResourcesFrom(resources) { | ||
return filterByTypes(resources, canvasTypes.imageTypes); | ||
} | ||
|
||
/** */ | ||
export function textResourcesFrom(resources) { | ||
return filterByTypes(resources, canvasTypes.textTypes); | ||
} | ||
|
||
/** */ | ||
export function videoResourcesFrom(resources) { | ||
return filterByTypes(resources, canvasTypes.videoTypes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** values for profile that indicate an image service */ | ||
const iiifImageProfiles = [ | ||
'level2', | ||
'level1', | ||
'level0', | ||
'http://iiif.io/api/image/2/level2.json', | ||
'http://iiif.io/api/image/2/level1.json', | ||
'http://iiif.io/api/image/2/level0.json', | ||
]; | ||
|
||
export default { | ||
iiifImageProfiles, | ||
}; |