Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warn/xfd: Replace jquery.chosen menus with select2 #692

Closed
wants to merge 9 commits into from

Conversation

siddharthvp
Copy link
Member

@siddharthvp siddharthvp commented Aug 31, 2019

Replace jQuery Chosen select menu with select2.

This fully fixes the bug described here (closes #665), also eliminating the stop-gap solution of removing the resize handles. The problems associated with using jquery.chosen within a modal dialog (like in all TW scripts) are not there in select2. Also, select2 is better both in terms of customizability and programmatic control, not to mention having a proper documentation (unlike chosen). Select2 unfortunately doesn't natively support highlighting of search results, so I have implemented that manually (as I did in Tag module).

Select2 is being loaded using the CDN, which is obviously non-ideal, especially since the support for loading of scripts from non-WMF sites may likely be removed in the future. Before that happens, we should get select2 added to the ResourceLoader, maybe as a replacement to chosen.

Copy link
Collaborator

@MusikAnimal MusikAnimal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cannot be merged as-is for obvious reasons. I think getting Select2 into ResourceLoader requires WMF intervention (maybe?), so I've added the "Depends on MW" tag. The other possible solution is to use the Toolforge CDN, if it happens to get whitelisted.

@Amorymeltzer
Copy link
Collaborator

Just noting that I agree with @MusikAnimal on this. Loading external code from non-WMF sites is something I think Twinkle users would be fairly uncomfortable with, and given the wide usage of TW, simply isn't safe.

@siddharthvp
Copy link
Member Author

@MusikAnimal any suggestion on how to move ahead with this, considering that we can all agree that is an improvement. (Also, the delsort stuff added in #143 is turning out to be widely popular among users - it's time we provide a more pleasant UX for that.)

Why isn't wmflabs.org a whitelisted domain, when the Toolforge CDN homepage says it's a "Collection of very useful JS / CSS libraries served in a fast, Wikimedia Privacy Policy friendly way." ?

Should I open a phab ticket for inclusion of select2 into ResourceLoader? Or would it be shot down with the standard "please use OOUI instead" response? I am not very sure whether OOUI can be used to do what we're doing with select2 here, but select2 definitely can be used for a lot of stuff that can't be done using OOUI at present.

@Amorymeltzer
Copy link
Collaborator

Why isn't wmflabs.org a whitelisted domain

T223840: Can/should *.wmflabs.org be added to the default-src Content Security Policy?

@siddharthvp
Copy link
Member Author

T223840

Didn't go over it thoroughly. Would the reasons why *.wmflabs.org is disallowed also apply to tools-static.wmflabs.org, noting that tools-static only hosts trustworthy libraries and no user-created tools?

@Amorymeltzer
Copy link
Collaborator

There's arguments to be made for whitelisting certain domains, but to some folks * means * so ¯_(ツ)_/¯. I suppose, per @MusikAnimal, the interim use of the cdn is okay-ish? I dunno, I don't think the CSP is going to get implemented for a while, as there are plenty of *.wmflabs things that are relied upon.

@MusikAnimal
Copy link
Collaborator

I also don't think the CSP headers are going to be implemented in the very near future, or at least we'll have some sort of solution for whitelisting domains by then (and I concur /cdnjs should be whitelisted for everyone).

I should note tools-static.wmflabs.org does go down sometimes... but lots of things break when this happens. The only weird bit here is that Twinkle is an on-wiki gadget, so one wouldn't expect it to break when everything on-wiki is otherwise working fine.

I guess we could upload the select2 assets to the wiki, and declare them as a dependency in the gadget definition. This would put them behind ResourceLoader. However I'm not sure how acceptable this practice is, as it is third-party code. I'm assuming the licensing is fine, so long as it's prominently stated at the top, otherwise it inherently is published under CC-BY-SA.

I think we can give the Toolforge CDN a try for now. My only real concern was pulling from Cloudfare or anything outside WMF.

I have not tested this PR.

@MusikAnimal MusikAnimal dismissed their stale review October 14, 2019 20:21

now pulling from Toolforge CDN

@siddharthvp siddharthvp force-pushed the select2x branch 3 times, most recently from 23eeda0 to 2cc441b Compare October 15, 2019 19:13
- Unify the code for highlightening search results from warn and xfd modules into Morebits.select2SearchHighlights
- Don't load select2 if it has already loaded.
- Don't apply select2 transformation to the select field if select2 failed to load, such as when the CDN server is down.
@siddharthvp
Copy link
Member Author

I should note tools-static.wmflabs.org does go down sometimes... but lots of things break when this happens. The only weird bit here is that Twinkle is an on-wiki gadget, so one wouldn't expect it to break when everything on-wiki is otherwise working fine.

See commit 2: The code where select2 acts on the interface is put into a if ($.fn.select2) { } block. Since it is only required for making the menu searchable, Twinkle will still work in basic mode even if it doesn't load. (This is good enough for warn, though not for xfd as the browser's native multi-select menu is pathetic & unusable when the number of options is large, but I guess something is better than nothing.)

morebits.js Outdated Show resolved Hide resolved
This is the only way to prevent it from being loaded twice (once each time from twinklewarn and twinklexfd), resulting in double the number of console warnings.
@siddharthvp
Copy link
Member Author

Ok, a new insanity has come up: as reported on WT:TW, the behaviour of the jquery.chosen-based select menu has changed for the worse (at least on Chrome in Win10 - quite alarming as this is probably the most popular browser-OS combination). I haven't bothered to investigate the reason (could be due to an update in chosen or something else).

Returning to this after 2 months, I've resolved the bug above (just a loader problem), and I think this is almost ready for merge. Before that, a couple of things need to be settled:

  • FWIW I'd be in favour of loading select2 from an on-wiki gadget page, rather than loading from the CDN because of the two massive console error messages it produces. (I seem to have read somewhere that gadgets shouldn't give console errors.) There is some precedence for this - MediaWiki:Gadget-moment.js (before moment was added to the RL), and also there's User:Evad37/qunit-2.8.0.js and User:Evad37/rater/lib/interact.js recently uploaded by @evad37 (the latter is unused atm I guess). All of these, like select2, are released under MIT license.
  • I say, let's deprecate the oldSelect preference option. It was added (warn: make jquery.chosen based menu optional, via-opt-out #661) hurriedly before we started making CSS tweaks. It's rather unnecessary now and will be even more so after this is merged. Native and select2 almost look alike. It's only being used by 5 users - 4 of whom enabled it on 7 June 2019, before the CSS-based fixes were deployed.
    Having multiple modes would be difficult to maintain in the long run.

