Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle NAC edge-case #46

Merged
merged 3 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/reader/components/ReaderView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<script>
import {
MetadataWidget,
NewAlexandriaWidget,
PassageAncestorsWidget,
PassageChildrenWidget,
PassageReferenceWidget,
Expand All @@ -26,6 +25,7 @@
import ScholiaWidget from '@/widgets/ScholiaWidget.vue';
import AudioWidget from '@/widgets/AudioWidget.vue';
import WordListWidget from '@/widgets/WordListWidget.vue';
import NewAlexandriaWidget from '@/widgets/NewAlexandriaWidget.vue';
import { FETCH_METADATA, FETCH_LIBRARY } from '@/constants';

export default {
Expand Down
135 changes: 135 additions & 0 deletions src/widgets/NewAlexandriaWidget.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<script>
/*
This widget contains customizations for Explore Homer that handle
special-case queries tothe New Alexandria Commentary API.

In the future, we may want to take this type of customization
into consideration when implementing hooks / extensibility points
within `scaife-widgets`.

We may also find that the Vue Composition API
(https://composition-api.vuejs.org/) lends itself to these types of
customizations.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! composition API is going to change everything for us on Scaife.

*/
import gql from 'graphql-tag';

import WIDGETS_NS, {
NewAlexandriaWidget,
} from '@scaife-viewer/scaife-widgets';

export default {
extends: NewAlexandriaWidget,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool. Didn't know it was possible to do this.

name: 'NewAlexandriaWidget',
scaifeConfig: {
displayName: 'New Alexandria Commentary',
},
data: () => ({
passageTextParts: [],
healedPassage: '',
}),
computed: {
originalPassage() {
// the passage retrieved from the store
return this.$store.getters[`${WIDGETS_NS}/passage`];
},
passage() {
// overrides `NewAlexandriaWidget.computed.passage`
// so that only the healedPassage is passed to NAC
// via the `passage` watcher
return this.healedPassage;
},
},
watch: {
originalPassage: {
immediate: true,
handler() {
// special-case handling for the Folios version of
// the Iliad
if (
this.originalPassage.version ===
'urn:cts:greekLit:tlg0012.tlg001.msA-folios:'
jacobwegner marked this conversation as resolved.
Show resolved Hide resolved
) {
this.healFolioURN();
} else {
this.healedPassage = this.originalPassage.toString();
}
},
},
passageTextParts() {
// after we have retrieved passage text parts,
// extract the refs and populate healedPassage
const refs = this.extractPassageTextPartRefs();
const healedRefs = this.extractHealedRefs(refs);
this.updateHealedPassage(healedRefs);
},
},
methods: {
hasLinesInRef(ref) {
// good: 2r.1.1
// bad: 12r, 12r.1
return ref.split('.').length === 3;
},
healFolioURN() {
const refs = this.originalPassage.reference.split('-');
if (this.hasLinesInRef(refs[0])) {
const healedRefs = this.extractHealedRefs(refs);
this.updateHealedPassage(healedRefs);
} else {
this.fetchPassageTextParts();
}
},
extractHealedRefs(refs) {
// pops the folio off of refs,
// e.g. 12r.1.1-12r.1.7 --> 1.1-1.7
const healedRefs = [];
refs.forEach(ref => {
const refParts = ref.split('.');
healedRefs.push(refParts.slice(1).join('.'));
});
return healedRefs;
},
updateHealedPassage(refs) {
if (refs.length > 0) {
const refPart = refs.join('-');
this.healedPassage = `${this.originalPassage.version}${refPart}`;
}
},
fetchPassageTextParts() {
// retrieves the lowest text parts for a folio level reference,
// e.g. 12r --> 12r.1.1-12.r.1.25
const query = gql`
{
passageTextParts(
reference: "${this.originalPassage}"
) {
edges {
node {
id
ref
}
}
}
}
`;
this.$gql(query).then(data => {
this.passageTextParts = data.passageTextParts.edges.map(edge => {
const { node } = edge;
return node;
});
});
return query;
},
extractPassageTextPartRefs() {
// extracts the ref(s) from the text parts
const refs = [];
// start ref
refs.push(this.passageTextParts.slice(0, 1)[0].ref);
if (this.passageTextParts.length > 1) {
// end ref
refs.push(this.passageTextParts.slice(-1)[0].ref);
}
return refs;
},
},
};
</script>