User talk:The Transhumanist/ViewAnnotationToggler.js: Difference between revisions

Content deleted Content added
Talk-through 2: {{Further|Pseudocode}}
m Replaced deprecated <source> tags with <syntaxhighlight>
 
(33 intermediate revisions by 2 users not shown)
Line 1:
{{User:The Transhumanist/Workshop boilerplate/Lead hatnote}}
 
: '''''This script is operational: its main features work; it is under further development'''''
 
AnnotationToggler = Annotation Toggler. It adds a tab menu item to toggle list item (LI) annotations.
 
= Script's workshop =
: ''This is the work area for developing the script and its documentation. The talk page portion of this page starts at [[#Discussions]], below.''
Line 4 ⟶ 10:
== Description / instruction manual ==
 
: '''''This script is operational: its main features work; it is under further development'''''
 
ViewAnnotationToggler.js: adds a tab menu item and hot key to turn list item annotations off and on, across all pages..
== Explanatory notes (source code walk-through) ==
 
The hot key is {{keypress|Shift|Alt|a}}.
This section explains the source code, in detail.
 
This script works on the list items in bulleted lists, lists in which each item is
You can only use so many comments in the source code before you start to choke or bury the programming itself. So, I've put short summaries in the source code, and have provided in-depth explanations here. My intention is twofold:
preceded by a bullet. Bulleted lists abound upon Wikipedia. They are used in stand-alone
# to thoroughly document the script so that even relatively new JavaScript beginners can understand what it does.
lists which are entire articles that are lists, and they are used in embedded lists
# to refresh my memory of exactly how the script works, in case I don't look at the source code (or any JavaScript) for weeks or months.
situated within articles.
 
Many list items have an annotation, that is, the list item is followed by a description.
The explanatory notes include examples, and links to relevant documentation pages, tutorials, etc.
These descriptions are useful, but they may obscure the items in the list that they
describe. Sometimes, it is useful to look at the bare list, without the annotations.
 
This script turns those descriptions on and off. It provides a tab menu command and
In addition to some standard [[JavaScript]] code, this script also relies heavily on the [[jQuery]] library.
a hotkey for doing so.
 
When you don't need to see the descriptions, turn them off. When you need more detail,
If you have any questions, feel free to ask me ''[[#Discussions|at the bottom of this page under '''Discussions''']]''. Trying to answer them will help me learn JavaScript better.
hit the toggle again, and the descriptions return. The script stores its status so it
doesn't start over between pages &mdash; when annotations are turned off, they are off for
all pages until you turn them back on again.
 
{{User:The Transhumanist/Workshop boilerplate/Install}}
 
{{User:The Transhumanist/Workshop boilerplate/Explanatory notes}} <!--includes h2 heading-->
 
=== General approach ===
Line 63 ⟶ 81:
</syntaxhighlight>
 
''For the best explanation of the bodyguard function I've found so far, see: [http://codeimpossible.com/2010/01/13/solving-document-ready-is-not-a-function-and-other-problems/ Solving "$(document).ready is not a function" and other problems]''
 
=== The ready() event listener/handler ===
Line 69 ⟶ 87:
The ready() event listener/handler makes the rest of the script wait until the page (and its [[Document Object Model|DOM]]) is loaded and ready to be worked on. If the script tries to do its thing before the page is loaded, there won't be anywhere for it to place the menu item (mw.util.addPortletLink), and the script will fail.
 
ItIn jQuery, it looks like this: [http://learn.jquery.com/using-j\query-core/document-ready/ <code>$( document ).ready() {});</code>]
 
The part of the script that is being made to wait goes inside the curly brackets. (startingBut you would generally start that on the next line, withand put the ending curly bracket, closing parenthesis, and semicolon following that on a line of their own)., like this:
 
See also: http://docs.jquery.com/Tutorials:Introducing_$(document).ready()
 
The jQuery shorthand version of the same thing looks like this:
 
<syntaxhighlight lang="javascript">
1 $(function() {<br>
2 // Body of function (or even the rest of the script) goes here, such as a click handler.<br>
2 // Handler for .ready() called.<br>
3 });
</syntaxhighlight>
 
''This is all explained further onat [https://api.jquery.com/ready/ the jQuery page for <code>.ready()</code>]''
 
For the plain vanilla version see: http://docs.jquery.com/Tutorials:Introducing_$(document).ready()
 
=== Prep work ===
Line 89 ⟶ 105:
==== var ====
 
This is a [[reserved word]] [https://www.w3schools.com/js/js_reserved.asp reserved] (aka "keyword]"). The keyword [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var var] is used to declare variables in variable statements. A variable is a container you can put a value in. To declare the variable portletlink, write this:
 
<syntaxhighlight lang="javascript">
Line 108 ⟶ 124:
* [https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById document.getElementById()]
 
A key use of this method is to assign the contents of an element to a variable. (''[[#cont.outerHTML|See below]]'').
 
==== "mw-content-text" ====
Line 114 ⟶ 130:
<code>mw-content-text</code> is an element ID. Unlike classes, IDs are unique (or are supposed to be).
 
Unfortunately, this ID is not covered in [[WP:IDS]].
 
But it is included in the list [[mw:Manual:Interface/IDs and classes]]:
Line 189 ⟶ 205:
First, it checks if the Vector skin is being used and runs the rest of the script only if it is.
 
Then it checks the status of the script, that is, whether it was set to "show" or "hide" the last time it was used.
 
Then it calls the function that corresponds with the current status (calling either annoHide or annoShow, defaulting to annoShow if there is no status).
Line 253 ⟶ 269:
==== localStorage.setItem ====
 
The [https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage <code>localStorage</code> property] allows you to store some data in the browser's memory area for later use, so you don't lose it when changing pages or closing the browser.
 
What we use it for in the script is to store the script's show/hide status, so we can check it (each time the script starts) to see whether the script is supposed to execute the annoHide or the annoShow function. To create a data item in localStorage named "annostatus" for storing the value "show", we do this:
Line 311 ⟶ 327:
</syntaxhighlight>
 
It has up to 7 parameters, with the first 3 being required. Only those 3 are used above.
 
'''Important:''' It won't do anything until you bind it to a click handler (see below).
 
General usage:
<syntaxhighlight lang="javascript">
mw.util.addPortletLink( 'portletId', 'href', 'text', 'id', 'tooltip', 'accesskey', 'nextnode');
</syntaxhighlight>
 
It's components:
<code>mw.util.addPortletLink( portletId, href, text [, id [, tooltip [, accesskey [, nextnode ]]]] );</code>
* <code>mw.util.addPortletLink</code>: the ResourceLoader module to add links to the portlets.
* <code>portletId</code>: portlet id— the section where the new menu item is to be placed. Valid values:
** <code>p-navigation</code>: Navigation section in left sidebar
** <code>p-interaction</code>: Interaction section in left sidebar
** <code>p-tb</code>: Toolbox section in left sidebar
** <code>coll-print_export</code>: Print/export section in left sidebar
** <code>p-personal</code> Personal toolbar at the top of the page
** <code>p-views</code> Upper right tabs in Vector only (read, edit, history, watch, etc.)
** <code>p-cactions</code> Drop-down menu containing move, etc. (in Vector); subject/talk links and action links in other skins
* <code>href</code>: Link to the Wikipedia or external page
* <code>text</code>: Text that displays
* <code>id</code>: HTML id (optional)
* <code>tooltip</code>: Tooltip to display on mouseover (optional)
* <code>accesskey</code>: Shortcut key press (optional)
* <code>nextnode</code>: Existing portlet link to place the new portlet link before (optional)
 
The optional fields must be included in the above order. To skip a field without changing it, use the value <var>null</var>.
* portletID = ID of the menu you want to add the menu item to
* href =
* text = the name of the menu item as it is to appear in the menu
* ID = element ID
* tooltip = the tip that shows up when you hover the mouse cursor over the menu item
* accesskey = the name of the hot key (without the Shift-alt)
* nextnode =
 
''For the documentation on this function, see https://www.mediawiki.org/wiki/ResourceLoader/Modules#addPortletLink'' and [[Help:Customizing toolbars]].
 
'''Important:''' It won't do anything until you bind it to a click handler (see below).
Line 341 ⟶ 369:
4 }
</syntaxhighlight>
 
 
The "handler" is the part between the curly brackets.
Line 351 ⟶ 378:
== Change log ==
 
* 2017-07-07
** Version 0.4 &ndash; [https://en.wikipedia.org/w/index.php?title=User:The_Transhumanist/anno.js&oldid=789519032 ready to embark on fixing the viewport]
* 2017-07-06
** Moved the functions to the end of the script to make it easier to follow; marked the sections clearly
* 2017-07-05
** Added document ready function, so the script waits until the DOM is loaded before running
** Changed menu items to "Annotations (show)" and "Annotations (hide)"
* 2017-01-19
** Version 0.3 &ndash; [https://en.wikipedia.org/w/index.php?title=User:The_Transhumanist/anno.js&oldid=760834841 Simplified the script to wrap annotations and then hide or show them].
* 2016-12-11
** Version 0.2 &ndash; [https://en.wikipedia.org/w/index.php?title=User:The_Transhumanist/anno.js&oldid=754184988 on/off status persists across pages].
* 2016-12-08
** Anchor regexes on <nowiki><li></nowiki> (starting only) element delimiters
Line 368 ⟶ 384:
*** Uses regex to remove annotations
*** Uses a toggle to switch back and forth between the two states
* 2016-12-11
** Version 0.2 &ndash; [https://en.wikipedia.org/w/index.php?title=User:The_Transhumanist/anno.js&oldid=754184988 on/off status persists across pages].
* 2017-01-19
** Version 0.3 &ndash; [https://en.wikipedia.org/w/index.php?title=User:The_Transhumanist/anno.js&oldid=760834841 Simplified the script to wrap annotations and then hide or show them].
* 2017-07-05
** Added document ready function, so the script waits until the DOM is loaded before running
** Changed menu items to "Annotations (show)" and "Annotations (hide)"
* 2017-07-06
** Moved the functions to the end of the script to make it easier to follow; marked the sections clearly
* 2017-07-07
** Version 0.4 &ndash; [https://en.wikipedia.org/w/index.php?title=User:The_Transhumanist/anno.js&oldid=789519032 ready to embark on fixing the viewport]
* 2018-02-23
** Changed name to ViewAnnotationToggler.js
** removed activation filter so it works on all pages
* 2018-03-13
** Script editor wouldn't work while ViewAnnotationToggler was running, so added a deactivation filter so that this script doesn't run on edit pages.
 
== Task list ==
 
=== Bug reports ===
 
=== Desired/completed features ===
 
: ''Completed features are marked with {{done}}''
 
== Desired features ==
Improvements needed:
 
* Don't run on move pages
* Since it hides content, an indicator is needed to show when it is on/off.
* Fix the conflict with sidebar toggle (it also uses hot-key Shift-Alt-a)
Line 388 ⟶ 428:
http://stackoverflow.com/questions/9614622/equivalent-of-jquery-hide-to-set-visibility-hidden
 
It states:
There isn't one [(a jQuery function)] built in but you could write your own quite easily:
 
Line 415 ⟶ 455:
 
Here's a [http://jsfiddle.net/nGhjQ/ working example].
 
 
=== Find/replace annotations of other formats ===
Line 436 ⟶ 475:
** The topic?
** The bullet?
 
== Completed features ==
 
 
=== Program on/off switch ===
Line 465 ⟶ 501:
 
https://en.wikipedia.org/w/index.php?title=User:The_Transhumanist/anno.js&oldid=760834841
 
 
It shrank by several lines of code. [[User talk:The Transhumanist|<i>The&nbsp;Transhumanist</i>]] 13:27, 19 January 2017 (UTC)
 
== Development notes ==
 
=== Trycatch needed, and more ===
:The Transhumanist, where you use local storage.getItem() or setItem() you should always wrap that in try catch, as it can fail at any moment (even if you checked previously). This can be due to the browser running out of storage space for the ___domain, or because the browser is running in privacy mode or with an ad blocker extensions or something. Also, your new RegExp() calls should be lifted outside of the for loops, so that they aren't continuously recreated. For wpTextbox1.value, realise that sometimes the content might be managed by an editor (The syntaxhighlighting beta does this for instance). We use the [https://phabricator.wikimedia.org/source/mediawiki/browse/master/resources/src/jquery/jquery.textSelection.js jquery.textSelection plugin] to abstract way from these differences. Don't check document.title, check mw.config.get( 'wgTitle' ) or mw.config.get( 'wgPageName' ). And when you use mw.util.addPortlink, you have to ensure that the mediawiki.util plugin is loaded already, which you can do by using [https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.loader mw.loader.using]. —[[User:TheDJ|Th<span style="color: green">e</span>DJ]] ([[User talk:TheDJ|talk]] • [[Special:Contributions/TheDJ|contribs]]) 14:47, 27 October 2017 (UTC)
 
=== Links to related discussions ===
Line 475 ⟶ 513:
* [[Wikipedia talk:WikiProject JavaScript#I'm stumped: How do you save a ___location in the viewport to reposition the viewport later?]] (May/June 2017)
* [[User talk:Anomie/Archives/2017#I'm stumped]](Jan 2017)
** wrap with span and toggle display property
** [https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint Document.elementFromPoint()]
* [[Wikipedia:Reference desk/Archives/Computing/2016 December 9#Scroll position in JavaScript]] (Dec 2016)
Line 494 ⟶ 532:
=== Injecting/inserting/embedding style ===
 
One way to hide/show a class is to define that class on the page, and then toggle its display property. Where are the instructions on how to do this?
 
Here are some related links:
Line 737 ⟶ 775:
 
=== Find/verify non-li list-lead entries ===
 
=== Fix annotation delimiters (to ndashes) ===
 
See [[User:GregU/dashes.js]].
 
= Discussions =
 
Post new discussion threads below.
 
== The Bug Report ==
 
Hi, {{ping|The Transhumanist}}. I've discovered an unintended side-effect caused by a line of code in your script, or should I say ''the'' line of code.
 
<syntaxhighlight lang="JavaScript>
cont.outerHTML = cont.outerHTML.replace(/(<li>.*?)( –.*)/g,'$1<span class="anno">$2</span>');
</syntaxhighlight>
 
This code strips action handlers from DOM elements which are descendants of <code>mw-content-text</code>. This notably affects 'buttons', such as the 'show/hide' button in the following templates:
 
{{collapse top}}
Peekaboo.
{{collapse bottom}}
 
{{Fragaria|state=expanded}}
 
You ''might'' be able to prevent this by iterating through <code>&lt;li&gt;</code> elements, like so:
 
<syntaxhighlight lang="JavaScript">
$("#mw-content-text li").each(function()
{
$(this).html($(this).html().replace(/(.*?)( –.*)/g,'$1<span class="anno">$2</span>'));
});
</syntaxhighlight>
 
Let me know if this helps! Regards, <span style="font-family:Times New Roman">[[User:Guywan|''GUYWAN'']] ( [[User talk:Guywan|''t'']] &middot; [[Special:Contributions/Guywan|''c'']] )</span> 12:20, 6 June 2019 (UTC)