Skip to content

Commit

Permalink
Update application-attachment to typescript (#793)
Browse files Browse the repository at this point in the history
* Update application-attachment to typescript

* Fix missing polyfill for NodeList

Co-authored-by: niladic <git@nil.choron.cc>
  • Loading branch information
mdulac and niladic authored Oct 28, 2020
1 parent 9a3e8a8 commit 355e6cc
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ node_modules/
/public/generated-js/
project/metals.sbt
project/project/metals.sbt
project/project/project/metals.sbt
project/project/project/metals.sbt
1 change: 0 additions & 1 deletion app/views/createApplication.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -343,5 +343,4 @@ <h5 class="title--addline">
}
</div>
}{
<script type="application/javascript" src="@routes.Assets.versioned("javascripts/application-attachment.js")"></script>
}
1 change: 0 additions & 1 deletion app/views/showApplication.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -679,5 +679,4 @@ <h5 class="mdl-cell mdl-cell--12-col">Votre réponse :</h5>
We use twemoji for emoji : https://github.com/twitter/twemoji
-->
}{
<script type="application/javascript" src="@routes.Assets.versioned("javascripts/application-attachment.js")"></script>
}
35 changes: 0 additions & 35 deletions public/javascripts/application-attachment.js

This file was deleted.

55 changes: 55 additions & 0 deletions typescript/src/application-attachment.ts
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);
5 changes: 4 additions & 1 deletion typescript/src/index.ts
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"

0 comments on commit 355e6cc

Please sign in to comment.