Files
firefly-iii/public/v1/js/ff/rules/create-edit.js

437 lines
16 KiB
JavaScript
Raw Normal View History

2016-01-14 16:41:15 +01:00
/*
* create-edit.js
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-01-14 16:41:15 +01:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:43:13 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-01-14 16:41:15 +01:00
*/
2017-01-22 09:15:53 +01:00
/** global: triggerCount, actionCount */
$(function () {
"use strict";
2018-12-22 06:21:06 +01:00
$(".content-wrapper form input:enabled:visible:first").first().focus().select();
2017-01-22 09:15:53 +01:00
if (triggerCount > 0) {
2018-08-05 15:34:20 +02:00
console.log('trigger count is larger than zero, call onAddNewTrigger.');
2017-01-22 09:15:53 +01:00
onAddNewTrigger();
}
if (actionCount > 0) {
2018-08-05 15:34:20 +02:00
console.log('action count is larger than zero, call onAddNewAction.');
2017-01-22 09:15:53 +01:00
onAddNewAction();
}
2018-08-05 15:34:20 +02:00
if (triggerCount === 0) {
console.log('trigger count is zero, add trigger.');
addNewTrigger();
}
if (actionCount === 0) {
console.log('action count is zero, add action.');
addNewAction();
}
2017-01-22 09:15:53 +01:00
$('.add_rule_trigger').click(addNewTrigger);
$('.add_rule_action').click(addNewAction);
$('.test_rule_triggers').click(testRuleTriggers);
$('.remove-trigger').unbind('click').click(removeTrigger);
$('.remove-action').unbind('click').click(removeAction);
});
2016-01-14 16:41:15 +01:00
/**
* This method triggers when a new trigger must be added to the form.
*/
2016-01-14 16:41:15 +01:00
function addNewTrigger() {
"use strict";
triggerCount++;
2018-08-05 15:34:20 +02:00
console.log('In addNewTrigger(), count is now ' + triggerCount);
2017-01-22 09:15:53 +01:00
// disable the button
$('.add_rule_trigger').attr('disabled', 'disabled');
2016-01-14 16:41:15 +01:00
// get the HTML for the new trigger
2016-02-04 07:30:12 +01:00
$.getJSON('json/trigger', {count: triggerCount}).done(function (data) {
2017-01-22 09:15:53 +01:00
// append it to the other triggers
2016-01-14 16:41:15 +01:00
$('tbody.rule-trigger-tbody').append(data.html);
2017-01-22 09:15:53 +01:00
$('.remove-trigger').unbind('click').click(removeTrigger);
// update all "select trigger type" dropdowns
// so the accompanying text-box has the correct autocomplete.
onAddNewTrigger();
2017-01-22 09:15:53 +01:00
$('.add_rule_trigger').removeAttr('disabled');
2016-01-14 16:41:15 +01:00
}).fail(function () {
alert('Cannot get a new trigger.');
2017-01-22 09:15:53 +01:00
$('.add_rule_trigger').removeAttr('disabled');
2016-01-14 16:41:15 +01:00
});
2017-01-22 09:15:53 +01:00
return false;
2016-02-10 15:04:06 +01:00
2016-01-14 16:41:15 +01:00
}
/**
* Method triggers when a new action must be added to the form..
*/
2016-01-14 16:41:15 +01:00
function addNewAction() {
"use strict";
2016-01-14 21:34:17 +01:00
actionCount++;
2018-08-05 15:34:20 +02:00
console.log('In addNewAction(), count is now ' + actionCount);
2017-01-22 09:15:53 +01:00
// disable the button
$('.add_rule_action').attr('disabled', 'disabled');
2016-02-04 07:30:12 +01:00
$.getJSON('json/action', {count: actionCount}).done(function (data) {
2016-01-14 16:41:15 +01:00
$('tbody.rule-action-tbody').append(data.html);
2016-02-10 15:04:06 +01:00
// add action things.
2017-01-22 09:15:53 +01:00
$('.remove-action').unbind('click').click(removeAction);
// update all "select trigger type" dropdowns
// so the accompanying text-box has the correct autocomplete.
onAddNewAction();
2016-02-10 15:04:06 +01:00
2017-01-22 09:15:53 +01:00
$('.add_rule_action').removeAttr('disabled');
2016-02-10 15:04:06 +01:00
2016-01-14 16:41:15 +01:00
}).fail(function () {
alert('Cannot get a new action.');
2017-01-22 09:15:53 +01:00
$('.add_rule_action').removeAttr('disabled');
2016-01-14 16:41:15 +01:00
});
2017-01-22 09:15:53 +01:00
return false;
2016-02-10 15:04:06 +01:00
}
/**
* Method fires when a trigger must be removed from the form.
*
* @param e
*/
2016-02-10 15:04:06 +01:00
function removeTrigger(e) {
"use strict";
var target = $(e.target);
2017-04-09 07:56:46 +02:00
if (target.prop("tagName") === "I") {
2016-02-10 15:04:06 +01:00
target = target.parent();
}
// remove grand parent:
target.parent().parent().remove();
// if now at zero, immediatly add one again:
2017-04-09 07:56:46 +02:00
if ($('.rule-trigger-tbody tr').length === 0) {
2016-02-10 15:04:06 +01:00
addNewTrigger();
}
2017-01-22 09:15:53 +01:00
return false;
2016-02-10 15:04:06 +01:00
}
/**
* Method fires when an action must be removed from the form.
*
* @param e
*/
2016-02-10 15:04:06 +01:00
function removeAction(e) {
"use strict";
var target = $(e.target);
2017-04-09 07:56:46 +02:00
if (target.prop("tagName") === "I") {
2016-02-10 15:04:06 +01:00
target = target.parent();
}
// remove grand parent:
target.parent().parent().remove();
// if now at zero, immediatly add one again:
2017-04-09 07:56:46 +02:00
if ($('.rule-action-tbody tr').length === 0) {
2016-02-10 15:04:06 +01:00
addNewAction();
}
2017-01-22 09:15:53 +01:00
return false;
}
/**
* Method fires when a new action is added. It will update ALL action value input fields.
*/
function onAddNewAction() {
"use strict";
2018-08-05 15:34:20 +02:00
console.log('Now in onAddNewAction()');
2018-12-21 09:01:21 +01:00
var selectQuery = 'select[name^="actions["][name$="][type]"]';
2018-08-05 15:34:20 +02:00
var selectResult = $(selectQuery);
console.log('Select query is "' + selectQuery + '" and the result length is ' + selectResult.length);
2017-01-22 09:15:53 +01:00
// update all "select action type" dropdown buttons so they will respond correctly
2018-08-05 15:34:20 +02:00
selectResult.unbind('change').change(function (e) {
2017-01-22 09:15:53 +01:00
var target = $(e.target);
updateActionInput(target)
});
2018-09-13 20:45:16 +02:00
// make sure each select thing is triggered at least once.
$.each($('.rule-action-holder'), function (i, v) {
var holder = $(v);
var select = holder.find('select');
console.log('Trigger updateActionInput() for select ' + select);
updateActionInput(select);
});
}
/**
* Method fires when a new trigger is added. It will update ALL trigger value input fields.
*/
function onAddNewTrigger() {
"use strict";
2018-08-05 15:34:20 +02:00
console.log('Now in onAddNewTrigger()');
2017-01-22 09:15:53 +01:00
2018-12-21 09:01:21 +01:00
var selectQuery = 'select[name^="triggers["][name$="][type]"]';
2018-08-05 15:34:20 +02:00
var selectResult = $(selectQuery);
console.log('Select query is "' + selectQuery + '" and the result length is ' + selectResult.length);
// trigger when user changes the trigger type.
selectResult.unbind('change').change(function (e) {
2017-01-22 09:15:53 +01:00
var target = $(e.target);
updateTriggerInput(target)
});
2018-09-13 20:45:16 +02:00
$.each($('.rule-trigger-holder'), function (i, v) {
var holder = $(v);
var select = holder.find('select');
console.log('Trigger updateTriggerInput() for select ' + select);
updateTriggerInput(select);
});
}
2017-01-22 09:15:53 +01:00
/**
* Creates a nice auto complete action depending on the type of the select list value thing.
*
* @param selectList
*/
function updateActionInput(selectList) {
2018-08-05 15:34:20 +02:00
console.log('Now in updateActionInput() for a select list, currently with value "' + selectList.val() + '".');
2017-01-22 09:15:53 +01:00
// the actual row this select list is in:
var parent = selectList.parent().parent();
// the text input we're looking for:
2018-12-09 20:54:11 +01:00
var inputQuery = 'input[name^="actions["][name$="][value]"]';
2018-08-05 15:34:20 +02:00
var inputResult = parent.find(inputQuery);
console.log('Searching for children in this row with query "' + inputQuery + '" resulted in ' + inputResult.length + ' results.');
inputResult.removeAttr('disabled');
2017-01-22 09:15:53 +01:00
switch (selectList.val()) {
case 'set_category':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/categories');
2017-01-22 09:15:53 +01:00
break;
case 'clear_category':
case 'clear_budget':
case 'clear_notes':
2017-01-22 09:15:53 +01:00
case 'remove_all_tags':
console.log('Select list value is ' + selectList.val() + ', so input needs to be disabled.');
2018-08-05 15:34:20 +02:00
inputResult.attr('disabled', 'disabled');
2017-01-22 09:15:53 +01:00
break;
case 'set_budget':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/budgets');
2017-01-22 09:15:53 +01:00
break;
case 'add_tag':
case 'remove_tag':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/tags');
2017-01-22 09:15:53 +01:00
break;
case 'set_description':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/transaction-journals/all');
2017-01-22 09:15:53 +01:00
break;
case 'set_source_account':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/all-accounts');
2017-01-22 09:15:53 +01:00
break;
case 'set_destination_account':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/all-accounts');
2017-01-22 09:15:53 +01:00
break;
2018-09-15 13:43:57 +02:00
case 'convert_withdrawal':
console.log('Select list value is ' + selectList.val() + ', so input needs expense accounts auto complete.');
createAutoComplete(inputResult, 'json/expense-accounts');
break;
case 'convert_deposit':
console.log('Select list value is ' + selectList.val() + ', so input needs revenue accounts auto complete.');
createAutoComplete(inputResult, 'json/revenue-accounts');
break;
case 'convert_transfer':
console.log('Select list value is ' + selectList.val() + ', so input needs asset accounts auto complete.');
createAutoComplete(inputResult, 'json/asset-accounts');
break;
case 'link_to_bill':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/bills');
break;
2017-01-30 10:33:18 +01:00
default:
console.log('Select list value is ' + selectList.val() + ', destroy auto complete, do nothing else.');
2018-08-05 15:34:20 +02:00
inputResult.typeahead('destroy');
2017-01-30 10:33:18 +01:00
break;
2017-01-22 09:15:53 +01:00
}
}
/**
* Creates a nice auto complete trigger depending on the type of the select list value thing.
*
* @param selectList
*/
2017-01-22 09:15:53 +01:00
function updateTriggerInput(selectList) {
2018-08-05 15:34:20 +02:00
console.log('Now in updateTriggerInput() for a select list, currently with value "' + selectList.val() + '".');
// the actual row this select list is in:
var parent = selectList.parent().parent();
// the text input we're looking for:
2018-12-09 20:54:11 +01:00
var inputQuery = 'input[name^="triggers["][name$="][value]"]';
2018-08-05 15:34:20 +02:00
var inputResult = parent.find(inputQuery);
console.log('Searching for children in this row with query "' + inputQuery + '" resulted in ' + inputResult.length + ' results.');
inputResult.prop('disabled', false);
2019-01-07 18:19:48 +01:00
inputResult.prop('type', 'text');
switch (selectList.val()) {
case 'from_account_starts':
case 'from_account_ends':
case 'from_account_is':
case 'from_account_contains':
case 'to_account_starts':
case 'to_account_ends':
case 'to_account_is':
case 'to_account_contains':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/all-accounts');
break;
2017-01-24 15:38:41 +01:00
case 'tag_is':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/tags');
2017-01-24 15:38:41 +01:00
break;
case 'budget_is':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/budgets');
2017-01-24 15:38:41 +01:00
break;
case 'category_is':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/categories');
2017-01-24 15:38:41 +01:00
break;
case 'transaction_type':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/transaction-types');
break;
case 'description_starts':
case 'description_ends':
case 'description_contains':
case 'description_is':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/transaction-journals/all');
break;
2017-09-03 10:39:05 +02:00
case 'has_no_category':
case 'has_any_category':
case 'has_no_budget':
case 'has_any_budget':
case 'has_no_tag':
case 'no_notes':
case 'any_notes':
2017-09-03 10:39:05 +02:00
case 'has_any_tag':
console.log('Select list value is ' + selectList.val() + ', so input needs to be disabled.');
2018-08-05 15:34:20 +02:00
inputResult.prop('disabled', true);
inputResult.typeahead('destroy');
2017-09-03 10:39:05 +02:00
break;
2018-04-08 16:27:52 +02:00
case 'currency_is':
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
2018-08-05 15:34:20 +02:00
createAutoComplete(inputResult, 'json/currency-names');
2018-04-08 16:27:52 +02:00
break;
2019-01-07 18:19:48 +01:00
case 'amount_less':
case 'amount_more':
case 'amount_exactly':
console.log('Set value to type=number');
inputResult.prop('type', 'number');
inputResult.prop('step','any');
break;
2017-01-30 10:33:18 +01:00
default:
console.log('Select list value is ' + selectList.val() + ', destroy auto complete, do nothing else.');
2018-08-05 15:34:20 +02:00
inputResult.typeahead('destroy');
2017-01-30 10:33:18 +01:00
break;
}
}
/**
* Create actual autocomplete
* @param input
* @param URI
*/
function createAutoComplete(input, URI) {
console.log('Now in createAutoComplete("' + URI + '").');
input.typeahead('destroy');
var source = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
2018-10-28 19:17:33 +01:00
url: URI + '?uid=' + uid,
filter: function (list) {
return $.map(list, function (name) {
return {name: name};
});
}
},
remote: {
2018-10-28 19:17:33 +01:00
url: URI + '?search=%QUERY&uid=' + uid,
wildcard: '%QUERY',
filter: function (list) {
return $.map(list, function (name) {
return {name: name};
});
}
}
});
source.initialize();
input.typeahead({hint: true, highlight: true,}, {source: source, displayKey: 'name', autoSelect: false});
}
function testRuleTriggers() {
"use strict";
2018-04-14 09:59:04 +02:00
// find the button:
var button = $('.test_rule_triggers');
// replace with spinner. fa-spin fa-spinner
button.html('<i class="fa fa-spin fa-spinner"></i> ' + testRuleTriggersText);
button.attr('disabled', 'disabled');
// Serialize all trigger data
var triggerData = $('.content').find("#ffInput_strict, .rule-trigger-tbody input[type=text], .rule-trigger-tbody input[type=checkbox], .rule-trigger-tbody select").serializeArray();
console.log('Found the following trigger data: ');
console.log(triggerData);
// Find a list of existing transactions that match these triggers
2016-03-20 11:46:27 +01:00
$.get('rules/test', triggerData).done(function (data) {
var modal = $("#testTriggerModal");
// Set title and body
modal.find(".transactions-list").html(data.html);
2018-04-14 09:59:04 +02:00
button.attr('disabled', '');
// 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();
}
2018-04-14 09:59:04 +02:00
button.removeAttr('disabled');
button.html('<i class="fa fa-flask"></i> ' + testRuleTriggersText);
// Show the modal dialog
2017-02-25 05:57:01 +01:00
modal.modal();
}).fail(function () {
alert('Cannot get transactions for given triggers.');
});
2017-01-22 09:15:53 +01:00
return false;
2016-01-14 16:41:15 +01:00
}