-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update application-attachment to typescript (#793)
* Update application-attachment to typescript * Fix missing polyfill for NodeList Co-authored-by: niladic <git@nil.choron.cc>
- Loading branch information
Showing
6 changed files
with
60 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
interface ApplicationAttachment { | ||
list: HTMLElement | ||
fileInputCount: number | ||
idSequence: number | ||
increaseOrDecreaseFileInputList: (EventListener) => any | ||
} | ||
|
||
let attachment: any = {} | ||
|
||
|
||
const createAttachment = (list: HTMLElement, count: number, sequence: number): ApplicationAttachment => { | ||
return { | ||
list, | ||
fileInputCount: count, | ||
idSequence: sequence, // In order to avoid id collisions | ||
increaseOrDecreaseFileInputList: event => { | ||
if (event.target.files.length > 0) { | ||
// If a new file is added, add a new file input. | ||
const li: HTMLLIElement = document.createElement("li"); | ||
const input: HTMLInputElement = document.createElement("input"); | ||
input.setAttribute("type", "file"); | ||
input.setAttribute("name", "file[" + (attachment.idSequence++) + "]"); | ||
attachment.fileInputCount++; | ||
input.addEventListener("change", attachment.increaseOrDecreaseFileInputList); | ||
attachment.list.appendChild(li); | ||
li.appendChild(input); | ||
} else { | ||
// If a previous file is unset for upload, remove the corresponding input file. | ||
if (attachment.fileInputCount > 1) { | ||
event.target.parentElement.parentElement.removeChild(event.target.parentElement); | ||
attachment.fileInputCount--; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
const main = (list: HTMLElement | null) => { | ||
if (list != null) { | ||
const attachment = createAttachment(list, 1, 0) | ||
// For all file inputs, add a listener that adds a new file input to allow | ||
// for more attachments to be made. | ||
attachment.list.querySelectorAll("input").forEach(input => { | ||
input.addEventListener("change", attachment.increaseOrDecreaseFileInputList); | ||
}); | ||
|
||
return attachment; | ||
} | ||
} | ||
|
||
window.document.addEventListener("DOMContentLoaded", _ => { | ||
"use strict"; | ||
const attachmentList: HTMLElement | null = document.getElementById("attachment-list"); | ||
attachment = main(attachmentList) | ||
}, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
// Polyfills | ||
// See https://github.com/ryanelian/ts-polyfill/tree/master/lib | ||
import 'ts-polyfill/lib/es2015-core'; | ||
// This adds NodeList.forEach, etc. | ||
import 'core-js/web/dom-collections'; | ||
|
||
// Our scripts | ||
import "./admin.ts" | ||
import "./mdl-extensions.ts" | ||
import "./validateAccount" | ||
import "./application-attachment" | ||
import "./validateAccount" |