forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswipeview-tests.ts
252 lines (212 loc) · 8.71 KB
/
swipeview-tests.ts
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/// <reference path="swipeview.d.ts" />
function demo1() {
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
var
el,
i,
page,
dots = document.querySelectorAll('#nav li'),
slides = [
{
img: 'images/pic01.jpg',
width: 300,
height: 213,
desc: 'Piazza del Duomo, Florence, Italy'
},
{
img: 'images/pic02.jpg',
width: 300,
height: 164,
desc: 'Tuscan Landscape'
}
];
var gallery = new SwipeView('#wrapper', { numberOfPages: slides.length });
// Load initial data
for (i = 0; i < 3; i++) {
page = i == 0 ? slides.length - 1 : i - 1;
el = document.createElement('img');
el.className = 'loading';
el.src = slides[page].img;
el.width = slides[page].width;
el.height = slides[page].height;
el.onload = function () { this.className = ''; }
gallery.masterPages[i].appendChild(el);
el = document.createElement('span');
el.innerHTML = slides[page].desc;
gallery.masterPages[i].appendChild(el)
}
gallery.onFlip(function () {
var el,
upcoming,
i;
for (i = 0; i < 3; i++) {
upcoming = gallery.masterPages[i].dataset.upcomingPageIndex;
if (upcoming != gallery.masterPages[i].dataset.pageIndex) {
el = gallery.masterPages[i].querySelector('img');
el.className = 'loading';
el.src = slides[upcoming].img;
el.width = slides[upcoming].width;
el.height = slides[upcoming].height;
el = gallery.masterPages[i].querySelector('span');
el.innerHTML = slides[upcoming].desc;
}
}
});
gallery.onMoveOut(function () {
gallery.masterPages[gallery.currentMasterPage].className = gallery.masterPages[gallery.currentMasterPage].className.replace(/(^|\s)swipeview-active(\s|$)/, '');
});
gallery.onMoveIn(function () {
var className = gallery.masterPages[gallery.currentMasterPage].className;
/(^|\s)swipeview-active(\s|$)/.test(className) || (gallery.masterPages[gallery.currentMasterPage].className = !className ? 'swipeview-active' : className + ' swipeview-active');
});
}
function demo2() {
var carousel: SwipeView,
el,
i,
page,
slides = [
'<strong>Swipe</strong> to know more >>><br>Or scroll down for <em>Lorem Ipsum</em>',
'1. A robot may not injure a human being or, through inaction, allow a human being to come to harm.',
'2. A robot must obey the orders given to it by human beings, except where such orders would conflict with the First Law.',
'3. A robot must protect its own existence as long as such protection does not conflict with the First or Second Laws.'
];
carousel = new SwipeView('#wrapper', {
numberOfPages: slides.length,
hastyPageFlip: true
});
// Load initial data
for (i = 0; i < 3; i++) {
page = i == 0 ? slides.length - 1 : i - 1;
el = document.createElement('span');
el.innerHTML = slides[page];
carousel.masterPages[i].appendChild(el)
}
carousel.onFlip(function () {
var el,
upcoming,
i;
for (i = 0; i < 3; i++) {
upcoming = carousel.masterPages[i].dataset.upcomingPageIndex;
if (upcoming != carousel.masterPages[i].dataset.pageIndex) {
el = carousel.masterPages[i].querySelector('span');
el.innerHTML = slides[upcoming];
}
}
});
}
function demo3() {
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
window.addEventListener('load', function () {
var ereader: SwipeView,
el,
i,
pageIndex,
pages = [],
req = new XMLHttpRequest();
ereader = new SwipeView('#wrapper', { hastyPageFlip: true });
// Ajax request
req.open('GET', 'flowers.txt', true);
req.onreadystatechange = function () {
if (req.readyState != 4) return;
paginate(req.status != 200 && req.status != 304 ? false : req.responseText);
req = null;
}
req.send(null);
function paginate(book) {
var that = this,
container,
helper,
words = [],
segment,
wordCount = 80,
avgWordCount = 0,
progressTotal = 0,
progressCurrent = 0,
progressMaxWidth = document.getElementById('progressbar').clientWidth,
progressToBookRatio = 0,
progressBar = document.querySelector('#progressbar > span'),
size;
if (!book) return;
book = book.replace(/\n\n/g, ' <br><br>').replace(/\n/g, ' ');
progressTotal = book.length;
progressToBookRatio = progressMaxWidth / book.length;
container = document.createElement('div');
container.style.visibility = 'hidden';
container.innerHTML = '<div id="ereader-helper"></div>';
ereader.slider.appendChild(container);
helper = document.getElementById('ereader-helper');
helper.innerHTML = '';
var loopy = function () {
words = book.split(' ', wordCount);
segment = words.join(' ');
helper.innerHTML = segment;
if (helper.offsetHeight > ereader.wrapperHeight) {
if (size == -1) {
words.pop();
segment = words.join(' ');
pages.push(segment);
book = book.substr(segment.length);
avgWordCount = Math.round((wordCount + avgWordCount) / 2);
wordCount = avgWordCount;
size = 0;
progressTotal -= segment.length;
} else {
size = 1;
wordCount--;
}
} else {
if (size == 1) {
pages.push(segment);
book = book.substr(segment.length);
avgWordCount = Math.round((wordCount + avgWordCount) / 2);
wordCount = avgWordCount;
size = 0;
progressTotal -= segment.length;
} else {
if (segment == book) {
pages.push(segment);
book = '';
}
size = -1;
wordCount++;
}
}
if (book) {
progressBar.style.width = 150 - Math.round(progressToBookRatio * progressTotal) + 'px';
setTimeout(loopy, 1);
} else {
book = null;
words = null;
segment = null;
helper.innerHTML = '';
ereader.slider.removeChild(container);
ereader.updatePageCount(pages.length);
ereader.masterPages[0].dataset.pageIndex = pages.length - 1;
ereader.masterPages[0].dataset.upcomingPageIndex = ereader.masterPages[0].dataset.pageIndex;
// Load initial data
for (i = 0; i < 3; i++) {
pageIndex = i == 0 ? pages.length - 1 : i - 1;
el = document.createElement('div');
el.innerHTML = pages[pageIndex];
ereader.masterPages[i].appendChild(el)
}
document.getElementById('loading').style.display = 'none';
}
}
loopy();
}
ereader.onFlip(function () {
var el,
upcoming,
i;
for (i = 0; i < 3; i++) {
upcoming = ereader.masterPages[i].dataset.upcomingPageIndex;
if (upcoming != ereader.masterPages[i].dataset.pageIndex) {
el = ereader.masterPages[i].querySelector('div');
el.innerHTML = pages[upcoming];
}
}
});
}, false);
}