Skip to content

Commit 621002a

Browse files
Merge pull request #1687 from adobe/fileinput_deleteIcon
@fix: file attachment delete correct file when duplicate names of files
2 parents 6011323 + e8e6570 commit 621002a

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
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/fileinputv3.runtime.cy.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ 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 delete 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+
deleteSelectedFiles(fileInput, sampleFileNames);
186+
187+
cy.get('.cmp-adaptiveform-fileinput__fileitem').should('have.length', 0);
188+
});
189+
176190
it("should toggle description and tooltip", () => {
177191
cy.toggleDescriptionTooltip(bemBlock, 'fileinput_tooltip_scenario_test');
178192
})

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,20 @@ describe("Form with File Input V-4 - Basic Tests", () => {
181181
getFormObjTest(['empty.pdf', 'empty.pdf', 'empty.pdf', 'empty.pdf', 'empty.pdf'])
182182
});
183183

184+
it("check delete functionality of duplicate files", () => {
185+
let sampleFileNames = ['sample2.txt', 'sample.txt', 'sample2.txt'];
186+
const fileInput = "input[name='fileinput1']";
187+
188+
// Attach files
189+
cy.attachFile(fileInput, [sampleFileNames[0]]);
190+
cy.attachFile(fileInput, [sampleFileNames[1]]);
191+
cy.attachFile(fileInput, [sampleFileNames[2]]);
192+
193+
deleteSelectedFiles(fileInput, sampleFileNames);
194+
195+
cy.get('.cmp-adaptiveform-fileinput__fileitem').should('have.length', 0);
196+
});
197+
184198
it("should toggle description and tooltip", () => {
185199
cy.toggleDescriptionTooltip(bemBlock, 'fileinput_tooltip_scenario_test');
186200
});

0 commit comments

Comments
 (0)