From b5e8850aa89650fdd4c20b2d7aedd218da8e84da Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sun, 13 Jan 2013 13:32:23 +0100 Subject: [PATCH] Commented, cleaned up some calls. --- src/html/js/entries.js | 72 +++++++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/src/html/js/entries.js b/src/html/js/entries.js index af8fbcc..0c7d3d9 100644 --- a/src/html/js/entries.js +++ b/src/html/js/entries.js @@ -56,13 +56,15 @@ var ListViewModel = function() { // 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 unwrap = ko.utils.unwrapObservable; + + var entries=unwrap(self.entries); // 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; + var category = unwrap(entry.category); + var value = unwrap(entry.value) ? Number(unwrap(entry.value)) : null; if(category && value && value < 0.0) { var oldValue = 0.0; @@ -86,20 +88,22 @@ var ListViewModel = function() { // Return the data for the sold chart. self.entriesChart = ko.computed(function() { + var unwrap = ko.utils.unwrapObservable; + // We assume that entries are sorted by value date descending. - var entries = ko.utils.unwrapObservable(self.entries).slice().reverse(); + var entries = unwrap(self.entries).slice().reverse(); // 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(); - var value = entry.value ? Number(entry.value()) : null; + var date = unwrap(entry.value_date); + var value = unwrap(entry.value) ? Number(unwrap(entry.value())) : null; if(date && value) { var values = {}; - var sold = Number(entry.sold()); + var sold = Number(unwrap(entry.sold)); var open = Number((sold - value).toFixed(2)); values['open'] = open; @@ -208,28 +212,36 @@ var ListViewModel = function() { }); }; + // Function to select template in function of selected item. self.templateToUse = function (item) { return self.selectedItem() === item ? 'editTmpl' : 'itemsTmpl'; }; + // Function to edit an item self.edit = function(item) { + // Cancel previous editing. if(self.savedItem) { self.cancel(); } + // Save current item self.savedItem=ko.toJS(item); self.selectedItem(item); + // Initialize date picker for value date column. $("#value_date").datepicker().on('changeDate', function(ev){ self.selectedItem().value_date(ev.date.format(ev.currentTarget.dataset.dateFormat)); }); + // Initialize date picker for operation date column. $("#operation_date").datepicker().on('changeDate', function(ev){ self.selectedItem().operation_date(ev.date.format(ev.currentTarget.dataset.dateFormat)); }); }; + // Function to cancel current editing. self.cancel = function() { + // Reset selected item fields to saved item ones. if(self.selectedItem() && self.savedItem) { self.selectedItem().id(self.savedItem.id); // id should not change, but just in case... self.selectedItem().operation_date(self.savedItem.operation_date); @@ -239,57 +251,80 @@ var ListViewModel = function() { self.selectedItem().account_id(self.savedItem.account_id); // account_id should not change, but just in case... } - // This item was just added. + // This item was just added: remove it from the entries array. if(self.selectedItem() && !self.selectedItem().id()) { self.entries.remove(self.selectedItem()); } + // Reset saved and selected items to null. self.savedItem = null; self.selectedItem(null); }; + // Function to add a new entry. self.add = function() { - var newEntry = new entry(); - newEntry.account_id(self.account().id()); + self.entries.unshift(ko.mapping.fromJS({ + id: null, + value_date: null, + operation_date: null, + label: null, + value: null, + sold: null, + pointedsold: null, + category: null, + account_id: self.account().id() + })); - self.entries.unshift(newEntry); - - self.edit(newEntry); + self.edit(self.entries()[0]); }; + // Function to save the current selected entry. self.save = function() { + // Transform selected entry to a javascript object. var item = ko.toJS(self.selectedItem()); + // Ajax call to save the entry. $.post("api/entry.php", {action: "save_entry", entry:item}).success(function(data) { message("success", "Save", data.message); self.selectedItem(null); + self.savedItem = null; + + // Reload accounts to update solds. self.loadAccounts(); - }).error(function() { - message("error", "Error.", "Unexpected error."); }); }; + // Function to remove an entry. self.remove = function (item) { + // Cancel current editing. + self.cancel(); + if (item.id()) { - self.itemToRemove(item); + // This entry is saved in database, we show a modal dialog to confirm the removal. + self.removedItem = item; $('#remove-confirm').modal(); } else { + // This entry was not saved in database yet, we just remove it from the list. self.entries.remove(item); } }; + // Function to confirm the removal of an entry. self.confirmRemove = function() { - var item = self.itemToRemove(); + var item = self.removedItem; $.post("api/entry.php", {action: "remove_entry", entry:item}).success(function (result) { + // Reload accounts to update solds. self.loadAccounts(); }).complete(function (result) { - self.itemToRemove(null); + // Reset removed item to null and hide the modal dialog. + self.removedItem = null; $('#remove-confirm').modal('hide'); }); }; + // Callback function to select a new month. self.selectMonth = function(month) { if(month) { self.month(month); @@ -297,6 +332,7 @@ var ListViewModel = function() { } }; + // Callback function to select a new account. self.selectAccount = function(account) { if(account) { self.account(account);