From d4756f5ea07efffff9ba63d907155907b81cd0ca Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sun, 13 Jan 2013 10:14:43 +0100 Subject: [PATCH] Commented and cleaned up code. --- src/html/js/entries.js | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/html/js/entries.js b/src/html/js/entries.js index 6901f8c..02942be 100644 --- a/src/html/js/entries.js +++ b/src/html/js/entries.js @@ -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('

' + title + '

' + message + '
'); @@ -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 = [];