Files
firefly-iii/public/v1/js/ff/rules/index.js

193 lines
5.9 KiB
JavaScript
Raw Normal View History

2016-01-14 16:41:15 +01:00
/*
* index.js
2020-02-16 13:59:41 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2016-01-14 16:41:15 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2016-01-14 16:41:15 +01:00
*/
2017-12-29 09:05:35 +01:00
/** global: token */
var fixHelper = function (e, tr) {
2016-01-22 07:27:49 +01:00
"use strict";
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;
};
2020-11-05 06:16:22 +01:00
function createCookie(name, value, days) {
"use strict";
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
}
function readCookie(name) {
"use strict";
var nameEQ = encodeURIComponent(name) + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
}
return null;
}
$(function () {
2016-01-29 17:53:49 +01:00
"use strict";
2020-06-26 07:28:25 +02:00
$('.group-rules').find('tbody').sortable(
2016-01-29 17:53:49 +01:00
{
helper: fixHelper,
stop: sortStop,
2020-06-26 07:28:25 +02:00
handle: '.rule-handle',
2016-01-29 17:53:49 +01:00
cursor: "move"
}
);
2020-11-05 06:16:22 +01:00
$('.rules-box').each(function (i, v) {
var box = $(v);
var groupId = box.data('group');
var cookieName = 'rule-box-collapse-' + groupId;
if ('collapsed' === readCookie(cookieName)) {
box.addClass('collapsed-box');
console.log('Box ' + groupId + ' is collapsed');
return;
}
console.log('Box ' + groupId + ' is not collapsed');
});
$('.rules-box').on('expanded.boxwidget', function (e) {
var box = $(e.currentTarget);
var groupId = box.data('group');
var cookieName = 'rule-box-collapse-' + groupId;
createCookie(cookieName, 'expanded', 90);
//console.log('Box ' + box.data('group') + ' is now expanded.');
//alert('hi!')
});
$('.rules-box').on('collapsed.boxwidget', function (e) {
var box = $(e.currentTarget);
var groupId = box.data('group');
var cookieName = 'rule-box-collapse-' + groupId;
createCookie(cookieName, 'collapsed', 90);
//console.log('Box ' + box.data('group') + ' is now collapsed.');
//alert('ho!')
});
//collapsed-box
// test rule triggers button:
2017-08-12 07:47:42 +02:00
$('.test_rule_triggers').click(testRuleTriggers);
2016-01-29 17:53:49 +01:00
}
);
function testRuleTriggers(e) {
2017-09-25 09:28:16 +02:00
var obj = $(e.target);
var ruleId = parseInt(obj.data('id'));
2017-09-25 09:28:16 +02:00
var icon = obj;
2017-11-15 11:33:07 +01:00
if (obj.prop("tagName") === 'A') {
2021-06-11 19:50:05 +02:00
icon = $('span', obj);
2017-09-25 09:28:16 +02:00
}
// change icon:
icon.addClass('fa-spinner fa-spin').removeClass('fa-flask');
var modal = $("#testTriggerModal");
// respond to modal:
2020-10-24 17:27:36 +02:00
modal.on('hide.bs.modal', function () {
2017-09-25 09:28:16 +02:00
disableRuleSpinners();
});
// Find a list of existing transactions that match these triggers
$.get('rules/test-rule/' + ruleId).done(function (data) {
2017-09-25 09:28:16 +02:00
// Set title and body
modal.find(".transactions-list").html(data.html);
// Show warning if appropriate
if (data.warning) {
modal.find(".transaction-warning .warning-contents").text(data.warning);
modal.find(".transaction-warning").show();
} else {
modal.find(".transaction-warning").hide();
}
// Show the modal dialog
modal.modal();
}).fail(function () {
alert('Cannot get transactions for given triggers.');
2017-09-25 09:28:16 +02:00
disableRuleSpinners();
});
return false;
}
2017-09-25 09:28:16 +02:00
function disableRuleSpinners() {
$('i.test_rule_triggers').removeClass('fa-spin fa-spinner').addClass('fa-flask');
}
function sortStop(event, ui) {
"use strict";
2020-06-26 07:28:25 +02:00
// resort / move rule
2020-11-05 06:16:22 +01:00
$.each($('.group-rules'), function (i, v) {
2020-10-24 17:27:36 +02:00
$.each($('tr.single-rule', $(v)), function (counter, value) {
var holder = $(value);
2020-06-26 07:28:25 +02:00
var position = parseInt(holder.data('position'));
var ruleGroupId = holder.data('group-id');
var ruleId = holder.data('id');
var originalOrder = parseInt(holder.data('order'));
var newOrder;
2020-10-24 17:27:36 +02:00
if (position === counter) {
2020-06-26 07:28:25 +02:00
// not changed, position is what it should be.
return;
}
2020-10-24 17:27:36 +02:00
if (position < counter) {
2020-06-26 07:28:25 +02:00
// position is less.
2020-10-24 17:27:36 +02:00
console.log('Rule #' + ruleId + ' moved down from position ' + originalOrder + ' to ' + (counter + 1));
2020-06-26 07:28:25 +02:00
}
2020-10-24 17:27:36 +02:00
if (position > counter) {
2020-11-05 06:16:22 +01:00
console.log('Rule #' + ruleId + ' moved up from position ' + originalOrder + ' to ' + (counter + 1));
2020-06-26 07:28:25 +02:00
}
// update position:
2020-10-24 17:27:36 +02:00
holder.data('position', counter);
newOrder = counter + 1;
2020-06-26 07:28:25 +02:00
$.post('rules/move-rule/' + ruleId + '/' + ruleGroupId, {order: newOrder, _token: token});
});
2020-06-26 07:28:25 +02:00
});
2016-01-22 07:27:49 +01:00
}