Page MenuHomePhabricator

Implement "smart" On-Off and Partial Custom Toggles in jquery.makeCollapsible.js
Open, Needs TriagePublic

Description

Motivation

Not long after the creation of the first COVID-19 medical cases chart (for Mainland China) in the English Wikipedia, month buttons were implemented to filter the amount of data/rows displayed at once. Besides these buttons, Last X days (which would be the only button toggled on by default) and All buttons were thought of and partially implemented to make visualization even more convenient and meaningful.

Problem

The problem arises with the latter two buttons because their set of affected/toggled elements overlaps with those of the month buttons. The current implementation of custom toggles simply flips/toggles the state of all collapsibles attached to the button. This is not desired in many situations. For example, check this basic demo using the appropriate dependencies. If one clicks the "Odd" button in the first subsection and then repeatedly clicks "Blue", he will never get all the blue numbers shown/hidden at the same time. This is because part of the blue numbers set was already toggled by the "Odd" button. Not only this, but the current custom toggles do not attach a state/class (shown by the light blue border) to the button, only on the collapsible elements. All in all, these make it impossible to properly create buttons similar to the former two as verifiable on the first chart of the applied example if one clicks Aug (its first days will be shown while the last will be hidden, creating a data gap, instead of just ALSO showing August days).

Proposed solution

I propose the creation of two new custom toggle classes: mw-onofftoggle, which adds a state interface to the toggle, but otherwise keeps its original behavior; and mw-partialtoggle, which has a modified toggling behavior that takes the state of the toggle into account (collapsed->expand all, partial->expand all, expanded->collapse all) instead of just flipping each element independently. The latter behaves like a potentially partial toggle because each toggle tracks the state of its attached elements. If only some of its elements are shown, the toggle state will be mw-collapsible-toggle-partial. This can be achieved if another toggle affects an elements set that partially overlaps with the former one.

The mw-onofftoggle works great for toggles like the "+10" button in the basic demo and the, to be implemented, Cumulative button in the second chart of the applied example (it would switch all chart data between new and cumulative). The mw-partialtoggle is perfect for the other applied example's chart buttons, especially the All button. These two new classes would probably be useful in many other situations in Wikipedia pages (200+ just in the medical cases charts) and even MediaWiki. Therefore, I thought the best place to propose such changes would be in jquery.makeCollapsible.js. My proposed script also has some bug fixes and code reordering.

This task supersedes T262622. This is my first JS project :)

Event Timeline

Aklapper renamed this task from Implement "smart" On-Off and Partial Custom Toggles in the makeCollapsible plugin to Implement "smart" On-Off and Partial Custom Toggles in jquery.makeCollapsible.js.Dec 4 2020, 10:44 AM
Aklapper added a project: JavaScript.

Hey @Alexiscoutinho . I'm a fairly new contributor here at Wikimedia. Can you point me to the Gerrit repo containing the code for this? I would like to give it a shot.

Thanks for the quick response! Just a quick doubt do you guys want it to be fixed with jQuery only or can I use vanilla JS?

Thanks @SarthakKundra for taking a look at my feature request. Recently, however, I started suspecting that most of my proposed changes belong to the Extensions territory. This came especially after I implemented year buttons in the aforementioned charts, the same Chinese one for example, and noticed that their ideal behavior principle would be a lot like Tabber's. Is my unfortunate suspicion justified?

Jdlrobson subscribed.

This is not a focus of my team right now. @SarthakKundra if you are planning on taking this on I suggest you find a code reviewer who is able to review and merge your changes and has an understanding of the collapsible code before beginning. This code is quite delicate and patches for this part of the code base as a result tend to linger un-reviewed for some time.

Thanks for the heads up @Jdlrobson . I'm quite new here so I don't know any maintainers. If you know someone who might be able to do the expected that'd be great otherwise I'll look for a new issue.

Thanks @SarthakKundra for taking a look at my feature request. Recently, however, I started suspecting that most of my proposed changes belong to the Extensions territory. This came especially after I implemented year buttons in the aforementioned charts, the same Chinese one for example, and noticed that their ideal behavior principle would be a lot like Tabber's. Is my unfortunate suspicion justified?

I'm very new here so I'm not yet versed with all the repositories yet.

I glanced at Tabber and Tabs and think they are a bit bulkier (with css and extra stuff) than what I envision would be the simplest implementation. Therefore, I believe that the toggling principles suggested here and of tabs should be implemented in core and that extra styling and convenience functionalities should remain in the extension space.

Thanks for the quick response! Just a quick doubt do you guys want it to be fixed with jQuery only or can I use vanilla JS?

I don't know the global preference of jQuery or vanilla, but I think it's jQuery in this case since the file is in the jquery "namespace". Consequently, I used it whenever adequate in the code.

@Alexiscoutinho If I submit a patch will you be able to review it? Since this is not being actively worked upon by Jdlrobson.

I can look at it, but I'm not a proper reviewer. Back in the day, I tried @TheDJ at 650016, but it seems like he wasn't available.