File: /www/wwwroot/biographybirthday.com/wp-content/themes/bio/js/main.js
var REQUEST_DEBOUNCE_TIME_MS = 250
var debounce = function(func, delay) {
var inDebounce
return function() {
var context = this
var args = arguments
clearTimeout(inDebounce)
inDebounce = setTimeout(function() {
return func.apply(context, args)
}, delay)
}
}
var request = debounce(function(method, url, success, data) {
if (!method) method = 'GET'
if (!url) return
var xhttp = new XMLHttpRequest()
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
success && success(this.responseText)
}
}
xhttp.open(method, url, true)
if (method === 'POST') {
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
}
xhttp.send(serialize(data))
}, REQUEST_DEBOUNCE_TIME_MS)
function serialize(obj) {
var str = []
for (var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]))
}
return str.join('&')
}
function getCookie(cn) {
var name = cn + '='
var allCookie = decodeURIComponent(document.cookie).split(';')
var cval = []
for (var i = 0; i < allCookie.length; i++) {
if (allCookie[i].trim().indexOf(name) == 0) {
cval = allCookie[i].trim().split('=')
}
}
return cval.length > 0 ? cval[1] : ''
};
(function() {
;
(function() {
var $searchbars = document.querySelectorAll('input.search-bar')
var $autocompleteSuggestions = document.getElementById('autocomplete-suggestions')
var cache = {}
var $currentSearchbar = $searchbars[0]
var currentSelection = -1
for (var i = 0; i < $searchbars.length; i++) {
var $searchbar = $searchbars[i]
$searchbar.oninput = function() {
var $searchbar = this
var term = $searchbar.value
if (!term || term.length < 2) return ($autocompleteSuggestions.style.display = 'none')
if (cache[term]) return renderAutocomplete($searchbar, term, cache[term])
else
request('GET', ajaxURL + '?q=' + encodeURIComponent(term), function(response) {
renderAutocomplete($searchbar, term, response)
})
}
}
function formatResult($searchbar, suggestion, currentValue) {
var value = suggestion.value
if (!currentValue) return value
try {
var pattern = '(' + currentValue.replace(/\(/, '\\(').replace(/\)/, '\\)') + ')'
value = value.replace(new RegExp(pattern, 'i'), '<span>$1</span>')
} catch (e) {}
try {
value = value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/<(\/?span)>/g, '<$1>')
} catch (e) {}
if ((suggestion.value && suggestion.value.toLowerCase()) === (currentValue && currentValue.toLowerCase()))
selectPerson($searchbar, currentValue)
return value
}
function selectPerson($currSearchbar, value) {
for (var i = 0; i < $searchbars.length; i++) {
var $searchbar = $searchbars[i]
$searchbar.value = value
}
document.getElementById('fb_search').submit()
}
function renderAutocomplete($searchbar, term, response) {
var hasSuggestions = false
var suggestions = JSON.parse(response)
currentSelection = -1
cache[term] = response
$autocompleteSuggestions.innerHTML = ''
for (var index in suggestions) {
hasSuggestions = true
var child = document.createElement('div')
child.classList = ['autocomplete-suggestion']
child.innerHTML = formatResult($searchbar, suggestions[index], term)
child.onclick = function() {
selectPerson($searchbar, this.innerText)
}
if (child.dataset) child.dataset.index = index
$autocompleteSuggestions.appendChild(child)
}
if (hasSuggestions) {
$autocompleteSuggestions.style.display = 'block'
$autocompleteSuggestions.style.zIndex = 10000
if ($searchbar === $searchbars[0]) {
$autocompleteSuggestions.style.position = 'fixed'
} else {
$autocompleteSuggestions.style.position = 'absolute'
}
$currentSearchbar = $searchbar
updateAutocompleteLocation()
} else {
$autocompleteSuggestions.style.display = 'none'
}
}
function updateAutocompleteLocation() {
var rect = $currentSearchbar && $currentSearchbar.getBoundingClientRect()
if (!rect) return
$autocompleteSuggestions.style.top = rect.bottom + ($currentSearchbar === $searchbars[0] ? -2 : pageYOffset) + 'px'
$autocompleteSuggestions.style.left = rect.left + 'px'
$autocompleteSuggestions.style.width = rect.width + 'px'
}
window.addEventListener('resize', updateAutocompleteLocation, {
passive: true
})
window.addEventListener('scroll', updateAutocompleteLocation, {
passive: true
})
$currentSearchbar.addEventListener('keydown', function(e) {
var children = $autocompleteSuggestions.children
var len = children.length - 1
for (var i = 0; i <= len; i++) children[i].style.removeProperty('background-color')
switch (e.code) {
case "ArrowUp":
currentSelection--
if (currentSelection < -1) currentSelection = -1
setTimeout(function() {
$currentSearchbar.selectionStart = $currentSearchbar.selectionEnd = 1e5;
}, 0);
break;
case "ArrowDown":
currentSelection++
if (currentSelection >= len) currentSelection = len
break;
case "Enter":
children[currentSelection].click()
break;
}
if (children[currentSelection]) {
children[currentSelection].style.backgroundColor = '#ddd'
}
}, {
passive: true
})
})();
(function() {
try {
var $boost = document.getElementById('btn-boost')
$boost.onclick = function(e) {
e.preventDefault()
request('POST', '/api/people/boost', function(r) {
$boost.onclick = function() {}
$boost.classList.add('boosted')
$boost.classList.add('disabled')
}, {
url: $boost.dataset.url
})
}
} catch (e) {}
try {
document.getElementById('btn-edit').onclick = function(e) {
e.preventDefault()
document.getElementById('edit-form').submit()
}
} catch (e) {}
try {
var $groupBoosts = document.getElementsByClassName('btn-group-boost')
for (var i = 0; i < $groupBoosts.length; i++) {
var $groupBoost = $groupBoosts[i]
$groupBoost.onclick = function(e) {
e.preventDefault()
request('POST', '/api/groups/boost', function(r) {
for (var ii = 0; ii < $groupBoosts.length; ii++) {
var $gb = $groupBoosts[ii]
$gb.onclick = function() {}
$gb.classList.add('boosted')
$gb.classList.add('disabled')
}
}, {
group_id: $groupBoost.dataset.group_id
})
}
}
} catch (e) {}
try {
document.getElementById('btn-group-edit').onclick = function(e) {
e.preventDefault()
document.getElementById('group-edit-form').submit()
}
} catch (e) {}
try {
document.getElementById('random-person').onclick = function(e) {
e.preventDefault()
document.getElementById('random-form').submit()
}
} catch (e) {}
try {
document.getElementById('random-person-mobile').onclick = function(e) {
e.preventDefault()
document.getElementById('random-form').submit()
}
} catch (e) {}
try {
document.getElementById('random-group-mobile').onclick = function(e) {
e.preventDefault()
document.getElementById('random-group-form').submit()
}
} catch (e) {}
})()
})()
function getParam(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}