Skip to content

Instantly share code, notes, and snippets.

@David-Melo
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save David-Melo/9836871 to your computer and use it in GitHub Desktop.
Save David-Melo/9836871 to your computer and use it in GitHub Desktop.
Goals JS Logic
my.vm.orders.goal = function(title,url,weight){
this.title = ko.observable(title);
this.url = ko.observable(url);
this.weight = ko.observable(weight);
};
my.vm.orders.goals = {
type: ko.observable(),
list: ko.observableArray([]),
add: function(){
my.vm.orders.goals.list.push(new my.vm.orders.goal('New Goal','URL',0));
},
calculate: function(){
var goals = my.vm.orders.goals.list().length,
total = 0,
diff = 0,
empty = [];
if(goals){
// Loop Though Goals
ko.utils.arrayForEach(my.vm.orders.goals.list(), function(item) {
var value = parseInt(item.weight());
value = Math.max(0,value);
if (isNaN(value)) value = 0;
if(value<1){
// If Empty Add to List
empty.push(item);
} else {
// Add Value to Total
total += value;
}
});
// Subtract Established Goals From Possible Total
diff = 100 - total;
// How much of the diff do we give to each Goal
if(empty.length > 0){
var eachgets = diff/empty.length;
// Loop through empty Goals
ko.utils.arrayForEach(my.vm.orders.goals.list(), function(item) {
var value = parseInt(item.weight());
value = Math.max(0,value);
if (isNaN(value)) value = 0;
value += eachgets;
if(value<1){
value = 0;
}
item.weight(value);
// Add Value to Tals
total += value;
});
}
// Ok What Is The Difference Now?
diff = 100 - total;
// If we are not on point
if(diff!==0 && empty.length<1){
// How much to change each one
var quant = diff/goals
// Loop Though Goals
ko.utils.arrayForEach(my.vm.orders.goals.list(), function(item) {
var value = parseFloat(item.weight());
if (isNaN(value)) value = 0;
value += quant;
total += quant;
diff -= quant;
// if the result is negative we need to adjust for a remainder
if(value<1){
// save remainder
var remainder = value;
// add back to diff
diff -= remainder;
//round up to minimum
value = 0;
}
item.weight(value);
});
}
} else {
return false;
}
},
reset: function(){
my.vm.orders.goals.list([]);
},
process: function(){
var goals = ko.toJS(my.vm.orders.goals.list()),
total = 0;
// lets round each item
ko.utils.arrayForEach(goals, function(item) {
var value = Math.floor(item.weight);
total += value;
item.weight = value;
});
console.log(total);
if(total>100){
var diff = total - 100;
goals[0].weight = goals[0].weight() - diff;
} else if (total<100) {
var diff = 100 - diff;
goals[0].weight = goals[0].weight() + diff;
}
return goals;
}
};
my.vm.orders.goals.totalWeight = ko.computed(function(){
var total = 0;
ko.utils.arrayForEach(my.vm.orders.goals.list(), function(item) {
var value = parseFloat(item.weight());
if (!isNaN(value)) {
total += value;
}
});
my.vm.orders.item.tracking.DWTGoalsWeight(parseInt(total.toFixed(0)));
return total.toFixed(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment