258 lines
7.4 KiB
JavaScript
258 lines
7.4 KiB
JavaScript
(function (OC, window, $, undefined) {
|
|
'use strict';
|
|
$(document).ready(function () {
|
|
|
|
var translations = {
|
|
//newNote: $('#new-note-string').text()
|
|
months: ["Jan", "Feb", "Mar", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
|
|
};
|
|
|
|
// this transactions holds all our transactions
|
|
var Transactions = function (baseUrl) {
|
|
this._baseUrl = baseUrl;
|
|
this._transactions = [];
|
|
};
|
|
|
|
Transactions.prototype = {
|
|
delete: function (id) {
|
|
var deferred = $.Deferred();
|
|
$.ajax({
|
|
url: this._baseUrl + '/' + id,
|
|
method: 'DELETE'
|
|
}).done(function () {
|
|
deferred.resolve();
|
|
}).fail(function () {
|
|
deferred.reject();
|
|
});
|
|
return deferred.promise();
|
|
},
|
|
create: function (transactions) {
|
|
var deferred = $.Deferred();
|
|
var self = this;
|
|
$.ajax({
|
|
url: this._baseUrl,
|
|
method: 'POST',
|
|
contentType: 'application/json',
|
|
data: JSON.stringify(transactions)
|
|
}).done(function (transactions) {
|
|
self._transactions.push(transactions);
|
|
deferred.resolve();
|
|
}).fail(function () {
|
|
deferred.reject();
|
|
});
|
|
return deferred.promise();
|
|
},
|
|
update: function (id, name) {
|
|
var transactions = {
|
|
id: id,
|
|
name: name
|
|
}
|
|
|
|
return $.ajax({
|
|
url: this._baseUrl + '/' + transactions.id,
|
|
method: 'PUT',
|
|
contentType: 'application/json',
|
|
data: JSON.stringify(transactions)
|
|
});
|
|
},
|
|
getTransactions(accountid) {
|
|
if (!Array.isArray(this._transactions))
|
|
return [];
|
|
return this._transactions.filter(function(t) {
|
|
return t.accountFrom === accountid || t.accountTo === accountid;
|
|
});
|
|
},
|
|
loadAll: function () {
|
|
var deferred = $.Deferred();
|
|
var self = this;
|
|
$.get(this._baseUrl).done(function (transactions) {
|
|
self._transactions = transactions;
|
|
deferred.resolve();
|
|
}).fail(function () {
|
|
deferred.reject();
|
|
});
|
|
return deferred.promise();
|
|
},
|
|
};
|
|
|
|
// this accounts object holds all our accounts
|
|
var Accounts = function (baseUrl) {
|
|
this._baseUrl = baseUrl;
|
|
this._accounts = [];
|
|
var d = new Date();
|
|
var sy = d.getYear();
|
|
var sm = d.getMonth();
|
|
this._months = [];
|
|
for (var i=0; i<12; i++)
|
|
this._months.push({year: sy+1900+Math.floor((sm+i)/12), month: (sm+i)%12});
|
|
};
|
|
|
|
Accounts.prototype = {
|
|
delete: function (id) {
|
|
var deferred = $.Deferred();
|
|
$.ajax({
|
|
url: this._baseUrl + '/' + id,
|
|
method: 'DELETE'
|
|
}).done(function () {
|
|
deferred.resolve();
|
|
}).fail(function () {
|
|
deferred.reject();
|
|
});
|
|
return deferred.promise();
|
|
},
|
|
create: function (account) {
|
|
var deferred = $.Deferred();
|
|
var self = this;
|
|
$.ajax({
|
|
url: this._baseUrl,
|
|
method: 'POST',
|
|
contentType: 'application/json',
|
|
data: JSON.stringify(account)
|
|
}).done(function (account) {
|
|
self._accounts.push(account);
|
|
deferred.resolve();
|
|
}).fail(function () {
|
|
deferred.reject();
|
|
});
|
|
return deferred.promise();
|
|
},
|
|
update: function (id, name) {
|
|
var account = {
|
|
id: id,
|
|
name: name
|
|
}
|
|
|
|
return $.ajax({
|
|
url: this._baseUrl + '/' + account.id,
|
|
method: 'PUT',
|
|
contentType: 'application/json',
|
|
data: JSON.stringify(account)
|
|
});
|
|
},
|
|
getAccounts: function () {
|
|
return this._accounts;
|
|
},
|
|
getMonths: function () {
|
|
return this._months;
|
|
},
|
|
loadAll: function () {
|
|
var deferred = $.Deferred();
|
|
var self = this;
|
|
$.get(this._baseUrl).done(function (accounts) {
|
|
self._accounts = accounts;
|
|
deferred.resolve();
|
|
}).fail(function () {
|
|
deferred.reject();
|
|
});
|
|
return deferred.promise();
|
|
},
|
|
};
|
|
|
|
// this will be the view that is used to update the html
|
|
var View = function (accounts, transactions) {
|
|
this._accounts = accounts;
|
|
this._transactions = transactions;
|
|
};
|
|
|
|
View.prototype = {
|
|
createTransactionRow: function (css, account, transaction, month_tds) {
|
|
var tds = month_tds.replace(/###ACCOUNT###/g, account).replace(/###TRANS###/g, transaction)
|
|
var row = document.createElement("tr");
|
|
row.innerHTML = "<th class='"+css+"'>"+transaction+"</th>"+tds;
|
|
return row;
|
|
},
|
|
renderOverview: function () {
|
|
var html = "";
|
|
$('#accounts').find("tr").remove();
|
|
var cols = this._accounts.getMonths().length+1;
|
|
|
|
var row = document.createElement("tr");
|
|
row.innerHTML = "<th>Monat</th>";
|
|
var month_tds = "";
|
|
var colodd = true;
|
|
for (var mnr in this._accounts.getMonths()) {
|
|
var month = this._accounts.getMonths()[mnr].month;
|
|
var year = this._accounts.getMonths()[mnr].year
|
|
row.innerHTML+= "<th class='month"+month+" "+(colodd ? "col_odd" : "col_even")+"'>"+translations.months[month]+" "+year+"</th>";
|
|
month_tds+= "<td id='###ACCOUNT###.###TRANS###."+year+"."+month+"' class='month"+month+" "+(colodd ? "col_odd" : "col_even")+"'></td>";
|
|
|
|
colodd = !colodd;
|
|
}
|
|
$("#accounts")[0].appendChild(row);
|
|
|
|
for (var a in this._accounts.getAccounts()) {
|
|
row = document.createElement("tr");
|
|
row.innerHTML = "<th></th><th class='account_title' colspan='"+(cols-1)+"'>"+this._accounts.getAccounts()[a].name+"</th>";
|
|
$("#accounts")[0].appendChild(row);
|
|
|
|
$("#accounts")[0].appendChild(this.createTransactionRow("account_start", this._accounts.getAccounts()[a].name, "Beginn", month_tds));
|
|
|
|
var transactions = this._transactions.getTransactions(this._accounts.getAccounts()[a].id);
|
|
for (var t in transactions) {
|
|
|
|
}
|
|
$("#accounts")[0].appendChild(this.createTransactionRow("account_end", this._accounts.getAccounts()[a].name, "Ende", month_tds));
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// create a new note
|
|
/*var self = this;
|
|
$('#new-note').click(function () {
|
|
var note = {
|
|
title: translations.newNote,
|
|
content: ''
|
|
};
|
|
|
|
self._notes.create(note).done(function() {
|
|
self.render();
|
|
$('#editor textarea').focus();
|
|
}).fail(function () {
|
|
alert('Could not create note');
|
|
});
|
|
});*/
|
|
|
|
// show app menu
|
|
/*$('#app-navigation .app-navigation-entry-utils-menu-button').click(function () {
|
|
var entry = $(this).closest('.note');
|
|
entry.find('.app-navigation-entry-menu').toggleClass('open');
|
|
});*/
|
|
|
|
// delete a note
|
|
/*$('#app-navigation .note .delete').click(function () {
|
|
var entry = $(this).closest('.note');
|
|
entry.find('.app-navigation-entry-menu').removeClass('open');
|
|
|
|
self._notes.removeActive().done(function () {
|
|
self.render();
|
|
}).fail(function () {
|
|
alert('Could not delete note, not found');
|
|
});
|
|
});*/
|
|
|
|
// load a note
|
|
/*$('#app-navigation .note > a').click(function () {
|
|
var id = parseInt($(this).parent().data('id'), 10);
|
|
self._notes.load(id);
|
|
self.render();
|
|
$('#editor textarea').focus();
|
|
});*/
|
|
},
|
|
render: function () {
|
|
this.renderOverview();
|
|
}
|
|
};
|
|
|
|
var accounts = new Accounts(OC.generateUrl('/apps/moneyplanner/accounts'));
|
|
var transactions = new Transactions(OC.generateUrl('/apps/moneyplanner/transactions'));
|
|
var view = new View(accounts, transactions);
|
|
accounts.loadAll().done(function () {
|
|
view.render();
|
|
}).fail(function () {
|
|
alert('Could not load accounts');
|
|
});
|
|
|
|
}); /// document.ready()
|
|
})(OC, window, jQuery); //IIFE Scope
|