Initial commit: is_searchbar out of the box. v3.0.1
This commit is contained in:
4
is_searchbar/_theme_dev/.eslintignore
Normal file
4
is_searchbar/_theme_dev/.eslintignore
Normal file
@ -0,0 +1,4 @@
|
||||
/node_modules/
|
||||
/webpack/
|
||||
postcss.config.js
|
||||
webpack.config.js
|
||||
27
is_searchbar/_theme_dev/.eslintrc.js
Normal file
27
is_searchbar/_theme_dev/.eslintrc.js
Normal file
@ -0,0 +1,27 @@
|
||||
module.exports = {
|
||||
root: true,
|
||||
env: {
|
||||
browser: true,
|
||||
node: false,
|
||||
es6: true,
|
||||
jquery: true,
|
||||
},
|
||||
globals: {
|
||||
google: true,
|
||||
document: true,
|
||||
navigator: false,
|
||||
window: true,
|
||||
prestashop: true,
|
||||
},
|
||||
extends: ['airbnb-base'],
|
||||
rules: {
|
||||
'max-len': ['error', {code: 140}],
|
||||
'no-underscore-dangle': 'off',
|
||||
'no-restricted-syntax': 'off',
|
||||
'no-param-reassign': 'off',
|
||||
'import/no-unresolved': 'off',
|
||||
},
|
||||
parserOptions: {
|
||||
ecmaVersion: 2022
|
||||
},
|
||||
}
|
||||
1
is_searchbar/_theme_dev/.nvmrc
Normal file
1
is_searchbar/_theme_dev/.nvmrc
Normal file
@ -0,0 +1 @@
|
||||
v18.12.1
|
||||
6
is_searchbar/_theme_dev/.stylelintrc.json
Normal file
6
is_searchbar/_theme_dev/.stylelintrc.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "stylelint-config-recommended-scss",
|
||||
"rules": {
|
||||
"scss/at-extend-no-missing-placeholder": null
|
||||
}
|
||||
}
|
||||
11
is_searchbar/_theme_dev/index.php
Normal file
11
is_searchbar/_theme_dev/index.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
21
is_searchbar/_theme_dev/package.json
Normal file
21
is_searchbar/_theme_dev/package.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "is_searchbar",
|
||||
"label": "Searchbar module",
|
||||
"version": "3.0.0",
|
||||
"description": "Searchbar module",
|
||||
"author": "Igor Stępień",
|
||||
"scripts": {
|
||||
"scss-lint": "stylelint \"**/*.scss\" --fix",
|
||||
"scss-lint-fix": "stylelint \"**/*.scss\" --fix",
|
||||
"js-lint": "eslint -c .eslintrc.js --ext .js,.vue ./src",
|
||||
"js-lint-fix": "eslint -c .eslintrc.js --ext .js,.vue ./src --fix"
|
||||
},
|
||||
"devDependencies": {
|
||||
"stylelint": "^14.11.0",
|
||||
"stylelint-config-recommended-scss": "^7.0.0",
|
||||
"eslint": "^8.23.0",
|
||||
"eslint-config-airbnb-base": "^15.0.0",
|
||||
"eslint-plugin-import": "^2.26.0",
|
||||
"postcss": "^8.3.6"
|
||||
}
|
||||
}
|
||||
11
is_searchbar/_theme_dev/src/index.php
Normal file
11
is_searchbar/_theme_dev/src/index.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
11
is_searchbar/_theme_dev/src/js/index.php
Normal file
11
is_searchbar/_theme_dev/src/js/index.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
128
is_searchbar/_theme_dev/src/js/theme/components/SearchInput.js
Normal file
128
is_searchbar/_theme_dev/src/js/theme/components/SearchInput.js
Normal file
@ -0,0 +1,128 @@
|
||||
function SearchInput({
|
||||
searchUrl,
|
||||
input,
|
||||
onType,
|
||||
onResult,
|
||||
beforeSend,
|
||||
onRemoveResult,
|
||||
perPage,
|
||||
appendTo,
|
||||
min,
|
||||
timeout,
|
||||
}) {
|
||||
this.searchUrl = searchUrl;
|
||||
this.input = input;
|
||||
this.appendTo = document.querySelector(appendTo);
|
||||
this.onType = onType || (() => {});
|
||||
this.onResult = onResult || (() => {});
|
||||
this.onRemoveResult = onRemoveResult || (() => {});
|
||||
this.beforeSend = beforeSend || (() => {});
|
||||
this.min = min || 3;
|
||||
this.perPage = perPage || 10;
|
||||
this.timeout = timeout || 300;
|
||||
this.resultBox = null;
|
||||
|
||||
const cache = {};
|
||||
|
||||
let typeTimeout = null;
|
||||
const resultBoxClass = 'js-search-result';
|
||||
|
||||
const getInputString = () => this.input.value;
|
||||
|
||||
const handleResultIfStringMatchMinLength = (str) => str.length >= this.min;
|
||||
|
||||
const resetResultIfExits = () => {
|
||||
if (this.resultBox) {
|
||||
this.onRemoveResult();
|
||||
this.resultBox.remove();
|
||||
}
|
||||
};
|
||||
|
||||
const displayResult = (data, str) => {
|
||||
resetResultIfExits();
|
||||
|
||||
const element = document.createElement('div');
|
||||
element.classList.add(resultBoxClass);
|
||||
element.innerHTML = data.content;
|
||||
|
||||
this.appendTo.appendChild(element);
|
||||
this.resultBox = document.querySelector(`.${resultBoxClass}`);
|
||||
|
||||
this.onResult({
|
||||
input: this.input,
|
||||
appendTo: this.appendTo,
|
||||
s: str,
|
||||
data,
|
||||
});
|
||||
};
|
||||
|
||||
const handleAjax = (str) => {
|
||||
this.beforeSend({
|
||||
input: this.input,
|
||||
appendTo: this.appendTo,
|
||||
s: str,
|
||||
});
|
||||
|
||||
if (typeof cache[str] !== 'undefined') {
|
||||
displayResult(cache[str], str);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
let data = {
|
||||
s: str,
|
||||
perPage: this.perPage,
|
||||
ajax: 1,
|
||||
};
|
||||
|
||||
data = Object.keys(data).map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`).join('&');
|
||||
|
||||
fetch(this.searchUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: data,
|
||||
})
|
||||
/// ISSUE WITH RESPONSE.JSON() AND HTML CONTENT INSIDE JSON
|
||||
.then((response) => response.text())
|
||||
.then((responseData) => {
|
||||
responseData = JSON.parse(responseData);
|
||||
cache[str] = responseData;
|
||||
|
||||
displayResult(responseData, str);
|
||||
})
|
||||
.catch((err) => console.error(err)); // eslint-disable-line
|
||||
};
|
||||
|
||||
this.removeResults = () => {
|
||||
resetResultIfExits();
|
||||
};
|
||||
|
||||
this.input.addEventListener('keyup', () => {
|
||||
if (typeTimeout) {
|
||||
clearTimeout(typeTimeout);
|
||||
}
|
||||
|
||||
const str = getInputString();
|
||||
|
||||
this.onType({
|
||||
input: this.input,
|
||||
appendTo: this.appendTo,
|
||||
s: str,
|
||||
});
|
||||
|
||||
if (!handleResultIfStringMatchMinLength(str)) {
|
||||
resetResultIfExits();
|
||||
return;
|
||||
}
|
||||
|
||||
typeTimeout = setTimeout(() => {
|
||||
handleAjax(str);
|
||||
}, this.timeout);
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
export default SearchInput;
|
||||
11
is_searchbar/_theme_dev/src/js/theme/components/index.php
Normal file
11
is_searchbar/_theme_dev/src/js/theme/components/index.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
50
is_searchbar/_theme_dev/src/js/theme/index.js
Normal file
50
is_searchbar/_theme_dev/src/js/theme/index.js
Normal file
@ -0,0 +1,50 @@
|
||||
import SearchInput from './components/SearchInput';
|
||||
|
||||
const init = () => {
|
||||
const searchInput = document.querySelector('.js-search-input');
|
||||
|
||||
if (!searchInput) {
|
||||
return;
|
||||
}
|
||||
|
||||
const getAjaxUrlFromElement = (el) => (el && el.length ? el.getAttribute('data-search-controller-url') : null);
|
||||
const ajaxUrl = getAjaxUrlFromElement(document.querySelector('[data-search-controller-url]'));
|
||||
const body = document.querySelector('body');
|
||||
const inputForm = searchInput.closest('.js-search-form');
|
||||
const backdrop = document.createElement('div');
|
||||
backdrop.classList.add('search-backdrop');
|
||||
body.appendChild(backdrop);
|
||||
|
||||
if (!ajaxUrl) {
|
||||
return;
|
||||
}
|
||||
|
||||
const Search = new SearchInput({
|
||||
searchUrl: ajaxUrl,
|
||||
input: searchInput,
|
||||
appendTo: '.js-search-form',
|
||||
perPage: 6,
|
||||
onResult: () => {
|
||||
body.classList.add('search-result-open');
|
||||
prestashop.pageLazyLoad.update();
|
||||
},
|
||||
onRemoveResult: () => {
|
||||
body.classList.remove('search-result-open');
|
||||
},
|
||||
beforeSend: () => {
|
||||
// console.log('BEFORE SEND ' + e);
|
||||
},
|
||||
onType: () => {
|
||||
// console.log('ON TYPE ' + e);
|
||||
},
|
||||
});
|
||||
|
||||
body.addEventListener('click', ({ target }) => {
|
||||
if (body.classList.contains('search-result-open') && target !== inputForm && !target.closest('.js-search-form')) {
|
||||
body.classList.remove('search-result-open');
|
||||
Search.removeResults();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
document.addEventListener('DOMContentLoaded', init);
|
||||
11
is_searchbar/_theme_dev/src/js/theme/index.php
Normal file
11
is_searchbar/_theme_dev/src/js/theme/index.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
2096
is_searchbar/_theme_dev/yarn.lock
Normal file
2096
is_searchbar/_theme_dev/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user