Commons talk:JavaScript styleguide

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

This is a great idea! pfctdayelise (说什么?) 02:51, 6 July 2007 (UTC)[reply]

Use single whitespace indent - Show me an example on commons obeying[edit]

I do not see any script on commons obeying this guideline. Editing .js in the big Browser-Edit is not very clever and convenient (and I think nobody does it). Therefore this rule should be removed. --RE RILLKE Questions? 12:11, 24 March 2011 (UTC)[reply]

I agree. Besides, the sentense "Reducing the indent [...] can considerably reduce the weight of the script." seems not to be valid with ResourceLoader minification in use. Helder 13:26, 24 March 2011 (UTC)
By the way: the resource loader does not touch or modify any MediaWiki.js from Commons. How should it accomplish this task? -- RE RILLKE Questions? 17:48, 25 March 2011 (UTC)[reply]
A lot of Mediawiki:Common.js still does. But if people do whatever the heck they want to do there is not much point in having such a rule. Hell, let's just ditch the entire styleguide, who needs schmanchy fancy stuff like that anyways? --Dschwen (talk) 16:39, 25 March 2011 (UTC)[reply]
Ironic or serious? And what the hell is schmanchy fancy? My dictionary cannot find it. --RE RILLKE Questions? 17:03, 25 March 2011 (UTC)[reply]
A bit of both. I would like to see a clean unified look of code. I used to edit in a lot of the JS files on commons and it was a royal pain in the butt to switch back and forth between zillions of individual indenting styles. I made an effort by writing this Styleguide, but I can see that it is indeed pointless as people still continue to ignore it and format the code however they please. So, yes, I think it is a good idea, but unfortunately the commons world does not seem to be ready for it. --Dschwen (talk) 17:08, 25 March 2011 (UTC)[reply]
Don't worry. We can feel happy that nobody gets the idea of compacting the code (replacing long variables with short letters, ...). But there is a JavaScript solution, that "beautifies" code: http://jsbeautifier.org/ that makes correcting indents easy. --RE RILLKE Questions? 17:49, 25 March 2011 (UTC)[reply]
Yeah, I also looked for scripts to auto indent in textareas. That stuff exists and we might think about adding something here for *.js pages. Catching tab alone and replacing it by n-spaces would be a huge help. Adding the same number of indent characters (spaces) as the previous line on a keyupevent for the return key would be 90% of the way in my opinion. This would effectively force consistent indentation and save a ton of work. --Dschwen (talk) 05:10, 27 March 2011 (UTC)[reply]
Coincidentally Brion posted on teh Mailinglist about his frustration with editing js in browser. He whipped up a script which you can include like this:
importScriptURI('http://www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-CodeEditor.js&action=raw&ctype=text/javascript&dontcountme=s');
--Dschwen (talk) 04:33, 13 April 2011 (UTC)[reply]

Thanks for this information. The editor is very useful and works fine; however scrolling through long scripts is slow (because DOM tree has to become modified). I tried to do this myself, but the result was horrible, especially because IE and Firefox treat selections differently. Therefore this approach, using HTML DOM is a blessing. We should include the script into the settings (not by default because there are maybe Opera-users) on .js pages. -- RE rillke questions? 09:29, 13 April 2011 (UTC)[reply]

Seems pretty snappy to me on Google Chrome. Yeah, we should make this into a gadget. I was just too lazy/busy. --Dschwen (talk) 20:46, 13 April 2011 (UTC)[reply]

jquery-tipsy "live" is deprecated[edit]

Hi @Rillke, and others,

MediaWiki:JSONListUploads.js and maybe other gadgets use the live: true option of jquery-tipsy. This option used a discouraged feature of jQuery that was removed in jQuery 1.9 (https://api.jquery.com/live/). In 2014, special extended support was introduced for jquery-tipsy (phab:T85048) - this was possible due to an internal aspect of jQuery live() that survived. This too, however, is coming to an end as it was removed in jQuery 3.0. See also mw-js-deprecate Hitrates. –Krinkle 03:24, 23 November 2016 (UTC)[reply]

MediaWiki:VisualFileChange.js uses tipsy-live at MediaWiki:VisualFileChange.js/ui.js and re-creates live() using the same code as tipsy-live, which won't work in jQuery 3.0 as private state this.context and this.selector no longer exists there. –Krinkle 03:28, 23 November 2016 (UTC)[reply]
@Krinkle: Thanks for reporting, unfortunately Rillke is not back from the summer break. Maybe you can do the fix!? Greetings User: Perhelion 13:21, 24 November 2016 (UTC)[reply]
I've fixed MediaWiki:JSONListUploads.js by doing the tipsy bindings manually after each round of element creations. Seems to have worked. MediaWiki:VisualFileChange.js/ui.js still needs a fix though. Krinkle 01:39, 17 December 2016 (UTC)[reply]
I fixed VisualFileChange too, in a similar manner: [1]. Matma Rex (talk) 02:31, 17 December 2016 (UTC)[reply]

use var?[edit]

I am seeing var c:Commons:JavaScript styleguide#Use var. That keyword is prone to error-prone hoisting behavior. Does anyone mind if I change this detail to const / let?

Aavindraa (talk) 17:20, 1 August 2021 (UTC)[reply]

@Aavindraa let, const and other ES6 features may not be used, because the code in site scripts must be compatible with ES5, otherwise MediaWiki will not serve it. See T75714. Matma Rex (talk) 12:33, 2 August 2021 (UTC)[reply]
Thank you. I believe writing modern JS and transpiling down to ES5 is preferable to just writing old-school JavaScript. I replied in the thread (thanks for linking it here). Aavindraa (talk) 17:12, 2 August 2021 (UTC)[reply]