cmdonovann: self portrait (Default)
cmdonovann ([personal profile] cmdonovann) wrote in [community profile] fancoded2023-07-06 10:46 am

Looking for Javascript advice

So, I'm trying to set up a little fanfiction archive for a tiny fandom that only has around 60 fics total. I would like to be able to sort these fics by a couple of categories: length (sorting shortest to longest), rating (sorting lowest to highest), ship (sorting ships alphabetically and by date within each ship category), completion (showing only completed fics in order of recency), and just a list of all the fics from oldest to newest.

Since I'm currently hosting this on Neocities, and there really aren't that many fics, my current solution is to just hard-code each category's list myself, and then have a little javascript to toggle between each list/sorting method. However, in the interest of making this project easier to maintain in the future, I am interested in eventually setting up a script that actually does this sorting for me, rather than having to hardcode each list individually myself. Is there a way to set custom parameters/tags of some kind to sort a list of items using javascript? (To be clear, I'm a total javascript noob, so if this question seems obvious or is worded badly, that's why.)

Example: for sorting fics by length, I would want to be able to give each fic a "length in words" attribute, then have a script that sorts and displays them from shortest to longest. For displaying all of the fic, I would want to be able to give each fic a "date published" attribute to sort from oldest to newest. For completion I would use the date attribute to sort them oldest to newest as well, but additionally filter out any that are marked as "unfinished" somehow? But I'm not sure how to practically do all this.

Any and all advice on javascript is welcome! Advising me to use something else will probably be ignored, because the entire purpose of doing it this way is to force myself to get better at javascript XD thanks for understanding!
citrodorks: Loremaster from Helltaker putting her hands up in a defensive gesture, she's smiling though (Helltaker - Loremaster)

Ah shit there I go being an Astro build evangelist again

[personal profile] citrodorks 2023-07-06 04:59 pm (UTC)(link)

Oh! This might actually be an excellent use case for Astro and its collections!!

They’ll allow you to assign that sort of metadata to each fic and then use JS to sort through the lists just like you said. ^^ I use it in my own neocities to have my fanfic sorted by publication date but the work behind it could be used to build filters and sorting by ship, fandom, word count, etc, etc. I can give you a closer look at what I have if you’d like? My code base is a bit messy but it might be illustrative enough combined with the docs above.

Edited 2023-07-06 17:05 (UTC)
citrodorks: A chibi icon of Azazel's head over a red background, she looks a bit despondent (Helltaker - Blankzazel)

Re: Ah shit there I go being an Astro build evangelist again

[personal profile] citrodorks 2023-07-06 11:25 pm (UTC)(link)

Ah yes!

For what it’s worth once you get the hang of Astro converting a project to it can become almost painless, but as a Very First Astro project converting a nearly complete website might not be the most fun, agreed. Perhaps a good second project tho!

Edited (Grammar ) 2023-07-06 23:28 (UTC)
momijizukamori: Green icon with white text - 'I do believe in phosphorylation! I do!' with a string of DNA basepairs on the bottom (Default)

[personal profile] momijizukamori 2023-07-06 10:03 pm (UTC)(link)
Absolutely doable in JS - I've done it before in jQuery, and will dig up the code for you (possibly tomorrow at this point, it was an internal work thing and I don't feel like logging back on), but jQuery is mostly just a helper layer on top of vanilla JS, so it shouldn't take me long to convert.

You can uses CSS classes, or data-[whatever] attributes, or if it's stuff you want to have as part of the visible HTML too, you can just yoink it out of that if it's got a handy label.
momijizukamori: Grey tabby cat with paws on keyboard and mouse. The text reads 'code cat is on the job', lolcats-style (CODE CAT)

[personal profile] momijizukamori 2023-07-08 08:19 pm (UTC)(link)
OKAY SO. It turned out the existing JS is an ancient confusing mess (the last time I worked on it was like four years ago, and I inherited a lot of it from a previous person), so I went ahead and did some simple demos with hopefully sufficient comments to understand them. Both are pure vanilla JS - simple sort demo, and simple filter demo. These employ the same strategies the work code does, which performed fine on lists up to a couple hundred items, but above that I can't say.

A fancy addition to these would be to read and write the window url query parameters, so that you could share a specific filter configuration as a link - if you'd like, I can whip up a demo for that too.

[personal profile] lieutenant_sanya 2023-07-07 09:25 pm (UTC)(link)
Hello! I stumbled across your entry and thought I'd try a hand at it. Do you mean something like this? Very bare-bones example—but JS can filter and organize your works for you. In the first version I made, it needed you to edit the HTML and JS for new fics, but I rewrote it so that all you have to do is add more objects to an array; the JS will do the rest of the work in sorting and displaying it.

All of the fics' details (name, length, completion status, summary, etc.) are objects in an array I named "fics". The incomplete/complete checkboxes toggle {display: none} or {display: block} on the respective works depending on if they're checked. For the sorting functions, I made the parent main tag a flex box so JS can use the {order} property to organize the child sections according to word count/name/etc.

I can send the HTML and JS files to you if you'd like. :) Now I too want to start some sort of fannish fic archive, hahah.