tab/app/assets/javascripts/transactions.js

119 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

$(document).on('turbolinks:load', function() {
2016-02-01 13:30:47 +01:00
form = $("#new_transaction");
submit_button = $(form).find("input[type=submit]");
errors = $("#transaction_errors");
panel_ul = $(errors).find(".panel-body ul");
flash_success = $("#transaction_success");
2015-09-11 20:46:37 +02:00
if ($("#users-transactions")[0]) {
return;
}
2015-09-11 17:54:37 +02:00
var table = $('#transactions').DataTable({
processing: true,
serverSide: true,
searching: true,
lengthChange: false,
ordering: false,
ajax: $('#transactions').data('source'),
pagingType: 'full_numbers',
autoWidth: false,
responsive: true,
columns: [
2015-09-17 13:16:13 +02:00
{ data: 'time', name: 'Time', width: "15%", className: 'min-tablet-l date-column'},
{ data: 'amount', name: 'Amount', width: "10%", className: 'min-mobile amount-column'},
{ data: 'peer', name: 'Peer', width: "15%", className: 'min-mobile peer-column'},
{ data: 'issuer', name: 'Issuer', width: "15%", className: 'min-desktop issuer-column'},
{ data: 'message', name: 'Message', width: "45%", className: 'min-tablet-p message-column'}
2015-09-11 17:54:37 +02:00
],
columnDefs: [
{
targets: 0,
render: function(data, type, full, meta) {
return $.format.date(data, 'E dd/MM/yyyy HH:mm');
}
},
{
targets: 1,
render: function(data, type, full, meta) {
return (data/100).toFixed(2);
}
},
{
targets: 4,
render: function(data, type, full, meta) {
return new Option(data).innerHTML
}
2015-09-11 17:54:37 +02:00
}
]
});
$('.dataTables_filter').hide();
$('.input-listen').each(function(index, element) {
var filter = $(element);
var type = filter.attr('data-input-type');
var column = table.column(filter.attr('data-filter-name') + ':name');
2015-09-17 12:43:29 +02:00
var refreshTable = function() {
var value = null
if(filter.hasClass('bound')) {
var lower = filter.find('.lower-bound');
var upper = filter.find('.upper-bound');
value = lower.val() + '~' + upper.val();
} else {
value = $(this).val();
}
value = filter.attr('data-input-type') + ':' + value;
if(column.search() !== value) {
column.search(value).draw();
}
};
filter.find('.value-thing').on('change', refreshTable);
filter.find('.value-thing.live-updating').on('keyup', refreshTable);
2015-09-11 17:54:37 +02:00
});
2015-09-11 13:16:53 +02:00
2015-09-11 20:46:37 +02:00
// filters
filters = $("#transactions-filters");
filters_body = filters.find(".panel-body");
filters.find(".panel-heading").click( function() {
filters_body.slideToggle();
});
filters_body.hide();
$(form).submit(function(e) {
euros = parseInt($(form).find('input[name="transaction[euros]"]').val());
console.log(euros);
if (euros < 6) {
return true;
} else {
e.preventDefault();
return confirm("Are you sure? " + euros + " monies is a lot of money ...");
}
});
2015-09-11 13:16:53 +02:00
$(form).on("ajax:before", function(xhr, settings) {
2016-02-01 13:30:47 +01:00
$(flash_success).addClass("hidden");
$(submit_button).val("Processing");
2015-09-11 13:16:53 +02:00
$(submit_button).attr('disabled', 'disabled');
}).on("ajax:success", function(data, status, xhr) {
2016-02-01 13:30:47 +01:00
$(flash_success).removeClass("hidden");
$(errors).addClass("hidden");
$(form)[0].reset();
2016-02-02 17:49:32 +01:00
$('#transaction_creditor').select2('val', null);
2015-09-11 13:16:53 +02:00
}).on("ajax:error", function(xhr, status, error) {
2016-02-01 13:30:47 +01:00
$(errors).removeClass("hidden");
$(panel_ul).empty();
2015-09-11 13:16:53 +02:00
$.each(JSON.parse(status.responseText), function(index, val) {
2016-02-01 13:30:47 +01:00
$(panel_ul).append("<li>" + val + "</li>");
});
2015-09-11 13:16:53 +02:00
}).on("ajax:complete", function(xhr, status) {
2016-02-01 13:30:47 +01:00
$(submit_button).val("Send it");
2015-09-11 13:16:53 +02:00
$(submit_button).attr('disabled', false);
2015-09-11 17:54:37 +02:00
table.ajax.reload();
2016-02-01 13:30:47 +01:00
});
});
2015-09-11 13:16:53 +02:00
2015-09-11 17:54:37 +02:00
2015-09-11 13:16:53 +02:00
$.ajaxSetup({
dataType: 'text'
2016-02-01 13:30:47 +01:00
});