summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/stream/hootstream.js
diff options
context:
space:
mode:
authorjulian laplace <julescarbon@gmail.com>2022-10-26 23:03:51 +0200
committerjulian laplace <julescarbon@gmail.com>2022-10-26 23:03:51 +0200
commitd393b5478c0278956d5c0cbd26e0e86d9428cc1f (patch)
treee7d3fc977445b00260692bb51348866486b8a28c /public/assets/js/lib/views/stream/hootstream.js
parent76bead776ed0380ab6d1ebf7cb133cc797ae7d54 (diff)
filters
Diffstat (limited to 'public/assets/js/lib/views/stream/hootstream.js')
-rw-r--r--public/assets/js/lib/views/stream/hootstream.js129
1 files changed, 104 insertions, 25 deletions
diff --git a/public/assets/js/lib/views/stream/hootstream.js b/public/assets/js/lib/views/stream/hootstream.js
index 70d2b2a..a67f4b8 100644
--- a/public/assets/js/lib/views/stream/hootstream.js
+++ b/public/assets/js/lib/views/stream/hootstream.js
@@ -1,5 +1,3 @@
-const IMAGE_REGEXP = /(.gif|.jpg|.jpeg|.png)$/gi;
-
var HootStream = View.extend({
el: "#hootstream",
@@ -47,28 +45,109 @@ var HootStream = View.extend({
// this.parent.onKeyword(keyword)
},
- load: function (data) {
- this.$hootevents.empty();
- const { order, threadLookup } = this.agglutinate(data);
- // console.log(data, threadLookup, order);
- const $els = order.map(
- function (item) {
- // console.log(item.type, item.thread_id);
- return item.type === "thread"
- ? this.renderThread({
- ...threadLookup[item.thread_id],
+ load: function (data, filters) {
+ this.state = {
+ ...this.agglutinate(data),
+ data,
+ filters,
+ };
+ this.build(filters);
+ },
+
+ build: function (filters) {
+ const { data, order, threadLookup } = this.state;
+ const threads = Object.values(threadLookup);
+
+ let sortedOrder;
+
+ if (filters.sort === "name") {
+ sortedOrder = threads
+ .reduce((names, values) => {
+ if (values.thread.length) {
+ return names.concat([
+ [values.thread[0].title, values.thread[0].id],
+ ]);
+ }
+ return names;
+ }, [])
+ .sort(
+ filters.order === "asc"
+ ? (a, b) => a[0].localeCompare(b[0])
+ : (a, b) => b[0].localeCompare(a[0])
+ )
+ .map(([, thread_id]) => ({
+ type: "thread",
+ thread_id,
+ }));
+ } else if (filters.sort === "date") {
+ sortedOrder = filters.order === "desc" ? order : [...order].reverse();
+ } else {
+ sortedOrder = order;
+ }
+ // console.log(sortedOrder, threadLookup);
+
+ const $els = sortedOrder
+ .filter(({ type, thread_id }) => {
+ if (filters.hoots && (type === "hoot" || type === "lastlog")) {
+ return true;
+ }
+ const thread = threadLookup[thread_id];
+ if (filters.hoots && thread.comments?.length) {
+ return true;
+ }
+ if (
+ filters.music &&
+ thread.files?.some((file) => AUDIO_REGEXP.test(file.filename))
+ ) {
+ return true;
+ }
+ if (
+ filters.images &&
+ thread.files?.some((file) => IMAGE_REGEXP.test(file.filename))
+ ) {
+ return true;
+ }
+ return false;
+ })
+ .map(
+ function ({ type, thread_id, data: itemData }) {
+ // console.log(type, thread_id);
+ if (type === "thread") {
+ const thread = threadLookup[thread_id];
+ const threadData = {
+ ...thread,
+ files:
+ filters.images || filters.music
+ ? thread.files.filter((file) => {
+ if (AUDIO_REGEXP.test(file.filename)) {
+ return filters.music;
+ }
+ if (IMAGE_REGEXP.test(file.filename)) {
+ return filters.images;
+ }
+ return filters.files;
+ })
+ : [],
+ comments: filters.hoots ? thread.comments : [],
query: data.query,
- }).reduce(
- ($el, $item) => $el.append($item),
- $("<div class='thread'>")
- )
- : item.type === "hoot"
- ? this.renderHoot(item.data)
- : item.type === "lastlog"
- ? this.renderLastlog(item.data)
- : "Unknown item";
- }.bind(this)
- );
+ };
+ if (threadData.files.length || threadData.comments.length) {
+ return this.renderThread(threadData).reduce(
+ ($el, $item) => $el.append($item),
+ $("<div class='thread'>")
+ );
+ } else {
+ return $();
+ }
+ }
+ return type === "hoot"
+ ? this.renderHoot(itemData)
+ : type === "lastlog"
+ ? this.renderLastlog(itemData)
+ : "Unknown item";
+ }.bind(this)
+ );
+ this.$hootevents.empty();
this.$hootevents.append($els);
audio.init();
},
@@ -142,7 +221,7 @@ var HootStream = View.extend({
console.error(thread, comments, files, images);
return ["<div>Missing thread!</div>"];
}
- thread = thread.shift();
+ thread = thread[0];
const isViewingKeyword = query.keyword === thread.keyword;
const isViewingThread = query.thread === thread.id;
// console.log(thread, comments, files, images);
@@ -153,7 +232,7 @@ var HootStream = View.extend({
0.0,
1.0
);
- console.log(thread);
+ // console.log(thread);
return [
"<div class='divider dark'></div>",
this.renderHoot({