Skip to content

Commit

Permalink
Work for surveyjs/survey-creator#5264 - fixed event options properties
Browse files Browse the repository at this point in the history
  • Loading branch information
tsv2013 committed Mar 18, 2024
1 parent da29517 commit c0ea35b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/base-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/question_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 2 additions & 6 deletions src/survey-events-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,6 @@ export interface UploadFilesEvent extends LoadFilesEvent {
*/
files: Array<File>;
}
export interface IChooseFileContext {
target: any;
type: string;
property: string;
}
export interface OpenFileChooserEvent {
/**
* A file input HTML element.
Expand All @@ -527,7 +522,8 @@ export interface OpenFileChooserEvent {
* @param files An array of selected files.
*/
callback: (files: Array<File>) => void;
context: IChooseFileContext;
elementType: String;
propertyName: String;
}
export interface DownloadFileEvent extends LoadFilesEvent {
/**
Expand Down
12 changes: 9 additions & 3 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions tests/utilstests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit c0ea35b

Please sign in to comment.