Newer
Older
$(document).ready(function()
{
$('#modalConfirmDelete').modal('open');
// open filter modal if corresponding anchor is in url (originating from hotkeys.js)
if(window.location.href.endsWith('#modalFilter'))
{
$('#modalFilter').modal('open');
}
if($("#transaction-name").length)
{
let elements = document.querySelectorAll('#transaction-name');
let autoCompleteInstances = M.Autocomplete.init(elements, {
data: transactionNameSuggestions,
});
// prevent tab traversal for dropdown (otherwise "tab" needs to be hit twice to jump from name input to amount input)
autoCompleteInstances[0].dropdown.dropdownEl.tabIndex = -1;
document.getElementById('transaction-name').focus();
}
if($("#transaction-description").length)
{
$("#transaction-description").characterCounter();
}

Robert Goldmann
committed
if($(".datepicker-simple".length) && $("#transaction-repeating-end-date-input").length)
{
let pickerEndDate = document.getElementById('transaction-repeating-end-date-input');
// select corresponding radio button
let endDate = document.getElementById("repeating-end-date");
pickerEndDate.addEventListener('input', function()
{
endDate.checked = true;
});
pickerEndDate.addEventListener('focus', function()
{
endDate.checked = true;
});
}
if($(".datepicker").length)
{
let pickerStartDate = M.Datepicker.init(document.getElementById('transaction-datepicker'), {

Robert Goldmann
committed
firstDay: 1,
showClearBtn: false,
setDefaultDate: true,

Robert Goldmann
committed
defaultDate: startDate,
i18n: {
// Strings and translations
months: monthNames,
monthsShort: monthNamesShort,
weekdays: weekDays,
weekdaysShort: weekDaysShort,
weekdaysAbbrev: weekDaysLetters,
// Buttons
done: buttonClose,
// Accessibility labels
labelMonthNext: '>',
labelMonthPrev: '<'
},

Robert Goldmann
committed
// Formats
format: 'dd.mm.yyyy',
formatSubmit: 'dd.mm.yyyy',

Robert Goldmann
committed
{
if(typeof pickerEndDate !== "undefined")
{
pickerEndDate.destroy();
pickerEndDate = createDatePickerEnd(this.date, pickerEndDate.date);
}

Robert Goldmann
committed
}
});

Robert Goldmann
committed
// picker end date
let pickerEndDate = createDatePickerEnd(pickerStartDate.date, endDate);

Robert Goldmann
committed
}

Robert Goldmann
committed

Robert Goldmann
committed
function createDatePickerEnd(minDate, selectedDate)
{
if(selectedDate < minDate)

Robert Goldmann
committed
{

Robert Goldmann
committed
selectedDate = minDate;

Robert Goldmann
committed
}
return M.Datepicker.init(document.getElementById('transaction-repeating-end-date-input'), {
firstDay: 1,
showClearBtn: false,
setDefaultDate: true,

Robert Goldmann
committed
minDate: minDate,

Robert Goldmann
committed
defaultDate: selectedDate,
i18n: {
// Strings and translations
months: monthNames,
monthsShort: monthNamesShort,
weekdays: weekDays,
weekdaysShort: weekDaysShort,
weekdaysAbbrev: weekDaysLetters,
// Buttons
done: buttonClose,
// Accessibility labels
labelMonthNext: '>',
labelMonthPrev: '<'
},
// Formats
format: 'dd.mm.yyyy',
formatSubmit: 'dd.mm.yyyy',
// select corresponding radio button
let endDate = document.getElementById("repeating-end-date");
endDate.checked = true;
}
}

Robert Goldmann
committed
if($('#transaction-amount').length)
{
$('#transaction-amount').on('change keydown paste input', function()
{
validateAmount($(this).val());
});
}

Robert Goldmann
committed
if($(transactionRepeatingModifierID).length)
$(transactionRepeatingModifierID).on('change keydown paste input', function()
{
// substr(1) removes "#" at the beginning
validateNumber($(this).val(), transactionRepeatingModifierID.substr(1), "hidden-" + transactionRepeatingModifierID.substr(1), numberValidationMessage, REGEX_NUMBER);
});
}

Robert Goldmann
committed
if($(transactionRepeatingEndAfterXTimesInputID).length)
$(transactionRepeatingEndAfterXTimesInputID).on('change keydown paste input', function()
{
validateNumber($(this).val(), transactionRepeatingEndAfterXTimesInputID.substr(1), null, numberValidationMessage, REGEX_NUMBER);
// select corresponding radio button
let endAfterXTimes = document.getElementById("repeating-end-after-x-times");
endAfterXTimes.checked = true;
if($(".chips-autocomplete").length)
{
let elements = document.querySelectorAll('.chips-autocomplete');
let instances = M.Chips.init(elements, {
autocompleteOptions: {
data: tagAutoComplete,
limit: Infinity,
minLength: 1
},

Robert Goldmann
committed
placeholder: tagsPlaceholder,
data: initialTags
// prevent tab traversal for dropdown (otherwise "tab" needs to be hit twice to jump from tag input to account input)
instances[0].autocomplete.dropdown.dropdownEl.tabIndex = -1;
}
// prevent form submit on enter (otherwise tag functionality will be hard to use)
$(document).on("keypress", 'form', function(e)
{
if(e.ctrlKey)
{
return true;
}
if(code === 13)
{
if(e.target.nodeName === 'TEXTAREA' || e.target.id === 'search')
{
return true;
}
e.preventDefault();
return false;
}
});
$('.buttonIncome').click(function()
$('.buttonIncome').each(function()
{
$(this).removeClass("budgetmaster-grey");
$(this).removeClass("budgetmaster-text-isPayment");
$(this).addClass("budgetmaster-green");
});
$('.buttonExpenditure').each(function()
{
$(this).removeClass("budgetmaster-red");
$(this).addClass("budgetmaster-grey");
$(this).addClass("budgetmaster-text-isPayment");
});
document.getElementById("input-isPayment").value = 0;
});
$('.buttonExpenditure').click(function()
$('.buttonExpenditure').each(function()
{
$(this).removeClass("budgetmaster-grey");
$(this).removeClass("budgetmaster-text-isPayment");
$(this).addClass("budgetmaster-red");
});
$('.buttonIncome').each(function()
{
$(this).removeClass("budgetmaster-green");
$(this).addClass("budgetmaster-grey");
$(this).addClass("budgetmaster-text-isPayment");
});
document.getElementById("input-isPayment").value = 1;
});
M.FloatingActionButton.init(document.querySelectorAll('.new-transaction-button'), {
direction: 'bottom',
hoverEnabled: false
});
// scroll to highlighted transaction
let highlightedSmall = document.getElementById("highlighted-small");
let highlightedLarge = document.getElementById("highlighted-large");
if(highlightedSmall !== undefined && highlightedSmall != null && !isHidden(highlightedSmall))
{
$('html, body').animate({
scrollTop: $(highlightedSmall).offset().top
}, 500);
}
else if(highlightedLarge !== undefined && highlightedLarge != null && !isHidden(highlightedLarge))
{
$('html, body').animate({
scrollTop: $(highlightedLarge).offset().top
}, 500);
}
});
function isHidden(el)
{
return (style.display === 'none' || style.display === 'none !important')
}
let transactionRepeatingModifierID = "#transaction-repeating-modifier";
let transactionRepeatingEndAfterXTimesInputID = "#transaction-repeating-end-after-x-times-input";
AMOUNT_REGEX = new RegExp("^-?\\d+(,\\d+)?(\\.\\d+)?$");
ALLOWED_CHARACTERS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ",", "."];

Robert Goldmann
committed
DATE_REGEX_SHORT = new RegExp("^(\\d{2}.\\d{2}.)(\\d{2})$");
DATE_REGEX_LONG = new RegExp("^\\d{2}.\\d{2}.\\d{4}$");

Robert Goldmann
committed
function validateAmount(text, allowEmpty = false)
{
if(allowEmpty && text.length === 0)
{
removeTooltip(id);
document.getElementById("hidden-" + id).value = "";
if(text.match(AMOUNT_REGEX) == null)
{
addTooltip(id, amountValidationMessage);
document.getElementById("hidden-" + id).value = "";
}
else
{
let amount = parseFloat(text.replace(",", ".")) * 100;
document.getElementById("hidden-" + id).value = amount.toFixed(0);
}

Robert Goldmann
committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
function validateDate(inputId)
{
let dateInput = document.getElementById(inputId);
dateInput.value = dateInput.value.trim();
let date = dateInput.value;
if(date.match(DATE_REGEX_LONG) != null)
{
removeTooltip(inputId);
return true;
}
let match = date.match(DATE_REGEX_SHORT);
if(match != null)
{
let dayAndMonth = match[1];
let year = match[2];
dateInput.value = dayAndMonth + '20' + year;
removeTooltip(inputId);
return true;
}
else
{
addTooltip(inputId, dateValidationMessage);
return false;
}
}
function validateForm(allowEmptyAmount = false)
// amount
let isValidAmount = validateAmount($('#transaction-amount').val(), allowEmptyAmount);
if(!isValidAmount)
{
return false;
}

Robert Goldmann
committed
// start date
let isValidDate = validateDate('transaction-datepicker');
if(!isValidDate)
{
return false;
}
// description
let description = document.getElementById('transaction-description').value;
if(description.length > 250)
{
return false;
}
// handle tags
if($(".chips-autocomplete").length)
let tags = M.Chips.getInstance(document.querySelector('.chips-autocomplete')).chipsData;
let parent = document.getElementById("hidden-transaction-tags");
for(let i = 0; i < tags.length; i++)
input.setAttribute("type", "hidden");
input.setAttribute("name", "tags[" + i + "].name");
input.setAttribute("value", tags[i].tag);
parent.appendChild(input);
}
if($(transactionRepeatingModifierID).length)
if(!validateNumber($(transactionRepeatingModifierID).val(), transactionRepeatingModifierID.substr(1), "hidden-" + transactionRepeatingModifierID.substr(1), numberValidationMessage, REGEX_NUMBER))
let endNever = document.getElementById("repeating-end-never");
let endAfterXTimes = document.getElementById("repeating-end-after-x-times");
let endDate = document.getElementById("repeating-end-date");
let endInput = document.getElementById("hidden-transaction-repeating-end-value");
if(endNever.checked)
{
return true;
}
if(endAfterXTimes.checked)
{
if(!validateNumber($(transactionRepeatingEndAfterXTimesInputID).val(), transactionRepeatingEndAfterXTimesInputID.substr(1), null, numberValidationMessage, REGEX_NUMBER))
{
}
endInput.value = $(transactionRepeatingEndAfterXTimesInputID).val();
}
{

Robert Goldmann
committed
// start date
let isValidDate = validateDate('transaction-repeating-end-date-input');
if(!isValidDate)
{
return false;
}
endInput.value = $("#transaction-repeating-end-date-input").val();
}