Jump to content
MediaWiki

MediaWiki:Gadget-Global-ExcerptTree.js

From mediawiki.org

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
 /**
  * ExcerptTree enables navigation of excerpt trees
  * Documentation: https://www.mediawiki.org/wiki/ExcerptTree
  * License: GNU General Public License (http://www.gnu.org/licenses/gpl.html)
  * Author: Sophivorus
  */
 varExcerptTree={

 /**
 	 * Initialization script
 	 */
 init:function(){
 $('.ExcerptTree').show();
 $('.ExcerptTree li').each(ExcerptTree.addChildren);
 },

 addChildren:function(){
 varnode=$(this),
 title=$(node).children('a').text();
 ExcerptTree.getContent(title).done(function(data){
 if(data.query.pages[0].missing){
 return;
 }
 varcontent=data.query.pages[0].revisions[0].slots.main.content;
 names=mw.config.get('excerpt-templates')||['Excerpt'],
 regexp=newRegExp('{{ *(?:'+names.join('|')+') *\\| *([^}]+) *}}','gi'),
 matches=[...content.matchAll(regexp)];
 if(matches.length){
 ExcerptTree.addButton(node);
 varlist=$('<ul>').hide().data('virgin',true);
 matches.forEach(function(match){
 varparams=match[1].split('|').filter(function(param){
 return/=/.test(param)===false;// Remove all named params
 });
 vartitle=params[0].split('#')[0];
 title=title.charAt(0).toUpperCase()+title.slice(1);// Capitalize the title
 varsection=params[0].split('#')[1]||params[1];
 vartext=section?title+' § '+section:title;
 varhref=mw.util.getUrl((section?title+'#'+section:title));
 varlink=$('<a>',{'href':href}).text(text);
 varitem=$('<li>').html(link);
 list.append(item);
 });
 node.append(list);
 }
 });
 },

 /**
 	 * Add a clickable right-arrow to the given node
 	 */
 addButton:function(node){
 varbutton=$('<span>').text('►').css({
 'cursor':'pointer',
 'position':'absolute',
 'left':'-1em'
 });
 node.css({
 'list-style':'none',
 'position':'relative'
 }).prepend(button);
 button.click(node,ExcerptTree.toggleChildren);
 },

 toggleChildren:function(event){
 varnode=event.data,
 button=$(node).children('span'),
 list=$(node).children('ul'),
 virgin=list.data('virgin');

 list.toggle();
 if(virgin){
 list.data('virgin',false);
 list.children('li').each(ExcerptTree.addChildren);
 button.text('▼');
 }else{
 if(list.is(':visible')){
 button.text('▼');
 }else{
 button.text('►');
 }
 }
 },

 getContent:function(title){
 varapi=newmw.Api();
 returnapi.get({
 titles:title,
 action:'query',
 formatversion:2,
 prop:'revisions',
 rvprop:'content',
 rvslots:'main',
 redirects:true
 });
 }
 };

 mw.loader.using('mediawiki.api',ExcerptTree.init);

AltStyle によって変換されたページ (->オリジナル) /