-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
131 lines (115 loc) · 3.27 KB
/
client.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
$(document).ready(function(){
var loans = [];
$.get("http://api.kivaws.org/v1/loans/newest.json", function(data){
loans = data.loans;
setupLoansTable(loans);
});
$('#save').click(function(){
console.log("Saved loans to DB");
$.ajax({
url: '/save/',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(loans)
});
});
});
function setupLoansTable(loans){
var loanInfo = [];
var loanDictionary = [];
//populate loan dictionary with loan id as the key
$.each(loans, function(i, loan){
loanDictionary[loan.id] = loan;
});
//Extract the loan information we want to display
$.each(loans, function(i, loan) {
loanInfo.push([
imageIdToImage(loan.image.id),
loan.id,
loan.name,
loan.activity,
loan.sector,
locationToString(loan.location),
loan.loan_amount,
loan.funded_amount,
loan.borrower_count,
loan.use]);
});
//Create data table and add data
var dataTable = $('#loans-table').dataTable({
"aaData": loanInfo,
"aoColumns": [
{ "sTitle": ""},
{ "sTitle": "Id"},
{ "sTitle": "Name"},
{ "sTitle": "Activity"},
{ "sTitle": "Sector"},
{ "sTitle": "Location"},
{ "sTitle": "Loan Amount"},
{ "sTitle": "Funded Amount"},
{ "sTitle": "Borrower Count"},
{ "sTitle": "Use"},
],
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bAutoWidth": false
}, "json");
//set up loan-view
var width = $(window).width() * 0.80;
var height = $(window).height() * 0.80;
$("#loan-view").dialog({
autoOpen: false,
modal: true,
height: height,
width: width
});
//To get table row event binding to work, datatables suggests using the live function
//Unfortunately jquery 1.9 deprecated 'live' for 'on' and datatables has yet to update
//their functions (planned for datatables 1.10). While I wait for them, using jquery 1.8
$('#loans-table tbody tr').live('click', function () {
var tRow = $('td', this);
var id = $(tRow[1]).text();
var loan = loanDictionary[id];
var html = compileHtml(loan, width, height);
$("#loan-view").html(html).dialog("open");
});
//Make rows look clickable
dataTable.$('tr').css('cursor', 'pointer');
}
//Using handlebars for templating
function compileHtml(loan, width, height){
var source = $("#loan-template").html();
var template = Handlebars.compile(source);
var context = loan;
context.location_text = locationToString(loan.location);
context.description_text = descriptionToString(loan.description)
context.imgwidth = Math.round(width * 0.40);
context.imgheight = Math.round(height * 0.95);
context.percentage = Math.round(loan.funded_amount * 100 / loan.loan_amount);
return template(context);
}
function imageIdToImage(id) {
try {
return "<img src='http://www.kiva.org/img/80x80/" + id + ".jpg'>";
}
catch(error) {
return "";
}
}
function locationToString(location) {
try {
return location.town + ", " + location.country;
}
catch(error) {
return "";
}
}
function descriptionToString(description) {
try {
return description.languages.join(", ");
}
catch(error) {
return "";
}
}
window.alert = function(){return null;}; // disable alerts