nightTab/js/search.js

111 lines
3.0 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();
link.clear();
link.render.link();
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) {
2019-04-04 17:52:29 +02:00
var html = helper.e("html");
if (input.value != "") {
helper.setObject({
object: state.get(),
path: "search",
newValue: true
});
2019-04-04 17:52:29 +02:00
helper.addClass(html, "is-header-searching");
} else {
helper.setObject({
object: state.get(),
path: "search",
newValue: false
});
2019-04-04 17:52:29 +02:00
helper.removeClass(html, "is-header-searching");
};
};
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().search) {
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 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.replace(/^https?\:\/\//i, "").replace(/\/$/, "").toLowerCase().includes(searchInput.value.toLowerCase().replace(/\s/g, "")) || arrayItem.name.toLowerCase().includes(searchInput.value.toLowerCase().replace(/\s/g, ""))) {
var bookmarkDataCopy = JSON.parse(JSON.stringify(arrayItem));
searchedBookmarks.matching.push(bookmarkDataCopy);
};
});
return searchedBookmarks;
};
2018-12-26 08:45:53 +01:00
};
var render = 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 clear = function() {
var searchInput = helper.e(".search-input");
searchInput.value = "";
searchInput.focus();
link.clear();
link.render.link();
};
2018-12-26 08:45:53 +01:00
var _focus = function() {
if (state.get().header.search.focus) {
window.addEventListener("load", function(event) {
helper.e(".search-input").focus();
});
};
};
2018-12-26 08:45:53 +01:00
var init = function() {
bind();
render();
_focus();
2018-12-26 08:45:53 +01:00
};
// exposed methods
return {
init: init,
get: get,
render: render,
2018-12-26 08:45:53 +01:00
clear: clear
};
})();