-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
51 lines (42 loc) · 1.32 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var app = angular.module("app", []);
app.controller("main", function($scope) {
var vm = $scope;
vm.items = [];
vm.combination = [];
vm.quality = null;
vm.$on('itemProcessed', function(event, item) { // When an item has been pasted and processed
vm.items.push(item);
calculateCombination();
vm.$apply();
});
vm.deleteItem = function(index) { // Delete a single item
vm.items.splice(index, 1);
calculateCombination();
};
vm.clearItems = function() { // Clear all items
vm.items = [];
vm.combination = [];
vm.quality = null;
};
vm.clearOptimal = function() { // Clear optimal items
var items = [];
vm.items.forEach(function(item) { // See what items to keep
if (vm.combination.indexOf(item) === -1) // Not in optimal, so keep it
items.push(item);
});
vm.items = items;
calculateCombination();
};
function calculateCombination() {
var combination = smallest( // Get the optimal combination of items
40, vm.items.slice());
if (combination.length) { // Set new combination and quality
vm.combination = combination;
vm.quality = sum(combination) + '%';
}
else { // Clear previous combination and quality
vm.combination = [];
vm.quality = null;
}
}
});