diff --git a/public/js/ff/accounts/edit-reconciliation.js b/public/js/ff/accounts/edit-reconciliation.js
index c7148cae4a..af72947062 100644
--- a/public/js/ff/accounts/edit-reconciliation.js
+++ b/public/js/ff/accounts/edit-reconciliation.js
@@ -18,8 +18,6 @@
* along with Firefly III. If not, see .
*/
-/** global: what, Modernizr, selectsForeignCurrency, convertForeignToNative, validateCurrencyForTransfer, convertSourceToDestination, journalData, journal, accountInfo, exchangeRateInstructions, currencyInfo */
-
$(document).ready(function () {
"use strict";
setAutocompletes();
@@ -30,47 +28,7 @@ $(document).ready(function () {
* Set the auto-complete JSON things.
*/
function setAutocompletes() {
-
-
-
- // do categories auto complete:
- var categories = new Bloodhound({
- datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
- queryTokenizer: Bloodhound.tokenizers.whitespace,
- prefetch: {
- url: 'json/categories',
- filter: function (list) {
- return $.map(list, function (name) {
- return {name: name};
- });
- }
- }
- });
- categories.initialize();
- $('input[name="category"]').typeahead({}, {source: categories, displayKey: 'name', autoSelect: false});
-
-
- // do tags auto complete:
- var tagTags = new Bloodhound({
- datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
- queryTokenizer: Bloodhound.tokenizers.whitespace,
- prefetch: {
- url: 'json/tags',
- filter: function (list) {
- return $.map(list, function (tagTag) {
- return {name: tagTag};
- });
- }
- }
- });
- tagTags.initialize();
- $('input[name="tags"]').tagsinput({
- typeaheadjs: {
- name: 'tags',
- displayKey: 'name',
- valueKey: 'name',
- source: tagTags.ttAdapter()
- }
- });
+ initCategoryAC();
+ initTagsAC();
}
diff --git a/public/js/ff/common/autocomplete.js b/public/js/ff/common/autocomplete.js
new file mode 100644
index 0000000000..03d6dd890b
--- /dev/null
+++ b/public/js/ff/common/autocomplete.js
@@ -0,0 +1,127 @@
+/*
+ * autocomplete.js
+ * Copyright (c) 2018 thegrumpydictator@gmail.com
+ *
+ * This file is part of Firefly III.
+ *
+ * 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
+ * along with Firefly III. If not, see .
+ */
+
+/**
+ * Do tags auto complete.
+ */
+function initTagsAC() {
+ var tagTags = new Bloodhound({
+ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
+ queryTokenizer: Bloodhound.tokenizers.whitespace,
+ prefetch: {
+ url: 'json/tags',
+ filter: function (list) {
+ return $.map(list, function (tagTag) {
+ return {name: tagTag};
+ });
+ }
+ }
+ });
+ tagTags.initialize();
+ $('input[name="tags"]').tagsinput({
+ typeaheadjs: {
+ name: 'tags',
+ displayKey: 'name',
+ valueKey: 'name',
+ source: tagTags.ttAdapter()
+ }
+ });
+}
+
+/**
+ * Do destination name (expense accounts) auto complete.
+ */
+function initExpenseAC() {
+ initExpenseACField('destination_name');
+}
+
+/**
+ * Do destination name (expense accounts) auto complete.
+ */
+function initExpenseACField(fieldName) {
+ if ($('input[name="' + fieldName + '"]').length > 0) {
+ console.log('Init expense AC for field "'+fieldName+'"');
+ var destNames = new Bloodhound({
+ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
+ queryTokenizer: Bloodhound.tokenizers.whitespace,
+ prefetch: {
+ url: 'json/expense-accounts',
+ filter: function (list) {
+ return $.map(list, function (name) {
+ return {name: name};
+ });
+ }
+ }
+ });
+ destNames.initialize();
+ $('input[name="' + fieldName + '"]').typeahead({}, {source: destNames, displayKey: 'name', autoSelect: false});
+ }
+}
+
+/**
+ * Do source name (revenue accounts) auto complete.
+ */
+function initRevenueAC() {
+ initRevenueACField('source_name');
+}
+
+/**
+ * Do source name (revenue accounts) auto complete.
+ */
+function initRevenueACField(fieldName) {
+
+ if ($('input[name="' + fieldName + '"]').length > 0) {
+ console.log('Init revenue AC for field "'+fieldName+'"');
+ var sourceNames = new Bloodhound({
+ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
+ queryTokenizer: Bloodhound.tokenizers.whitespace,
+ prefetch: {
+ url: 'json/revenue-accounts',
+ filter: function (list) {
+ return $.map(list, function (name) {
+ return {name: name};
+ });
+ }
+ }
+ });
+ sourceNames.initialize();
+ $('input[name="' + fieldName + '"]').typeahead({}, {source: sourceNames, displayKey: 'name', autoSelect: false});
+ }
+}
+
+/**
+ * Do categories auto complete.
+ */
+function initCategoryAC() {
+ var categories = new Bloodhound({
+ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
+ queryTokenizer: Bloodhound.tokenizers.whitespace,
+ prefetch: {
+ url: 'json/categories',
+ filter: function (list) {
+ return $.map(list, function (name) {
+ return {name: name};
+ });
+ }
+ }
+ });
+ categories.initialize();
+ $('input[name="category"]').typeahead({}, {source: categories, displayKey: 'name', autoSelect: false});
+}
\ No newline at end of file
diff --git a/public/js/ff/recurring/create.js b/public/js/ff/recurring/create.js
index 4d47190f70..8665021ce7 100644
--- a/public/js/ff/recurring/create.js
+++ b/public/js/ff/recurring/create.js
@@ -137,39 +137,10 @@ function parseRepetitionSuggestions(data) {
}
function initializeAutoComplete() {
- // auto complete things:
- $.getJSON('json/tags').done(function (data) {
- var opt = {
- typeahead: {
- source: data,
- afterSelect: function () {
- this.$element.val("");
- },
- autoSelect: false,
- },
- autoSelect: false,
- };
-
- $('input[name="tags"]').tagsinput(
- opt
- );
- });
-
- if ($('input[name="destination_name"]').length > 0) {
- $.getJSON('json/expense-accounts').done(function (data) {
- $('input[name="destination_name"]').typeahead({source: data, autoSelect: false});
- });
- }
-
- if ($('input[name="source_name"]').length > 0) {
- $.getJSON('json/revenue-accounts').done(function (data) {
- $('input[name="source_name"]').typeahead({source: data, autoSelect: false});
- });
- }
-
- $.getJSON('json/categories').done(function (data) {
- $('input[name="category"]').typeahead({source: data, autoSelect: false});
- });
+ initTagsAC();
+ initExpenseAC();
+ initRevenueAC();
+ initCategoryAC();
}
/**
diff --git a/public/js/ff/recurring/edit.js b/public/js/ff/recurring/edit.js
index 2d01c524f3..26a85e59da 100644
--- a/public/js/ff/recurring/edit.js
+++ b/public/js/ff/recurring/edit.js
@@ -109,11 +109,11 @@ function respondToFirstDateChange() {
// preselected value:
var preSelected = currentRepType;
- if(preSelected === '') {
+ if (preSelected === '') {
preSelected = select.val();
}
- $.getJSON(suggestUri, {date: date,pre_select: preSelected,past:true}).fail(function () {
+ $.getJSON(suggestUri, {date: date, pre_select: preSelected, past: true}).fail(function () {
console.error('Could not load repetition suggestions');
alert('Could not load repetition suggestions');
}).done(parseRepetitionSuggestions);
@@ -128,8 +128,8 @@ function parseRepetitionSuggestions(data) {
if (data.hasOwnProperty(k)) {
console.log('label: ' + data[k].label + ', selected: ' + data[k].selected);
opt = $('