MediaWiki

Difference between revisions of "Common.js"

m (Document legacy code.)
m (JS beautify (proper and consistent indentation).)
Line 10: Line 10:
 
// LastAudiosBoxes
 
// LastAudiosBoxes
 
if (/^LinguaLibre:Main/.test(mw.config.get('wgPageName'))) {
 
if (/^LinguaLibre:Main/.test(mw.config.get('wgPageName'))) {
    console.log('🎸 Last Audios');
+
  console.log('🎸 Last Audios');
    mw.loader.load('/index.php?title=MediaWiki:LastAudios.js&action=raw&ctype=text/javascript');
+
  mw.loader.load('/index.php?title=MediaWiki:LastAudios.js&action=raw&ctype=text/javascript');
 
}
 
}
 
// SoundLibrary
 
// SoundLibrary
 
if (/^LinguaLibre:Explore_the_sound_library(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
 
if (/^LinguaLibre:Explore_the_sound_library(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
    console.log('🥁 Sound library');
+
  console.log('🥁 Sound library');
    mw.loader.load('/index.php?title=MediaWiki:SoundLibrary.js&action=raw&ctype=text/javascript');
+
  mw.loader.load('/index.php?title=MediaWiki:SoundLibrary.js&action=raw&ctype=text/javascript');
 
}
 
}
 
// Lexeme Queries Generator (OOUI)
 
// Lexeme Queries Generator (OOUI)
if(/^Template:LexemeQueriesGenerator$/.test( mw.config.get( 'wgPageName' )) || /^Help:SPARQL_2$/.test( mw.config.get( 'wgPageName' )) ) {
+
if (/^Template:LexemeQueriesGenerator$/.test(mw.config.get('wgPageName')) || /^Help:SPARQL_2$/.test(mw.config.get('wgPageName'))) {
    console.log('📔 Lexeme Queries Generator');
+
  console.log('📔 Lexeme Queries Generator');
    mw.loader.load( '/index.php?title=MediaWiki:LexemeQueriesGenerator.js&action=raw&ctype=text/javascript');
+
  mw.loader.load('/index.php?title=MediaWiki:LexemeQueriesGenerator.js&action=raw&ctype=text/javascript');
 
}
 
}
 
// SPARQL to data (OOUI)
 
// SPARQL to data (OOUI)
if(/SPARQL/.test( mw.config.get( 'wgPageName' )) || /parql/.test( mw.config.get( 'wgPageName' )) ) {
+
if (/SPARQL/.test(mw.config.get('wgPageName')) || /parql/.test(mw.config.get('wgPageName'))) {
    console.log('🚀 SPARQL to data');
+
  console.log('🚀 SPARQL to data');
    mw.loader.load( '/index.php?title=MediaWiki:Sparql2data.js&action=raw&ctype=text/javascript');
+
  mw.loader.load('/index.php?title=MediaWiki:Sparql2data.js&action=raw&ctype=text/javascript');
 
}
 
}
 
/* In development *************************************** */
 
/* In development *************************************** */
 
// Language overview
 
// Language overview
if(/Languages/.test( mw.config.get( 'wgPageName' )) ) {
+
if (/Languages/.test(mw.config.get('wgPageName'))) {
    console.log('🌏 Languages gallery');
+
  console.log('🌏 Languages gallery');
    mw.loader.load( '/index.php?title=MediaWiki:LanguagesGallery.js&action=raw&ctype=text/javascript');
+
  mw.loader.load('/index.php?title=MediaWiki:LanguagesGallery.js&action=raw&ctype=text/javascript');
 
}
 
}
 
// Search by words, see https://jsfiddle.net/hugolpz/ecpzy0fo/89/
 
// Search by words, see https://jsfiddle.net/hugolpz/ecpzy0fo/89/
 
if (/^LinguaLibre:Search_by(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
 
if (/^LinguaLibre:Search_by(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
    console.log('🐣 Search by word');
+
  console.log('🐣 Search by word');
    mw.loader.load('/index.php?title=MediaWiki:SearchBy.js&action=raw&ctype=text/javascript');
+
  mw.loader.load('/index.php?title=MediaWiki:SearchBy.js&action=raw&ctype=text/javascript');
 
}
 
}
 
// Minorities languages statistics
 
// Minorities languages statistics
Line 60: Line 60:
 
           'action': 'wbgetentities',
 
           'action': 'wbgetentities',
 
           'format': 'json',
 
           'format': 'json',
           'ids': node.text(),   // some Qid
+
           'ids': node.text(), // some Qid
           'props': 'labels|descriptions',   // get labels and descriptions...
+
           'props': 'labels|descriptions', // get labels and descriptions...
           'languages': lang, // in user's language
+
           'languages': lang, // in user's language
 
           'languagefallback': 1,
 
           'languagefallback': 1,
 
           'origin': '*'
 
           'origin': '*'
 
         }).then(function(data) {
 
         }).then(function(data) {
        // Use returned data:
+
          // Use returned data:
 
           var entity = data.entities[node.text()],
 
           var entity = data.entities[node.text()],
             label = (entity.labels[lang] !== undefined ?  
+
             label = (entity.labels[lang] !== undefined ?
             entity.labels[lang].value + ' <i id="cmjs-label">(' + node.text() + ')</i>'  
+
             entity.labels[lang].value + ' <i id="cmjs-label">(' + node.text() + ')</i>'
                 :'<text id="cmjs-label">'+ node.text()+'</text>'),
+
                 :'<text id="cmjs-label">' + node.text() + '</text>'),
             description = (entity.descriptions[lang] !== undefined ? '<small id="cmjs-description">' + entity.descriptions[lang].value + '</small>' : '<small id="cmjs-description"></small>');
+
             description = (entity.descriptions[lang] !== undefined ?
// Inject created elements
+
            '<small id="cmjs-description">' + entity.descriptions[lang].value + '</small>'
 +
                : '<small id="cmjs-description"></small>');
 +
          // Inject created elements
 
           node.html(label + '<br>' + description);
 
           node.html(label + '<br>' + description);
 
         });
 
         });
