Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update application-attachment to typescript #793

Merged
merged 3 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -345,5 +345,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"