From 8750ea54a9e65803381417a29f1144eab6620dc5 Mon Sep 17 00:00:00 2001 From: janis Date: Tue, 24 Oct 2023 10:14:06 +0200 Subject: [PATCH] add sorting to voting plugin --- .../plugins/others/poll/html/voting.html | 12 +++++-- .../backend/plugins/others/poll/js/voting.js | 33 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/server/backend/plugins/others/poll/html/voting.html b/src/server/backend/plugins/others/poll/html/voting.html index 8a50da3..b1d90cb 100644 --- a/src/server/backend/plugins/others/poll/html/voting.html +++ b/src/server/backend/plugins/others/poll/html/voting.html @@ -94,11 +94,19 @@

Voting on {{ votingDetails.display ?? 'untitled' }}

{{ votingDetails.description }}

-
+
-
+ +

{{ entry.title }}

{{ entry.comment }}

diff --git a/src/server/backend/plugins/others/poll/js/voting.js b/src/server/backend/plugins/others/poll/js/voting.js index 6b5fdbf..e6f82b3 100644 --- a/src/server/backend/plugins/others/poll/js/voting.js +++ b/src/server/backend/plugins/others/poll/js/voting.js @@ -10,8 +10,41 @@ createApp( { votedOn: {}, hasLoadedBasics: false, hasLoadedVotes: false, + sorting: 'newest', }; }, + computed: { + orderedVotes() { + if ( this.sorting === 'oldest' ) { + return Object.values( this.entries ); + } else if ( this.sorting === 'newest' ) { + const ent = Object.keys( this.entries ).reverse(); + let ret = []; + for ( let entry in ent ) { + ret.push( this.entries[ ent[ entry ] ] ); + } + return ret; + } else { + let ent = Object.keys( this.entries ).sort( ( a, b ) => { + if ( this.sorting === 'nameUp' ) { + return this.entries[ a ].title.localeCompare( this.entries[ b ].title ); + } else if ( this.sorting === 'nameDown' ) { + return this.entries[ b ].title.localeCompare( this.entries[ a ].title ); + } else if ( this.sorting === 'mostVoted' ) { + return this.entries[ b ].count - this.entries[ a ].count; + } else if ( this.sorting === 'leastVoted' ) { + return this.entries[ a ].count - this.entries[ b ].count; + } + } ); + console.log( ent ); + let ret = []; + for ( let entry in ent ) { + ret.push( this.entries[ ent[ entry ] ] ); + } + return ret; + } + } + }, methods: { getData() { fetch( '/polls/get/' + location.pathname.substring( 7 ) ).then( response => {