From c0ea35b8488d4aee11d693cbbba54c6eafa8528a Mon Sep 17 00:00:00 2001 From: tsv2013 Date: Mon, 18 Mar 2024 16:10:02 +0300 Subject: [PATCH] Work for https://github.com/surveyjs/survey-creator/issues/5264 - fixed event options properties --- src/base-interfaces.ts | 2 +- src/question_file.ts | 4 ++-- src/survey-events-api.ts | 8 ++------ tests/surveytests.ts | 12 +++++++++--- tests/utilstests.ts | 16 ++++++++-------- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/base-interfaces.ts b/src/base-interfaces.ts index 71cd87d7b8..82ed251442 100644 --- a/src/base-interfaces.ts +++ b/src/base-interfaces.ts @@ -231,7 +231,7 @@ export interface ISurvey extends ITextProcessor, ISurveyErrorOwner { elementContentVisibilityChanged(element: ISurveyElement): void; onCorrectQuestionAnswer(question: IQuestion, options: any): void; processPopupVisiblityChanged(question: IQuestion, popupModel: PopupModel, visible: boolean): void; - chooseFiles(input: HTMLInputElement, callback: (files: File[]) => void, context?: { element: ISurveyElement, item?: any, target?: any, type?: string, property?: string }): void; + chooseFiles(input: HTMLInputElement, callback: (files: File[]) => void, context?: { element: Base, item?: any, elementType?: string, propertyName?: string }): void; } export interface ISurveyImpl { getSurveyData(): ISurveyData; diff --git a/src/question_file.ts b/src/question_file.ts index 6e2238c9f8..1e95c504dd 100644 --- a/src/question_file.ts +++ b/src/question_file.ts @@ -478,14 +478,14 @@ export class QuestionFileModel extends QuestionFileModelBase { this.setPropertyValue("maxSize", val); } public chooseFile(event: MouseEvent): void { - if(!DomDocumentHelper.isAvailable()) return; + if (!DomDocumentHelper.isAvailable()) return; const inputElement = DomDocumentHelper.getDocument().getElementById(this.inputId) as HTMLInputElement; event.preventDefault(); event.stopImmediatePropagation(); if (inputElement) { if (this.survey) { - this.survey.chooseFiles(inputElement, files => this.loadFiles(files), { element: this, target: this.survey, type: this.getType(), property: this.name }); + this.survey.chooseFiles(inputElement, files => this.loadFiles(files), { element: this, elementType: this.getType(), propertyName: this.name }); } else { inputElement.click(); } diff --git a/src/survey-events-api.ts b/src/survey-events-api.ts index 700c9c9e1e..efc25657b8 100644 --- a/src/survey-events-api.ts +++ b/src/survey-events-api.ts @@ -504,11 +504,6 @@ export interface UploadFilesEvent extends LoadFilesEvent { */ files: Array; } -export interface IChooseFileContext { - target: any; - type: string; - property: string; -} export interface OpenFileChooserEvent { /** * A file input HTML element. @@ -527,7 +522,8 @@ export interface OpenFileChooserEvent { * @param files An array of selected files. */ callback: (files: Array) => void; - context: IChooseFileContext; + elementType: String; + propertyName: String; } export interface DownloadFileEvent extends LoadFilesEvent { /** diff --git a/tests/surveytests.ts b/tests/surveytests.ts index 9719d67f8b..b931b1d4d9 100644 --- a/tests/surveytests.ts +++ b/tests/surveytests.ts @@ -19321,15 +19321,21 @@ QUnit.test("onOpenFileChooser fires", function (assert) { const survey = new SurveyModel(); let log = ""; let lastContext; + let lastContextElement; + let lastContextPropertyName; survey.onOpenFileChooser.add((s, o) => { log += "->onOpenFileChooser"; - lastContext = o.context; + lastContextElement = o.element; + lastContextPropertyName = o.propertyName; + lastContext = (o as any).context; o.callback([]); }); assert.equal(log, ""); - survey.chooseFiles(document.createElement("input"), () => { }, { target: { a: 1 } } as any); + survey.chooseFiles(document.createElement("input"), () => { }, { element: { a: 1 }, propertyName: "a" } as any); assert.equal(log, "->onOpenFileChooser"); - assert.deepEqual(lastContext, { target: { a: 1 } }); + assert.deepEqual(lastContext, { element: { a: 1 }, propertyName: "a" }); + assert.deepEqual(lastContextElement, { a: 1 }); + assert.equal(lastContextPropertyName, "a"); }); QUnit.test("Advanced header title/description color", function (assert) { const survey = new SurveyModel(); diff --git a/tests/utilstests.ts b/tests/utilstests.ts index d218c264a2..7ed5cdfa15 100644 --- a/tests/utilstests.ts +++ b/tests/utilstests.ts @@ -9,15 +9,15 @@ export default QUnit.module("utils"); function checkSanitizer(element, text, selectionNodeIndex, selectionStart, cleanLineBreaks = true) { element.innerHTML = text; const selection = document.getSelection(); - const range = document.createRange(); - if(selectionNodeIndex >= 0) { + let range = document.createRange(); + if (selectionNodeIndex >= 0) { range.setStart(element.childNodes[selectionNodeIndex], selectionStart); } range.collapse(true); selection.removeAllRanges(); selection.addRange(range); sanitizeEditableContent(element, cleanLineBreaks); - const range = document.getSelection().getRangeAt(0); + range = document.getSelection().getRangeAt(0); if (element.childNodes.length > 0) range.setStart(element.childNodes[0], 0); return { text: element.innerHTML, @@ -29,7 +29,7 @@ function checkSanitizer(element, text, selectionNodeIndex, selectionStart, clean } QUnit.test( "utils: sanitizer", - function(assert) { + function (assert) { var element: HTMLSpanElement = document.createElement("span"); document.body.appendChild(element); element.contentEditable = "true"; @@ -61,7 +61,7 @@ QUnit.test( QUnit.test( "utils: sanitizer with linebreaks", - function(assert) { + function (assert) { var element: HTMLSpanElement = document.createElement("span"); document.body.appendChild(element); element.contentEditable = "true"; @@ -223,15 +223,15 @@ QUnit.test( mouseInfo.hasMouse = false; const hasTouchEvent = mouseInfo.hasTouchEvent; assert.equal(mouseInfo.isTouch, hasTouchEvent, "isTouch, #2. hasTouch in window: " + hasTouchEvent); - if(!hasTouchEvent) { - window["ontouchstart"] = ()=>{}; + if (!hasTouchEvent) { + window["ontouchstart"] = () => { }; } mouseInfo.hasMouse = true; assert.equal(mouseInfo.isTouch, false, "isTouch, #3"); mouseInfo.hasMouse = false; assert.equal(mouseInfo.isTouch, true, "isTouch, #4"); mouseInfo.hasMouse = true; - if(!hasTouchEvent) { + if (!hasTouchEvent) { window["ontouchstart"] = undefined; } }