1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-10 10:40:54 +00:00

Merge pull request #559 from lightest/survey_signature_pad_resize_bug

Signature pad resize bug.
This commit is contained in:
Alain Pitiot 2023-07-19 10:00:33 +02:00 committed by GitHub
commit bf2b75f0c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -82,6 +82,10 @@ export class Survey extends VisualStim
{ {
super({ name, win, units, ori, depth, pos, size, autoDraw, autoLog }); super({ name, win, units, ori, depth, pos, size, autoDraw, autoLog });
// Storing all existing signaturePad questions to properly handle their resize.
// Unfortunately signaturepad question type can't handle resizing properly by itself.
this._signaturePads = [];
// whether the user is done with the survey, independently of whether the survey is completed: // whether the user is done with the survey, independently of whether the survey is completed:
this.isFinished = false; this.isFinished = false;
@ -968,8 +972,6 @@ export class Survey extends VisualStim
this.psychoJS.logger.warn(`Flag _isCompletedAll is false!`); this.psychoJS.logger.warn(`Flag _isCompletedAll is false!`);
} }
this._detachResizeObservers();
this._surveyRunningPromiseResolve(completionCode); this._surveyRunningPromiseResolve(completionCode);
} }
@ -1137,34 +1139,46 @@ export class Survey extends VisualStim
this._lastPageSwitchHandledIdx = -1; this._lastPageSwitchHandledIdx = -1;
} }
_handleSignaturePadResize(entries) _handleWindowResize(e)
{ {
for (let i = 0; i < entries.length; i++) if (this._surveyModel)
{ {
// const signatureCanvas = entries[i].target.querySelector("canvas"); for (let i = this._signaturePads.length - 1; i >= 0; i--)
const question = this._surveyModel.getQuestionByName(entries[i].target.dataset.name); {
question.signatureWidth = Math.min(question.maxSignatureWidth, entries[i].contentBoxSize[0].inlineSize); // As of writing this (24.03.2023). SurveyJS doesn't have a proper event
// for question being removed from nested locations, such as dynamic panel.
// However, surveyJS will set .signaturePad property to null once the question is removed.
// Utilising this knowledge to sync our lists.
if (this._signaturePads[ i ].question.signaturePad)
{
this._signaturePads[ i ].question.signatureWidth = Math.min(
this._signaturePads[i].question.maxSignatureWidth,
this._signaturePads[ i ].htmlElement.getBoundingClientRect().width
);
}
else
{
// Signature pad was removed. Syncing list.
this._signaturePads.splice(i, 1);
}
}
} }
} }
_addEventListeners() _addEventListeners()
{ {
this._signaturePadRO = new ResizeObserver(this._handleSignaturePadResize.bind(this)); window.addEventListener("resize", (e) => this._handleWindowResize(e));
} }
_handleAfterQuestionRender (sender, options) _handleAfterQuestionRender (sender, options)
{ {
if (options.question.getType() === "signaturepad") if (options.question.getType() === "signaturepad")
{ {
this._signaturePadRO.observe(options.htmlElement); this._signaturePads.push(options);
options.question.signatureWidth = Math.min(options.question.maxSignatureWidth, options.htmlElement.getBoundingClientRect().width);
} }
} }
_detachResizeObservers()
{
this._signaturePadRO.disconnect();
}
/** /**
* Init the SurveyJS.io library and various extensions, setup the theme. * Init the SurveyJS.io library and various extensions, setup the theme.
* *