Skip to content

Commit 2241195

Browse files
committed
initial commit - working precised demo
0 parents  commit 2241195

25 files changed

+18438
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Ki

app.js

+280
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
//requirejs.config({waitSeconds:0});
2+
require([
3+
'./jquery.js',
4+
'./handlebars.min.js',
5+
'./elasticlunr.min.js',
6+
'text!templates/question_view.mustache',
7+
'text!templates/question_list.mustache',
8+
'text!templates/word_list.mustache',
9+
'text!data.json'
10+
], function (_, Mustache, elasticlunr, questionView, questionList, wordList, data, indexDump) {
11+
12+
var WLtemplate = Mustache.compile(wordList);
13+
var QLtemplate = Mustache.compile(questionList);
14+
var QVtemplate = Mustache.compile(questionView);
15+
16+
var seeder = 999999;
17+
18+
var BodyRef = document.getElementById("body");
19+
var clearButton = $('#clearButton');
20+
21+
22+
var renderQuestionList = function (qs) {
23+
$("#question-list-container")
24+
.empty()
25+
.append(QLtemplate({questions: qs}))
26+
}
27+
28+
var renderWordList = function (qs) {
29+
$("#question-list-container")
30+
.empty()
31+
.append(WLtemplate({list: qs}))
32+
}
33+
34+
var renderQuestionView = function (question) {
35+
$("#question-view-container")
36+
.empty()
37+
.append(QVtemplate(question))
38+
39+
}
40+
41+
window.profile = function (term) {
42+
console.profile('search')
43+
window.idx.search(term)
44+
console.profileEnd('search')
45+
}
46+
47+
window.search = function (term) {
48+
console.time('search')
49+
idx.search(term)
50+
console.timeEnd('search')
51+
}
52+
53+
54+
var idx = elasticlunr(function () {
55+
this.setRef('id');
56+
this.addField('title');
57+
this.addField('searchTerms');
58+
this.addField('tags');
59+
this.saveDocument(false);
60+
});
61+
62+
var fulllist = new Array();
63+
64+
var questions = JSON.parse(data).questions.map(function (raw) {
65+
var holder = ""
66+
for (i in raw.question.content){
67+
holder +=i +' : '+ raw.question.content[i]+'\n';
68+
}
69+
var make_id = murmurhash3_32_gc(raw.img.filename, seeder);
70+
71+
for (thing in raw.question.content )
72+
{
73+
fulllist[fulllist.length]= thing; //{'x': thing};
74+
}
75+
76+
var clickableTags = Object.keys(raw.question.content).map(function (k) {
77+
78+
var result = '<a style="cursor:pointer" onclick="searchTerm(\''+k+'\')">'+k+'</a>';
79+
80+
return result;
81+
})
82+
83+
84+
return {
85+
86+
id: make_id,
87+
title: raw.img.filename.replace('.JPG',''),
88+
body: holder,
89+
searchTerms: Object.keys(raw.question.content).join(' '),
90+
tags: clickableTags,
91+
img: raw.img.filename,
92+
thumb: raw.thumb.filename
93+
}
94+
})
95+
96+
97+
questions.forEach(function (question) {
98+
idx.addDoc(question);
99+
});
100+
101+
102+
window.idx = idx;
103+
104+
var config = '{ "fields": { "searchTerms": {"boost": 2}, "title": {"boost": 1} }, "boolean": "AND"}';
105+
var json_config = new elasticlunr.Configuration(config, window.idx.getFields()).get();
106+
107+
document.getElementById("loader").style.display = "none" ;
108+
document.getElementById("hiding_title").style.display = "none" ;
109+
110+
111+
// this is how one might expose the complete engine for saving in a NON Node.js scenario.
112+
// var code = JSON.stringify(idx);
113+
// $('#codehere').text(code);
114+
115+
renderQuestionView(questions[0])
116+
117+
118+
var shortlist = fulllist.filter((v, i, a) => a.indexOf(v) === i).map(function (raw) {
119+
return { x : raw }
120+
});
121+
122+
var emptyFunction = function emptyMe (){
123+
124+
$('input').val('');
125+
renderWordList(shortlist);
126+
}
127+
window.emptyFunction = emptyFunction;
128+
129+
renderWordList(shortlist);
130+
131+
132+
var debounce = function (fn) {
133+
var timeout
134+
return function () {
135+
var args = Array.prototype.slice.call(arguments),
136+
ctx = this
137+
138+
clearTimeout(timeout)
139+
timeout = setTimeout(function () {
140+
fn.apply(ctx, args)
141+
}, 100)
142+
}
143+
}
144+
145+
146+
147+
var renderPartialQuestionList = function (results, startPos, endPos){
148+
var temp = results.slice(startPos, endPos);
149+
$("#question-list-container")
150+
.append(QLtemplate({questions: temp}))
151+
}
152+
153+
function doHeavyWork(results, start, totalResultsToRender, term) {
154+
var total = totalResultsToRender;
155+
var fragment = 50;
156+
var end = start + fragment;
157+
var left = totalResultsToRender - end ;
158+
159+
160+
// partially render list
161+
// the thing to render, the start record and the end record
162+
renderPartialQuestionList(results, end-fragment, end)
163+
clearButton.text(end+fragment +' of '+results.length+' for '+term);
164+
165+
166+
if (end >= total) {
167+
168+
// If we reached the end, stop and change status
169+
clearButton.removeClass('blink');
170+
clearButton.text(results.length+' for '+term);
171+
BodyRef.style.cursor = '';
172+
173+
} else {
174+
// Otherwise, process next fragment
175+
setTimeout(function() {
176+
doHeavyWork(results, end, totalResultsToRender, term);
177+
}, 0);
178+
}
179+
}
180+
181+
function dowork(results, totalResultsToRender, term) {
182+
// Set "working" status
183+
document.body.style.cursor = "wait";
184+
document.getElementById("clearButton").innerHTML = "working";
185+
// render the single view and set term in search bar
186+
187+
renderQuestionView(results[0]);
188+
189+
// render big view in chunks
190+
doHeavyWork(results, 0, totalResultsToRender, term);
191+
}
192+
193+
194+
function searchTerm(term){
195+
$('input').val(term);
196+
var results = null;
197+
results = window.idx.search(term, json_config).map(function (result) {
198+
return questions.filter(function (q) { return q.id === parseInt(result.ref, 10) })[0]
199+
})
200+
if(results.length<1) { renderWordList(shortlist); }
201+
else
202+
{
203+
$("#question-list-container").empty();
204+
dowork(results, results.length, term);
205+
}
206+
}
207+
208+
window.searchTerm = searchTerm; // Make it available via the javascript window object rather than require.js
209+
210+
// on key up search on 3 letters or more.
211+
$('input').bind('keyup', debounce(function () {
212+
if ($(this).val().length < 2) return;
213+
214+
searchTerm($(this).val());
215+
}))
216+
217+
$("#question-list-container").delegate('li', 'click', function () {
218+
var li = $(this)
219+
var id = li.data('question-id')
220+
221+
renderQuestionView(questions.filter(function (question) {
222+
return (question.id == id)
223+
})[0])
224+
})
225+
226+
})
227+
228+
function murmurhash3_32_gc(key, seed) {
229+
var remainder, bytes, h1, h1b, c1, c1b, c2, c2b, k1, i;
230+
231+
remainder = key.length & 3; // key.length % 4
232+
bytes = key.length - remainder;
233+
h1 = seed;
234+
c1 = 0xcc9e2d51;
235+
c2 = 0x1b873593;
236+
i = 0;
237+
238+
while (i < bytes) {
239+
k1 =
240+
((key.charCodeAt(i) & 0xff)) |
241+
((key.charCodeAt(++i) & 0xff) << 8) |
242+
((key.charCodeAt(++i) & 0xff) << 16) |
243+
((key.charCodeAt(++i) & 0xff) << 24);
244+
++i;
245+
246+
k1 = ((((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16))) & 0xffffffff;
247+
k1 = (k1 << 15) | (k1 >>> 17);
248+
k1 = ((((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16))) & 0xffffffff;
249+
250+
h1 ^= k1;
251+
h1 = (h1 << 13) | (h1 >>> 19);
252+
h1b = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16))) & 0xffffffff;
253+
h1 = (((h1b & 0xffff) + 0x6b64) + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16));
254+
}
255+
256+
k1 = 0;
257+
258+
switch (remainder) {
259+
case 3: k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16;
260+
case 2: k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
261+
case 1: k1 ^= (key.charCodeAt(i) & 0xff);
262+
263+
k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
264+
k1 = (k1 << 15) | (k1 >>> 17);
265+
k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
266+
h1 ^= k1;
267+
}
268+
269+
h1 ^= key.length;
270+
271+
h1 ^= h1 >>> 16;
272+
h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff;
273+
h1 ^= h1 >>> 13;
274+
h1 = ((((h1 & 0xffff) * 0xc2b2ae35) + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff;
275+
h1 ^= h1 >>> 16;
276+
277+
return h1 >>> 0;
278+
}
279+
280+

assets/bootstrap/js/bootstrap.min.js

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)