modules-search-miyu/js/00-functions.js

41 lines
1.1 KiB
JavaScript

// helper function to be able to change the event listener
function updateState() {
states.search = this.value;
}
// checks if the pressed key equals to return key
function updateStateOnReturnKey(e) {
if (e.keyCode === 13) {
updateState.call(this);
}
}
// removes the old event listener and applies the new one
function changeEvent(newEvent) {
// remove the old listener
switch (states.event) {
case "onenterkey":
this.removeEventListener("keyup", updateStateOnReturnKey);
break;
default:
this.removeEventListener(states.event, updateState);
break;
}
states.event = newEvent;
// add the new listener
switch (newEvent) {
case "onenterkey":
this.addEventListener("keyup", updateStateOnReturnKey);
break;
default:
this.addEventListener(states.event, updateState);
break;
}
}
// updates the search field if the state does not match the value
function updateSearchInput(state) {
if (this.value !== state) {
this.value = state;
}
}