", {
class: "ui-menu-item",
"data-value": item.value
});
var divWrapper = $("", {
class: "ui-menu-item-wrapper"
});
li.append(divWrapper);
divWrapper.append(proposalIcon).append(proposalLabel);
if (removeButton != null) {
divWrapper.append(removeButton);
}
// If a search suggestion is chosen the form is submitted
li.find(".ui-menu-item-wrapper").on("click", function (event) {
$("#textToSearch").val($(this).find(".search-autocomplete-proposal-label").attr('data-value'));
$("#searchForm").submit();
});
return li.appendTo(ul);
};
$(window).resize(function () {
var autocompleteObj = $("#textToSearch").autocomplete("instance");
autocompleteObj.search();
});
}
});
/**
* Remove from local storage a history item.
*
* @param hi The renderer element.
* @returns {boolean}
*/
function removeHistoryItem(hi) {
var historyItem = hi.getAttribute("data-value");
var removed = searchHistory.removeSearchHistoryItem(historyItem);
// Change label
if (removed) {
$(hi).attr("class", "oxy-icon oxy-icon-ok");
$(hi).parents("div").find(".search-autocomplete-proposal-label").addClass("removed-from-history");
}
// Do not close the menu
event.preventDefault();
event.stopPropagation();
return false;
}
/**
* Compute search proposals from history items.
*
* @param searchQuery The search query.
* @returns {Array} The array with search proposals.
*/
function getHistoryProposals(searchQuery) {
var toRet = [];
var historyItems = searchHistory.getHistorySearchItems();
if (historyItems != null) {
var words = searchQuery.split(" ");
for (var i = 0; i < historyItems.length; i++) {
/*console.log("History item", historyItems[i]);*/
// Test if history item match the serch query
if (matchSearchHistoryItem(historyItems[i], words)) {
var hp = {
label: historyItems[i],
value: historyItems[i],
type: "history"
};
toRet.push(hp);
} else {
/*console.log("History item does not match...");*/
}
}
}
return toRet;
}
/**
* Test if a history item match all words from search query.
*
* @param historyPhrase The history phrase.
* @param words The list with words from search query.
* @returns {boolean} True if history item matches the search words.
*/
function matchSearchHistoryItem(historyPhrase, words) {
// Iterate over words
var historyWords = historyPhrase.split(" ");
var allWordsMatch = true;
for (var wi = 0; wi < words.length && allWordsMatch; wi++) {
var cw = words[wi].trim();
if (cw.length > 0) {
// Iterate over keywords to find the ones that contains the word
var wordFound = false;
for (var i = 0; i < historyWords.length; i++) {
if (historyWords[i].toLowerCase().indexOf(cw.toLowerCase()) == 0) {
wordFound = true;
break;
}
}
allWordsMatch = allWordsMatch && wordFound;
}
}
return allWordsMatch;
}
});