-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllers.js
94 lines (78 loc) · 2.46 KB
/
controllers.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(function() {
var app = angular.module("compareTool", ["filters", "services"]);
// Compare Controller
app.controller("CompareCtrl", function($scope, $filter, companyService) {
// The max number of companies the user can compare
var maxSymbols = 5;
// Define what will be displayed
$scope.dataPoints = [
{ label: "Last Price", field: "LastPrice", format: "currency" },
{ label: "High", field: "High", format: "currency" },
{ label: "Low", field: "Low", format: "currency" },
{ label: "Open", field: "Open", format: "currency" },
{ label: "Market Cap", field: "MarketCap", format: "number" },
{ label: "Volume", field: "Volume", format: "number" },
{ label: "Change", field: "Change", format: "currency" },
{ label: "Change %", field: "ChangePercent", format: "percent:2" },
{ label: "Change YTD", field: "ChangeYTD", format: "currency" },
{ label: "Change % YTD", field: "ChangePercentYTD", format: "percent:2" }
];
// List of symbols we're comparing
// Sample: http://dev.markitondemand.com/Api/Quote/jsonp?symbol=aapl
$scope.issues = [];
// Get the number of empty compare slots
$scope.numRemaining = function() {
return maxSymbols - $scope.issues.length;
};
$scope.addCompany = function(ticker) {
var self = this;
// Use THIS instead of $scope, because we're actually in the scope
// of the clicked dom element
this.isLoading = true;
companyService.getBySymbol(ticker, {
success: function(data) {
$scope.issues.push(data);
self.searchSymbol = "";
},
error: function(msg) {
msg = msg || "Sorry, that didn't work for some reason";
alert(msg);
},
complete: function() {
self.isLoading = false;
}
});
};
// Pull the company out of comparison
$scope.removeCompany = function(ticker) {
// Remove the symbol
for (var i = 0; i < $scope.issues.length; i++) {
if ($scope.issues[i].Symbol === ticker) {
$scope.issues.splice(i, 1);
break;
}
}
};
// Multi-purpose format func
$scope.format = function(value, type) {
var out = value;
if (type) {
var params = type.split(":");
type = params[0];
switch (type) {
case "percent":
out = value.toFixed(params[1] || 0) + "%";
break;
case "number":
out = value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
break;
default:
// Use the default angular filter
out = $filter(type)(value);
break;
}
}
return out;
};
});
})();