forked from atom/flight-manual.atom.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
89 lines (74 loc) · 2.14 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
require([
'/example/jquery.js',
'/example/mustache.js',
'../lunr.js',
'text!templates/question_view.mustache',
'text!templates/question_list.mustache',
'text!example_data.json',
'text!example_index.json'
], function (_, Mustache, lunr, questionView, questionList, data, indexDump) {
var renderQuestionList = function (qs) {
$("#question-list-container")
.empty()
.append(Mustache.to_html(questionList, {questions: qs}))
}
var renderQuestionView = function (question) {
$("#question-view-container")
.empty()
.append(Mustache.to_html(questionView, question))
}
window.profile = function (term) {
console.profile('search')
idx.search(term)
console.profileEnd('search')
}
window.search = function (term) {
console.time('search')
idx.search(term)
console.timeEnd('search')
}
var indexDump = JSON.parse(indexDump)
console.time('load')
window.idx = lunr.Index.load(indexDump)
console.timeEnd('load')
var questions = JSON.parse(data).questions.map(function (raw) {
return {
id: raw.question_id,
title: raw.title,
body: raw.body,
tags: raw.tags.join(' ')
}
})
renderQuestionList(questions)
renderQuestionView(questions[0])
$('a.all').bind('click', function () {
renderQuestionList(questions)
$('input').val('')
})
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)
}
}
$('input').bind('keyup', debounce(function () {
if ($(this).val() < 2) return
var query = $(this).val()
var results = idx.search(query).map(function (result) {
return questions.filter(function (q) { return q.id === parseInt(result.ref, 10) })[0]
})
renderQuestionList(results)
}))
$("#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])
})
})