Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions htdocs/bookmarkManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
const contentEl = document.getElementById('content')
const formEl = document.getElementById('filterform')
const queryEl = document.getElementById('query')
const dataEl = document.querySelector('[data-b]')

const page = JSON.parse(dataEl.dataset.b)

async function request(data) {
const res = await fetch('', {
Expand All @@ -11,8 +14,7 @@
credentials: 'include'
})

const text = await res.text()
return JSON.parse(text)
return await res.json()
}

async function deleteEntry(id) {
Expand Down Expand Up @@ -144,13 +146,13 @@
return false
}

document.location.href = '?filter=' + encodeURIComponent(query)
document.location.href = `?${new URLSearchParams({ filter: query })}`

return false
})

let loadingMore = false
let ifStep = window.infiniteScrolling
let ifStep = page.infiniteScrolling
let ifSkip = ifStep

async function loadMore() {
Expand All @@ -160,9 +162,12 @@

loadingMore = true

const url =
`?filter=${encodeURIComponent(window.filter)}` +
`&format=html&count=${ifStep}&skip=${ifSkip}`
const url = `?${new URLSearchParams({
filter: page.filter,
format: 'html',
count: ifStep,
skip: ifSkip,
})}`
const res = await fetch(url, { credentials: 'include' })
const text = await res.text()

Expand All @@ -175,7 +180,24 @@
loadingMore = false
}

if (window.infiniteScrolling) {
window.addEventListener('load', () => {
queryEl.value = page.add || page.filter

/* Place cursor at end of query text.
* https://stackoverflow.com/a/10576409 */
queryEl.addEventListener('focus', e => {
setTimeout(() => { queryEl.selectionStart = queryEl.selectionEnd = 10000 }, 0)
})

queryEl.focus()

if (page.add) {
/* Remove query string from URL */
history.replaceState({}, null, window.location.pathname)
}
})

if (page.infiniteScrolling) {
window.addEventListener('scroll', e => {
const offset =
document.body.offsetHeight - (window.pageYOffset + window.innerHeight)
Expand Down
68 changes: 22 additions & 46 deletions htdocs/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ function dumpEntries($entries)
}
}

$pageConfig = [
'filter' => $filter ? $filter : '',
'infiniteScrolling' => $b->getConfig('infiniteScrolling'),
'add' => $_GET['add'] ?? null,
];

$h = 'htmlspecialchars';

?>
<!DOCTYPE html>
<html>
Expand All @@ -99,55 +107,23 @@ function dumpEntries($entries)
<body>

<div id="content">

<div class="header">
<form id="filterform">
<input id="query"
autofocus
type="text"
name="query"
placeholder=""
value="<?php echo htmlspecialchars($filter); ?>"
/>
</form>
<div class="header">
<form id="filterform">
<input id="query"
autofocus
type="text"
name="query"
placeholder=""
value="<?php echo $h($filter); ?>"
/>
</form>
</div>

<?php dumpEntries($entries); ?>
</div>

<?php dumpEntries($entries); ?>
<div data-b="<?php echo $h(json_encode($pageConfig)) ?>"></div>

</div>

<script>
window.filter = <?php echo $filter ? json_encode($filter) : "''"; ?>

<?php if ($step = $b->getConfig('infiniteScrolling')): ?>
window.infiniteScrolling = <?php echo $step; ?>
<?php endif; ?>
</script>
<script src="bookmarkManager.js"></script>

<script>

(function () {
window.onload = function() {
const queryEl = document.getElementById('query')
queryEl.value = <?php echo json_encode(!empty($_GET['add']) ? $_GET['add'] : (string)$filter); ?>

/* Place cursor at end of query text.
* https://stackoverflow.com/a/10576409 */
queryEl.addEventListener('focus', e => {
setTimeout(() => { queryEl.selectionStart = queryEl.selectionEnd = 10000 }, 0)
})

queryEl.focus()

<?php if (!empty($_GET['add'])): ?>
/* Remove query string from URL */
history.replaceState({}, null, window.location.pathname)
<?php endif; ?>
}
}())

</script>

</body>
</html>