Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: foo/test.js

Issue 1000523002: . Base URL: git@github.com:chromium/dom-distiller.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « foo/test.html ('k') | get_screenshots.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 data = null;
2 idx = 0;
3
4 nextUpdateId = 0;
5
6 urlHolder = document.querySelector('#url-holder');
7 dataTable = document.querySelector('#data-table');
8 $dataRows = null;
9
10 baseImage = document.querySelector('#base-img');
11 distilledImage = document.querySelector('#distilled-img');
12 imageHolder = document.querySelector('#image-holder');
13
14 goodButton = document.querySelector('#good');
15 badButton = document.querySelector('#bad');
16 resetButton = document.querySelector('#reset');
17 errorButton = document.querySelector('#error');
18
19 function changeIndex(newIndex) {
20 var oldIndex = idx;
21 idx = Math.max(0, Math.min(data.length - 1, newIndex));
22 showImage(idx);
23 $dataRows.eq(oldIndex).removeClass('active');
24 var $currentRow = $dataRows.eq(idx);
25 $currentRow.addClass('active');
26 $('.current-position').text(idx + 1);
27 }
28
29 function recordGood() {
30 var e = data[idx];
31 e['good'] = 1;
32 updateTableRow(idx);
33 sendUpdate(e, function() {
34 changeIndex(idx + 1);
35 });
36 }
37
38 function recordBad() {
39 var e = data[idx];
40 e['good'] = 0;
41 updateTableRow(idx);
42 sendUpdate(e, function() {
43 changeIndex(idx + 1);
44 });
45 }
46
47 function recordError() {
48 var e = data[idx];
49 e['good'] = -1;
50 updateTableRow(idx);
51 sendUpdate(e, function() {
52 changeIndex(idx + 1);
53 });
54 }
55
56 function resetEntry() {
57 var e = data[idx];
58 delete e['good']
59 updateTableRow(idx);
60 sendUpdate(e, function() {
61 });
62 }
63
64 function sendMessage(type, message, callback) {
65 $.ajax({
66 'type': type,
67 'url': '/message',
68 'contentType': 'application/json',
69 'processData': false,
70 'data': JSON.stringify(message),
71 'error': function(response, status) {
72 console.log('wtf response: ', response, status);
73 },
74 'success': function(response) {
75 console.log('response: ', response);
76 callback(response);
77 },
78 'dataType': 'json',
79 });
80 }
81
82 function sendUpdate(e, callback) {
83 sendMessage(
84 'POST',
85 {
86 'action': 'update',
87 'data': e,
88 },
89 callback);
90 }
91
92 function back() {
93 changeIndex(idx - 1);
94 }
95
96 function skip() {
97 changeIndex(idx + 1);
98 }
99
100 goodButton.onclick = recordGood
101 badButton.onclick = recordBad
102 resetButton.onclick = resetEntry
103 errorButton.onclick = recordError
104 $('.right-caret').click(skip);
105 $('.left-caret').click(back);
106
107 function showImage(i) {
108 goodButton.disabled = false;
109 badButton.disabled = false;
110 var e = data[i]
111 baseImage.src = '/images/' + e['screenshot'].replace(/.*\//, '')
112 distilledImage.src = '/images/' + e['distilled'].replace(/.*\//, '')
113 urlHolder.innerText = e['url']
114 urlHolder.href = e['url']
115 imageHolder.classList.remove('bad');
116 imageHolder.classList.remove('good');
117 imageHolder.classList.remove('error');
118 if (typeof e['good'] != 'undefined') {
119 imageHolder.classList.add(
120 e['good'] == 1
121 ? 'good'
122 : e['good'] == -1
123 ? 'error'
124 : 'bad');
125 }
126 }
127
128 function updateTableRow(i) {
129 var row = dataTable.querySelectorAll('tr')[i];
130 row.classList.remove('good');
131 row.classList.remove('bad');
132 imageHolder.classList.remove('error');
133 var e = data[i];
134 if (typeof e['good'] != 'undefined') {
135 row.classList.add(
136 e['good'] == 1
137 ? 'good'
138 : e['good'] == -1
139 ? 'error'
140 : 'bad');
141 }
142 if (i == idx) {
143 // forces ui update
144 changeIndex(idx);
145 }
146 }
147
148 function createTable() {
149 var dataRows = [];
150 for (var i = 0; i < data.length; i++) {
151 row = document.createElement('tr');
152 cell = document.createElement('td');
153 row.appendChild(cell);
154 cell.appendChild(document.createTextNode(data[i]['url']));
155 dataTable.appendChild(row);
156 dataRows.push(row);
157 }
158
159 $dataRows = $(dataRows);
160
161 $dataRows.each(function(i, r) {
162 $(r).click(function() {
163 console.log(i);
164 // forces ui update
165 changeIndex(i);
166 });
167 })
168 }
169
170 function setData(d, i) {
171 var needsTable = data == null;
172 data = d; //.slice(0, 100);
173 $('.total-entries').text(data.length);
174 if (needsTable) {
175 createTable();
176 }
177 for (var i = 0; i < data.length; i++) {
178 updateTableRow(i);
179 }
180 }
181
182 function handleVisibilityChange() {
183 if (!document.hidden) sendGetUpdates(1000);
184 }
185
186 function getData() {
187 sendMessage(
188 'POST',
189 {
190 'action': 'getData'
191 },
192 function(rsp) {
193 document.addEventListener('visibilitychange', handleVisibilityChange, fa lse);
194 var data = rsp['response']['data'];
195 nextUpdateId = rsp['response']['nextId'];
196 setData(data);
197 var startIndex = parseInt(window.location.hash.substr(1)) - 1;
198 if (isNaN(startIndex)) {
199 startIndex = 0;
200 }
201 changeIndex(startIndex);
202 sendGetUpdates(1000);
203 }
204 );
205 }
206
207
208 getUpdatesSent = false;
209 function sendGetUpdates(delay) {
210 if (document.hidden) return;
211 if (getUpdatesSent) return;
212 getUpdatesSent = true;
213 setTimeout(function() { getUpdates(delay); }, delay);
214 }
215
216 function getUpdates(delay) {
217 $.ajax({
218 'type': 'GET',
219 'url': '/getupdates?nextId=' + nextUpdateId,
220 'contentType': 'application/json',
221 'processData': false,
222 'error': function(response, status) {
223 console.log('xx: wtf response: ', response, status);
224 },
225 'success': function(rawResponse) {
226 getUpdatesSent = false;
227 var response = rawResponse['response'];
228 nextUpdateId = response['nextId'];
229 if (response['data'] != null) {
230 setData(response['data']);
231 } else if (response['updates'] != null) {
232 updates = response['updates'];
233 for (var i = 0; i < updates.length; i++) {
234 var u = updates[i];
235 var idx = u['index'];
236 var entry = u['entry'];
237 var curr = data[idx];
238 if (curr['url'] != entry['url']) {
239 console.error(curr, entry);
240 }
241 data[idx] = entry;
242 updateTableRow(idx);
243 }
244 nextUpdateId = nextUpdateId;
245 delay = 1000;
246 } else {
247 delay = Math.min(30 * 1000, 1.5 * delay);
248 }
249 console.log('xx: response: ', response, delay);
250 sendGetUpdates(delay);
251 },
252 'dataType': 'json',
253 });
254 }
255
256 $(document).ready(function(){
257 getData();
258 $(document).keypress(function(e){
259 console.log(e)
260 switch (e.which) {
261 case 42: // *
262 case 48: // 0
263 recordError();
264 break;
265 case 43: // +
266 case 61: // =
267 recordGood();
268 break;
269 case 45: // -
270 recordBad();
271 break;
272 }
273 });
274 $(document).keydown(function(e) {
275 switch (e.which) {
276 case 37: // <--
277 case 39: // >--
278 e.preventDefault();
279 }
280 });
281 $(document).keyup(function(e){
282 switch (e.which) {
283 case 37: // <--
284 back();
285 break;
286 case 39: // >--
287 skip();
288 break;
289 }
290 });
291 });
OLDNEW
« no previous file with comments | « foo/test.html ('k') | get_screenshots.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698