Commit 4677e16f authored by Eduardo Sanz García's avatar Eduardo Sanz García Committed by Eduardo

Cleanup bucket bar

- Added types
- Moved out of the plugin directory
- Avoid extending from Delegator (unnecessary)
parent e19983b0
import Delegator from '../delegator';
import { render } from 'preact'; import { render } from 'preact';
import Buckets from '../components/Buckets'; import Buckets from './components/Buckets';
import { anchorBuckets } from '../util/buckets';
export default class BucketBar extends Delegator { import { anchorBuckets } from './util/buckets';
constructor(container, options, annotator) {
const defaultOptions = {
// Selectors for the scrollable elements on the page
scrollables: [],
};
const opts = { ...defaultOptions, ...options }; /**
* @typedef BucketBarOptions
* @prop {string[]} [scrollables] - Selectors for the scrollable elements on the page
*/
const el = document.createElement('div'); export default class BucketBar {
el.className = 'annotator-bucket-bar'; /**
super(el, opts); * @param {HTMLElement} container
* @param {Pick<import('./guest').default, 'anchors'|'selectAnnotations'>} guest
* @param {BucketBarOptions} [options]
*/
constructor(container, guest, options = {}) {
this.options = options;
this.element = document.createElement('div');
this.element.className = 'annotator-bucket-bar';
this.annotator = annotator; this.guest = guest;
container.appendChild(this.element); container.appendChild(this.element);
this.updateFunc = () => this.update(); this.updateFunc = () => this.update();
window.addEventListener('resize', this.updateFunc); window.addEventListener('resize', this.updateFunc);
window.addEventListener('scroll', this.updateFunc); window.addEventListener('scroll', this.updateFunc);
this.options.scrollables.forEach(scrollable => { this.options.scrollables?.forEach(scrollable => {
const scrollableElement = /** @type {HTMLElement | null} */ (document.querySelector( const scrollableElement = /** @type {HTMLElement | null} */ (document.querySelector(
scrollable scrollable
)); ));
...@@ -36,7 +37,7 @@ export default class BucketBar extends Delegator { ...@@ -36,7 +37,7 @@ export default class BucketBar extends Delegator {
destroy() { destroy() {
window.removeEventListener('resize', this.updateFunc); window.removeEventListener('resize', this.updateFunc);
window.removeEventListener('scroll', this.updateFunc); window.removeEventListener('scroll', this.updateFunc);
this.options.scrollables.forEach(scrollable => { this.options.scrollables?.forEach(scrollable => {
const scrollableElement = /** @type {HTMLElement | null} */ (document.querySelector( const scrollableElement = /** @type {HTMLElement | null} */ (document.querySelector(
scrollable scrollable
)); ));
...@@ -56,14 +57,14 @@ export default class BucketBar extends Delegator { ...@@ -56,14 +57,14 @@ export default class BucketBar extends Delegator {
} }
_update() { _update() {
const buckets = anchorBuckets(this.annotator.anchors); const buckets = anchorBuckets(this.guest.anchors);
render( render(
<Buckets <Buckets
above={buckets.above} above={buckets.above}
below={buckets.below} below={buckets.below}
buckets={buckets.buckets} buckets={buckets.buckets}
onSelectAnnotations={(annotations, toggle) => onSelectAnnotations={(annotations, toggle) =>
this.annotator.selectAnnotations(annotations, toggle) this.guest.selectAnnotations(annotations, toggle)
} }
/>, />,
this.element this.element
......
...@@ -9,7 +9,7 @@ import features from './features'; ...@@ -9,7 +9,7 @@ import features from './features';
import Guest from './guest'; import Guest from './guest';
import { ToolbarController } from './toolbar'; import { ToolbarController } from './toolbar';
import { createShadowRoot } from './util/shadow-root'; import { createShadowRoot } from './util/shadow-root';
import BucketBar from './plugin/bucket-bar'; import BucketBar from './bucket-bar';
/** /**
* @typedef LayoutState * @typedef LayoutState
...@@ -78,9 +78,7 @@ export default class Sidebar extends Guest { ...@@ -78,9 +78,7 @@ export default class Sidebar extends Guest {
if (config.theme === 'clean') { if (config.theme === 'clean') {
frame.classList.add('annotator-frame--theme-clean'); frame.classList.add('annotator-frame--theme-clean');
} else { } else {
// BucketBar is a "plugin" for legacy reasons and is now constructed here so this.plugins.BucketBar = new BucketBar(frame, this, config.BucketBar);
// that the parent element can be passed into the constructor.
this.plugins.BucketBar = new BucketBar(frame, config.BucketBar, this);
} }
// Undocumented switch to enable/disable the wrapping of the sidebar inside a shadow DOM // Undocumented switch to enable/disable the wrapping of the sidebar inside a shadow DOM
......
...@@ -10,7 +10,7 @@ describe('BucketBar', () => { ...@@ -10,7 +10,7 @@ describe('BucketBar', () => {
const createBucketBar = function (options) { const createBucketBar = function (options) {
const element = document.createElement('div'); const element = document.createElement('div');
return new BucketBar(element, options || {}, fakeAnnotator); return new BucketBar(element, fakeAnnotator, options);
}; };
beforeEach(() => { beforeEach(() => {
...@@ -30,8 +30,8 @@ describe('BucketBar', () => { ...@@ -30,8 +30,8 @@ describe('BucketBar', () => {
}; };
$imports.$mock({ $imports.$mock({
'../components/Buckets': FakeBuckets, './components/Buckets': FakeBuckets,
'../util/buckets': fakeBucketUtil, './util/buckets': fakeBucketUtil,
}); });
sandbox.stub(window, 'requestAnimationFrame').yields(); sandbox.stub(window, 'requestAnimationFrame').yields();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment