Commented, cleaned up some calls.

This commit is contained in:
Alexis Lahouze 2013-01-13 13:32:23 +01:00
parent 1a6fa5de0d
commit b5e8850aa8

View File

@ -56,13 +56,15 @@ var ListViewModel = function() {
// Returns the data for the categories by summing values with same category // Returns the data for the categories by summing values with same category
self.expenseCategoriesChart = ko.computed(function() { 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. // First pass: get sum values for each category.
var chartValuesTmp = {}; var chartValuesTmp = {};
$.each(entries, function(index, entry) { $.each(entries, function(index, entry) {
var category = entry.category(); var category = unwrap(entry.category);
var value = entry.value() ? Number(entry.value()) : null; var value = unwrap(entry.value) ? Number(unwrap(entry.value)) : null;
if(category && value && value < 0.0) { if(category && value && value < 0.0) {
var oldValue = 0.0; var oldValue = 0.0;
@ -86,20 +88,22 @@ var ListViewModel = function() {
// Return the data for the sold chart. // Return the data for the sold chart.
self.entriesChart = ko.computed(function() { self.entriesChart = ko.computed(function() {
var unwrap = ko.utils.unwrapObservable;
// We assume that entries are sorted by value date descending. // 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. // First pass: get open, high, low and close values for each day.
var chartValuesTmp = {}; var chartValuesTmp = {};
$.each(entries, function(index, entry) { $.each(entries, function(index, entry) {
//var date = entry.value_date() ? entry.value_date().toString() : null; //var date = entry.value_date() ? entry.value_date().toString() : null;
var date = entry.value_date(); var date = unwrap(entry.value_date);
var value = entry.value ? Number(entry.value()) : null; var value = unwrap(entry.value) ? Number(unwrap(entry.value())) : null;
if(date && value) { if(date && value) {
var values = {}; var values = {};
var sold = Number(entry.sold()); var sold = Number(unwrap(entry.sold));
var open = Number((sold - value).toFixed(2)); var open = Number((sold - value).toFixed(2));
values['open'] = open; values['open'] = open;
@ -208,28 +212,36 @@ var ListViewModel = function() {
}); });
}; };
// Function to select template in function of selected item.
self.templateToUse = function (item) { self.templateToUse = function (item) {
return self.selectedItem() === item ? 'editTmpl' : 'itemsTmpl'; return self.selectedItem() === item ? 'editTmpl' : 'itemsTmpl';
}; };
// Function to edit an item
self.edit = function(item) { self.edit = function(item) {
// Cancel previous editing.
if(self.savedItem) { if(self.savedItem) {
self.cancel(); self.cancel();
} }
// Save current item
self.savedItem=ko.toJS(item); self.savedItem=ko.toJS(item);
self.selectedItem(item); self.selectedItem(item);
// Initialize date picker for value date column.
$("#value_date").datepicker().on('changeDate', function(ev){ $("#value_date").datepicker().on('changeDate', function(ev){
self.selectedItem().value_date(ev.date.format(ev.currentTarget.dataset.dateFormat)); 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){ $("#operation_date").datepicker().on('changeDate', function(ev){
self.selectedItem().operation_date(ev.date.format(ev.currentTarget.dataset.dateFormat)); self.selectedItem().operation_date(ev.date.format(ev.currentTarget.dataset.dateFormat));
}); });
}; };
// Function to cancel current editing.
self.cancel = function() { self.cancel = function() {
// Reset selected item fields to saved item ones.
if(self.selectedItem() && self.savedItem) { if(self.selectedItem() && self.savedItem) {
self.selectedItem().id(self.savedItem.id); // id should not change, but just in case... self.selectedItem().id(self.savedItem.id); // id should not change, but just in case...
self.selectedItem().operation_date(self.savedItem.operation_date); 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... 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()) { if(self.selectedItem() && !self.selectedItem().id()) {
self.entries.remove(self.selectedItem()); self.entries.remove(self.selectedItem());
} }
// Reset saved and selected items to null.
self.savedItem = null; self.savedItem = null;
self.selectedItem(null); self.selectedItem(null);
}; };
// Function to add a new entry.
self.add = function() { self.add = function() {
var newEntry = new entry(); self.entries.unshift(ko.mapping.fromJS({
newEntry.account_id(self.account().id()); 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(self.entries()[0]);
self.edit(newEntry);
}; };
// Function to save the current selected entry.
self.save = function() { self.save = function() {
// Transform selected entry to a javascript object.
var item = ko.toJS(self.selectedItem()); var item = ko.toJS(self.selectedItem());
// Ajax call to save the entry.
$.post("api/entry.php", {action: "save_entry", entry:item}).success(function(data) { $.post("api/entry.php", {action: "save_entry", entry:item}).success(function(data) {
message("success", "Save", data.message); message("success", "Save", data.message);
self.selectedItem(null); self.selectedItem(null);
self.savedItem = null;
// Reload accounts to update solds.
self.loadAccounts(); self.loadAccounts();
}).error(function() {
message("error", "Error.", "Unexpected error.");
}); });
}; };
// Function to remove an entry.
self.remove = function (item) { self.remove = function (item) {
// Cancel current editing.
self.cancel();
if (item.id()) { 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(); $('#remove-confirm').modal();
} else { } else {
// This entry was not saved in database yet, we just remove it from the list.
self.entries.remove(item); self.entries.remove(item);
} }
}; };
// Function to confirm the removal of an entry.
self.confirmRemove = function() { self.confirmRemove = function() {
var item = self.itemToRemove(); var item = self.removedItem;
$.post("api/entry.php", {action: "remove_entry", entry:item}).success(function (result) { $.post("api/entry.php", {action: "remove_entry", entry:item}).success(function (result) {
// Reload accounts to update solds.
self.loadAccounts(); self.loadAccounts();
}).complete(function (result) { }).complete(function (result) {
self.itemToRemove(null); // Reset removed item to null and hide the modal dialog.
self.removedItem = null;
$('#remove-confirm').modal('hide'); $('#remove-confirm').modal('hide');
}); });
}; };
// Callback function to select a new month.
self.selectMonth = function(month) { self.selectMonth = function(month) {
if(month) { if(month) {
self.month(month); self.month(month);
@ -297,6 +332,7 @@ var ListViewModel = function() {
} }
}; };
// Callback function to select a new account.
self.selectAccount = function(account) { self.selectAccount = function(account) {
if(account) { if(account) {
self.account(account); self.account(account);