diff --git a/assets/js/app.js b/assets/js/app.js index 322fb3fa..ce425388 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -7,30 +7,20 @@ global.MonsieurBizInstantSearch = class { keyUpTimeOut, minQueryLength ) { + let self = this; // Init a timeout variable to be used below var instantSearchTimeout = null; const searchInput = document.querySelector(searchInputSelector); if (!searchInput) { return; } + self.searchCalled = false; searchInput.addEventListener('keyup', function (e) { clearTimeout(instantSearchTimeout); var query = e.currentTarget.value; var resultElement = e.currentTarget.closest(resultClosestSelector).querySelector(resultFindSelector); instantSearchTimeout = setTimeout(function () { - if (query.length >= minQueryLength) { - var httpRequest = new XMLHttpRequest(); - httpRequest.onload = function () { - if (this.status === 200) { - resultElement.innerHTML = this.responseText; - resultElement.style.display = 'block'; - } - }; - httpRequest.open("POST", instantUrl); - httpRequest.setRequestHeader("X-Requested-With", "XMLHttpRequest"); - httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - httpRequest.send(new URLSearchParams({query: query}).toString()); - } + self.callSearch(query, minQueryLength, instantUrl, resultElement); }, keyUpTimeOut); }); @@ -45,12 +35,32 @@ global.MonsieurBizInstantSearch = class { searchInput.addEventListener('focus', function (e) { var query = e.currentTarget.value; - if (query !== '') { + if (query !== '' && !self.searchCalled) { + const resultElement = searchForm.querySelector(resultFindSelector); + self.callSearch(query, minQueryLength, instantUrl, resultElement); + self.searchCalled = true; + } else if (query !== '') { const resultElement = searchForm.querySelector(resultFindSelector); resultElement.style.display = 'block'; } }); } + + callSearch (query, minQueryLength, instantUrl, resultElement) { + if (query.length >= minQueryLength) { + var httpRequest = new XMLHttpRequest(); + httpRequest.onload = function () { + if (this.status === 200) { + resultElement.innerHTML = this.responseText; + resultElement.style.display = 'block'; + } + }; + httpRequest.open("POST", instantUrl); + httpRequest.setRequestHeader("X-Requested-With", "XMLHttpRequest"); + httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + httpRequest.send(new URLSearchParams({ query: query }).toString()); + } + } } document.addEventListener("DOMContentLoaded", function() { diff --git a/src/Resources/public/entrypoints.json b/src/Resources/public/entrypoints.json index 1a1cc0cb..c587a109 100644 --- a/src/Resources/public/entrypoints.json +++ b/src/Resources/public/entrypoints.json @@ -2,7 +2,7 @@ "entrypoints": { "monsieurbiz-search": { "js": [ - "/bundles/monsieurbizsyliussearchplugin/js/monsieurbiz-search.d29ecf2c.js" + "/bundles/monsieurbizsyliussearchplugin/js/monsieurbiz-search.bd704c68.js" ] } } diff --git a/src/Resources/public/js/monsieurbiz-search.bd704c68.js b/src/Resources/public/js/monsieurbiz-search.bd704c68.js new file mode 100644 index 00000000..57c17717 --- /dev/null +++ b/src/Resources/public/js/monsieurbiz-search.bd704c68.js @@ -0,0 +1 @@ +!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="/bundles/monsieurbizsyliussearchplugin/",n(n.s="ng4s")}({"2mad":function(e,t){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(e){"object"==typeof window&&(n=window)}e.exports=n},ng4s:function(e,t,n){(function(e){function t(e){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function n(e,n){for(var r=0;r=t){var o=new XMLHttpRequest;o.onload=function(){200===this.status&&(r.innerHTML=this.responseText,r.style.display="block")},o.open("POST",n),o.setRequestHeader("X-Requested-With","XMLHttpRequest"),o.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),o.send(new URLSearchParams({query:e}).toString())}}}])&&n(t.prototype,r),o&&n(t,o),Object.defineProperty(t,"prototype",{writable:!1}),e}(),document.addEventListener("DOMContentLoaded",(function(){new MonsieurBizInstantSearch(monsieurbizSearchPlugin.instantUrl,monsieurbizSearchPlugin.searchInputSelector,monsieurbizSearchPlugin.resultClosestSelector,monsieurbizSearchPlugin.resultFindSelector,monsieurbizSearchPlugin.keyUpTimeOut,monsieurbizSearchPlugin.minQueryLength)}))}).call(this,n("2mad"))}}); \ No newline at end of file diff --git a/src/Resources/public/js/monsieurbiz-search.d29ecf2c.js b/src/Resources/public/js/monsieurbiz-search.d29ecf2c.js deleted file mode 100644 index c5baa0d6..00000000 --- a/src/Resources/public/js/monsieurbiz-search.d29ecf2c.js +++ /dev/null @@ -1 +0,0 @@ -!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="/bundles/monsieurbizsyliussearchplugin/",n(n.s="ng4s")}({ng4s:function(e,t,n){(function(e){function t(e,t){for(var n=0;n=i){var e=new XMLHttpRequest;e.onload=function(){200===this.status&&(c.innerHTML=this.responseText,c.style.display="block")},e.open("POST",t),e.setRequestHeader("X-Requested-With","XMLHttpRequest"),e.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),e.send(new URLSearchParams({query:n}).toString())}}),u)}));var l=c.closest(r);l.addEventListener("focusout",(function(e){null!==e.relatedTarget&&l.contains(e.relatedTarget)||(l.querySelector(o).style.display="none")})),c.addEventListener("focus",(function(e){""!==e.currentTarget.value&&(l.querySelector(o).style.display="block")}))}},n&&t(e.prototype,n),r&&t(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,n,r}(),document.addEventListener("DOMContentLoaded",(function(){new MonsieurBizInstantSearch(monsieurbizSearchPlugin.instantUrl,monsieurbizSearchPlugin.searchInputSelector,monsieurbizSearchPlugin.resultClosestSelector,monsieurbizSearchPlugin.resultFindSelector,monsieurbizSearchPlugin.keyUpTimeOut,monsieurbizSearchPlugin.minQueryLength)}))}).call(this,n("yLpj"))},yLpj:function(e,t){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(e){"object"==typeof window&&(n=window)}e.exports=n}}); \ No newline at end of file diff --git a/src/Resources/public/manifest.json b/src/Resources/public/manifest.json index 51ee424f..140b0bbb 100644 --- a/src/Resources/public/manifest.json +++ b/src/Resources/public/manifest.json @@ -1,3 +1,3 @@ { - "bundles/monsieurbizsyliussearchplugin/monsieurbiz-search.js": "/bundles/monsieurbizsyliussearchplugin/js/monsieurbiz-search.d29ecf2c.js" + "bundles/monsieurbizsyliussearchplugin/monsieurbiz-search.js": "/bundles/monsieurbizsyliussearchplugin/js/monsieurbiz-search.bd704c68.js" } \ No newline at end of file