www.fgks.org   »   [go: up one dir, main page]

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

Library Explorer Tweaks + ?jumpTo alpha release #4617

Merged
merged 15 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 16 additions & 3 deletions openlibrary/components/LibraryExplorer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
:class="bookRoomClass"
:features="bookRoomFeatures"
:appSettings="settingsState"
:jumpTo="jumpTo"
/>

<LibraryToolbar :filterState="filterState" :settingsState="settingsState" :sortState="sortState" />
Expand Down Expand Up @@ -62,14 +63,15 @@ export default {
LibraryToolbar,
},
data() {
/** @type {import('./LibraryExplorer/utils').ClassificationTree[]} */
const classifications = [
{
name: 'DDC',
longName: 'Dewey Decimal Classification',
field: 'ddc',
fieldTransform: ddc => ddc,
chooseBest: ddcs => maxBy(ddcs, ddc => ddc.replace(/[\d.]/g, '') ? ddc.length : 100 + ddc.length),
root: recurForEach({ children: DDC }, n => {
root: recurForEach({ children: DDC, query: 'ddc:*' }, n => {
n.position = 'root';
n.offset = 0;
n.requests = {};
Expand All @@ -87,22 +89,33 @@ export default {
.replace(/-+/, '')
.replace(/0+(\.\D)/, ($0, $1) => $1),
chooseBest: lccs => maxBy(lccs, lcc => lcc.length),
root: recurForEach({ children: LCC }, n => {
root: recurForEach({ children: LCC, query: 'lcc:*' }, n => {
n.position = 'root';
n.offset = 0;
n.requests = {};
})
}
];

const urlParams = new URLSearchParams(location.search);
let selectedClassification = classifications[0];
let jumpTo = null;
if (urlParams.has('jumpTo')) {
const [classificationName, classificationString] = urlParams.get('jumpTo').split(':');
selectedClassification = classifications.find(c => c.field == classificationName);
jumpTo = classificationString;
}
return {
filterState: new FilterState(),

sortState: {
order: 'editions',
},

jumpTo,

settingsState: {
selectedClassification: classifications[0],
selectedClassification,
classifications,

labels: ['classification'],
Expand Down
47 changes: 45 additions & 2 deletions openlibrary/components/LibraryExplorer/components/BookRoom.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,33 @@ import RightArrowIcon from './icons/RightArrowIcon.vue';
import ExpandIcon from './icons/ExpandIcon.vue';
import debounce from 'lodash/debounce';
import Vue from 'vue';
import { hierarchyFind, testLuceneSyntax } from '../utils.js';
/** @typedef {import('../utils.js').ClassificationNode} ClassificationNode */

/**
cdrini marked this conversation as resolved.
Show resolved Hide resolved
* @param {ClassificationNode} classificationNode
* @param {string} classification (e.g. 658.91500202854)
*/
function findClassification(classificationNode, classification) {
// First we find the closest matching node in the current classification tree
const path = hierarchyFind(
classificationNode,
node => testLuceneSyntax(node.query.split(':')[1], classification));
if (!path.length) return;

// Jump as deep into it as we can. I.e. the last node is the shelf, the second last the bookcase, and the 3rd last is the room.
if (path.length > 3) {
// e.g. [658, 65X, 6XX]
const [shelf, bookcase, room] = path.reverse();
path.reverse();
return {
room,
bookcase,
shelf,
breadcrumbs: path.slice(0, -3),
};
}
}

export default {
components: {
Expand All @@ -88,9 +115,12 @@ export default {
ExpandIcon,
},
props: {
/** @type {import('../utils.js').ClassificationTree} */
classification: Object,
appSettings: Object,

/** The classification to jump to @example 658.91500202854 */
jumpTo: String,
sort: String,
filter: {
default: '',
Expand All @@ -114,9 +144,12 @@ export default {
}
},
data() {
const jumpToData = this.jumpTo && findClassification(this.classification.root, this.jumpTo);

return {
activeRoom: this.classification.root,
breadcrumbs: [],
activeRoom: jumpToData?.room || this.classification.root,
breadcrumbs: jumpToData?.breadcrumbs || [],
jumpToData,

expandingAnimation: false,

Expand All @@ -132,6 +165,12 @@ export default {
},
mounted() {
this.updateWidths();
if (this.jumpToData) {
this.$el.querySelector(`[data-short="${this.jumpToData.shelf.short}"]`).scrollIntoView({
inline: 'center',
block: 'center',
});
}
},
destroyed() {
window.removeEventListener('resize', this.debouncedUpdateWidths);
Expand All @@ -151,6 +190,10 @@ export default {
}
},
methods: {
/**
* @param {ClassificationNode} bookshelf something that is currently a bookcase, that will be the new room
* @param {ClassificationNode} [shelf] the shelf (child of bookshelf)
*/
async expandBookshelf(bookshelf, shelf=null) {
this.expandingAnimation = true;
await new Promise(r => setTimeout(r, 200));
Expand Down
57 changes: 57 additions & 0 deletions openlibrary/components/LibraryExplorer/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,60 @@ export function hashCode(str) {
}
return hash;
}

/*
* Given a hierarchical tree, finds the path from the root to the deepest matching node
* @template {{ children: T[] }} T
* @param {T} node
* @param {(node: T) => boolean} predicate
* @returns {T[]}
*/
export function hierarchyFind(node, predicate) {
if (!predicate(node)) return [];
for (const child of (node.children || [])) {
const childResult = hierarchyFind(child, predicate);
if (childResult.length) return [node, ...childResult];
}
return [node];
}

/**
* OBVIOUSLY this is a HUGE simplification; only supports:
* - range queries (e.g. `[KJ TO KKZ]`)
* - prefix queries (e.g. `0*`)
* @param {string} pattern
* @param {string} string
*/
export function testLuceneSyntax(pattern, string) {
if (pattern.endsWith('*')) {
return string.startsWith(pattern.slice(0, -1));
} else if (pattern.endsWith(']')) {
const [lo, hi] = pattern.slice(1, -1).split(' TO ');
return string >= lo && string <= hi;
} else {
throw new Error(`Unsupported lucene syntax: ${pattern}`);
}
}

/**
* @typedef {object} ClassificationNode
* @property {string} name
* @property {string} short
* @property {string} query
* @property {number} count
* @property {ClassificationNode[]} children
* Internal (not in ddc/lcc.json):
* @property {string | number} position
* @property {number} offset
* @property {object} requests
*/

/**
* @typedef {object} ClassificationTree
* @property {string} name
* @property {string} longName
* @property {string} field
* @property {(classification: string) => string} fieldTransform
* @property {(classifications: string[]) => string} chooseBest
* @property {ClassificationNode} root
*/