-
Notifications
You must be signed in to change notification settings - Fork 15
/
page.js
523 lines (501 loc) · 17.5 KB
/
page.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/**
* Pages module.
*
* @module pages
*/
import $ from 'jquery';
import config from 'enketo/config';
import events from './event';
import { getSiblingElement, getAncestors } from './dom-utils';
import 'jquery-touchswipe';
/**
* @typedef {import('./form').Form} Form
*/
export default {
/**
* @type {Form}
*/
// @ts-expect-error - this will be populated during form init, but assigning
// its type here improves intellisense.
form: null,
/**
* @type {boolean}
* @default
*/
active: false,
/**
* @type {Array|jQuery}
* @default
*/
current: null,
/**
* @type {jQuery}
*/
activePages: [],
/**
* @type {Function}
*/
init() {
if (!this.form) {
throw new Error(
'Repeats module not correctly instantiated with form property.'
);
}
if (this.form.features.pagination) {
const allPages = [
...this.form.view.html.querySelectorAll(
'.question, .or-appearance-field-list'
),
]
.concat([
...this.form.view.html.querySelectorAll(
'.or-repeat.or-appearance-field-list + .or-repeat-info'
),
])
.filter(
(el) =>
// something tells me there is a more efficient way to doing this
// e.g. by selecting the descendants of the .or-appearance-field-list and removing those
!el.parentElement.closest(
'.or-appearance-field-list'
) &&
!(
el.matches('.question') &&
el.querySelector('[data-for]')
)
)
.map((el) => {
el.setAttribute('role', 'page');
return el;
});
if (
allPages.length > 0 ||
allPages[0].classList.contains('or-repeat')
) {
const formWrapper = this.form.view.html.parentNode;
this.$formFooter = $(formWrapper.querySelector('.form-footer'));
this.$btnFirst = this.$formFooter.find('.first-page');
this.$btnPrev = this.$formFooter.find('.previous-page');
this.$btnNext = this.$formFooter.find('.next-page');
this.$btnNext.attr('tabindex', 2);
this.$btnLast = this.$formFooter.find('.last-page');
this.$toc = $(formWrapper.querySelector('.pages-toc__list'));
this._updateAllActive(allPages);
this._updateToc();
this._toggleButtons(0);
this._setButtonHandlers();
this._setRepeatHandlers();
this._setBranchHandlers();
this._setSwipeHandlers();
this._setTocHandlers();
this._setLangChangeHandlers();
this.active = true;
this._flipToFirst();
}
/* else {
form.view.$.removeClass( 'pages' );
} */
}
},
/**
* flips to the page provided as jQueried parameter or the page containing
* the jQueried element provided as parameter
* alternatively, (e.g. if a top level repeat without field-list appearance is provided as parameter)
* it flips to the page contained with the jQueried parameter;
*
* @param {jQuery} $e - Element on page to flip to
*/
flipToPageContaining($e) {
const e = $e[0];
const closestPage = e.closest('[role="page"]');
if (closestPage) {
this._flipTo(closestPage);
} else if (e.closest('.question')) {
// If $e is a comment question, and it is not inside a group, there will be no closestPage.
const referer = e.querySelector('[data-for]');
const ancestor = e.closest('.or-repeat, form.or');
if (referer && ancestor) {
const linkedQuestion = ancestor.querySelector(
`[name="${referer.dataset.for}"]`
);
if (linkedQuestion) {
this._flipTo(linkedQuestion.closest('[role="page"]'));
}
}
}
this.$toc.parent().find('.pages-toc__overlay').click();
},
/**
* sets button handlers
*/
_setButtonHandlers() {
const that = this;
// Make sure eventhandlers are not duplicated after resetting form.
this.$btnFirst.off('.pagemode').on('click.pagemode', () => {
if (!that.form.pageNavigationBlocked) {
that._flipToFirst();
}
return false;
});
this.$btnPrev.off('.pagemode').on('click.pagemode', () => {
if (!that.form.pageNavigationBlocked) {
that._prev();
}
return false;
});
this.$btnNext.off('.pagemode').on('click.pagemode', () => {
if (!that.form.pageNavigationBlocked) {
that._next();
}
return false;
});
this.$btnLast.off('.pagemode').on('click.pagemode', () => {
if (!that.form.pageNavigationBlocked) {
that._flipToLast();
}
return false;
});
},
/**
* sets swipe handlers
*/
_setSwipeHandlers() {
if (config.swipePage === false) {
return;
}
const that = this;
const $main = this.form.view.$.closest('.main');
$main.swipe('destroy');
$main.swipe({
allowPageScroll: 'vertical',
threshold: 250,
preventDefaultEvents: false,
swipeLeft() {
that.$btnNext.click();
},
swipeRight() {
that.$btnPrev.click();
},
swipeStatus(evt, phase) {
if (phase === 'start') {
/*
* Triggering blur will fire a change event on the currently focused form control
* This will trigger validation and is required to block page navigation on swipe
* with form.pageNavigationBlocked
* The only potential problem with this approach is that the threshold (250ms)
* may theoretically not be sufficient to ensure validation is completed to
* set form.pageNavigationBlocked to true. The edge case will be very slow devices
* and/or amazingly complex constraint expressions.
*/
const focused = that._getCurrent()
? that._getCurrent().querySelector(':focus')
: null;
if (focused) {
focused.blur();
}
}
},
});
},
/**
* sets toc handlers
*/
_setTocHandlers() {
const that = this;
this.$toc
.on('click', 'a', function () {
if (!that.form.pageNavigationBlocked) {
if (
this.parentElement &&
this.parentElement.getAttribute('tocId')
) {
const tocId = parseInt(
this.parentElement.getAttribute('tocId'),
10
);
const destItem = that.form.toc.tocItems.find(
(item) => item.tocId === tocId
);
if (destItem && destItem.element) {
const destEl = destItem.element;
that.form.goToTarget(destEl);
}
}
}
return false;
})
.parent()
.find('.pages-toc__overlay')
.on('click', () => {
that.$toc.parent().find('#toc-toggle').prop('checked', false);
});
},
/**
* sets repeat handlers
*/
_setRepeatHandlers() {
// TODO: can be optimized by smartly updating the active pages
this.form.view.html.addEventListener(
events.AddRepeat().type,
(event) => {
this._updateAllActive();
// Don't flip if the user didn't create the repeat with the + button.
// or if is the default first instance created during loading.
// except if the new repeat is actually the first page in the form, or contains the first page
if (
event.detail.trigger === 'user' ||
this.activePages[0] === event.target ||
getAncestors(this.activePages[0], '.or-repeat').includes(
event.target
)
) {
this.flipToPageContaining($(event.target));
} else {
this._toggleButtons();
}
}
);
this.form.view.html.addEventListener(
events.RemoveRepeat().type,
(event) => {
// if the current page is removed
// note that that.current will have length 1 even if it was removed from DOM!
if (this.current && !this.current.closest('html')) {
this._updateAllActive();
let $target = $(event.target).prev();
if ($target.length === 0) {
$target = $(event.target);
}
// is it best to go to previous page always?
this.flipToPageContaining($target);
}
}
);
},
/**
* sets branch handlers
*/
_setBranchHandlers() {
const that = this;
// TODO: can be optimized by smartly updating the active pages
this.form.view.$
// .off( 'changebranch.pagemode' )
.on('changebranch.pagemode', () => {
that._updateAllActive();
// If the current page has become inactive (e.g. a form whose first page during load becomes non-relevant)
if (!that.activePages.includes(that.current)) {
that._next();
}
that._toggleButtons();
});
},
/**
* sets language change handlers
*/
_setLangChangeHandlers() {
this.form.view.html.addEventListener(
events.ChangeLanguage().type,
() => {
this._updateToc();
}
);
},
/**
* @return {Element} current page
*/
_getCurrent() {
return this.current;
},
/**
* @param {Array<Node>} all - all elements that represent a page
*/
_updateAllActive(all) {
all = all || [...this.form.view.html.querySelectorAll('[role="page"]')];
this.activePages = all.filter(
(el) =>
!el.closest('.disabled') &&
(el.matches('.question') ||
el.querySelector('.question:not(.disabled)') ||
// or-repeat-info is only considered a page by itself if it has no sibling repeats
// When there are siblings repeats, we use CSS trickery to show the + button underneath the last
// repeat.
(el.matches('.or-repeat-info') &&
!getSiblingElement(el, '.or-repeat')))
);
this._updateToc();
},
/**
* @param {number} currentIndex - current index
* @return {jQuery} Previous page
*/
_getPrev(currentIndex) {
return this.activePages[currentIndex - 1];
},
/**
* @param {number} currentIndex - current index
* @return {jQuery} Next page
*/
_getNext(currentIndex) {
return this.activePages[currentIndex + 1];
},
/**
* @return {number} Current page index
*/
_getCurrentIndex() {
return this.activePages.findIndex((el) => el === this.current);
},
/**
* Changes the `pages.next()` function to return a `Promise`, wrapping one of the following values:
*
* @return {Promise} wrapping {boolean} or {number}. If a {number}, this is the index into
* `activePages` of the new current page; if a {boolean}, {false} means that validation
* failed, and {true} that validation passed, but the page did not change.
*/
_next() {
const that = this;
let currentIndex;
let validate;
currentIndex = this._getCurrentIndex();
validate =
config.validatePage === false || !this.current
? Promise.resolve(true)
: this.form.validateContent($(this.current));
return validate.then((valid) => {
let next;
let newIndex;
if (!valid) {
return false;
}
next = that._getNext(currentIndex);
if (next) {
newIndex = currentIndex + 1;
that._flipTo(next, newIndex);
// return newIndex;
}
return true;
});
},
/**
* Switches to previous page
*/
_prev() {
const currentIndex = this._getCurrentIndex();
const prev = this._getPrev(currentIndex);
if (prev) {
this._flipTo(prev, currentIndex - 1);
}
},
/**
* @param {Element} pageEl - page element
*/
_setToCurrent(pageEl) {
pageEl.classList.add('current', 'hidden');
// Was just added, for animation?
pageEl.classList.remove('hidden');
getAncestors(
pageEl,
'.or-group, .or-group-data, .or-repeat',
'.or'
).forEach((el) => el.classList.add('contains-current'));
this.current = pageEl;
this.form.goToTarget(pageEl, { isPageFlip: true });
},
/**
* Switches to a page
*
* @param {Element} pageEl - page element
* @param {number} newIndex - new index
*/
_flipTo(pageEl, newIndex) {
// if there is a current page (note: if current page was removed it is not null, hence the .closest('html') check)
if (this.current && this.current.closest('html')) {
// if current page is not same as pageEl
if (this.current !== pageEl) {
this.current.classList.remove('current', 'fade-out');
getAncestors(
this.current,
'.or-group, .or-group-data, .or-repeat',
'.or'
).forEach((el) => el.classList.remove('contains-current'));
this._pauseMultimedia(this.current);
this._setToCurrent(pageEl);
this._focusOnFirstQuestion(pageEl);
this._toggleButtons(newIndex);
pageEl.dispatchEvent(events.PageFlip());
this.form.goToTarget(pageEl, { isPageFlip: true });
}
} else if (pageEl) {
this._setToCurrent(pageEl);
this._focusOnFirstQuestion(pageEl);
this._toggleButtons(newIndex);
pageEl.dispatchEvent(events.PageFlip());
this.form.goToTarget(pageEl, { isPageFlip: true });
pageEl.setAttribute('tabindex', 1);
}
},
/**
* Switches to first page
*/
_flipToFirst() {
this._flipTo(this.activePages[0]);
},
/**
* Switches to last page
*/
_flipToLast() {
this._flipTo(this.activePages[this.activePages.length - 1]);
},
/**
* Focuses on first question and scrolls it into view
*
* @param {Element} pageEl - page element
*/
_focusOnFirstQuestion(pageEl) {
// triggering fake focus in case element cannot be focused (if hidden by widget)
$(pageEl)
.find('.question:not(.disabled)')
.addBack('.question:not(.disabled)')
.filter(function () {
return $(this).parentsUntil('.or', '.disabled').length === 0;
})
.eq(0)
.find('input, select, textarea')
.eq(0)
.trigger('fakefocus');
// focus on element
pageEl.focus();
pageEl.scrollIntoView();
},
/**
* Updates status of navigation buttons
*
* @param {number} [index] - index of current page
*/
_toggleButtons(index = this._getCurrentIndex()) {
const next = this._getNext(index);
const prev = this._getPrev(index);
this.$btnNext.add(this.$btnLast).toggleClass('disabled', !next);
this.$btnPrev.add(this.$btnFirst).toggleClass('disabled', !prev);
this.$formFooter.toggleClass('end', !next);
},
/**
* Pauses video and audio from playing when switching to a page.
*
* @param {Element} pageEl - page element
*/
_pauseMultimedia(pageEl) {
$(pageEl)
.find('audio, video')
.each((idx, element) => element.pause());
},
/**
* Updates Table of Contents
*/
_updateToc() {
if (this.$toc.length) {
// regenerate complete ToC from first enabled question/group label of each page
this.$toc.empty()[0].append(this.form.toc.getHtmlFragment());
this.$toc.closest('.pages-toc').removeClass('hide');
}
},
};