// @jsx h
import { h } from 'dom-chef'
import classnames from 'classnames'
import { loadStyle } from '../helpers'
let alreadyRunning = false
const store = {}
const cachedFetch = (url) =>
store[url]
? new Promise((resolve) => {
resolve(store[url])
store[url] = store[url].clone()
})
: new Promise((resolve) =>
fetch(url).then((response) => {
resolve(response)
store[url] = response.clone()
})
)
const getPreviewElFor = ({
hasThumbs,
post: {
title: { rendered },
link: href,
_embedded = {},
product_price = 0,
},
}) => {
return (
{_embedded['wp:featuredmedia'] && hasThumbs && (
current.width <
currentSmallest.width
? current
: currentSmallest,
{
width: 9999999999,
}
).source_url ||
_embedded['wp:featuredmedia'][0].source_url,
}}
/>
)}
{rendered}
{product_price ? (
) : null}
)
}
export const mount = (formEl, args = {}) => {
const clickOutsideHandler = (e) => {
let mode = { mode: 'inline', ...args }.mode
if (mode === 'modal') {
return
}
if (formEl.contains(e.target)) {
return
}
fadeOutAndRemove(formEl.querySelector('.ct-search-results'))
}
const maybeEl = formEl.querySelector('input[type="search"]')
const options = {
postType: 'ct_forced_any',
// inline | modal
mode: 'inline',
perPage: 5,
...args,
}
if (!maybeEl) {
return
}
options.postType = formEl.querySelector('[name="post_type"]')
? `ct_forced_${formEl.querySelector('[name="post_type"]').value}`
: formEl.querySelector('[name="ct_post_type"]')
? `ct_forced_${formEl.querySelector('[name="ct_post_type"]').value}`
: 'ct_forced_any'
options.productPrice = formEl.querySelector('[name="ct_product_price"]')
? !!formEl.querySelector('[name="ct_product_price"]').value
: false
console.log(options)
if (!window.fetch) return
let listener = debounce((e) => {
document.removeEventListener('click', clickOutsideHandler)
document.addEventListener('click', clickOutsideHandler)
if (e.target.value.trim().length === 0) {
fadeOutAndRemove(formEl.querySelector('.ct-search-results'))
let maybeStatusEl = formEl.querySelector('[aria-live]')
if (maybeStatusEl) {
maybeStatusEl.innerHTML = ct_localizations.search_live_no_result
}
return
}
formEl.classList.add('ct-searching')
cachedFetch(
`${ct_localizations.rest_url}wp/v2/posts${
ct_localizations.rest_url.indexOf('?') > -1 ? '&' : '?'
}_embed=1&post_type=${options.postType}&per_page=${
options.perPage
}&product_price=${options.productPrice}&search=${e.target.value}`
).then((response) => {
let totalAmountOfPosts = parseInt(
response.headers.get('X-WP-Total'),
10
)
loadStyle(ct_localizations.dynamic_styles.search_lazy).then(() => {
response.json().then((posts) => {
if (alreadyRunning) {
return
}
formEl.classList.remove('ct-searching')
let itHadSearchResultsBefore =
!!formEl.querySelector('.ct-search-results')
alreadyRunning = true
let searchResults =
formEl.querySelector('.ct-search-results')
let { height: heightBeforeRemoval } = searchResults
? searchResults.getBoundingClientRect()
: 0
if (
searchResults &&
!(
e.target.value.trim().length === 0 ||
posts.length === 0
)
) {
/**
* Should just quickly replace the list
* when results are available
*/
searchResults && formEl.removeChild(searchResults)
} else {
if (
e.target.value.trim().length === 0 ||
posts.length === 0
) {
fadeOutAndRemove(searchResults)
}
}
let searchResultsCountElLabel =
ct_localizations.search_live_no_result
if (posts.length > 0 && e.target.value.trim().length > 0) {
searchResultsCountElLabel = (
posts.length > 1
? ct_localizations.search_live_many_results
: ct_localizations.search_live_one_result
).replace('%s', posts.length)
}
let maybeStatusEl = formEl.querySelector('[aria-live]')
if (maybeStatusEl) {
maybeStatusEl.innerHTML = searchResultsCountElLabel
}
if (posts.length > 0 && e.target.value.trim().length > 0) {
let searchResultsEl = (