-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpageable-mixin.js
68 lines (52 loc) · 1.85 KB
/
pageable-mixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { CollectionMixin } from '../../mixins/collection/collection-mixin.js';
import { html } from 'lit';
import { SubscriberRegistryController } from '../../controllers/subscriber/subscriberControllers.js';
export const PageableMixin = superclass => class extends CollectionMixin(superclass) {
static get properties() {
return {
_itemShowingCount: { state: true },
};
}
constructor() {
super();
this._itemShowingCount = 0;
this._pageableSubscriberRegistry = new SubscriberRegistryController(this, 'pageable', {
onSubscribe: this._updatePageableSubscriber.bind(this),
onUnsubscribe: this._clearPageableSubscriber.bind(this),
updateSubscribers: this._updatePageableSubscribers.bind(this)
});
}
firstUpdated(changedProperties) {
super.firstUpdated(changedProperties);
this._updateItemShowingCount();
}
updated(changedProperties) {
super.updated(changedProperties);
if (changedProperties.has('itemCount') || changedProperties.has('_itemShowingCount')) {
this._pageableSubscriberRegistry.updateSubscribers();
}
}
_clearPageableSubscriber(subscriber) {
subscriber._pageableInfo = null;
}
/* must be implemented by consumer */
_getItemByIndex(index) { } // eslint-disable-line no-unused-vars
/* must be implemented by consumer */
_getItemShowingCount() { }
_getLastItemIndex() {
return this._itemShowingCount - 1;
}
_renderPagerContainer() {
return html`<slot name="pager"></slot>`;
}
_updateItemShowingCount() {
this._itemShowingCount = this._getItemShowingCount();
}
_updatePageableSubscriber(subscriber, doUpdate = true) {
if (doUpdate) this._updateItemShowingCount();
subscriber._pageableInfo = { itemShowingCount: this._itemShowingCount, itemCount: this.itemCount };
}
_updatePageableSubscribers(subscribers) {
subscribers.forEach(subscriber => this._updatePageableSubscriber(subscriber, false));
}
};