Commented and cleaned up code.

This commit is contained in:
Alexis Lahouze 2013-01-13 10:14:43 +01:00
parent 7e8fd0a410
commit d4756f5ea0

View File

@ -11,6 +11,7 @@ function entry(data){
this.category=ko.observable(ko.utils.unwrapObservable(data.category));
}
// Util function to show a message in message placeholder.
function message(alertType, title, message) {
$(".alert").alert('close');
$("#message-placeholder").append('<div class="alert alert-' + alertType + '"><button type="button" class="close" data-dismiss="alert">&times;</button><h4>' + title + '</h4><strong>' + message + '</strong></div>');
@ -19,30 +20,33 @@ function message(alertType, title, message) {
var ListViewModel = function() {
var self = this;
// Account store and selection
self.account = ko.observable();
self.accounts = ko.observableArray([]);
self.entries = ko.observableArray([]);
// Month store and selection
self.months = ko.observableArray();
self.month = ko.observable();
// Entry store and selection
self.entries = ko.observableArray([]);
self.selectedItem = ko.observable();
// Placeholder for saved value to cancel entry edition
self.savedItem = ko.observable();
// Placeholder for entry to remove to be available in modal function "yes" click callback
self.itemToRemove = ko.observable();
self.chart = null;
// Returns the data for the categories by summing values with same category
self.expenseCategoriesChart = ko.computed(function() {
var entries=ko.utils.unwrapObservable(self.entries);
var chartValues = [];
// First pass: get sum values for each category.
var chartValuesTmp = {};
$.each(entries, function(index, entry) {
var category = entry.category();
var value = entry.value() ? Number(entry.value()) : null;
if(category && value) {
if(category && value && value < 0.0) {
var oldValue = 0.0;
if(chartValuesTmp[category]) {
@ -53,6 +57,8 @@ var ListViewModel = function() {
}
});
// Second pass: transform to an array readable by jqplot.
var chartValues = [];
$.each(chartValuesTmp, function(key, value) {
chartValues.push([key, value]);
});
@ -60,12 +66,13 @@ var ListViewModel = function() {
return chartValues;
});
// Return the data for the sold chart.
self.entriesChart = ko.computed(function() {
// We assume that entries are sorted by value date descending.
var entries = ko.utils.unwrapObservable(self.entries).slice().reverse();
var chartValues = [];
// First pass: get open, high, low and close values for each day.
var chartValuesTmp = {};
$.each(entries, function(index, entry) {
//var date = entry.value_date() ? entry.value_date().toString() : null;
var date = entry.value_date();
@ -100,24 +107,22 @@ var ListViewModel = function() {
}
});
// Second pass: transform to an array readable by jqplot OHLC renderer.
var chartValues = [];
$.each(chartValuesTmp, function(key, value) {
var ohlc = [key, value['open'], value['high'], value['low'], value['close']];
chartValues.push(ohlc);
});
chartValues.sort(function(a, b){
var aDate = Date.parse(a[0]);
var bDate = Date.parse(b[0]);
return aDate < bDate ? -1 : 1;
chartValues.push([key, value['open'], value['high'], value['low'], value['close']]);
});
return chartValues;
}, self);
// Function to load entries from server for a specific account and month.
self.loadEntries = function(account, month) {
$.post("api/entry.php", {action: "get_entries", account: account.id, year: month.year, month: month.month}, function(data) {
// Clean up current entries.
self.entries.removeAll();
// Clean up selected entry.
self.selectedItem(null);
var entries = [];