Skip to content

Commit

Permalink
Export additional properties with photo
Browse files Browse the repository at this point in the history
  • Loading branch information
styts committed Dec 21, 2017
1 parent e307ab8 commit 545d6e7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
36 changes: 31 additions & 5 deletions src/export/export.js
Expand Up @@ -7,13 +7,37 @@ const { ITEM, PHOTO, SELECTION } = require('../constants/type')
const makeNote = require('./note')

const TR = 'https://tropy.org/v1/tropy#'
const IFFF = 'http://iiif.io/api/image/2#'

const PROP = {
TEMPLATE: `${TR}template`,
ITEM: `${TR}item`,
PHOTO: `${TR}photo`,
SELECTION: `${TR}selection`,
NOTE: `${TR}note`
NOTE: `${TR}note`,
WIDTH: `${IFFF}width`,
HEIGHT: `${IFFF}height`,
}

const EXPORT_PROPERTIES = {
SELECTION: [
'angle',
'height',
'mirror',
'width',
'x',
'y',
],
PHOTO: [
'angle',
'checksum',
'height',
'mimetype',
'mirror',
'orientation',
'size',
'width',
]
}

const { newProperties } = require('./utils')
Expand Down Expand Up @@ -41,7 +65,9 @@ function makeContext(template, items, resources) {
'@id': PROP.SELECTION,
'@container': '@list',
'@context': {}
}
},
width: PROP.WIDTH,
height: PROP.HEIGHT
}
}
}
Expand Down Expand Up @@ -86,15 +112,14 @@ function addInfo(target, ids, key, state, fn = x => x.name) {
}

function addSelections(template, photo, ids, resources) {
const selProps = ['x', 'y', 'width', 'height', 'angle', 'mirror', 'template']
const [props, metadata,,,, notes, selections] = resources

if (ids) {
photo.selection = ids.map(sID => {
let selection = { '@type': SELECTION }
const original = selections[sID]
// add selection properties
Object.assign(selection, pick(original, selProps))
Object.assign(selection, pick(original, EXPORT_PROPERTIES.SELECTION))

// add selection notes
selection = addInfo(selection, original.notes, 'note', notes, makeNote)
Expand Down Expand Up @@ -130,7 +155,8 @@ function renderItem(item, template, resources) {
let photo = {
'@type': PHOTO,
'path': p.path,
'selection': []
'selection': [],
...pick(p, EXPORT_PROPERTIES.PHOTO)
}

photo = newProperties(metadata[p.id], photo, false, props, template)
Expand Down
20 changes: 14 additions & 6 deletions test/export/export_test.js
Expand Up @@ -60,12 +60,11 @@ describe('export', () => {
it('has photo metadata', async () => {
const data = (await ld)[0]['@graph'][0]
expect(keys(data.photo).length).to.eql(2)
expect(data.photo[0]).to.eql({
'@type': 'Photo',
'path': '/path',
'helloWorld': 'photo property',
'nonTemplateProperty': 'custom property'
})
const photo = data.photo[0]
expect(photo).to.have.property('@type', 'Photo')
expect(photo).to.have.property('path', '/path')
expect(photo).to.have.property('helloWorld', 'photo property')
expect(photo).to.have.property('nonTemplateProperty', 'custom property')
})

it('has photo->selection @context', async () => {
Expand Down Expand Up @@ -106,6 +105,15 @@ describe('export', () => {
expect(item1.photo[1]['note']['text']).to.eql('photo note')
expect(item1.photo[1]['note']['html']).to.eql('<p>photo note</p>')
})

it('width, etc.', async () => {
const item1 = (await ld)[0]['@graph'][0]
expect(item1.photo[1]).to.not.have.property('width')
const photo = item1.photo[0]
expect(photo).to.have.property('width', 30)
expect(photo).to.have.property('height', 40)
})

})

describe('selection has', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/export.js
Expand Up @@ -66,7 +66,7 @@ const props = {
}

const photos = {
11: { id: 11, path: '/path' },
11: { id: 11, path: '/path', width: 30, height: 40 },
12: { id: 12, path: '/another', selections: [21], notes: [1] }
}

Expand Down

0 comments on commit 545d6e7

Please sign in to comment.