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

308 lines
8.4 KiB
JavaScript
Raw Normal View History

2016-01-14 16:41:15 +01:00
/*
* create-edit.js
2016-04-01 16:46:11 +02:00
* Copyright (C) 2016 thegrumpydictator@gmail.com
2016-01-14 16:41:15 +01:00
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
2016-01-14 16:41:15 +01:00
*/
2017-01-22 09:15:53 +01:00
/** global: triggerCount, actionCount */
$(function () {
"use strict";
if (triggerCount === 0) {
addNewTrigger();
}
if (actionCount === 0) {
addNewAction();
}
if (triggerCount > 0) {
onAddNewTrigger();
}
if (actionCount > 0) {
onAddNewAction();
}
$('.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++;
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++;
2016-01-14 16:41:15 +01:00
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);
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:
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);
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:
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";
// update all "select action type" dropdown buttons so they will respond correctly
$('select[name^="rule-action["]').unbind('change').change(function (e) {
var target = $(e.target);
updateActionInput(target)
});
$.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";
2017-01-22 09:15:53 +01:00
// update all "select trigger type" dropdown buttons so they will respond correctly
$('select[name^="rule-trigger["]').unbind('change').change(function (e) {
var target = $(e.target);
updateTriggerInput(target)
});
$.each($('.rule-trigger-holder'), function (i, v) {
var holder = $(v);
var select = holder.find('select');
2017-01-22 09:15:53 +01:00
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) {
// the actual row this select list is in:
var parent = selectList.parent().parent();
// the text input we're looking for:
var input = parent.find('input[name^="rule-action-value["]');
input.removeAttr('disabled');
switch (selectList.val()) {
case 'set_category':
createAutoComplete(input, 'json/categories');
break;
case 'clear_category':
case 'clear_budget':
case 'remove_all_tags':
input.attr('disabled', 'disabled');
break;
case 'set_budget':
createAutoComplete(input, 'json/budgets');
break;
case 'add_tag':
case 'remove_tag':
createAutoComplete(input, 'json/tags');
break;
case 'set_description':
createAutoComplete(input, 'json/transaction-journals/all');
break;
case 'set_source_account':
createAutoComplete(input, 'json/all-accounts');
break;
case 'set_destination_account':
createAutoComplete(input, 'json/all-accounts');
break;
2017-01-30 10:33:18 +01:00
default:
input.typeahead('destroy');
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) {
// the actual row this select list is in:
var parent = selectList.parent().parent();
// the text input we're looking for:
var input = parent.find('input[name^="rule-trigger-value["]');
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':
createAutoComplete(input, 'json/all-accounts');
break;
2017-01-24 15:38:41 +01:00
case 'tag_is':
// also make tag thing?
createAutoComplete(input, 'json/tags');
break;
case 'budget_is':
createAutoComplete(input, 'json/budgets');
break;
case 'category_is':
createAutoComplete(input, 'json/categories');
break;
case 'transaction_type':
createAutoComplete(input, 'json/transaction-types');
break;
case 'description_starts':
case 'description_ends':
case 'description_contains':
case 'description_is':
createAutoComplete(input, 'json/transaction-journals/all');
break;
2017-01-30 10:33:18 +01:00
default:
input.typeahead('destroy');
break;
}
}
/**
* Create actual autocomplete
* @param input
* @param URI
*/
function createAutoComplete(input, URI) {
input.typeahead('destroy');
$.getJSON(URI).done(function (data) {
input.typeahead({source: data});
});
}
function testRuleTriggers() {
"use strict";
// Serialize all trigger data
var triggerData = $(".rule-trigger-tbody").find("input[type=text], input[type=checkbox], select").serializeArray();
// 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);
// 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
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
}