nightTab/js/search.js

82 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-12-26 08:45:53 +01:00
var search = (function() {
var bind = function() {
var searchInput = helper.e(".search-input");
var searchClear = helper.e(".search-clear");
searchInput.addEventListener("input", function() {
_toggle(this);
_searchClear();
render();
2018-12-26 08:45:53 +01:00
}, false);
searchClear.addEventListener("click", function() {
_toggle(this);
_searchClear();
clear();
2018-12-26 08:45:53 +01:00
}, false);
};
var _toggle = function(input) {
if (input.value != "") {
state.get().header.search.searching = true;
} else {
state.get().header.search.searching = false;
};
};
var _searchClear = function() {
2018-12-26 08:45:53 +01:00
var searchInput = helper.e(".search-input");
var searchClear = helper.e(".search-clear");
if (state.get().header.search.searching) {
2018-12-26 18:43:38 +01:00
searchClear.removeAttribute("disabled");
2018-12-26 08:45:53 +01:00
} else {
2018-12-26 18:43:38 +01:00
searchClear.setAttribute("disabled", "");
};
2018-12-26 08:45:53 +01:00
};
var render = function() {
var searchInput = helper.e(".search-input");
if (state.get().header.search.searching) {
var searchedBookmarks = [];
bookmarks.get().forEach(function(arrayItem, index) {
if (arrayItem.url.replace(/^https?\:\/\//i, "").replace(/\/$/, "").toLowerCase().includes(searchInput.value.toLowerCase()) || arrayItem.name.toLowerCase().includes(searchInput.value.toLowerCase())) {
2018-12-28 14:21:16 +01:00
var copy = JSON.parse(JSON.stringify(arrayItem));
copy.index = index;
searchedBookmarks.push(copy);
};
});
link.clear();
link.render(searchedBookmarks);
} else {
link.clear();
link.render();
};
2018-12-26 08:45:53 +01:00
};
var update = function() {
var search = helper.e(".search");
search.setAttribute("action", state.get().header.search.engine[state.get().header.search.engine.selected].url);
};
var clear = function() {
var searchInput = helper.e(".search-input");
searchInput.value = "";
searchInput.focus();
link.clear();
link.render();
};
2018-12-26 08:45:53 +01:00
var init = function() {
bind();
update();
2018-12-26 08:45:53 +01:00
};
// exposed methods
return {
init: init,
render: render,
update: update,
2018-12-26 08:45:53 +01:00
clear: clear
};
})();