docbrown/app/javascript/listings/utils.js
Michael Kohl 7f75f99560
[deploy] Rename classified listings (#7910)
* Change models and related files

* Update controllers and specs

* More renaming

* Seek and destroy, I mean search and replace

* Round up the stragglers

* Ground control to Major Travis...

* More fixes

* PR feedback

* Various fixes

* Rename view

* Fix list query builder

* Unify request specs

* Fix some API spec errors

* Fix remaining API specs

* Make spec conform to API

* Fix leftover problems

* Fix JS tests

* Fix column name in select

* Fix API specs

* Fix search specs

* Paging Mr. Travis
2020-05-27 13:35:09 +00:00

94 lines
2.5 KiB
JavaScript

/**
* How many listings to show per page
* @constant {number}
*/
export const LISTING_PAGE_SIZE = 75;
export const MATCH_LISTING = [
'single-listing-container__inner',
'listing-filters',
'listings-modal-background',
];
export function updateListings(listings) {
const fullListings = [];
listings.forEach((listing) => {
if (listing.bumped_at) {
fullListings.push(listing);
}
});
return fullListings;
}
export function getQueryParams() {
let queryString = document.location.search;
queryString = queryString.split('+').join(' ');
const params = {};
let tokens;
const re = /[?&]?([^=]+)=([^&]*)/g;
// eslint-disable-next-line no-cond-assign
while ((tokens = re.exec(queryString))) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}
return params;
}
function resizeMasonryItem(item) {
/* Get the grid object, its row-gap, and the size of its implicit rows */
const grid = document.getElementsByClassName('listings-columns')[0];
const rowGap = parseInt(
window.getComputedStyle(grid).getPropertyValue('grid-row-gap'),
10,
);
const rowHeight = parseInt(
window.getComputedStyle(grid).getPropertyValue('grid-auto-rows'),
10,
);
const rowSpan = Math.ceil(
(item.querySelector('.listing-content').getBoundingClientRect().height +
rowGap) /
(rowHeight + rowGap),
);
/* Set the spanning as calculated above (S) */
// eslint-disable-next-line no-param-reassign
item.style.gridRowEnd = `span ${rowSpan}`;
}
export function resizeAllMasonryItems() {
// Get all item class objects in one list
const allItems = document.getElementsByClassName('single-listing');
/*
* Loop through the above list and execute the spanning function to
* each list-item (i.e. each masonry item)
*/
// eslint-disable-next-line vars-on-top
for (let i = 0; i < allItems.length; i += 1) {
resizeMasonryItem(allItems[i]);
}
}
export function getLocation({ query = '', tags = [], category = '', slug }) {
let newLocation = '';
if (slug) {
newLocation = `/listings/${category}/${slug}`;
} else if (query.length > 0 && tags.length > 0) {
newLocation = `/listings/${category}?q=${query}&t=${tags}`;
} else if (query.length > 0) {
newLocation = `/listings/${category}?q=${query}`;
} else if (tags.length > 0) {
newLocation = `/listings/${category}?t=${tags}`;
} else if (category.length > 0) {
newLocation = `/listings/${category}`;
} else {
newLocation = '/listings';
}
return newLocation;
}