diff --git a/README.md b/README.md index 35aea5a..1eca319 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,50 @@ -# modules-search-miyu +# Lewp module "miyu" -A simple search form that provides the registration of callbacks when the search field changes. \ No newline at end of file +## Description +A simple search form that provides the registration of callbacks when the search field changes. In addition to that, it is possible to change the listener event and undo to the previous state. + +## Installation +In the terminal, go either to the global module or site module level of your hierarchy. +Then invoke the following command. +```bash +git clone git@gitlab.com:lewp/modules-search-miyu.git modules/search/miyu +``` + +## Dependencies + +The module depends on MoViS, which can be found [here](https://gitlab.com/emirror-de/movis). +Clone this repository into your global or site level lib/js folder: +```bash +git clone git@gitlab.com:emirror-de/movis.git lib/js/movis +``` + +## Configuration +In your page file, add the following command to add the module to your page. +```php +$this->addModule( + "search_miyu", + [ + "button_content_node" => $this->createTextNode('Submit'), + "searchinput_placeholder" => 'Input your search term' + ] +); +``` + +## Available parameters + +| Key | Default value | Example value | Description | +| ---- | ---- | ---- |---- | +| button_content_node | $this->createTextNode('Submit') | | Can be any DOMNode that you want to use. | +| searchinput_placeholder | | "Enter your search..." | The value of the placeholder property of the input field. | + +## Javascript API + +The following JavaScript functions are available. By default, all registered listeners are triggered on the keyup event of the input element. + +| Member name | Parameter | Description | +| ---- | ---- | ---- | +| addStateListener | callback | You can register a callback function that is triggered on the event that is registered on the input element. | +removeStateListener | callback | Provides the ability to remove a function that has previously been added. | +| getState | | Returns the current value of the input element. | +| undoState | | Restores the last value of the input element and triggers all registered state listener functions. | +| changeEventListener | newEvent | Provides the ability to use another event than "keyup" to trigger the listener functions. | \ No newline at end of file diff --git a/etc/config.php b/etc/config.php new file mode 100644 index 0000000..4a29275 --- /dev/null +++ b/etc/config.php @@ -0,0 +1,10 @@ + "form", + "dependencies" => [ + "javascript" => [ + "movis_js_movis" + ] + ] +]; diff --git a/js/00-functions.js b/js/00-functions.js new file mode 100644 index 0000000..3ac43a0 --- /dev/null +++ b/js/00-functions.js @@ -0,0 +1,41 @@ +// 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; + } +} diff --git a/js/01-init.js b/js/01-init.js new file mode 100644 index 0000000..d9e1d68 --- /dev/null +++ b/js/01-init.js @@ -0,0 +1,23 @@ +let states = new Movis(); +// add search state +states.addState("search", ""); +states.addState("event", "keyup"); + +let searchInput = self.querySelector('input[type="text"]'); +// change state when the search field changes +searchInput.addEventListener( + states.event, + updateState +); + +// the following function updates the input field when the state has been undone +// manually +states.addStateListener("search", (state) => { + updateSearchInput.call(searchInput, state); +}); + +// add button behavior +let submitButton = self.querySelector('button'); +submitButton.addEventListener("click", () => { + updateState.call(searchInput) +}); diff --git a/js/99-return.js b/js/99-return.js new file mode 100644 index 0000000..da95bcf --- /dev/null +++ b/js/99-return.js @@ -0,0 +1,17 @@ +return { + "addStateListener": (callback) => { + states.addStateListener("search", callback); + }, + "removeStateListener": (callback) => { + states.removeStateListener("search", callback); + }, + "getState": () => { + return states.search; + }, + "undoState": () => { + states.undoState("search"); + }, + "changeEventListener": (newEvent) => { + changeEvent.call(searchInput, newEvent); + } +}; diff --git a/module.php b/module.php new file mode 100644 index 0000000..36ffbe3 --- /dev/null +++ b/module.php @@ -0,0 +1,60 @@ +createAndSetupElement( + 'input', + '', + [ + "type" => "text", + "placeholder" => $placeholder + ] + ); + } + + private function createButton($content_node = null) + { + $button = $this->createAndSetupElement( + 'button', + '', + [ + "type" => "button" + ] + ); + + if (isset($content_node)) { + $button->appendChild($this->importNode($content_node, true)); + } else { + $button->appendChild($this->createTextNode('Submit')); + } + + return $button; + } + + public function run(array $options = []) : bool + { + $options += [ + self::KEY_OPTIONS_SEARCHINPUT_PLACEHOLDER => "", + self::KEY_OPTIONS_BUTTON_CONTENT_NODE => null + ]; + + $this->appendChild( + $this->createSearchInput( + $options[self::KEY_OPTIONS_SEARCHINPUT_PLACEHOLDER] + ) + ); + + $this->appendChild( + $this->createButton( + $options[self::KEY_OPTIONS_BUTTON_CONTENT_NODE] + ) + ); + return true; + } +};