forked from zombieFox/nightTab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
133 lines (122 loc) · 3.58 KB
/
Copy pathsearch.js
File metadata and controls
133 lines (122 loc) · 3.58 KB
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
var search = (function() {
var bind = function() {
var searchInput = helper.e(".search-input");
var searchClear = helper.e(".search-clear");
searchInput.addEventListener("input", function() {
_toggle();
render.clear.button();
link.clear();
link.render.item.all();
sortable(".link-area");
}, false);
searchClear.addEventListener("click", function() {
render.clear.input();
_toggle();
render.clear.button();
link.clear();
link.render.item.all();
sortable(".link-area");
}, false);
};
var _toggle = function() {
var html = helper.e("html");
var searchInput = helper.e(".search-input");
if (searchInput.value != "") {
helper.setObject({
object: state.get(),
path: "search",
newValue: true
});
helper.addClass(html, "is-header-searching");
} else {
helper.setObject({
object: state.get(),
path: "search",
newValue: false
});
helper.removeClass(html, "is-header-searching");
};
};
var get = function() {
var searchInput = helper.e(".search-input");
if (state.get().search) {
var searchedBookmarks = {
total: 0,
matching: []
};
searchedBookmarks.total = bookmarks.get().length;
bookmarks.get().forEach(function(arrayItem, index) {
if (arrayItem.url != null) {
if (arrayItem.url.replace(/^https?\:\/\//i, "").replace(/\/$/, "").toLowerCase().includes(searchInput.value.toLowerCase().replace(/\s/g, ""))) {
var bookmarkDataCopy = JSON.parse(JSON.stringify(arrayItem));
searchedBookmarks.matching.push(bookmarkDataCopy);
};
} else if (arrayItem.name != null) {
if (arrayItem.name.toLowerCase().includes(searchInput.value.toLowerCase().replace(/\s/g, ""))) {
var bookmarkDataCopy = JSON.parse(JSON.stringify(arrayItem));
searchedBookmarks.matching.push(bookmarkDataCopy);
};
};
});
return searchedBookmarks;
};
};
var render = {
engine: function() {
_renderEngine();
},
clear: {
input: function() {
_renderClearInput();
},
button: function() {
_renderClearButton();
}
}
};
var _renderEngine = function() {
var search = helper.e(".search");
var searchInput = helper.e(".search-input");
var placeholder = "";
if (state.get().link.show) {
placeholder = "Find bookmarks or search";
} else {
placeholder = "Search";
};
placeholder = placeholder + " " + state.get().header.search.engine[state.get().header.search.engine.selected].name;
searchInput.setAttribute("placeholder", placeholder);
search.setAttribute("action", state.get().header.search.engine[state.get().header.search.engine.selected].url);
};
var _renderClearButton = function() {
var searchClear = helper.e(".search-clear");
if (state.get().search) {
searchClear.removeAttribute("disabled");
} else {
searchClear.setAttribute("disabled", "");
};
};
var _renderClearInput = function() {
var searchInput = helper.e(".search-input");
searchInput.value = "";
searchInput.focus();
};
var _focus = function() {
if (state.get().header.search.focus) {
window.addEventListener("load", function(event) {
helper.e(".search-input").focus();
});
};
};
var init = function() {
bind();
render.engine();
_toggle();
_focus();
};
// exposed methods
return {
init: init,
get: get,
render: render
};
})();