1 | import {getRootRect} from "./dimensions";
|
2 | import * as Events from './events';
|
3 |
|
4 | function getPageBreak(pageBreaks, scrollTop, scrollContainer) {
|
5 | let rootRect = getRootRect(scrollContainer);
|
6 | let scrollBottom = scrollTop + rootRect.height;
|
7 |
|
8 | for (let b = pageBreaks.length - 1; b >= 0; b--) {
|
9 | let bottom = pageBreaks[b].sentinel.getBoundingClientRect().bottom + scrollTop;
|
10 |
|
11 | if (scrollBottom > bottom) {
|
12 | let x = Math.min(b + 1, pageBreaks.length - 1);
|
13 |
|
14 | return pageBreaks[x];
|
15 | }
|
16 | }
|
17 |
|
18 | return pageBreaks[0];
|
19 | }
|
20 |
|
21 | export default class Paging {
|
22 | constructor(ias) {
|
23 | this.ias = ias;
|
24 | this.pageBreaks = [];
|
25 | this.currentPageIndex = ias.pageIndex;
|
26 | this.currentScrollTop = 0;
|
27 |
|
28 | ias.on(Events.BINDED, this.binded.bind(this));
|
29 | ias.on(Events.NEXT, this.next.bind(this));
|
30 | ias.on(Events.PREV, this.prev.bind(this));
|
31 | ias.on(Events.SCROLLED, this.scrolled.bind(this));
|
32 | ias.on(Events.RESIZED, this.scrolled.bind(this));
|
33 | }
|
34 |
|
35 | binded() {
|
36 | let sentinel = this.ias.sentinel();
|
37 | if (!sentinel) {
|
38 | return;
|
39 | }
|
40 |
|
41 | this.pageBreaks.push({
|
42 | pageIndex: this.currentPageIndex,
|
43 | url: document.location.toString(),
|
44 | title: document.title,
|
45 | sentinel: this.ias.sentinel()
|
46 | });
|
47 | }
|
48 |
|
49 | next() {
|
50 | let url = document.location.toString();
|
51 | let title = document.title;
|
52 |
|
53 | let loaded = (event) => {
|
54 | url = event.url;
|
55 |
|
56 | if (event.xhr.response) {
|
57 | title = event.xhr.response.title
|
58 | }
|
59 | };
|
60 |
|
61 | this.ias.once(Events.LOADED, loaded);
|
62 |
|
63 | this.ias.once(Events.NEXTED, (event) => {
|
64 | this.pageBreaks.push({
|
65 | pageIndex: event.pageIndex,
|
66 | url,
|
67 | title,
|
68 | sentinel: this.ias.sentinel()
|
69 | });
|
70 |
|
71 | this.update();
|
72 |
|
73 | this.ias.off(Events.LOADED, loaded);
|
74 | });
|
75 | }
|
76 |
|
77 | prev() {
|
78 | let url = document.location.toString();
|
79 | let title = document.title;
|
80 |
|
81 | let loaded = (event) => {
|
82 | url = event.url;
|
83 |
|
84 | if (event.xhr.response) {
|
85 | title = event.xhr.response.title
|
86 | }
|
87 | };
|
88 |
|
89 | this.ias.once(Events.LOADED, loaded);
|
90 |
|
91 | this.ias.once(Events.PREVED, (event) => {
|
92 | this.pageBreaks.unshift({
|
93 | pageIndex: event.pageIndex,
|
94 | url,
|
95 | title,
|
96 | sentinel: this.ias.first()
|
97 | });
|
98 |
|
99 | this.update();
|
100 |
|
101 | this.ias.off(Events.LOADED, loaded);
|
102 | });
|
103 | }
|
104 |
|
105 | scrolled(event) {
|
106 | this.update(event.scroll.y);
|
107 | }
|
108 |
|
109 | update(scrollTop) {
|
110 | this.currentScrollTop = scrollTop || this.currentScrollTop;
|
111 |
|
112 | let pageBreak = getPageBreak(this.pageBreaks, this.currentScrollTop, this.ias.scrollContainer);
|
113 |
|
114 | if (pageBreak && pageBreak.pageIndex !== this.currentPageIndex) {
|
115 | this.ias.emitter.emit(Events.PAGE, pageBreak);
|
116 |
|
117 | this.currentPageIndex = pageBreak.pageIndex;
|
118 | }
|
119 | }
|
120 | }
|