-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
329 lines (248 loc) · 8.54 KB
/
app.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
requirejs.config({waitSeconds:0});
require([
'./jquery.js',
'./handlebars.min.js',
'./elasticlunr.min.js',
'text!templates/question_view.mustache',
'text!templates/question_list.mustache',
'text!templates/word_list.mustache',
'text!data.json'
], function (_, Mustache, elasticlunr, questionView, questionList, wordList, data, indexDump) {
//, 'text!example_index.json', 'text!example_data.json',
var WLtemplate = Mustache.compile(wordList);
var QLtemplate = Mustache.compile(questionList);
var QVtemplate = Mustache.compile(questionView);
var seeder = 11091974;
var BodyRef = document.getElementById("body");
var clearButton = $('#clearButton');
var renderQuestionList = function (qs) {
$("#question-list-container")
.empty()
.append(QLtemplate({questions: qs}));
}
var renderWordList = function (qs) {
$("#question-list-container")
.empty()
.append(PRERENDERED_LIST_HTML);
}
var renderQuestionView = function (question) {
$("#question-view-container")
.empty()
.append(QVtemplate(question));
}
window.profile = function (term) {
console.profile('search');
window.idx.search(term);
console.profileEnd('search');
}
window.search = function (term) {
console.time('search')
idx.search(term)
console.timeEnd('search')
}
var idx = elasticlunr(function () {
this.setRef('id');
this.addField('title');
this.addField('searchTerms');
this.addField('tags');
this.saveDocument(false);
});
Mustache.registerHelper('everyOther', function (index, options) {
if(index%2 == 0){
return options.fn(this);
} else {
return options.inverse(this);
}
});
// Modified to
Array.prototype.byCount= function(){
//var lenHolder = [];
var itm, a= [], L= this.length, o= {};
for(var i= 0; i<L; i++){
itm= this[i];
if(!itm) continue;
if(o[itm]== undefined) o[itm]= 1;
else ++o[itm];
}
countHolder = [];
for(var p in o) {a[a.length]= p;
countHolder[countHolder.length] = { name: a.slice(-1).toString() , amount: o[p]};
}
var returnWithCounts =
a.sort(function(a, b){
return o[b]-o[a];
})
countHolder.sort(function(a, b){
return a.amount >b.amount;
})
return returnWithCounts };
// prep the questions
var fulllist = [];
var rawQuestions = JSON.parse(data).questions;
rawQuestions.map(function (raw) {
for (thing in raw.question.content )
{
fulllist[fulllist.length]= thing; //{'x': thing};
}
})
var shortlist = fulllist.byCount();
dict = {}
countHolder.forEach(function(x) {
dict[x.name] = x.amount
})
var questions = rawQuestions.map(function (raw) {
rawQuestion = raw;
var holder = ""
for (i in raw.question.content){
holder +=i +' : '+ raw.question.content[i]+'\n';
}
var make_id = murmurhash3_32_gc(raw.img.filename, seeder);
//console.log(make_id);
var clickableTags = Object.keys(raw.question.content).map(function (k) {
var result = '<a title="'+dict[k]+' results for '+k+ '" style="cursor:pointer" onclick="searchTerm(\''+k+'\')">'+k+'</a>';
return result;
})
return {
id: make_id,
title: raw.img.filename.replace('.JPG',''),
body: holder,
searchTerms: Object.keys(raw.question.content).join(' '),
tags: clickableTags, // Object.keys(raw.question.content).join(' '),
img: raw.img.filename,
thumb: raw.thumb.filename
}
})
questions.forEach(function (question) {
idx.addDoc(question);
});
window.idx = idx;
var config = '{ "fields": { "searchTerms": {"boost": 2}, "title": {"boost": 1} }, "boolean": "AND"}';
var json_config = new elasticlunr.Configuration(config, window.idx.getFields()).get();
document.getElementById("loader").style.display = "none" ;
document.getElementById("hiding_title").style.display = "none" ;
renderQuestionView(questions[0])
shortlist = countHolder.map(function (raw) { return { x : raw.name, count: raw.amount } });
var PRERENDERED_LIST_HTML = WLtemplate({list: shortlist});
var emptyFunction = function emptyMe (){
$('input').val('');
renderWordList(shortlist);
}
window.emptyFunction = emptyFunction;
renderWordList(shortlist);
var debounce = function (fn) {
var timeout
return function () {
var args = Array.prototype.slice.call(arguments),
ctx = this
clearTimeout(timeout)
timeout = setTimeout(function () {
fn.apply(ctx, args)
}, 100)
}
}
var renderPartialQuestionList = function (results, startPos, endPos){
var temp = results.slice(startPos, endPos);
$("#question-list-container")
.append(QLtemplate({questions: temp}))
}
function doHeavyWork(results, start, totalResultsToRender, term) {
var total = totalResultsToRender;
var fragment = 20;
var end = start + fragment;
var left = totalResultsToRender - end ;
// console.log( left + ' results of '+ totalResultsToRender +' total left to render');
// partially render list
// the thing to render, the start record and the end record
renderPartialQuestionList(results, end-fragment, end)
clearButton.text(end +' of '+results.length+' for '+term);
//clearButton.addClass('blink');
if (end >= total) {
// If we reached the end, stop and change status
clearButton.removeClass('blink');
clearButton.text(results.length+' for '+term);
$('body').removeClass('custom');
//BodyRef.style.cursor = '';
} else {
// Otherwise, process next fragment
setTimeout(function() {
doHeavyWork(results, end, totalResultsToRender, term);
}, 0);
}
}
function dowork(results, totalResultsToRender, term) {
// Set "working" status
document.getElementById("clearButton").innerHTML = "working";
// render the single view and set term in search bar
$('body').addClass('custom'); //document.body.style.cursor = "progress";
renderQuestionView(results[0]);
// render big view in chunks
doHeavyWork(results, 0, totalResultsToRender, term);
}
function searchTerm(term){
$('input').val(term);
var results = null;
results = window.idx.search(term, json_config).map(function (result) {
return questions.filter(function (q) { return q.id === parseInt(result.ref, 10) })[0]
})
if(results.length<1) { renderWordList(shortlist); }
else
{
$("#question-list-container").empty();
dowork(results, results.length, term);
}
}
window.searchTerm = searchTerm; // Make it available via the javascript window object rather than require.js
// on key up search on 3 letters or more.
$('input').bind('keyup', debounce(function () {
if ($(this).val().length < 2) return;
searchTerm($(this).val());
}))
$("#question-list-container").delegate('li', 'click', function () {
var li = $(this)
var id = li.data('question-id')
renderQuestionView(questions.filter(function (question) {
return (question.id == id)
})[0])
})
})
function murmurhash3_32_gc(key, seed) {
var remainder, bytes, h1, h1b, c1, c1b, c2, c2b, k1, i;
remainder = key.length & 3; // key.length % 4
bytes = key.length - remainder;
h1 = seed;
c1 = 0xcc9e2d51;
c2 = 0x1b873593;
i = 0;
while (i < bytes) {
k1 =
((key.charCodeAt(i) & 0xff)) |
((key.charCodeAt(++i) & 0xff) << 8) |
((key.charCodeAt(++i) & 0xff) << 16) |
((key.charCodeAt(++i) & 0xff) << 24);
++i;
k1 = ((((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16))) & 0xffffffff;
k1 = (k1 << 15) | (k1 >>> 17);
k1 = ((((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16))) & 0xffffffff;
h1 ^= k1;
h1 = (h1 << 13) | (h1 >>> 19);
h1b = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16))) & 0xffffffff;
h1 = (((h1b & 0xffff) + 0x6b64) + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16));
}
k1 = 0;
switch (remainder) {
case 3: k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16;
case 2: k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
case 1: k1 ^= (key.charCodeAt(i) & 0xff);
k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
k1 = (k1 << 15) | (k1 >>> 17);
k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
h1 ^= k1;
}
h1 ^= key.length;
h1 ^= h1 >>> 16;
h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff;
h1 ^= h1 >>> 13;
h1 = ((((h1 & 0xffff) * 0xc2b2ae35) + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff;
h1 ^= h1 >>> 16;
return h1 >>> 0;
}