ponepaste/js/main.js

51 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-09-03 08:00:22 -04:00
import { $$, escape } from './dom';
2021-08-25 20:02:05 -04:00
import { TagsInput } from "./tag_input";
2021-09-03 08:00:22 -04:00
import { DataTable } from "./data_tables";
2021-08-25 20:02:05 -04:00
const setupSite = function() {
Array.prototype.forEach.call($$('.js-tag-input'), (el) => {
new TagsInput(el).attach();
});
2021-09-03 08:00:22 -04:00
if (document.querySelector('#archive')) {
const table = new DataTable(document.querySelector('#archive'), {
ajaxCallback: (resolve) => {
fetch('/api/ajax_pastes.php')
.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';
}
return `<a href="/tags/${tagData.slug}">
<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>`;
}
});
table.attach();
}
2021-08-25 20:02:05 -04:00
};
if (document.readyState !== 'loading') {
setupSite();
} else {
document.addEventListener('DOMContentLoaded', setupSite);
2021-08-25 02:08:30 -04:00
}