-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
190 lines (190 loc) · 6.2 KB
/
index.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contributively - peer-reviewed contributions to projects</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["piechart"]});
function drawPieChartForString(s) {
var start = s.start;
var end = s.end;
var rows = end-start+1;
var data = new google.visualization.DataTable();
data.addColumn('string', 'Contribution');
data.addColumn('number', 'Percentage');
data.addRows(rows);
var person;
for(var i=0;i<rows;i++) {
person = store[start+i];
data.setValue(i, 0, person.name);
data.setValue(i, 1, person.normalisedTotal);
}
var chart = new google.visualization.PieChart(document.getElementById('pieChart'));
chart.draw(data, {width: 400, height: 240, is3D: true, title: 'Contributions'});
}
</script>
<script type="text/javascript">
var store = [];
var calculateTotalsForString = function(s) {
var start = s.start;
var end = s.end;
var person = store[start];
var previousPerson;
var ratio;
var j = start+1;
var unNormalisedTotal;
// make everything relative to person at start of string
var total = person['amountWith'+j];
person.unNormalisedTotal = total;
for(var i=j;i<end+1;i++) {
// only runs once there are two or more people in a string
previousPerson = person;
// should probably check that all browsers make array[0] the
// first element pushed in
person = store[i];
ratio = person['amountWith'+previousPerson.position] / previousPerson['amountWith'+person.position];
unNormalisedTotal = previousPerson.unNormalisedTotal * ratio;
person.unNormalisedTotal = unNormalisedTotal;
total += unNormalisedTotal;
}
var normalisedTotal;
for(var i=start;i<end+1;i++) {
person = store[i];
normalisedTotal = person.unNormalisedTotal / total;
person.normalisedTotal = normalisedTotal;
$('#total'+person.position).html(normalisedTotal);
}
drawPieChartForString(s);
/*saveStoreToWeb();*/
};
var calculateConnectedStrings = function() {
var connectedStrings = [];
var connectedString = {
start:0,
end:0
};
var j;
for(var i=0;i<store.length;i++) {
j=i+1;
if(store[i]['amountWith'+j]) {
connectedString.end = j;
} else {
if(connectedString.end) {
connectedStrings.push(connectedString);
}
connectedString = {
start:j,
end:null
};
}
}
return connectedStrings;
};
var calculateTotalsFromStore = function() {
var ss = calculateConnectedStrings();
$('#pieChart').html('');
for(var i=0;i<ss.length;i++) {
calculateTotalsForString(ss[i]);
}
};
var saveStoreToWeb = function(successCallback) {
successCallback = typeof successCallback === 'function' ? successCallback : function() {};
var callback = function(response) {
if(response.indexOf("added successfully")!==-1) { // this is from appjet
$('#nameList .ajax').remove();
successCallback();
} else {
$('#nameList .ajax').addClass('error').html('Not saved to server! Try again.</div>');
}
};
$('#nameList').prepend('<div class="ajax">Saving...</div>');
// handle viewing over local file:/// uri
if(
window.Components &&
window.netscape &&
window.netscape.security &&
document.location.protocol.indexOf("http") == -1) {
window.netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
}
$.post("http://contributively.appjet.net",store,callback,"html");
};
$(document).ready(function() {
$('#nameForm .submit').click(function() {
var name = $('#name').val();
var addNameToTable = function() {
var table = $('#nameList tbody');
var people = $('#nameList tbody tr.person').length;
var htmlString = '<tr class="person"><td>'+name+'</td><td id="total'+people+'"></td></tr>';
if(people!==0) {
var id = $('#nameList input').length;
htmlString += '<tr><td><input type="text" id="'+(id+1)+'" /></td><td></td></tr><tr><td><input type="text" id="'+id+'" /></td><td></td></tr>';
}
table.prepend(htmlString);
};
store.push({
name:name,
position:store.length
});
addNameToTable();
/*saveStoreToWeb();*/
return false;
});
$('#nameList').change(function(ev) {
var target = ev.target;
var id = parseInt(target.id,10);
var partnerInputId = id%2===0 ? id+1 : id-1;
var position = Math.floor((id+1)/2);
var partnerPosition = Math.floor((partnerInputId+1)/2);
var amount = target.value;
if(target.value==='') {
amount = target.value = '0';
}
var updateInput = function() {
// check to see its only digits and less than or equal to 100
if(amount.match(/^\d+$/) && amount <= 100) {
var partnerInput = $('#'+partnerInputId);
partnerInput.val(100-amount);
}
};
for(var i=0;i<store.length;i++) {
if(store[i].position===position) {
store[i]['amountWith'+partnerPosition] = amount;
} else if(store[i].position===partnerPosition) {
store[i]['amountWith'+position] = 100-amount;
}
}
updateInput();
if(store.length>=2) {
calculateTotalsFromStore();
}
/*saveStoreToWeb();*/
});
});
</script>
</head>
<body>
<div id="container">
<div id="nameForm">
<form action="/">
<label for="name">Add name</label>
<input id="name" type="text"></input>
<input type="submit" class="submit" value="Add"></input>
</form>
</div>
<div id="nameList">
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Totals</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div id="pieChart"></div>
</div>
</div>
</body>
</html>