-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
98 lines (84 loc) · 3.06 KB
/
test.html
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
<!doctype html>
<html ng-app>
<head>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script>
function CnvCtrl($scope, $sce) {
document.getElementById('allInput').focus();
$scope.results = [];
$scope.allInput = '';
var dummy = {value:undefined, show:false};
var convertors = [toCelcius, minPerMilesToKmph, elapsedSong, milesPerDay];
$scope.$watch('allInput', function() {
$scope.results = _.map(convertors, function(f) {
var r = f($scope.allInput);
r.unit = $sce.trustAsHtml(r.unit);
r.show = r.show || !isNaN(r.value);
return r;
});
});
return;
function round(x, n) {
var precision = n || 2;
return Math.round(x * Math.pow(10, precision)) / Math.pow(10, precision);
}
function toCelcius(arg) {
if (arg === undefined || arg === '') {
return dummy;
}
// (°F - 32) x 5/9 = °C
return {value: round((arg - 32) * 5 / 9.0), unit:'°C'};
}
function minPerMilesToKmph(arg) {
if (arg === undefined || arg === '') {
return dummy;
}
var argv = _.map(arg.split(':'), function(x) {
if (x === '') return 0;
return parseInt(x);
});
if (argv.length > 2) {
return dummy;
}
if (argv.length === 1) {
argv[1] = 0;
}
if (_.some(_.map(argv, function(x) { return typeof n !== 'number' || x < 0 || x >= 60; })))
return dummy;
// x mpm => 60*1.60934/x kmph
return {value: round(60 * 1.60934 / (argv[0] + argv[1]/60.0)), unit:'km/h'};
}
function elapsedSong(arg) {
var data = ['4:06', '8:42', '13:20', '19:46', '24:58', '29:41', '34:28',
'39:21', '44:22', '50:36', '55:32', '59:40', '65:09', '70:18'];
var argv = arg.split('+');
var song_idx = argv[0];
var r = data[parseInt(song_idx)];
return {value: r, unit:'', show: r !== undefined};
}
function milesPerDay(achievedMiles) {
var remainingMiles = 100 - parseFloat(achievedMiles);
var currentDate = new Date();
var dayOfMonth = new Date().getUTCDate();
var daysInCurrentMonth = new Date(currentDate.getUTCFullYear(), currentDate.getUTCMonth(), 0).getDate();
var remainingDays = daysInCurrentMonth - dayOfMonth;
var r = round(remainingMiles / remainingDays, 2);
return {value: r, unit:' miles/day', show: r !== undefined && !isNaN(r)};
}
}
</script>
</head>
<body>
<div ng-controller="CnvCtrl">
<div class="container"><div class="row"><div class="col-xs-12">
<input type="text" id="allInput" ng-model="allInput" placeholder="Enter something here" class="form-control">
</div></div></div>
<div class="list-group">
<a href="#" class="list-group-item" ng-repeat="result in results track by $index"
ng-show="result.show">{{result.value}}<span ng-bind-html="result.unit"></span></a>
</div>
</div>
</body>
</html>