Skip to content

Commit

Permalink
refactored repeathandler customization, enketo#728
Browse files Browse the repository at this point in the history
  • Loading branch information
MartijnR committed Mar 29, 2024
1 parent 1a444ec commit 2efd996
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 53 deletions.
16 changes: 16 additions & 0 deletions packages/enketo-core/src/js/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,22 @@ export default {
events.AddRepeat().type,
(event) => {
this._updateAllActive();

// ---------- Custom OC --------------
/*
* The only thing we want to change in this function for OC,
* is to NOT flip to the next page when a repeat is the same as a page and
* and a new repeat instance is created,
* while there are empty reason-for-change fields.
*/
if (
event.target.getAttribute('role') === 'page' &&
!reasons.validate()

Check failure on line 265 in packages/enketo-core/src/js/page.js

View workflow job for this annotation

GitHub Actions / lint (20.5.1)

'reasons' is not defined

Check failure on line 265 in packages/enketo-core/src/js/page.js

View workflow job for this annotation

GitHub Actions / lint (20.5.1)

'reasons' is not defined
) {
this.toggleButtons();
}
// ------- End of Custom OC ----------

// 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
Expand Down
53 changes: 0 additions & 53 deletions packages/enketo-express/public/js/src/module/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,59 +31,6 @@ pageModule.flipToPageContaining = function ($e) {
this.$toc.parent().find('.pages-toc__overlay').click();
};

/*
* The only thing we want to change in this function for OC,
* is to NOT flip to the next page when a repeat is the same as a page and
* and a new repeat instance is created,
* while there are empty reason-for-change fields.
*/
pageModule.setRepeatHandlers = function () {
this.form.view.html.addEventListener(events.AddRepeat().type, (event) => {
this._updateAllActive();

// ---------- Custom OC --------------
if (
event.target.getAttribute('role') === 'page' &&
!reasons.validate()
) {
this.toggleButtons();
}
// ------- End of Custom OC ----------

// 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);
}
}
);
};

// const originalPageModuleNext = pageModule._next;

// We don't use the original call, because OC only wants to (sometimes) block strict validation (not regular non-strict)
Expand Down

0 comments on commit 2efd996

Please sign in to comment.