Line 91: Line 93:
 
// Create audio player element, in OO.ui style
 
// Create audio player element, in OO.ui style
 
const BASE_FILE_URL = 'https://commons.wikimedia.org/wiki/Special:Redirect/file?wptype=file&wpvalue=';
 
const BASE_FILE_URL = 'https://commons.wikimedia.org/wiki/Special:Redirect/file?wptype=file&wpvalue=';
 +
 
function playButton(audioUrl) {
 
function playButton(audioUrl) {
 
   var button = new OO.ui.ButtonWidget({
 
   var button = new OO.ui.ButtonWidget({
Line 119: Line 122:
 
(works from the second step onwards) */
 
(works from the second step onwards) */
 
$(function() {
 
$(function() {
if (mw.config.get('wgPageName') !== 'Special:RecordWizard') {
+
  if (mw.config.get('wgPageName') !== 'Special:RecordWizard') {
 
     return;
 
     return;
}
+
  }
window.onbeforeunload = function() {
+
  window.onbeforeunload = function() {
 
     return "Do you want to leave the page? All your unsaved changes will be lost.";
 
     return "Do you want to leave the page? All your unsaved changes will be lost.";
};
+
  };
 
});
 
});
 
/*
 
/*

Revision as of 17:09, 27 May 2022

// Page-specific scripts modules and conditional loading
// General sugar
// RecordWizard sugar

/* *************************************************************** */
/* PAGE-SPECIFIC SCRIPTS LOADINGS ******************************** */
// Documentation: https://m.mediawiki.org/wiki/ResourceLoader/Architecture#Resource:_Scripts
// Documentation: https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.loader
//🎷🥁🎸🐣📔🗻🌏
// LastAudiosBoxes
if (/^LinguaLibre:Main/.test(mw.config.get('wgPageName'))) {
  console.log('🎸 Last Audios');
  mw.loader.load('/index.php?title=MediaWiki:LastAudios.js&action=raw&ctype=text/javascript');
}
// SoundLibrary
if (/^LinguaLibre:Explore_the_sound_library(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
  console.log('🥁 Sound library');
  mw.loader.load('/index.php?title=MediaWiki:SoundLibrary.js&action=raw&ctype=text/javascript');
}
// Lexeme Queries Generator (OOUI)
if (/^Template:LexemeQueriesGenerator$/.test(mw.config.get('wgPageName')) || /^Help:SPARQL_2$/.test(mw.config.get('wgPageName'))) {
  console.log('📔 Lexeme Queries Generator');
  mw.loader.load('/index.php?title=MediaWiki:LexemeQueriesGenerator.js&action=raw&ctype=text/javascript');
}
// SPARQL to data (OOUI)
if (/SPARQL/.test(mw.config.get('wgPageName')) || /parql/.test(mw.config.get('wgPageName'))) {
  console.log('🚀 SPARQL to data');
  mw.loader.load('/index.php?title=MediaWiki:Sparql2data.js&action=raw&ctype=text/javascript');
}
/* In development *************************************** */
// Language overview
if (/Languages/.test(mw.config.get('wgPageName'))) {
  console.log('🌏 Languages gallery');
  mw.loader.load('/index.php?title=MediaWiki:LanguagesGallery.js&action=raw&ctype=text/javascript');
}
// Search by words, see https://jsfiddle.net/hugolpz/ecpzy0fo/89/
if (/^LinguaLibre:Search_by(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
  console.log('🐣 Search by word');
  mw.loader.load('/index.php?title=MediaWiki:SearchBy.js&action=raw&ctype=text/javascript');
}
// Minorities languages statistics

/* *************************************************************** */
/* GENERAL SUGAR (?) ********************************************* */
// Enhance external ids, such as Property:P12 (Wikidata ID), adds to its value their [label, description]
// Schematic example: "P12: Q25167" ⇒ "P12: bokmål (Q25167) langue/dialect"
// Currently Wikidata only.
if ($('.wb-external-id').length > 0) {
  mw.loader.using('mediawiki.ForeignApi', function() {
    $('.wb-external-id').each(function() {
      if ($(this).attr('href').lastIndexOf('https://www.wikidata.org', 0) === 0) {
        var wikidataApi = new mw.ForeignApi('https://www.wikidata.org/w/api.php', {
            anonymous: true,
            parameters: { 'origin': '*' },
            ajax: { timeout: 10000 }
          }),
          lang = mw.config.get('wgUserLanguage'),
          node = $(this);
        wikidataApi.get({
          'action': 'wbgetentities',
          'format': 'json',
          'ids': node.text(), // some Qid
          'props': 'labels|descriptions', // get labels and descriptions...
          'languages': lang, // in user's language
          'languagefallback': 1,
          'origin': '*'
        }).then(function(data) {
          // Use returned data:
          var entity = data.entities[node.text()],
            label = (entity.labels[lang] !== undefined ?
            	entity.labels[lang].value + ' <i id="cmjs-label">(' + node.text() + ')</i>'
                :'<text id="cmjs-label">' + node.text() + '</text>'),
            description = (entity.descriptions[lang] !== undefined ?
            	'<small id="cmjs-description">' + entity.descriptions[lang].value + '</small>'
                : '<small id="cmjs-description"></small>');
          // Inject created elements
          node.html(label + '<br>' + description);
        });
      }
    });
  });
}

// On record wikibase item, add audio player element
// If has records, has language, has language indicator...
/* 
if ($('#P3') && $('#P4') && $('.wb-language-fallback-indicator')) {
    $('#P4 .wikibase-statementview-mainsnak a').each(function() {
      $('.wb-language-fallback-indicator').text( $(this).text() );
    });
} */

// Create audio player element, in OO.ui style
const BASE_FILE_URL = 'https://commons.wikimedia.org/wiki/Special:Redirect/file?wptype=file&wpvalue=';

function playButton(audioUrl) {
  var button = new OO.ui.ButtonWidget({
    framed: false,
    icon: 'play',
    title: 'play'
  });
  button.on('click', function() {
    var audio = new Audio(audioUrl);
    audio.play();
  });
  return button.$element;
}

// On record wikibase item, add audio player element
if ($('#P3 a.extiw').length > 0) {
  mw.loader.using(['oojs-ui-widgets', 'oojs-ui.styles.icons-media'], function() {
    $('#P3 a.extiw').each(function() {
      var $node = $(this);
      $node.before(playButton(BASE_FILE_URL + $node.text()));
    });
  });
}

/* *************************************************************** */
/* RECORD WIZARD SUGAR ******************************************* */
/*Confirmation message when users try to leave the Record Wizard
(works from the second step onwards) */
$(function() {
  if (mw.config.get('wgPageName') !== 'Special:RecordWizard') {
    return;
  }
  window.onbeforeunload = function() {
    return "Do you want to leave the page? All your unsaved changes will be lost.";
  };
});
/*
if (mw.config.get('wgPageName') === 'Special:RecordWizard') {
    window.onbeforeunload = function() {
        return "Do you want to leave the page? All your unsaved changes will be lost.";
    };
}
*/