var vexInstance;
const query = {...deserialize(location.search)};
const place = {
currentFood: null,
init: function() {
fetchPlaceApi().then(response => {
this.foodItems = response.place.food;
this.filtered = response.place.food;
this.categories = response.place.category
delete response['food'];
this.place = response.place;
this.foodWrapper = document.querySelector('#food-container');
document.getElementById('place-details').innerHTML = detailsTemplate(this.place);
this.filterInput = document.querySelector('.filter-food')
this.clearFilterText = document.querySelector('.clear-text')
// document.getElementById('hero-name').innerHTML = this.place.name;
this.loadItems();
this.setUpListeners();
});
},
setUpListeners: function() {
document.addEventListener('click', e => {
if(e.target.matches('.open-comments')) {
this.getComments(e)
}
})
document.querySelector('.filter-food').addEventListener('keyup', this.filterItems.bind(this))
},
loadItems: function() {
this.foodWrapper.innerHTML = this.filtered ? this.filtered.sort(compare).map((food, i) => foodTemplate(food, i, this.place.slug)).join("") : "";
},
filterItems: function(e) {
const inp = e.target.value;
const newItems = [...this.foodItems].filter(item => item.name.toLowerCase().includes(inp.toLowerCase()));
this.filtered = newItems;
this.loadItems();
},
getComments: function(e) {
e.preventDefault();
this.fetchPage(`/reviews/${this.place.slug}`);
},
fetchPage: async function(link) {
var temp = await doFetch(link, {
redirect: 'follow',
headers: {
'Content-Type': 'text/plain',
'X-Requested-With': 'XMLHttpRequest'
}
});
load.style.display = "none";
vexInstance = vex.open({
unsafeContent: temp.payload,
showCloseButton: false
});
document.addEventListener('submit', this.post)
}
}
document.addEventListener('click', function(e){
if(e.target.matches('.go-back')){
const prev = document.referrer;
if(prev.includes('cravvings.com')){
window.history.back();
}else {
window.location.href = "/search";
}
}
});
const compare = (a, b) => {
if (query.q) {
if (a.name.toLowerCase().includes(query.q.toLowerCase())) {
return -1;
} else {
return 1;
}
}
}
place.init();
const phones = (phone) => `
${phone}
`
document.addEventListener('click', function(e){
if(vex && vexInstance && e.target.matches('.close-modal')){
vex.close(vexInstance)
}
})
const deliverys = (delivery) => {
if(delivery && delivery.length){
return `
${delivery.map(area => area).join(", ")}
`
}
return '';
}
const takeaway = (take) => {
return take ? `Pickup:
` : ''
}
function placeRating(rating){
return `
Rating ${starred(rating)}
`
}
function getMapUrl(place) {
const [long, lat] = place.coords;
return `https://www.google.com/maps/search/${plusEncode(place.name)}/@${lat},${long},15z`;
}
function detailsTemplate(place) {
let placeImage = place.image && place.image.length ? place.image[0] : 'https%3A%2F%2Fcravvings.com%2Fimages%2Fcard_1.jpg';
const twitter = `https://twitter.com/share?url=https%3A%2F%2Fcravvings.com%2Fplaces%2F${place.slug}&text=See%20${place.name}%20on%20Cravvings`;
const whatsapp = `https://wa.me/?text=See%20${place.name}%20on%20Cravvings%20via%20https%3A%2F%2Fcravvings.com/places/${place.slug}`;
const facebook = `https://www.facebook.com/dialog/feed?&app_id=875457879311564&link=https%3A%2F%2Fcravvings.com%2Fplaces%2F${place.slug}&display=popup"e=${place.name}%20on%20Cravvings&hashtag=#restaurantsinlagos`;
return `
`;
}
String.prototype.indexOfEnd = function(string) {
var io = this.indexOf(string);
return io == -1 ? -1 : io + string.length;
}
const starred = (avgRating) => {
let rat = "";
const avg = avgRating && avgRating.rate ? avgRating.rate : avgRating;
for (var i = 0; i < Math.floor(avg); i++) {
rat += ``;
}
return rat;
};
// console.log(capitalize("Rice and Beans"));
function foodTemplate(food, i, place_slug) {
food.name = capitalize(food.name);
// const avgRating = food.rating.rate ? ratings(food.rating) : null;
let edited, editedDesc;
if (query.q && food.name.toLowerCase().includes(query.q.toLowerCase())) {
edited = highlight(food.name, query.q);
}
if (query.q && food.description.toLowerCase().includes(query.q.toLowerCase())) {
editedDesc = highlight(food.description, query.q);
}else {
editedDesc = food.description;
}
var liked, color;
// if ($user !== "0" && food.likes && food.likes.length) {
// liked = food.likes.includes($user);
// color = liked ? '#C32022' : '#ccc';
// }else color = '#ccc';
//
//
//
function highlight(text, term) {
// const end = text.indexOf(term.substring(term.length - 1)) + 1;
const end = text.toLowerCase().indexOfEnd(term.toLowerCase());
const start = text.toLowerCase().indexOf(term.toLowerCase());
const toBeHighlighted = text.substring(start, end);
const highlighted = `${toBeHighlighted}`;
return text.replace(toBeHighlighted, highlighted);
}
let img = food.img ?
`` : '';
let truncated, desc = '';
if (editedDesc) {
truncated = editedDesc.slice(0, 70);
desc = `${truncated}${editedDesc > truncated ? '...' : ''}`
}
return `
`;
}
const review = (placeReviews) => {
const { rating, name } = placeReviews;
const revs = rating.length === 1 ? 'review' : 'reviews';
return ``;
}
document.addEventListener('click', function(e){
if(e.target.matches('.nav-tabs .nav-link')) {
e.preventDefault()
document.querySelector('.nav-link.active').classList.remove('active');
document.querySelector('.show').classList.remove('show', 'active');
e.target.classList.add('active')
const ges = e.target.getAttribute('href').split('#')[1];
document.querySelector(`#${ges}`).classList.add('show', 'active')
}
})