-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
130 lines (108 loc) · 3.42 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var app = angular.module('app',['ngRoute','ngMaterial','ngAria','ngAnimate','ngSanitize','RecursionHelper',]);
app.run(['$rootScope', function($rootScope) {
console.log('App is loading.');
}]);
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
app.controller('AppController', ['$scope','$q','$web3','$location','Profile','PriceFeed','NameService','$mdSidenav',
function($scope, $q, $web3, $location, Profile, PriceFeed, Names, $mdSidenav) {
console.log('Loading AppController');
var ready = $q.defer();
$scope.web3 = $web3;
$scope.whois = Names.get;
$scope.app = {
ready: ready.promise,
web3: {
network: $web3.networks.kovan,
connected: null,
currentBlock: null,
},
events: {
},
price: {
usd: {
eth: null
},
eth: {
usd: null
}
},
user: {
isLoggedIn: null,
address: null,
balance: null
},
path: parsePath(),
now: now(),
search: null,
error: null
};
$web3.getNetworkId().then(function(currentNetworkId){
$scope.app.web3.connected = true;
if($scope.app.web3.network != currentNetworkId){
var err = 'Connected to the wrong network!';
$scope.app.error = err;
ready.reject(err);
console.error(err);
console.log($scope.app);
return ready.promise;
} else {
return $q.all([
$web3.getBlock('latest'),
$web3.getCurrentAccount(),
PriceFeed.get('usd','eth'),
PriceFeed.get('eth','usd'),
]);
}
}).then(function(promises){
var currentBlock = promises[0];
var currentAccount = promises[1];
var usdPerEth = promises[2];
var ethPerUsd = promises[3];
$scope.app.web3.currentBlock = currentBlock;
Names.register(currentAccount, 'Me');
$scope.app.price['usd']['eth'] = usdPerEth;
$scope.app.price['eth']['usd'] = ethPerUsd;
return Profile.get(currentAccount);
}).then(function(profile){
$scope.app.user = profile;
ready.resolve();
}).catch(function(err){
$scope.app.web3Connected = false;
console.error(err);
console.log($scope.app);
$scope.app.error = err;
ready.reject(err);
});
function now(){
return parseInt((Date.now() / 1000).toFixed(0));
}
function parsePath(){
var path = $location.path();
return path.split('/').slice(1,path.length);
}
$scope.$on('$routeChangeStart', function($event, next, current) {
$scope.app.path = parsePath();
});
web3.eth.filter('latest', function(blockHash){
$web3.getBlock('latest').then(function(currentBlock){
$scope.app.web3.currentBlock = currentBlock;
})
})
$scope.toggleSidenav = function(id){
$mdSidenav(id).toggle()
.then(function () {
//console.log('Toggled: ' + id);
});
}
$scope.goto = function(path){
$location.path(path);
$scope.app.search = null;
}
$scope.search = function(search){
if(search)
$scope.goto('c/'+search.toLowerCase());
$scope.app.search = null;
}
}]);