User talk:Ohconfucius/test/MOSNUM dates.js: Difference between revisions

Content deleted Content added
m <nowiki/> to avoid {{ taken for template markup; properly close <p>
 
(24 intermediate revisions by 2 users not shown)
Line 1:
==Bug and corrections==
#<s>[http://en.wikipedia.org/w/index.php?title=Slobodan_Milo%C5%A1evi%C4%87&diff=prev&oldid=487041738 "{<nowiki/>{main" becomes "{<nowiki/>{mainmain"], 13 April 2012</s> resolved 13 April 2012
#<s>[http://en.wikipedia.org/w/index.php?title=Ala_Paredes&diff=prev&oldid=487022955 "| accessdate = 25 December 2008" becomes "December 25 2008"], 12 April 2012</s> resolved 15 April 2012
#<s>[http://en.wikipedia.org/w/index.php?title=Albert_C._Barnes&diff=487281456&oldid=465949367 <code>''New York Times'' 26 Mar 1961</code> becomes <code>''New York Times''Mar , 26 1961</code>],</s> resolved 15 April 2012
Line 16:
#[http://en.wikipedia.org/w/index.php?title=Byron_P._Howlett&diff=prev&oldid=489420973 missing comma]. <code>took place between 3–22 November 1967</code> becomes <code>took place between November 3–22 1967</code>
#[http://en.wikipedia.org/w/index.php?title=Minxin_Pei&diff=490399698&oldid=486062865 stripping of hyphens without changing month] "<code>accessed 12-08-2011</code>" becomes "<code>accessed 12 08 2011</code>"
#[http://en.wikipedia.org/w/index.php?title=Research_on_meditation&diff=492871207&oldid=492003800 order of month switched with year in range] <code>|date=2003 Jul-Aug</code> becomes <code>|date=July 2003–Aug</code>
#[http://en.wikipedia.org/w/index.php?title=Ballard%2C_Seattle&diff=493364177&oldid=493363733 fails to remove ordinal 'the 17th of May']
#[http://en.wikipedia.org/w/index.php?title=Florida&diff=494129180&oldid=494019625 <code>accessdate=2012 Apr 07</code> becomes <code>accessdate=April 2012 07</code>]
#[http://en.wikipedia.org/w/index.php?title=John_Charles_Felix_Rossi&diff=495562174&oldid=486766449 '1st Marquess' becomes '1 Marquess']
 
== Simplifications ==
 
Hi!
 
What do you think about [//en.wikipedia.org/w/index.php?title=User:Ohconfucius/test/MOSNUM_dates.js&oldid=500061806&diff=501012556 optimizing] some regexes? [[User talk:Helder.wiki|Helder]] 21:46, 6 July 2012 (UTC)
 
== Updates ==
 
Hello {{U|Ohconfucius}}!
 
Could you make [https://en.wikipedia.org/w/index.php?title=User:Ohconfucius/test/MOSNUM_dates.js&oldid=610151247&diff=621001820 these fixes and improvements] to the script? [[User:Helder.wiki|Helder]] 02:26, 13 August 2014 (UTC)
*{{done}} Do I have to change the function calls (from other scripts such as [[User:Ohconfucius/vector.js|this]]) in any way? --<small><span style="background-color:#ffffff;border: 1px solid;">[[User:Ohconfucius|'''<span style="color:#000000; background-color:#00FF00">&nbsp;Ohc&nbsp;</span>''']]</span></small>[[User talk:Ohconfucius|<sup>''¡digame!''</sup>]] 02:32, 13 August 2014 (UTC)
:{{reply to|Ohconfucius}} is any function defined in this script called from another js page? [[User:Helder.wiki|Helder]] 03:06, 13 August 2014 (UTC)
::[[User:Helder.wiki|Helder]], the only place these functions would normally be called from is my vector file, and it doesn't seem to be the case. But if I have to call it, how would this be achieved? --<small><span style="background-color:#ffffff;border: 1px solid;">[[User:Ohconfucius|'''<span style="color:#000000; background-color:#00FF00">&nbsp;Ohc&nbsp;</span>''']]</span></small>[[User talk:Ohconfucius|<sup>''¡digame!''</sup>]] 03:12, 13 August 2014 (UTC)
:::{{U|Ohconfucius}}: If you are referring to any of the functions which are used in the click events, you could trigger the click event of the link associated to the function (e.g. executing <code>$( '#t-ISOdmy' )[http://api.jquery.com/click/#click .click()];</code>), and jQuery would take care of passing the event object as a parameter (which is used in the lines <code>e.preventDefault()</code>). But if these functions are also intended to be called without a click event, then it would need some change to avoid errors such as "ReferenceError: e is not defined".
:::BTW: I just noticed the script is creating duplicated ids in the page (there are 4 "t-expand", 2 "t-dmy", 2 "ca-unitfixer", etc), which is invalid in HTML. You should choose a unique id for each link. [[User:Helder.wiki|Helder]] 03:43, 13 August 2014 (UTC)
::::Noted. I never knew what those IDs' functions were, and the fact they were the same never caused any problems. Now renamed. I will do that for my other scripts too. --<small><span style="background-color:#ffffff;border: 1px solid;">[[User:Ohconfucius|'''<span style="color:#000000; background-color:#00FF00">&nbsp;Ohc&nbsp;</span>''']]</span></small>[[User talk:Ohconfucius|<sup>''¡digame!''</sup>]] 03:52, 13 August 2014 (UTC)
*{{ping|Helder.wiki}} So, if I need to improve the other scripts (like [[User:Ohconfucius/test/formatgeneral.js|formatgeneral.js]]), presumably I need to put <code>e.preventDefault();</code> as the first line in each driver (the one activated directly by the button) and then change the syntax for the click buttons? Would I do that for <code>Ohc_run_formatgeneral()</code> only, or do I need to do it for <code>Ohc_downcase_headings()</code> and <code>Ohc_eject_zh()</code>, both of whose functions reside in a subscript? And what does that <code>e.</code> line do anyway? --<small><span style="background-color:#ffffff;border: 1px solid;">[[User:Ohconfucius|'''<span style="color:#000000; background-color:#00FF00">&nbsp;Ohc&nbsp;</span>''']]</span></small>[[User talk:Ohconfucius|<sup>''¡digame!''</sup>]] 02:58, 15 August 2014 (UTC)
*:{{reply to|Ohconfucius}} In these cases, the [http://api.jquery.com/event.preventdefault/ event.preventDefault()] is to avoid scrolling to the top of the page when clicking in the link (which is what a link with href "#" would do by default - you can check that commenting the line and clicking in the link). This is necessary in each function used as a click handler for a link, such as <code>Ohc_run_formatgeneral</code>, <code>Ohc_downcase_headings</code> and <code>Ohc_eject_zh</code>. [[User:Helder.wiki|Helder]] 03:25, 15 August 2014 (UTC)
@{{U|Ohconfucius}}: based on [[gerrit:139877]],it seems there is another option: create a function like
<syntaxhighlight lang="javascript">
function prevDef ( e ) {
e.preventDefault();
}</syntaxhighlight>
and then attach two click handlers to each link, as in
<syntaxhighlight lang="javascript">
$( add('p-tb', '#', 'Body dates to dmy', 't-dmy', 'day month', , ) ).click(prevDef).click(ohc_body_to_dmy_driver);
$( add('p-tb', '#', 'Body dates to mdy', 't-mdy', 'month day', , ) ).click(prevDef).click(ohc_body_to_mdy_driver);
...
</syntaxhighlight>
Then you don't need to add <code>e.preventDefault()</code> to every function. [[User:Helder.wiki|Helder]] 21:46, 15 August 2014 (UTC)
*Brilliant. Much more elegant solution. --<small><span style="background-color:#ffffff;border: 1px solid;">[[User:Ohconfucius|'''<span style="color:#000000; background-color:#00FF00">&nbsp;Ohc&nbsp;</span>''']]</span></small>[[User talk:Ohconfucius|<sup>''¡digame!''</sup>]] 01:30, 16 August 2014 (UTC)
*{{ping|Helder.wiki}}I've just been using some of the new buttons. I've observed that the script seems to place the script button higher up on the sidebar. <p>Also thinking that there is perhaps no advantage to having this instruction as most of my script buttons have a trailing instruction <code>doaction('diff');</code>. Any comments? --<small><span style="background-color:#ffffff;border: 1px solid;">[[User:Ohconfucius|'''<span style="color:#000000; background-color:#00FF00">&nbsp;Ohc&nbsp;</span>''']]</span></small>[[User talk:Ohconfucius|<sup>''¡digame!''</sup>]] 02:44, 16 August 2014 (UTC)</p>
:{{reply to|Ohconfucius}} both <code>mw.util.addPortletLink</code> and the old <code>addPortletLink</code> add the link (by default) [https://github.com/wikimedia/mediawiki-core/blob/master/resources/src/mediawiki/mediawiki.util.js#L341-L342 after the last item] which is in the portlet at the time they are executed (unless you specify where it should be in the 7th parameter, [https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.util-method-addPortletLink see its documentation] - currently you are specifying an empty string). Therefore, since [[Special:PermaLink/621009023|User:Ohconfucius/test/MOSNUM dates.js]] add the link "ALL dates to dmy" before "Body dates to dmy", this will be their relative order in the sidebar. Moreover, if you have multiple scripts adding buttons to the portlet (e.g. [[User:Ohconfucius/script/flagcruft.js|flagcruft.js]] and [[User:Ohconfucius/test/MOSNUM dates code.js|MOSNUM dates code.js]]), when the second script is executed all the buttons from the fist script will be in the portlet already, so the second script will add them after the existing links. So I assume you noticed some change in the order because your scripts are executed in a different order. Notice that <code>imporScript</code> is asynchronous, so the order of two calls to it on e.g. [[User:Ohconfucius/vector.js|your vector.js]] may not be the same in which the imported scripts will be executed. If you need a specific loading order you need to use some kind of callback or hook (there is a related discussion on [[WP:Village pump (technical)#Notify another JavaScript file that my script has finished running?]]).
:I think if you do not prevent the default behavior of the link, user who [[Special:Preferences#mw-prefsection-editing|enabled Live Preview]] would still see the page scrolling up even if you execute <code>doaction('diff');</code>. [[User:Helder.wiki|Helder]] 17:18, 16 August 2014 (UTC)
::Right, I knew it had to be something like that. Thanks fr the explanations. regards, --<small><span style="background-color:#ffffff;border: 1px solid;">[[User:Ohconfucius|'''<span style="color:#000000; background-color:#00FF00">&nbsp;Ohc&nbsp;</span>''']]</span></small>[[User talk:Ohconfucius|<sup>''¡digame!''</sup>]] 02:37, 17 August 2014 (UTC)