-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
152 lines (129 loc) · 4.06 KB
/
index.html
File metadata and controls
152 lines (129 loc) · 4.06 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="stylesheet" type="text/css" href="./css/default.css">
<title>Native Table Sort</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.min.js"></script>
<script type="text/javascript">
jQuery( document ).ready(function( $ ) {
$.support.cors = true;
App.init();
});
var App = {
dataUrl: "http://rocky-brushlands-8739.herokuapp.com/rates",
data: [],
template: "#data-template",
compiledTemplate: null,
container: "#container",
init: function () {
///<summary> Start the app </summary>
var app = this;
// convert the placeholder var to the real DOM object
app.container = $(app.container);
// load the template only once
app.template = $(app.template).html();
app.compiledTemplate = Handlebars.compile(app.template);
app.getData(function(){
app.showData(app.data);
});
app.attachEvents();
},
getData: function (callback){
///<summary> Fetch our rates data and save to app </summary>
var app = this;
app.say("getData: start");
// go fetch
$.ajax({
url: this.dataUrl,
})
.fail(function (jqXHR, textStatus, errorThrown){
app.say("getData: ajax error", textStatus, errorThrown)
})
.done(function( response ) {
app.say("getData: received data", response.slice(0,10));
if ( !response )
return false;
// response has actual data
if ( response.length > 0){
var data = [];
data.rates = response
app.data = data; // save to app
// run callback if any
if ( typeof callback == "function" )
callback();
}
});
},
showData: function (newData) {
///<summary> Populate our template and send it to the page </summary>
var app = this;
// prepare data
var data = [];
data = newData || app.data;
// populate template
app.container.empty().append(app.compiledTemplate(data));
app.say("template populated");
},
attachEvents: function () {
///<summary> Attach events </summary>
var app = this;
app.container.on("click", "th", function(evt) {
// determine sorting order
var sortBy = $(this).attr("rel"),
sortIsAsc = $(this).hasClass("desc"),
sortClass = sortIsAsc ? "asc" : "desc" ;
app.say("click: sort by "+sortBy+" "+sortClass)
var sortedData = app.sort(sortBy, sortIsAsc);
sortedData.sort = {};
sortedData.sort[sortBy] = sortClass;
app.showData(sortedData);
});
},
sort: function (sortBy, sortIsAsc) {
var app = this,
result = app.data;
// determine data type
if ( typeof result.rates[0][sortBy] == "number" ){
// sort int
result.rates.sort(function(a,b) {
return ( sortIsAsc ? parseFloat(a[sortBy]) - parseFloat(b[sortBy]) : parseFloat(b[sortBy]) - parseFloat(a[sortBy]) );
} );
} else {
// sort string
result.rates.sort(function(a,b) {
return ( sortIsAsc ? a[sortBy].localeCompare(b[sortBy]) : b[sortBy].localeCompare(a[sortBy]) );
} );
}
return result;
},
say: function (message){
///<summary> Central logging method </summary>
if ( console && console.log )
console.log( arguments.length > 1 ? arguments : message );
}
}
</script>
</head>
<body id="body" role="application">
<div id="container">
</div><!--container-->
<script id="data-template" type="text/x-handlebars-template">
<table>
<thead>
<th rel="code" {{#if sort.code}}class={{sort.code}}{{/if}}>Currency</th>
<th rel="rate" {{#if sort.rate}}class={{sort.rate}}{{/if}}>Rate</th>
</thead>
<tbody>
{{#rates}}
<tr>
<td>{{code}}</td>
<td>{{rate}}</td>
</tr>
{{/rates}}
</tbody>
</table>
</script>
</body>
</html>