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

394 lines
14 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-08-05 15:34:20 +02:00
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()');
var selectQuery = 'select[name^="rule_actions["][name$="][name]"]';
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-08-05 15:34:20 +02:00
// $.each($('.rule-action-holder'), function (i, v) {
// var holder = $(v);
// var select = holder.find('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-08-05 15:34:20 +02:00
var selectQuery = 'select[name^="rule_triggers["][name$="][name]"]';
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-08-05 15:34:20 +02:00
// $.each($('.rule-trigger-holder'), function (i, v) {
// var holder = $(v);
// var select = holder.find('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-08-05 15:34:20 +02:00
var inputQuery = 'input[name^="rule_actions["][name$="][value]"]';
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':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
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':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs to be disabled.');
inputResult.attr('disabled', 'disabled');
2017-01-22 09:15:53 +01:00
break;
case 'set_budget':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
createAutoComplete(inputResult, 'json/budgets');
2017-01-22 09:15:53 +01:00
break;
case 'add_tag':
case 'remove_tag':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
createAutoComplete(inputResult, 'json/tags');
2017-01-22 09:15:53 +01:00
break;
case 'set_description':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
createAutoComplete(inputResult, 'json/transaction-journals/all');
2017-01-22 09:15:53 +01:00
break;
case 'set_source_account':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
createAutoComplete(inputResult, 'json/all-accounts');
2017-01-22 09:15:53 +01:00
break;
case 'set_destination_account':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
createAutoComplete(inputResult, 'json/all-accounts');
2017-01-22 09:15:53 +01:00
break;
case 'link_to_bill':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
createAutoComplete(inputResult, 'json/bills');
break;
2017-01-30 10:33:18 +01:00
default:
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', destroy auto complete, do nothing else.');
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-08-05 15:34:20 +02:00
var inputQuery = 'input[name^="rule_triggers["][name$="][value]"]';
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);
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':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
createAutoComplete(inputResult, 'json/all-accounts');
break;
2017-01-24 15:38:41 +01:00
case 'tag_is':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
createAutoComplete(inputResult, 'json/tags');
2017-01-24 15:38:41 +01:00
break;
case 'budget_is':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
createAutoComplete(inputResult, 'json/budgets');
2017-01-24 15:38:41 +01:00
break;
case 'category_is':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
createAutoComplete(inputResult, 'json/categories');
2017-01-24 15:38:41 +01:00
break;
case 'transaction_type':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
createAutoComplete(inputResult, 'json/transaction-types');
break;
case 'description_starts':
case 'description_ends':
case 'description_contains':
case 'description_is':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
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':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs to be disabled.');
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':
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', so input needs auto complete.');
createAutoComplete(inputResult, 'json/currency-names');
2018-04-08 16:27:52 +02:00
break;
2017-01-30 10:33:18 +01:00
default:
2018-08-05 15:34:20 +02:00
console.log('Select list value is ' + selectList.val() +', destroy auto complete, do nothing else.');
inputResult.typeahead('destroy');
2017-01-30 10:33:18 +01:00
break;
}
}
/**
* Create actual autocomplete
* @param input
* @param URI
*/
function createAutoComplete(input, URI) {
2018-08-05 15:34:20 +02:00
console.log('Now in createAutoComplete().')
input.typeahead('destroy');
$.getJSON(URI).done(function (data) {
2018-08-05 15:34:20 +02:00
console.log('Input now has auto complete from URI ' + URI);
2018-01-22 18:37:59 +01:00
input.typeahead({source: data, autoSelect: false});
2018-08-05 15:34:20 +02:00
}).fail(function() {
console.log('Could not grab URI ' + URI + ' so autocomplete will not work');
});
}
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 = $(".rule-trigger-tbody").find("input[type=text], input[type=checkbox], select").serializeArray();
2018-08-05 15:34:20 +02:00
console.log('Found the following trigger data: ' + 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
}