-
Notifications
You must be signed in to change notification settings - Fork 1
/
results.html
226 lines (200 loc) · 6.72 KB
/
results.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<style>
#image-container,
#image-container img
{
width: 100%
}
#result-table>tbody>tr>td:first-child {
width: 1%;
white-space: nowrap;
}
#next-result,
#previous-result,
#random-result {
display: none;
}
#result-buttons {
text-align: right;
margin-top: 20px;
margin-bottom: 25px;
}
@media (max-width: 991px) {
#top-row,
#top-row #result-buttons {
text-align: center;
}
}
</style>
<div class="col-sm-9 col-sm-offset-3">
<div id="top-row" class="row">
<div class="col-xs-12 col-md-6">
<h2>Result <span id="result-id">loading...</span></h2>
</div>
<div class="col-xs-12 col-md-6">
<!-- Next/previous/random buttons -->
<div id="result-buttons">
<button id="previous-result" class="btn btn-default">Previous</button>
<button id="next-result" class="btn btn-default">Next</button>
<button id="random-result" class="btn btn-success">Random</button>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<!-- Result info loaded here -->
<table id="result-table" class="table table-stripped table-responsive table-bordered"></table>
</div>
<div class="col-sm-6">
<!-- Image loaded here -->
<div id="image-container">
<img src="https://i.imgur.com/GeHxzb7.png" alt="Loading">
</div>
</div>
</div>
</div>
<script>
var project_short_name = 'lord_chamberlins_plays';
var results = {},
resultIds = [];
/** Retrieve all results for a project. */
function getAllResults(project, deferred) {
var last_id = resultIds[resultIds.length - 1];
$.ajax({
url: '/api/result',
data: {
project_id: project.id,
last_id: last_id,
limit: 100,
all: 1
},
dataType: 'json'
}).done(function(data) {
data.forEach(function(item){
results[item.id] = item;
resultIds.push(item.id);
temp_last_id = item.id;
});
if (resultIds.length > 0 && resultIds[resultIds.length - 1] != last_id) {
getAllResults(project, deferred);
} else {
deferred.resolve();
}
});
}
/** Retrieve a project from its short name. */
function getProject(short_name, deferred) {
$.ajax({
url: '/api/project',
data: {short_name: short_name, all: 1},
dataType: 'json'
}).done(function(data){
if (data[0] == null){
pybossaLogError(new Error('Project not found'));
}
deferred.resolve(data[0]);
});
}
/** Retrive the task for a result. */
function getTask(result, deferred) {
$.ajax({
url: '/api/task/' + result.task_id,
dataType: 'json'
}).done(function(data) {
if (data == null) {
pybossaLogError(new Error('Task not found'));
}
deferred.resolve(data);
});
}
/** Load and display the task input. */
function loadImage(task) {
var img = $("<img />");
img.attr('src', task.info.url_b + "?now=" + new Date().getTime());
img.addClass("img-responsive");
$("#image-container").html("").append(img);
}
/** Load the result info into the table. */
function loadTable(result) {
$('#result-table').html('<tbody></tbody>');
if (typeof(result.info) === 'object') {
$.each(result.info, function(k, v) {
$('#result-table tbody').append('<tr><td><strong>' + k + '</strong></td>' +
'<td>' + v + '</td></tr>')
});
} else {
$('#result-table').html('<thead><tr><th>' + result.info + '</thead></tr></th>');
}
}
/** Load and display result. */
function loadResult(result) {
var def = $.Deferred();
getTask(result, def);
def.done(function(task) {
$("#task-id").text(task.id);
$("#result-id").text(result.id);
loadImage(task);
loadTable(result);
loadResultButtons();
});
}
/** Load the result buttons. */
function loadResultButtons() {
var currentResultId = $('#result-id').html();
var isFirst = resultIds[0] == currentResultId;
var isLast = resultIds[resultIds.length - 1] == currentResultId;
$('#previous-result').hide();
$('#next-result').hide();
if (!isLast) {
$('#next-result').show();
}
if (!isFirst) {
$('#previous-result').show();
}
$('#random-result').show();
}
/** Handle next result button click. */
$("#next-result").on('click', function(e){
var resultId = parseInt($('#result-id').html()),
nextId = resultIds[resultIds.indexOf(resultId) + 1],
nextResult = results[nextId];
if (nextResult == null) {
pybossaLogError(new Error('Next result not found'));
}
loadResult(nextResult);
});
/** Handle previous result button click. */
$("#previous-result").on('click', function(e){
var resultId = parseInt($('#result-id').html()),
previousId = resultIds[resultIds.indexOf(resultId) - 1],
previousResult = results[previousId];
if (previousResult == null) {
pybossaLogError(new Error('Previous not found'));
}
loadResult(previousResult);
});
/** Handle random result button click. */
$("#random-result").off('click').on('click', function(e){
var resultId = resultIds[Math.floor(Math.random()*resultIds.length)],
result = results[resultId];
loadResult(result);
});
/** Load all results and display the first. */
function run(short_name) {
var idDef = $.Deferred(),
resDef = $.Deferred();
getProject(short_name, idDef);
idDef.done(function(project) {
getAllResults(project, resDef);
resDef.done(function() {
if (results.length === 0 || resultIds.length === 0) {
pybossaNotify('No results found', 'info');
$('#image-container').hide();
$('#result-id').html('not found');
} else {
loadResult(results[resultIds[0]]);
}
});
});
}
run(project_short_name);
</script>