Skip to content

Commit acb3684

Browse files
fileinput duplicate file names fix:
* override handleClick method to adapt to v3 onwards structure of fileinput * cypress tests
1 parent c79662b commit acb3684

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

ui.af.apps/src/main/content/jcr_root/apps/core/fd/components/form/fileinput/v3/fileinput/clientlibs/site/js/fileinputwidget.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,40 @@ if (typeof window.FileInputWidget === 'undefined') {
8989
fileItem.appendChild(fileEndContainer);
9090
return fileItem;
9191
}
92+
93+
handleClick (event){
94+
let elem = event.target,
95+
text = elem.parentElement.previousSibling.textContent,
96+
index = this.getIndexOfText(text, elem.parentElement),
97+
url = elem.parentElement.previousSibling.dataset.key,
98+
objectUrl = elem.parentElement.previousSibling.dataset.objectUrl;
99+
if (index !== -1) {
100+
this.values.splice(index, 1);
101+
this.fileArr.splice(index, 1);
102+
// set the model with the new value
103+
this.model.value = this.fileArr;
104+
// value and fileArr contains items of both URL and file types, hence while removing from DOM
105+
// get the correct index as per this.#widget.files
106+
let domIndex = Array.from(this.widget.files).findIndex(function(file) {
107+
return file.name === text;
108+
});
109+
this.deleteFilesFromInputDom([domIndex]);
110+
if (url != null) {
111+
// remove the data so that others don't use this url
112+
delete elem.parentElement.previousSibling.dataset.key;
113+
}
114+
if(objectUrl) {
115+
// revoke the object URL to avoid memory leaks in browser
116+
// since file is anyways getting deleted, remove the object URL's too
117+
window.URL.revokeObjectURL(objectUrl);
118+
}
119+
}
120+
// Remove the dom from view
121+
//All bound events and jQuery data associated with the element are also removed
122+
elem.parentElement.parentElement.remove();
123+
// Set the focus on file upload button after click of close
124+
this.widget.focus();
125+
126+
}
92127
}
93128
}

ui.af.apps/src/main/content/jcr_root/apps/core/fd/components/form/fileinput/v4/fileinput/clientlibs/site/js/fileinputwidget.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,40 @@ if (typeof window.FileInputWidget === 'undefined') {
188188
};
189189
return messages[invalidFeature];
190190
}
191+
192+
handleClick (event){
193+
let elem = event.target,
194+
text = elem.parentElement.previousSibling.textContent,
195+
index = this.getIndexOfText(text, elem.parentElement),
196+
url = elem.parentElement.previousSibling.dataset.key,
197+
objectUrl = elem.parentElement.previousSibling.dataset.objectUrl;
198+
if (index !== -1) {
199+
this.values.splice(index, 1);
200+
this.fileArr.splice(index, 1);
201+
// set the model with the new value
202+
this.model.value = this.fileArr;
203+
// value and fileArr contains items of both URL and file types, hence while removing from DOM
204+
// get the correct index as per this.#widget.files
205+
let domIndex = Array.from(this.widget.files).findIndex(function(file) {
206+
return file.name === text;
207+
});
208+
this.deleteFilesFromInputDom([domIndex]);
209+
if (url != null) {
210+
// remove the data so that others don't use this url
211+
delete elem.parentElement.previousSibling.dataset.key;
212+
}
213+
if(objectUrl) {
214+
// revoke the object URL to avoid memory leaks in browser
215+
// since file is anyways getting deleted, remove the object URL's too
216+
window.URL.revokeObjectURL(objectUrl);
217+
}
218+
}
219+
// Remove the dom from view
220+
//All bound events and jQuery data associated with the element are also removed
221+
elem.parentElement.parentElement.remove();
222+
// Set the focus on file upload button after click of close
223+
this.widget.focus();
224+
225+
}
191226
}
192227
}

ui.tests/test-module/specs/fileinput/fileinput.runtime.cy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ describe("Form with File Input - Basic Tests", () => {
232232
})
233233
})
234234

235-
it("check preview functionality of duplicate files", () => {
235+
it("check preview and delete functionality of duplicate files", () => {
236236
let sampleFileNames = ['sample2.txt', 'sample.txt', 'sample2.txt'];
237237
const fileInput = "input[name='fileinput1']";
238238

ui.tests/test-module/specs/fileinput/fileinputv3.runtime.cy.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,22 @@ describe("Form with File Input V-3 - Basic Tests", () => {
173173
getFormObjTest(['empty.pdf', 'empty.pdf', 'empty.pdf', 'empty.pdf', 'empty.pdf'])
174174
});
175175

176+
it("check preview functionality of duplicate files", () => {
177+
let sampleFileNames = ['sample2.txt', 'sample.txt', 'sample2.txt'];
178+
const fileInput = "input[name='fileinput1']";
179+
180+
// Attach files
181+
cy.attachFile(fileInput, [sampleFileNames[0]]);
182+
cy.attachFile(fileInput, [sampleFileNames[1]]);
183+
cy.attachFile(fileInput, [sampleFileNames[2]]);
184+
185+
checkFilePreviewInFileAttachment(fileInput);
186+
187+
deleteSelectedFiles(fileInput, sampleFileNames);
188+
189+
cy.get('.cmp-adaptiveform-fileinput__fileitem').should('have.length', 0);
190+
});
191+
176192
it("should toggle description and tooltip", () => {
177193
cy.toggleDescriptionTooltip(bemBlock, 'fileinput_tooltip_scenario_test');
178194
})

0 commit comments

Comments
 (0)