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

Side by Side Diff: foo/test.js

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

Powered by Google App Engine
This is Rietveld 408576698