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() {
|
2019-01-03 23:30:27 +01:00
|
|
|
_toggle(this);
|
|
|
|
_searchClear();
|
2019-01-13 23:54:48 +01:00
|
|
|
link.clear();
|
|
|
|
link.render();
|
2018-12-26 08:45:53 +01:00
|
|
|
}, false);
|
2018-12-28 08:04:22 +01:00
|
|
|
searchClear.addEventListener("click", function() {
|
2019-01-03 23:30:27 +01:00
|
|
|
_toggle(this);
|
|
|
|
_searchClear();
|
2018-12-28 08:04:22 +01:00
|
|
|
clear();
|
2018-12-26 08:45:53 +01:00
|
|
|
}, false);
|
|
|
|
};
|
|
|
|
|
2019-01-03 23:30:27 +01:00
|
|
|
var _toggle = function(input) {
|
2018-12-28 08:04:22 +01:00
|
|
|
if (input.value != "") {
|
2019-01-13 23:54:48 +01:00
|
|
|
state.change({
|
|
|
|
path: "header.search.searching",
|
|
|
|
value: true
|
|
|
|
})
|
2018-12-28 08:04:22 +01:00
|
|
|
} else {
|
2019-01-13 23:54:48 +01:00
|
|
|
state.change({
|
|
|
|
path: "header.search.searching",
|
|
|
|
value: false
|
|
|
|
})
|
2018-12-28 08:04:22 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-01-03 23:30:27 +01:00
|
|
|
var _searchClear = function() {
|
2018-12-26 08:45:53 +01:00
|
|
|
var searchInput = helper.e(".search-input");
|
|
|
|
var searchClear = helper.e(".search-clear");
|
2019-01-03 23:30:27 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-01-13 23:54:48 +01:00
|
|
|
var get = function() {
|
2018-12-28 08:04:22 +01:00
|
|
|
var searchInput = helper.e(".search-input");
|
2019-01-03 23:30:27 +01:00
|
|
|
if (state.get().header.search.searching) {
|
2019-01-13 23:54:48 +01:00
|
|
|
var searchedBookmarks = {
|
|
|
|
total: 0,
|
|
|
|
matching: []
|
|
|
|
};
|
|
|
|
searchedBookmarks.total = bookmarks.get().length;
|
2018-12-28 08:04:22 +01:00
|
|
|
bookmarks.get().forEach(function(arrayItem, index) {
|
2019-01-13 23:54:48 +01:00
|
|
|
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);
|
2018-12-28 08:04:22 +01:00
|
|
|
};
|
|
|
|
});
|
2019-01-13 23:54:48 +01:00
|
|
|
return searchedBookmarks;
|
2018-12-28 08:04:22 +01:00
|
|
|
};
|
2018-12-26 08:45:53 +01:00
|
|
|
};
|
|
|
|
|
2019-01-03 23:30:27 +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);
|
|
|
|
};
|
|
|
|
|
2018-12-28 08:04:22 +01:00
|
|
|
var clear = function() {
|
|
|
|
var searchInput = helper.e(".search-input");
|
|
|
|
searchInput.value = "";
|
|
|
|
searchInput.focus();
|
2019-01-03 23:30:27 +01:00
|
|
|
link.clear();
|
|
|
|
link.render();
|
2018-12-28 08:04:22 +01:00
|
|
|
};
|
2018-12-26 08:45:53 +01:00
|
|
|
|
|
|
|
var init = function() {
|
|
|
|
bind();
|
2019-01-03 23:30:27 +01:00
|
|
|
update();
|
2018-12-26 08:45:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// exposed methods
|
|
|
|
return {
|
|
|
|
init: init,
|
2019-01-13 23:54:48 +01:00
|
|
|
get: get,
|
2019-01-03 23:30:27 +01:00
|
|
|
update: update,
|
2018-12-26 08:45:53 +01:00
|
|
|
clear: clear
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|