Skip to content

Commit 91c2f8b

Browse files
committed
[refactor] improve auto suggest module
1 parent ae905a9 commit 91c2f8b

File tree

5 files changed

+97
-77
lines changed

5 files changed

+97
-77
lines changed

‎src/js/auto-suggest.js‎

Lines changed: 91 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,27 @@ var autoSuggest = (function(){
44
var_currentInputOptions={};
55
var_autoSuggestActive=false;
66

7-
var_delayRender=function(options){
8-
render(options);
7+
varmod={};
8+
9+
mod.open=function(){
10+
helper.setObject({
11+
object: state.get(),
12+
path: "autoSuggest",
13+
newValue: true
14+
});
15+
};
16+
17+
mod.close=function(){
18+
helper.setObject({
19+
object: state.get(),
20+
path: "autoSuggest",
21+
newValue: false
22+
});
923
};
1024

11-
varbind=function(override){
25+
varbind={};
26+
27+
bind.input=function(override){
1228
varoptions={
1329
input: null,
1430
type: null,
@@ -21,17 +37,17 @@ var autoSuggest = (function(){
2137
options.input.addEventListener("focus",function(){
2238
if(!_autoSuggestActive){
2339
clearTimeout(_timer);
24-
_timer=setTimeout(_delayRender,300,options);
40+
_timer=setTimeout(render.delay,300,options);
2541
};
2642
},false);
2743
options.input.addEventListener("input",function(){
2844
clearTimeout(_timer);
29-
_timer=setTimeout(_delayRender,300,options);
45+
_timer=setTimeout(render.delay,300,options);
3046
},false);
3147
};
3248
};
3349

34-
var_navigateResults=function(event){
50+
bind.navigateResults=function(event){
3551
varelementToFocus=null;
3652
varfocusIndex=null;
3753
varallSuggestItems=helper.eA(".auto-suggest-link");
@@ -108,15 +124,15 @@ var autoSuggest = (function(){
108124
if(!event.shiftKey&&event.keyCode==9&&document.activeElement==allSuggestItems[allSuggestItems.length-1]){
109125
event.preventDefault();
110126
elementToFocus=_currentInputOptions.postFocus;
111-
destroy();
127+
render.close();
112128
};
113129
// shift tab
114130
if(event.shiftKey&&event.keyCode==9&&document.activeElement==allSuggestItems[0]){
115131
event.preventDefault();
116132
elementToFocus=_currentInputOptions.input;
117133
};
118134
if(event.shiftKey&&event.keyCode==9&&document.activeElement==_currentInputOptions.input){
119-
destroy();
135+
render.close();
120136
};
121137
};
122138
_findInput();
@@ -127,71 +143,35 @@ var autoSuggest = (function(){
127143
};
128144
};
129145

130-
var_addDocumentEvent=function(){
131-
document.addEventListener("click",_checkClick,false);
132-
document.addEventListener("keydown",_navigateResults,false);
146+
vardocumentEvent={};
147+
148+
documentEvent.add=function(){
149+
document.addEventListener("click",documentEvent.clickOut,false);
150+
document.addEventListener("keydown",bind.navigateResults,false);
133151
};
134152

135-
var_removeDocumentEvent=function(){
136-
document.removeEventListener("click",_checkClick,false);
137-
document.removeEventListener("keydown",_navigateResults,false);
153+
documentEvent.remove=function(){
154+
document.removeEventListener("click",documentEvent.clickOut,false);
155+
document.removeEventListener("keydown",bind.navigateResults,false);
138156
};
139157

140-
var_checkClick=function(event){
158+
documentEvent.clickOut=function(event){
141159
if(!(event.target.classList.contains("auto-suggest-list"))&&!(event.target.classList.contains("auto-suggest-input"))){
142-
destroy();
160+
render.close();
143161
};
144162
};
145163

146-
vardestroy=function(){
147-
varautoSuggestList=helper.e(".auto-suggest-list");
148-
if(autoSuggestList){
149-
autoSuggestList.remove();
150-
_removeDocumentEvent();
151-
helper.setObject({
152-
object: state.get(),
153-
path: "autoSuggest",
154-
newValue: false
155-
});
156-
_currentInputOptions={};
157-
_autoSuggestActive=false;
158-
};
159-
};
164+
varrender={};
160165

161-
var_getSuggestItems=function(){
162-
varsearchTerm=_currentInputOptions.input.value.replace(/^\s+/,"").replace(/\s+$/,"").toLowerCase();
163-
varaction={
164-
fontawesomeIcon: function(){
165-
if(searchTerm==""||searchTerm==undefined){
166-
returnfontawesome.icons;
167-
}else{
168-
returnfontawesome.icons.filter(function(item){
169-
varmatch=false;
170-
if(item.name.toLowerCase().includes(searchTerm)||item.label.toLowerCase().includes(searchTerm)){
171-
match=true;
172-
};
173-
item.search.forEach(function(item,index){
174-
if(item.toLowerCase().includes(searchTerm)){
175-
match=true;
176-
};
177-
});
178-
item.styles.forEach(function(item,index){
179-
if(item.toLowerCase().includes(searchTerm)){
180-
match=true;
181-
};
182-
});
183-
returnmatch;
184-
});
185-
};
186-
}
187-
};
188-
returnaction[_currentInputOptions.type]();
166+
render.delay=function(options){
167+
mod.open();
168+
render.open(options);
189169
};
190170

191-
varrender=function(options){
171+
render.open=function(options){
192172
_currentInputOptions=options;
193173
varbody=helper.e("body");
194-
varsuggestItems=_getSuggestItems(options);
174+
varsuggestItems=render.suggestItems(options);
195175
var_populateList=function(list){
196176
varaction={
197177
fontawesomeIcon: function(){
@@ -217,8 +197,7 @@ var autoSuggest = (function(){
217197
};
218198
action[_currentInputOptions.type]();
219199
};
220-
221-
var_render_autoSuggestList=function(){
200+
var_renderAutoSuggestList=function(){
222201
varautoSuggestWrapper=helper.e(".auto-suggest-wrapper");
223202
varautoSuggestList=helper.e(".auto-suggest-list");
224203
if(autoSuggestList){
@@ -234,27 +213,68 @@ var autoSuggest = (function(){
234213
varautoSuggestList=helper.node("ul|class:auto-suggest-list list-unstyled");
235214
body.appendChild(autoSuggestList);
236215
autoSuggestList.setAttribute("style","width: "+style.width+"px; top: "+style.top+"px; left: "+style.left+"px;");
237-
_addDocumentEvent();
216+
documentEvent.add();
238217
};
239218
_populateList(autoSuggestList);
240219
};
241220
if(suggestItems.length>0){
242221
_autoSuggestActive=true;
243-
helper.setObject({
244-
object: state.get(),
245-
path: "autoSuggest",
246-
newValue: true
247-
});
248-
_render_autoSuggestList();
222+
_renderAutoSuggestList();
249223
}else{
250-
destroy();
224+
render.close();
251225
};
252226
};
253227

228+
render.close=function(){
229+
mod.close();
230+
varautoSuggestList=helper.e(".auto-suggest-list");
231+
if(autoSuggestList){
232+
autoSuggestList.remove();
233+
documentEvent.remove();
234+
_currentInputOptions={};
235+
_autoSuggestActive=false;
236+
};
237+
};
238+
239+
render.suggestItems=function(){
240+
varsearchTerm=_currentInputOptions.input.value.replace(/^\s+/,"").replace(/\s+$/,"").toLowerCase();
241+
varaction={
242+
fontawesomeIcon: function(){
243+
if(searchTerm==""||searchTerm==undefined){
244+
returnfontawesome.icons;
245+
}else{
246+
returnfontawesome.icons.filter(function(item){
247+
varmatch=false;
248+
if(item.name.toLowerCase().includes(searchTerm)||item.label.toLowerCase().includes(searchTerm)){
249+
match=true;
250+
};
251+
item.search.forEach(function(item,index){
252+
if(item.toLowerCase().includes(searchTerm)){
253+
match=true;
254+
};
255+
});
256+
item.styles.forEach(function(item,index){
257+
if(item.toLowerCase().includes(searchTerm)){
258+
match=true;
259+
};
260+
});
261+
returnmatch;
262+
});
263+
};
264+
}
265+
};
266+
returnaction[_currentInputOptions.type]();
267+
};
268+
269+
varclose=function(){
270+
render.close();
271+
};
272+
254273
// exposed methods
255274
return{
256275
bind: bind,
257-
destroy: destroy
276+
render: render,
277+
close: close
258278
};
259279

260280
})();

‎src/js/keyboard.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var keyboard = (function(){
1212
menu.close();
1313
shade.close();
1414
}elseif(state.get().autoSuggest){
15-
autoSuggest.destroy();
15+
autoSuggest.close();
1616
}elseif(state.get().link.add){
1717
link.add.close();
1818
shade.close();

‎src/js/link.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ var link = (function(){
516516
cancelAction: function(){
517517
render.previousFocus();
518518
stagedLink.reset();
519-
autoSuggest.destroy();
519+
autoSuggest.close();
520520
shade.close();
521521
pagelock.unlock();
522522
},
@@ -702,7 +702,7 @@ var link = (function(){
702702
iconFormGroupClear.removeAttribute("disabled");
703703
iconFormGroupText.tabIndex=1;
704704
},false);
705-
autoSuggest.bind({
705+
autoSuggest.bind.input({
706706
input: iconInput,
707707
type: "fontawesomeIcon",
708708
postFocus: iconFormGroupText
@@ -752,7 +752,7 @@ var link = (function(){
752752
console.log("hit");
753753
mod.add.close();
754754
stagedLink.reset();
755-
autoSuggest.destroy();
755+
autoSuggest.close();
756756
shade.close();
757757
pagelock.unlock();
758758
},

‎src/js/version.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
varversion=(function(){
22

3-
varcurrent="3.55.0";
3+
varcurrent="3.56.0";
44

55
varcompare=function(a,b){
66
varpa=a.split(".");

‎src/manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nightTab",
33
"short_name": "nightTab",
44
"description": "A neutral new tab page accented with a chosen colour. Customise the layout, style, background and bookmarks in nightTab.",
5-
"version": "3.55.0",
5+
"version": "3.56.0",
66
"manifest_version": 2,
77
"chrome_url_overrides":{
88
"newtab": "index.html"

0 commit comments

Comments
(0)