firefly-iii/public/js/piggy-banks.js

81 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-05-26 00:50:38 -05:00
/* globals $, googleLineChart, token, piggyBankID */
2015-05-24 13:41:14 -05:00
2015-05-24 11:22:41 -05:00
// Return a helper with preserved width of cells
2015-05-24 13:41:14 -05:00
var fixHelper = function (e, tr) {
"use strict";
2015-05-24 11:22:41 -05:00
var $originals = tr.children();
var $helper = tr.clone();
$helper.children().each(function (index) {
// Set helper cell sizes to match the original sizes
$(this).width($originals.eq(index).width());
});
return $helper;
};
2015-02-24 14:10:25 -06:00
$(function () {
2015-05-24 13:41:14 -05:00
"use strict";
2015-02-24 14:10:25 -06:00
$('.addMoney').on('click', addMoney);
$('.removeMoney').on('click', removeMoney);
if (typeof(googleLineChart) === 'function' && typeof(piggyBankID) !== 'undefined') {
2015-05-16 02:57:31 -05:00
googleLineChart('chart/piggyBank/' + piggyBankID, 'piggy-bank-history');
2015-02-24 14:10:25 -06:00
}
2015-04-28 02:15:31 -05:00
$('#sortable tbody').sortable(
2015-03-15 12:00:33 -05:00
{
2015-04-28 02:15:31 -05:00
helper: fixHelper,
2015-03-15 12:00:33 -05:00
stop: stopSorting,
2015-04-28 13:17:31 -05:00
handle: '.handle',
start: function (event, ui) {
// Build a placeholder cell that spans all the cells in the row
var cellCount = 0;
$('td, th', ui.helper).each(function () {
// For each TD or TH try and get it's colspan attribute, and add that or 1 to the total
var colspan = 1;
var colspanAttr = $(this).attr('colspan');
if (colspanAttr > 1) {
colspan = colspanAttr;
}
cellCount += colspan;
});
// Add the placeholder UI - note that this is the item's content, so TD rather than TR
ui.placeholder.html('<td colspan="' + cellCount + '">&nbsp;</td>');
}
2015-03-15 12:00:33 -05:00
}
);
2015-02-24 14:10:25 -06:00
});
2015-05-24 11:22:41 -05:00
2015-02-24 14:10:25 -06:00
function addMoney(e) {
2015-05-24 13:41:14 -05:00
"use strict";
2015-02-24 14:10:25 -06:00
var pigID = parseInt($(e.target).data('id'));
2015-05-31 13:23:49 -05:00
$('#defaultModal').empty().load('piggy-banks/add/' + pigID, function () {
$('#defaultModal').modal('show');
2015-03-15 12:00:33 -05:00
});
2015-02-24 14:10:25 -06:00
return false;
}
function removeMoney(e) {
2015-05-24 13:41:14 -05:00
"use strict";
2015-02-24 14:10:25 -06:00
var pigID = parseInt($(e.target).data('id'));
2015-05-31 13:24:06 -05:00
$('#defaultModal').empty().load('piggy-banks/remove/' + pigID, function () {
$('#defaultModal').modal('show');
2015-03-15 12:00:33 -05:00
});
2015-02-24 14:10:25 -06:00
return false;
2015-03-15 12:00:33 -05:00
}
function stopSorting() {
2015-05-24 13:41:14 -05:00
"use strict";
2015-03-15 12:00:33 -05:00
$('.loadSpin').addClass('fa fa-refresh fa-spin');
var order = [];
2015-05-24 13:41:14 -05:00
$.each($('#sortable>tbody>tr'), function (i, v) {
2015-03-15 12:00:33 -05:00
var holder = $(v);
var id = holder.data('id');
order.push(id);
});
2015-05-24 13:41:14 -05:00
$.post('/piggy-banks/sort', {_token: token, order: order}).success(function () {
2015-03-15 12:00:33 -05:00
$('.loadSpin').removeClass('fa fa-refresh fa-spin');
});
2015-02-24 14:10:25 -06:00
}