Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored repeathandler customization in the Pages module, #728 #3

Merged
merged 4 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/enketo-core/src/js/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import config from 'enketo/config';
import events from './event';
import { getSiblingElement, getAncestors } from './dom-utils';
import 'jquery-touchswipe';
import reasons from './reasons';

/**
* @typedef {import('./form').Form} Form
Expand Down Expand Up @@ -252,6 +253,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()
) {
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { t } from './translator';
import { t } from 'enketo/translator';
import events from './event';

const range = document.createRange();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import records from './records-queue';
import formCache from './form-cache';
import FieldSubmissionQueue from './field-submission-queue';
import rc from './controller-webform';
import reasons from './reasons';
import reasons from 'enketo-core/src/js/reasons';
import { replaceMediaSources, replaceModelMediaSources } from './media';

let fieldSubmissionQueue;
Expand Down
56 changes: 0 additions & 56 deletions packages/enketo-express/public/js/src/module/page.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import pageModule from 'enketo-core/src/js/page';
import { getAncestors } from 'enketo-core/src/js/dom-utils';
import $ from 'jquery';
import events from './event';
import reasons from './reasons';
import settings from './settings';
import gui from './gui';

Expand Down Expand Up @@ -31,59 +28,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
2 changes: 1 addition & 1 deletion packages/enketo-express/public/js/src/module/repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import repeatModule from 'enketo-core/src/js/repeat';
import $ from 'jquery';
import events from './event';
import settings from './settings';
import reasons from './reasons';
import reasons from 'enketo-core/src/js/reasons';
import { t } from './translator';
import gui from './gui';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import settings from '../../public/js/src/module/settings';
import events from '../../public/js/src/module/event';
import fileManager from '../../public/js/src/module/file-manager';
import { Form } from '../../public/js/src/module/form';
import reasons from '../../public/js/src/module/reasons';
import reasons from 'enketo-core/src/js/reasons';

let currentUser;
let users;
Expand Down
Loading