You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.2 KiB
JavaScript

2 years ago
import padliographyStore from "./PadStore.js";
2 years ago
3 years ago
export default {
setup() {
2 years ago
const { ref, computed, watchEffect } = Vue;
2 years ago
const columns = ["title", "overview", "categories", "date"];
3 years ago
2 years ago
const loaded = ref(false);
2 years ago
2 years ago
const { padStore, currentPage, baseUrl } = padliographyStore();
2 years ago
const newPage = ref("");
const initPage = ref("");
const pageDescription = ref("");
const initialize = function () {
2 years ago
fetch(`${baseUrl.value}/api/${initPage.value}/init`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
description: pageDescription.value,
}),
}).then((res) => (currentPage.value = initPage.value));
};
2 years ago
const edit = ref(false);
const init = ref(false);
2 years ago
3 years ago
const pads = ref([]);
2 years ago
const currentSort = ref("date");
const currentSortDir = ref("desc");
2 years ago
2 years ago
const categories = ref([]);
2 years ago
const selected = ref(new Set());
const toggle = function (tag) {
selected.value.has(tag) ? selected.value.delete(tag) : selected.value.add(tag);
};
2 years ago
2 years ago
watchEffect(() => {
2 years ago
fetch(`${baseUrl.value}/api/${currentPage.value}/`)
2 years ago
.then((res) => res.json())
.then((data) => {
pads.value = data.pads;
2 years ago
pads.value.map(
(pad) =>
(pad.categories = pad.categories
.split(",")
.map((category) => category.trim()))
);
2 years ago
2 years ago
categories.value = Array.from(
new Set([...pads.value.map((pad) => pad.categories)].flat())
).sort();
2 years ago
2 years ago
loaded.value = true;
});
});
2 years ago
const sortedPads = computed(() => {
2 years ago
return filteredPads.value.sort((a, b) => {
2 years ago
let modifier = 1;
2 years ago
if (currentSortDir.value === "desc") modifier = -1;
if (currentSort.value === "date") {
a[currentSort.value] = new Date(a[currentSort.value]);
b[currentSort.value] = new Date(b[currentSort.value]);
}
2 years ago
if (a[currentSort.value] < b[currentSort.value]) return -1 * modifier;
if (a[currentSort.value] > b[currentSort.value]) return 1 * modifier;
return 0;
});
});
2 years ago
const sort = function (s) {
if (s == currentSort.value) {
currentSortDir.value = currentSortDir.value === "asc" ? "desc" : "asc";
}
currentSort.value = s;
};
2 years ago
const filteredPads = computed(() => {
2 years ago
if (selected.value.size == 0) return storedPads.value;
2 years ago
else
2 years ago
return storedPads.value.filter((pad) => {
2 years ago
return pad.categories.some((category) => selected.value.has(category));
});
});
2 years ago
const storedPads = computed(() => {
2 years ago
return [...padStore.value, ...pads.value];
2 years ago
});
const formatDate = function (date) {
2 years ago
date = new Date(date);
return `${String(date.getDate()).padStart(2, "0")}-${String(
date.getMonth() + 1
).padStart(2, "0")}-${date.getFullYear()}`;
};
return {
pads,
columns,
currentPage,
loaded,
2 years ago
filteredPads,
2 years ago
categories,
2 years ago
selected,
2 years ago
sortedPads,
currentSort,
currentSortDir,
2 years ago
toggle,
sort,
formatDate,
initialize,
2 years ago
padStore,
2 years ago
storedPads,
2 years ago
edit,
init,
2 years ago
newPage,
initPage,
pageDescription,
};
3 years ago
},
template: `
<div class="git">
<a href="https://git.xpub.nl/kamo/pad-bis" >git</a>, <a href="https://constantvzw.org/wefts/cc4r.en.html" >CC4r</a>
</div>
<div v-if="!loaded" class='loading grow'>
Loading loading loading loading loading loading loading loading loading loading loading loading loading loading loading loading loading loading etc
</div>
<div v-else>
2 years ago
<div class="filter">
<h2>Filter Categories</h2>
<ul>
2 years ago
<li :class="{active: selected.size == 0}" @click="selected.clear()">All</li>
2 years ago
<li v-for="tag in categories" :key="tag" @click="toggle(tag)" :class="{active: selected.has(tag)}">{{tag}}</li>
2 years ago
</ul>
</div>
2 years ago
<div class="from" v-if="!init">
2 years ago
Fetching pads from
<span class="editing" v-if="edit">
<input placeholder="New Page" v-model="newPage">
2 years ago
<button @click="edit=false, currentPage=newPage"> confirm </button>
<button @click="edit=false"> x </button>
</span>
2 years ago
<a v-else :href="'https://pzwiki.wdka.nl/mediadesign/Padliography/' + currentPage" target="_blank">{{currentPage}}</a>
<button class="change" v-if="!edit" @click="edit = true">change</button> <br>
</div>
<div v-if="init" class="init-form">
<input placeholder="New Page" v-model="initPage">
<input placeholder="Description" v-model="pageDescription">
<button @click="init=false, initialize()"> confirm </button>
<button @click="init=false, initPage=''"> x </button>
</div>
<button class="init" v-else @click="init = true"> Init a new Padliography</button>
2 years ago
2 years ago
<table>
<tr class="header">
2 years ago
<th v-for="column in columns" :class="column" @click='sort(column)'>
{{column }}
<span v-if='column == currentSort'> {{ currentSortDir == 'asc' ? '▼' : '▲'}}</span>
</th>
</tr>
<tr v-for="(pad, index) in sortedPads" key="pad.title, index" :class="pad.category">
<td class="title">
<a :href="pad.link" class='stretched' target="_blank"> {{pad.title}} </a>
</td>
2 years ago
<td class="overview">
<p>
{{pad.overview}}
</p>
</td>
<td class="categories">
<span class="category" v-for="category in pad.categories" :key="category">{{category}}</span>
</td>
<td class="date">{{formatDate(pad.date)}}</td>
</tr>
</table>
</div>
`,
3 years ago
};