@MusikAnimal and @Amorymeltzer

@Amorymeltzer
Copy link
Collaborator

Apologies for taking so long to get around to this @siddharthvp and @MusikAnimal, I misread some emails and thought you two had agreed to wait. I see now I was very wrong. I can test, review, and help make changes in the next couple of days, but wanted to make some broad comments to the above beforehand:

  • In general, a hearty +1 to this; you and I spent way too long on CSS crap with chosen, so anything that gets away from that the better AFAIAC! Plus, select2 is way better maintained (it should probably replace chosen in RL).
  • I don't like the idea of uploading select2 on-wiki, but I really don't the alternatives, so I think that's definitely the way to go. License is fine.
  • Let's leave oldSelect in place: the initial rollout in the warning menu could've been better, but the response was passionate. Is it causing issues here?

@siddharthvp
Copy link
Member Author

Regarding oldSelect, it isn't causing any issues here but I had always thought of that as a temporary measure to give us time to get our shit right, not something to keep around long-term. With select2, the menu is almost alike the native one, except that it supports search. Also it isn't the case that select2 won't be loaded if this is enabled (getting rid of console errors), so there's like zero utility.

Regarding users: 4 enabled it when the early chosen menu was very bad, and 1 now since the new issues started appearing. There's just 1 person who enabled it in between!

Copy link
Collaborator

@Amorymeltzer Amorymeltzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fabulous, @siddharthvp! Thanks as always for the thorough work. There are a few issues here but this is pretty damn good!

I didn't go into the visual changes/tweaks, as that way lies madness; I'll get around to it later, but they look fine.

