2021-11-23 03:17:29 -05:00
|
|
|
import { escape, whenReady } from './dom';
|
2021-09-03 08:00:22 -04:00
|
|
|
import { DataTable } from './data_tables';
|
2021-11-23 03:17:29 -05:00
|
|
|
import { globalSetup } from './main';
|
2021-09-03 08:00:22 -04:00
|
|
|
|
2021-11-23 03:17:29 -05:00
|
|
|
whenReady(() => {
|
|
|
|
globalSetup();
|
|
|
|
|
|
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
|
|
const myParam = urlParams.get('q');
|
2022-01-06 07:58:29 -05:00
|
|
|
const apiUrl = /* myParam !== null ? '/api/ajax_pastes.php?q=' + myParam : */ '/api/ajax_pastes.php';
|
2021-11-23 03:17:29 -05:00
|
|
|
|
|
|
|
const table = new DataTable(document.getElementById('archive'), {
|
|
|
|
ajaxCallback: (resolve) => {
|
|
|
|
fetch(apiUrl)
|
|
|
|
.then(r => r.json())
|
|
|
|
.then(resolve);
|
|
|
|
},
|
|
|
|
rowCallback: (rowData) => {
|
|
|
|
const tags = rowData.tags.map((tagData) => {
|
|
|
|
let tagColorClass;
|
|
|
|
if (tagData.name.indexOf('nsfw') !== -1) {
|
|
|
|
tagColorClass = 'is-danger';
|
|
|
|
} else if (tagData.name.indexOf('safe') !== -1) {
|
|
|
|
tagColorClass = 'is-success';
|
|
|
|
} else if (tagData.name.indexOf('/') !== -1) {
|
|
|
|
tagColorClass = 'is-primary';
|
|
|
|
} else {
|
|
|
|
tagColorClass = 'is-info';
|
|
|
|
}
|
|
|
|
|
2022-01-06 07:58:29 -05:00
|
|
|
return `<a href="/archive?q=${tagData.slug}">
|
2021-11-23 03:17:29 -05:00
|
|
|
<span class="tag ${tagColorClass}">${escape(tagData.name)}</span>
|
|
|
|
</a>`;
|
|
|
|
}).join('');
|
|
|
|
|
|
|
|
return `<tr>
|
|
|
|
<td><a href="/${rowData.id}">${escape(rowData.title)}</a></td>
|
|
|
|
<td><a href="/user/${escape(rowData.author)}">${escape(rowData.author)}</a></td>
|
|
|
|
<td>${tags}</td>
|
|
|
|
</tr>`;
|
2022-01-06 07:58:29 -05:00
|
|
|
},
|
|
|
|
filterCallback: (datum, query) => {
|
|
|
|
if (datum.title.indexOf(query) !== -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is inefficient */
|
|
|
|
for (const tag of datum.tags) {
|
|
|
|
if (tag.name.toLowerCase() === query.toLowerCase()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
preFilter: myParam
|
2021-11-23 03:17:29 -05:00
|
|
|
});
|
|
|
|
table.attach();
|
|
|
|
});
|