wmdeit_kaform/kaform.js

393 lines
7.9 KiB
JavaScript

var currentRowId=1;
var banktype = "bank-eu";
function renumberTableRows(){
var cur = document.getElementById("currency").value;
var row_no=1;
var table = document.getElementById("tabbody");
for (var i = 0, row; row = table.rows[i]; i++) {
row.cells[0].innerHTML=row_no++;
row.cells[4].innerHTML=cur;
}
return i;
}
function checkAllRows(){
var row_no=1;
var table = document.getElementById("tabbody");
for (var i = 0, row; row = table.rows[i]; i++) {
// row.cells[0].innerHTML=row_no++;
//
var f=1
var v =row.cells[f].firstElementChild.value;
if ( !v ){ // Datum
row.cells[f].firstElementChild.focus();
showAlert(error_alert_date);
return false;
}
f=2;
v =row.cells[f].firstElementChild.value;
if ( !v ){ // Datum
row.cells[f].firstElementChild.focus();
showAlert(error_alert_description);
return false;
}
f=3;
v =row.cells[f].firstElementChild.value;
if ( !isFloat(v) ){ // Amount
row.cells[f].firstElementChild.focus();
showAlert(error_alert_sum);
return false;
}
}
return true;
}
function isMailable(){
var row_no=1;
var table = document.getElementById("tabbody");
for (var i = 0, row; row = table.rows[i]; i++) {
pdfs = row.cells[5].querySelector('input[name="file-count"]').value;
if (pdfs==0)
return false;
}
return true;
}
function isFloat(val) {
var floatRegex = /^-?\d+(?:[.,]\d*?)?$/;
if (!floatRegex.test(val))
return false;
val = parseFloat(val);
if (isNaN(val))
return false;
return true;
}
function deleteTableRow(rowId){
var target = document.getElementById("upload-target"+rowId);
// this.message = app.message;
// var children = target.children;
// delete all uploaded elements
/* for (var i = 0; i < children.length; i++) {
var tmp = children[i].getAttribute("data-id");
alert ("DEL");
alert (tmp);
$K.ajax.post({
url: 'delete.php',
data: 'id='+tmp,
before: function(xhr) {},
success: function(response) {},
error: function(response) {}
});
}
*/
// delete the row from table
var element = document.getElementById("trow"+rowId);
element.parentNode.removeChild(element);
renumberTableRows();
}
function countRows(){
return renumberTableRows();
}
function showAlert(text) {
$K.app.message.show( { message: `${text}`, position: 'centered', type: 'is-error' });
}
function setTableRow(id)
{
var $node = $K.dom('#tabbody');
var element = document.createElement('tr');
element.setAttribute("id", "trow"+id);
var e
e = `<td>${a_date}
<input type="hidden" name="date[${currentRowId}]" value="${a_date}" /></td>`;
$node.append(element);
element.innerHTML=(e);
}
function getUploadField(rid)
{
var e = $K.dom('#uploadfield');
var uf =`<div class="upload"
data-kube="upload"
data-type="file"
data-multiple="true"
data-target="#upload-target${rid}"
data-url="upload.php?row=${rid}" data-url-remove="delete.php"
data-progress="true"
data-size="40"
data-placeholder="` + expenses_table_file_placeholder + `">
</div>
<div id="upload-target${rid}" class="upload-target small"></div>
</div>
`;
return uf;
}
// old input field
//+ `<input class="tab_input" name="desc[${rid}]" type="text" placeholder="` + expenses_table_description_placeholder + `"/>`
function getRow(rid,num) {
var cur = document.getElementById("currency").value;
return ""
+ "<td data-label=\"Pos\">"
+ `${num}`
+"</td>"
+ "<td data-label=\"Datum\">"
+ `<input class="tab_input" name="date[${rid}]" value="" type="date" placeholder="Datum*"/>`
+ "</td>"
+ "<td data-label=\"Beschr.\">"
+ `<textarea class="tab_input" name="desc[${rid}]" >` + `</textarea>`
+ "</td>"
+ "<td data-label=\"Betrag\">"
+ `<input class="tab_input" name="amount[${rid}] type="text" placeholder="0.00"/>`
+ "</td>"
+ "<td data-label=\"W&auml;hrung\" style=\"vertical-align: middle\">"
+ cur
// +
// `<select name="currency[${rid}]" >
// <option value="EUR">EUR</option>
// <option value="USD">USD</option>
// <option value="CHF">CHF</option>
// <option value="XBT">XBT</option>
// </select>`
+ "</td>"
+ "<td>"
+ getUploadField(rid)
+ "</td>"
+
`<td><span
onclick="deleteTableRow(${rid})"
class="close is-large">
</span>
</td>`;
}
var nextRid = 1
function addRow() {
var $node = $K.dom('#tabbody');
var e = getRow(nextRid,1);
var element = document.createElement('tr');
element.setAttribute("id", "trow"+nextRid);
$node.append(element);
element.innerHTML=(e);
renumberTableRows();
nextRid++;
}
$K.add('module', 'kaform', {
init: function(app, context)
{
this.app = app;
this.message = app.message;
},
// catch event
onmessage: {
tabs: {
opened: function(sender)
{
console.log('Tab box is ',sender.getActiveBox().nodes[0]);
banktype = sender.getActiveBox().nodes[0].attributes.id.value;
console.log(banktype);
// console.log(sender);
// this.message.show({ message: 'My message' });
}
}
},
});
$K.init({
observer: true,
lang: 'en'
});
// startup is here
// on startup clear session on server
$K.ajax.get({
url: 'clear.php',
data: '', // or key=value object
before: function(xhr) {},
success: function(response) {},
error: function(response) {}
});
function check_field(fieldid,message){
var f = document.getElementById(fieldid).value;
if (! f.trim().length ) {
document.getElementById(fieldid).focus();
showAlert(message)
return false;
}
return true;
}
// add on table row
//addTableRow();
//
//
//
//
function downloadDocument2(){
downloadDocument(true);
}
function downloadDocument1(){
downloadDocument(false);
}
function downloadDocument(mail)
{
if (!checkAllRows())
return;
if (!check_field("projectid", error_alert_projectid))
return;
if (!check_field("realname", error_alert_realname))
return;
if (!check_field("email", error_alert_email))
return;
if (banktype == "bank-eu"){
iban = document.getElementById('iban');
if (!IBAN.isValid(iban.value)){
document.getElementById("iban").focus();
showAlert(error_alert_iban);
return;
}
/* if (!document.getElementById('bic').value.trim().length){
document.getElementById("bic").focus();
showAlert(error_alert_bic);
return;
}
*/
}
if (banktype == "bank-paypal"){
ii = document.getElementById('paypal');
if (!document.getElementById('paypal').value.trim().length){
document.getElementById("paypal").focus();
showAlert(error_alert_paypal);
return;
}
}
if (countRows()==0){
document.getElementById("add_issue").focus();
showAlert(error_alert_expenses);
return;
}
if (!document.getElementById("agree").checked){
document.getElementById("agree").focus();
showAlert(error_alert_agreement);
return;
};
if (mail){
if (!isMailable()){
showAlert(error_alert_ismailable);
return;
}
document.getElementById('sendmail').value="yes";
}
else{
document.getElementById('sendmail').value="no";
}
document.getElementById('banktype').value=banktype;
document.getElementById("theform").submit();
}
/*
function checkIban()
{
iban = document.getElementById('iban');
if(IBAN.isValid(iban.value))
if( document.getElementById('eu').checked ) {
if(IBAN.isValid(iban.value))
{
if(iban.classList.contains('invalid')) iban.classList.remove('invalid');
iban.classList.add('valid');
}
else {
if(iban.classList.contains('valid')) iban.classList.remove('valid');
iban.classList.add('invalid');
}
} else {
if(iban.classList.contains('invalid')) iban.classList.remove('invalid');
if(iban.classList.contains('valid')) iban.classList.remove('valid');
}
}
*/