forked from AlberErre/iron-identicon-meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.js
140 lines (117 loc) · 3.58 KB
/
output.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
131
132
133
134
135
136
137
138
139
140
/**
The contract constants template
@class [template] dapp_output
@constructor
*/
/**
Formats the values for display
@method formatOutput
*/
var formatOutput = function(val) {
if(_.isArray(val))
return _.map(val, formatOutput);
else {
var value = 0;
console.log('val?', val, typeof val, _.isBoolean(val));
// stringify boolean
if(_.isBoolean(val))
value = val ? 'YES' : 'NO';
// convert bignumber objects
value = (_.isObject(val) && val.toString)
? val.toString(10)
: val;
return value;
}
};
var createTemplateDataFromInput = function (input){
input = _.clone(input);
input.typeShort = input.type.match(/[a-z]+/i);
input.typeShort = input.typeShort[0];
input.bits = input.type.replace(input.typeShort, '');
input.displayName = input.name
.replace(/([A-Z])/g, ' $1')
.replace(/([\-\_])/g, ' <span class="punctuation">$1</span> ');
if(input.type.indexOf('[') === -1 &&
(input.typeShort === 'string' ||
input.typeShort === 'uint' ||
input.typeShort == 'int' ||
input.typeShort == 'address' ||
input.typeShort == 'bool' ||
input.typeShort == 'bytes')) {
input.template = 'elements_input_'+ input.typeShort;
} else {
input.template = 'elements_input_json';
}
return input;
};
Template['dapp_output'].helpers({
/**
Formats the value if its a big number or array
@method (value)
*/
'value': function() {
var value = this.output.value;
var type = this.output.type;
if (type == "bool") {
if (value=="true")
return "YES"
else
return "NO"
} else if (type.indexOf("int")>0) {
return value.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")
} else {
return value;
}
},
/**
format the right bits
@method (value)
*/
'bits': function() {
var bits = this.output.type.match(/([0-9]+)/i);
return bits ? bits[0] : null;
},
/**
format the right type
@method (value)
*/
'type': function() {
if(typeof TAPi18n === 'undefined') {
return this.output.type;
} else {
var typeShort = this.output.type.match(/([a-z]+)/i);
return TAPi18n.__('elements.type.'+ typeShort[0] );
}
},
/**
Figures out extra data
@method (extra)
*/
'extra': function() {
// var data = formatOutput(this.output.value); // 1000000000
var value = this.output.value;
var type = this.output.type;
if (type == "bool") {
if (value == "true") {
return new Spacebars.SafeString('<span class="icon icon-check"></span>');
} else {
return new Spacebars.SafeString('<span class="icon icon-ban"></span>')
}
} else if(type.indexOf("int")>0) {
data = parseInt(value, 10);
if (data > 1400000000 && data < 1800000000 && Math.floor(data/1000) != data/1000) {
return '(' + moment(data*1000).fromNow() + ')';
}
} else if (type.indexOf("bytes") > 0 ) {
var returnData = "";
for(i = 0; i < value.length; i++) {
// console.log("returnData", returnData);
returnData += value.charAt(i);
}
return returnData;
} else if (value == '') {
return 'Value is empty';
}
// return;
}
});