-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
122 lines (106 loc) · 3.15 KB
/
search.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
var hSearch = document.getElementById( "line-input-search" );
var sSavedChapter = null;
var sSearchQuery = "";
var iSearchTimer = null;
function fNewSearch()
{
fEraseEventListeners(); // Erases event listeners and resource nodes.
fRemoveResourceNodes();
fRemoveTopicNodes();
fClearDResourceStack();
sSavedChapter = document.getElementsByClassName( "topic" );
if( sSavedChapter.length == 0 )
{
sSavedChapter = null;
}
else
{
sSavedChapter = document.getElementsByClassName( "topic" )[0].id;
}
fPopulateStackBySearch( sSearchQuery );
let aDistinctQueriedChapters = dResourceStack().distinct( "sSection" ); // This gets an array of refs sections.
// Now I just need topics.
let aQuerysTopics = [];
let nQuerysTopicsIndex = 0;
nTotalTasks = aDistinctQueriedChapters.length;
let nSectionIndexValue = 0;
fStartAsyncTask(
{ // And supply a bunch of arguments in the form of an object.
nMillisecondsAllotted: 25,
nTasksAllotted: nTotalTasks,
nTasksPerTick: 1,
fTask: function() // In here we attach a function.
{
let oSectionsTopicsIndices = fPopulateTopicsViaSearch( nSectionIndexValue, nQuerysTopicsIndex, aDistinctQueriedChapters, aQuerysTopics );
nSectionIndexValue = oSectionsTopicsIndices.nSectionIndexValue;
nQuerysTopicsIndex = oSectionsTopicsIndices.nTopicsIndexValue;
oSectionsTopicsIndices = null;
},
fUponAsyncTaskCompletion: function()
{
nTotalTasks = dResourceStack().count();
fStartAsyncTask(
{ // And supply a bunch of arguments in the form of an object.
nMillisecondsAllotted: 25,
nTasksAllotted: nTotalTasks,
nTasksPerTick: 1,
fTask: function() // In here we attach a function.
{
fAddNewResource();
},
fUponAsyncTaskCompletion: function()
{
fAddAllEventListeners();
}
}
);
}
}
);
}
/// Called when the search box is empty.
function fHandleSearchEmpty()
{
// First, we eliminate all resource nodes.
fEraseEventListeners(); // Erases event listeners and resource nodes.
fRemoveResourceNodes();
fRemoveTopicNodes();
fClearDResourceStack();
// Next, we check if we have saved a section.
// If there is one, we load that node.
if( sSavedChapter == null )
{
return;
}
setTimeout( function()
{
fPopulateTopicsViaSection( sSavedChapter );
sSavedChapter = null;
}, 26 );
}
/// fHandleSearchInput is the main processor of the search functions.
function fHandleSearchInput()
{
/// First, we need a iSearchTimer before we process all of these for loops.
// Compare the strings.
let sSearchValue = hSearch.value.toLowerCase();
if( sSearchValue == sSearchQuery || ( sSearchValue.length > 0 && sSearchValue.length < 4 ) )
{
return;
}
sSearchQuery = sSearchValue;
if( sSearchValue == "" )
{
// If we have completely cleared the input, we need to revert to normal.
clearTimeout(iSearchTimer);
fHandleSearchEmpty();
return;
}
// Otherwise, we need to check the iSearchTimer. If it is null, we start one.
if( iSearchTimer != null )
{
clearTimeout( iSearchTimer ); // Only counts down until we have a search result.
}
iSearchTimer = setTimeout( fNewSearch, 2000 );
}
hSearch.addEventListener( "keyup", fHandleSearchInput );