if (mw.config.get('wgRelevantUserName')) {
Twinkle.addPortletLink(Twinkle.warn.callback, 'Warn', 'tw-warn', 'Warn/notify user');
if (Twinkle.getPref('autoMenuAfterRollback') && mw.config.get('wgNamespaceNumber') === 3 &&
mw.util.getParamValue('vanarticle') && !mw.util.getParamValue('friendlywelcome') && !mw.util.getParamValue('noautowarn')) {
if (Twinkle.getPref('autoMenuAfterRollback') &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's just me so don't change anything, but I generally like a couple together, even if they go over the mythical 80. I guess this is easier to read and makes indenting less annoying, but bleh.

I suppose the real answer is I should get a bigger monitor! 😆

modules/twinklewarn.js Show resolved Hide resolved
modules/twinklexfd.js Show resolved Hide resolved
morebits.js Outdated Show resolved Hide resolved
twinkle.js Outdated Show resolved Hide resolved
morebits.js Outdated Show resolved Hide resolved
modules/twinklewarn.js Show resolved Hide resolved
modules/twinklexfd.js Show resolved Hide resolved
morebits.js Outdated Show resolved Hide resolved
modules/twinklewarn.js Show resolved Hide resolved
@Amorymeltzer Amorymeltzer added Module: morebits The morebits.js library Module: xfd labels Jan 18, 2020
@Amorymeltzer
Copy link
Collaborator

Precipitous timing, the chosen folks just marked the latest as deprecated: harvesthq/chosen@91041bc

That shouldn't affect anything (cdnjs doesn't really cull) but all the more reason for this. This PR is definitely going to go through, mutatis mutandis, so I've gone and opened #812 to take care of the work of adding select2 to the repo.

Amorymeltzer added a commit to Amorymeltzer/twinkle that referenced this pull request Jan 22, 2020
- Add noemail and nocreate items when issuing just a partial template
- Fix handling of partial parameter, was bugging out when just issuing a template
- Skip saveFieldset for the select2 menus, easier to just do `.join('|')` for the query
- CSS tweaks a la wikimedia-gadgets#692
Copy link
Collaborator

@Amorymeltzer Amorymeltzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CSS improvements look great, just two comments off

modules/twinklewarn.js Show resolved Hide resolved

// Reduce font size
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Reduce font size
// Increase font size

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be platform-dependent, because I was seeing a default font size of a massive 16px. Let's change the word to "Adjust".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What percent was that? I can't check my system tonight but it was around 11.5px iirc.

modules/twinklexfd.js Outdated Show resolved Hide resolved
@siddharthvp
Copy link
Member Author

siddharthvp commented Jan 23, 2020

@Amorymeltzer Dealt with mostly everything. Also did away with the hacky way of highlighting search hits with the more native-ish way using templateResult. All common code is in morebits under a new Morebits.select2 object. Wouldn't mind you double-checking that everything is ok.

@Amorymeltzer
Copy link
Collaborator

Thus far it all looks good? Would you mind explaining the queryInterceptor a bit? You're feeding the default text for searching into it so highlightSearchMatches can access it, is that right?

Speaking of which, highlightSearchMatches seems really neat. I'll play around with it and see if I can't break anything, but it all seems to work quite nicely!

@Amorymeltzer
Copy link
Collaborator

@siddharthvp @MusikAnimal Regarding #692 (comment), is it worth removing the $.fn.select2 ifs? Shouldn't be necessary, yeah?

@Amorymeltzer Amorymeltzer dismissed their stale review January 24, 2020 05:13

Changes look good AFAICT; one question about if removals, but otherwise LGTM!

Amorymeltzer added a commit to Amorymeltzer/twinkle that referenced this pull request Jan 24, 2020
Partial blocks (https://phabricator.wikimedia.org/T190350) were turned on following an RfC (https://en.wikipedia.org/wiki/Wikipedia:Requests_for_comment/Partial_blocks), so it's time to support them.  Done so by adding a checkbox that toggles a "partial" status for both the blocking and templating behaviors.

In order to support entering specific pages, the modules needs to support multiple custom user inputs, which can't be done nicely with chosen (See harvesthq/chosen#166).  We can, however, use select2's tags system, recently added in wikimedia-gadgets#812 to make wikimedia-gadgets#692 (warn/xfd) happen.

This is all an active WIP, including the policy (WP:PB) and the/any templates (right now, only {{uw-pblock}}) so a lot of this is likely to be in flux for a while.

Some stray notes:

- Adds a `defaultToPartialBlocks` preference option
- Makes use of a hidden partial item to help build the query (like `reblock`)
- Includes checks to prevent empty entries (T208645])
- Adds jumping boxes for `email`/`accountcreate` when just issuing a partial template
- Will ignore the `email`/`accountcreate` template parameters if there's an `area`
- CSS and select2 tweaks a la wikimedia-gadgets#692
- I've skipped proper processing in `saveFieldset` of the select2 menu items, it's easier to just do `.join('|')` for the query
- This use a variable for formatted namespaces, namely "(Article)" rather than "".  The empty string really is no good here; Morebits.wikipedia.namespacesFriendly previously handled this sort of thing, was removed in wikimedia-gadgets#600.
- Compared to the rest of Twinkle, this is a weird module!  Very different but pretty enjoyable all in all; @MusikAnimal built something really quite elegant here in wikimedia-gadgets#260.

Closes wikimedia-gadgets#802.
},

/** Underline matched part of options */
highlightSearchMatches: function(data) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I said this already, @siddharthvp, but these really are quite well done. Way more concise than I ever would have thought possible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea comes from https://forums.select2.org/t/how-can-i-highlight-the-results-on-a-search/52/2. I just condensed down the code.

That should also explain the queryInterceptor (they've used the implicitly global query variable). Too bad that templateResult doesn't expose the search query as a parameter. This hack is unlikely to break, unless select2 makes some deliberate breaking changes.

@Amorymeltzer Amorymeltzer changed the title Replace jquery.chosen menus with select2 warn/xfd: Replace jquery.chosen menus with select2 Jan 24, 2020
Amorymeltzer pushed a commit that referenced this pull request Jan 24, 2020
This fully fixes the bug [described here](https://en.wikipedia.org/wiki/Wikipedia_talk:Twinkle/Archive_41#Long_previews_overlap_and_block_%22Submit%22_button) (closes #665), also eliminating the stop-gap solution of removing the resize handles. The problems associated with using jquery.chosen within a modal dialog (like in all TW scripts) are not there in select2. Also, select2 is better both in terms of customizability and programmatic control, not to mention having a proper documentation (unlike chosen).  select2 added in #812.

This also adds Morebits.select2 with functions to enhance the select2 menus:
- matcher: show entire optgroup if matching the optgroup title
- highlightSearchMatches underline the search term in matches
- autostart: begin the search upon keydown
Amorymeltzer added a commit that referenced this pull request Jan 24, 2020
* 692-useselect2-notchosen-fixup:
  warn/xfd: replace jquery.chosen with select2 (#692)
Copy link
Collaborator

@Amorymeltzer Amorymeltzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for all the hard and consistent work on this, glad we could make it happen!

@Amorymeltzer
Copy link
Collaborator

Merged as 4a1330c

Amorymeltzer added a commit that referenced this pull request Jan 24, 2020
Replaced by select2; added in #580 and #641, removed in #692
@siddharthvp
Copy link
Member Author

Thanks for all your hardwork and research on getting select2 to load! Just tried this out on enwiki. Smooth and amazing!

Amorymeltzer added a commit to Amorymeltzer/twinkle that referenced this pull request Jun 30, 2020
Per follow-up in wikimedia-gadgets#971 from the addition in wikimedia-gadgets#692, these appear unnecessary.
wiki-ST47 pushed a commit to wiki-ST47/twinkle that referenced this pull request Sep 2, 2020
Per follow-up in wikimedia-gadgets#971 from the addition in wikimedia-gadgets#692, these appear unnecessary.
@siddharthvp siddharthvp deleted the select2x branch October 22, 2020 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warning module: warning information frame doesn't resize with window lower edge
3 participants