2017-04-20 13:32:09 +02:00
|
|
|
// Copyright (c) 2016 - 2017, Marcel Kapfer (mmk2410).
|
2016-11-20 08:47:12 +01:00
|
|
|
// MIT License
|
|
|
|
|
2017-06-16 00:04:50 +02:00
|
|
|
/// frontend code for filespread, a simple file over email spreading web app.
|
2016-11-20 08:47:12 +01:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:html';
|
|
|
|
|
2017-06-16 00:04:50 +02:00
|
|
|
/// main method with click listners.
|
2016-11-20 08:47:12 +01:00
|
|
|
void main() {
|
|
|
|
// send mail listener
|
|
|
|
querySelector("#send").onClick.listen(sendMail);
|
2017-06-16 00:04:50 +02:00
|
|
|
|
|
|
|
// save address list listner
|
|
|
|
querySelector("#save-addresses").onClick.listen(saveAddresses);
|
2016-11-20 08:47:12 +01:00
|
|
|
}
|
|
|
|
|
2017-06-16 00:04:50 +02:00
|
|
|
/// function for calling the backend to send mails.
|
2016-11-20 08:47:12 +01:00
|
|
|
void sendMail(MouseEvent event) {
|
|
|
|
InputElement title = querySelector("#mailtitle");
|
|
|
|
InputElement text = querySelector("#mailtext");
|
|
|
|
InputElement filename = querySelector("#filename");
|
|
|
|
InputElement addresslist = querySelector("#address_list");
|
2017-04-20 13:32:09 +02:00
|
|
|
CheckboxInputElement nofile = querySelector("#nofile");
|
2016-11-20 08:47:12 +01:00
|
|
|
|
|
|
|
Map data = {
|
|
|
|
"title": title.value,
|
|
|
|
"body": text.value,
|
|
|
|
"filename": filename.value,
|
2017-04-20 13:32:09 +02:00
|
|
|
"list": addresslist.value,
|
|
|
|
"nofile": nofile.checked
|
2016-11-20 08:47:12 +01:00
|
|
|
};
|
|
|
|
|
2016-11-24 11:13:12 +01:00
|
|
|
String url = "res/php/sendmail.php?data=${JSON.encode(data)}";
|
2016-11-20 08:47:12 +01:00
|
|
|
|
2017-06-16 00:04:50 +02:00
|
|
|
HttpRequest.getString(url).then((String response) {
|
2016-11-20 08:47:12 +01:00
|
|
|
if (response == "0") {
|
|
|
|
window.alert("Mails successfully sent.");
|
|
|
|
} else {
|
|
|
|
window.alert("Failed to send mail.");
|
|
|
|
}
|
|
|
|
});
|
2017-06-16 00:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// function to update the addresses list in the backend.
|
|
|
|
void saveAddresses(MouseEvent event) {
|
|
|
|
TextAreaElement addresses = querySelector("#edit-addresses");
|
|
|
|
SelectElement list = querySelector("#edit-address-list");
|
|
|
|
|
|
|
|
List<String> addressesList = new List();
|
|
|
|
|
|
|
|
addresses.value.split("\n").forEach((String address) {
|
|
|
|
if (address.isNotEmpty) addressesList.add(address);
|
|
|
|
});
|
|
|
|
|
|
|
|
Map data = {"list": list.value, "addresses": addressesList};
|
|
|
|
|
|
|
|
String url = "res/php/savelist.php";
|
|
|
|
|
|
|
|
HttpRequest.postFormData(url, {"data": JSON.encode(data)}).then((HttpRequest response) {
|
|
|
|
if (response.responseText == "0") {
|
|
|
|
window.alert("Addresses sucessfully saved.");
|
|
|
|
updateAddresses(addressesList, list.value);
|
|
|
|
} else {
|
|
|
|
window.alert("Failed to save adresses.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updated the frontend after an address change.
|
|
|
|
void updateAddresses(List<String> addresses, String list) {
|
|
|
|
SelectElement currentList = querySelector("#address_list");
|
|
|
|
|
|
|
|
if (currentList.value == list) {
|
|
|
|
DivElement addressesContainer = querySelector("#addresses");
|
|
|
|
addressesContainer.children .removeWhere((Element child) => addressesContainer.children.contains(child));
|
2017-04-20 13:32:09 +02:00
|
|
|
|
2017-06-16 00:04:50 +02:00
|
|
|
addresses.forEach((String address) {
|
|
|
|
ParagraphElement addressElement = new ParagraphElement();
|
|
|
|
addressElement.text = address;
|
|
|
|
addressesContainer.children.add(addressElement);
|
|
|
|
});
|
|
|
|
}
|
2016-11-20 08:47:12 +01:00
|
|
|
}
|