Difference between revisions of "MediaWiki:Common.js"

Line 11: Line 11:
 
     } );
 
     } );
 
}
 
}
 +
///////////////////////
 +
// Table Filter/Sort //
 +
///////////////////////
 +
/**
 +
* @description Table Filter/Sort
 +
* TODO ?replace by jQuery plugin datatable?
 +
*
 +
* @requires $.resource()
 +
* @requires $.jI18n.en
 +
* @returns {undefined}
 +
*/
 +
window.initTableFilterSort = function (){ // see MediaWiki:SortTableFilter.js
 +
/* Note: problem is auto-inserted <tbody></tbody> by the browser: must be removed and
 +
  replaced by thead + tbody. jQuery.unwrap() was not successful */
 +
  // add possibly more classes from http://www.javascripttoolbox.com/lib/table/documentation.php
 +
  // Note: only applies to non-nested tables
 +
  var jAutotables = $('table.table-autosort, table.table-autofilter, table-autostripe,table-sorted-asc,table-sorted-desc, table-filtered');
 +
  if (jAutotables.length) {
 +
    // Note: in MediaWiki:SortTableFilter.js 'SortTableFilter_InputFilterTitle' was not recognized
 +
    $.extend(true, $.jI18n, {
 +
      en: {
 +
        SortTableFilter_AutoSortTitle : 'Click to sort',
 +
        SortTableFilter_FilterAllLabel: 'Filter: All',
 +
        SortTableFilter_InputFilterTitle: 'Filter text (case sensitive, uses reg. expressions)'
 +
      },
 +
      de: {
 +
        SortTableFilter_AutoSortTitle : 'Zum Sortieren klicken',
 +
        SortTableFilter_FilterAllLabel: 'Zeige: alle',
 +
        SortTableFilter_InputFilterTitle: 'Text filtern (GROß/klein!, nutzt reg. Ausdrücke)'
 +
      }
 +
    });
 +
    // get sortable/filterable here already otherwise multiple th-filters
 +
    $.getScript(mw.config.get( 'wgServer' ) + mw.config.get( 'wgScript' ) + '?title=MediaWiki:SortTableFilter.js&action=raw&ctype=text/javascript',
 +
      function(){return true;});
 +
    // modify tables to introduce thead structure
 +
    jAutotables.each(function(index){ //TODO simplify code? if()…
 +
      // There may or may not be a tbody around tr. NOTE: $.unwrap() does not work here!
 +
      // Memo: find('tr th') finds th, .parent() retrieves tr! .wrapAll will wrap inside DOM, not in return value! .detach() returns detached
 +
      // OK in FF 3.6 and IE 7-8, not in IE6 (like the old code)
 +
      // FURTHER WORK: Ideally, all normal tr td should remain in a tbody.
 +
      var jThis = $(this),
 +
        jThead = jThis.find('tr th').parent().detach(),
 +
        jTfoot = jThis.find('tr[class=tfoot] td').parent().detach(),// remove it from the DOM
 +
        jTbody = jThis.find('tbody:first');
 +
      if (jTbody.length===0) {
 +
        jThis.children().wrapAll('<tbody/>');
 +
        jTbody = jThis.find('tbody:first');
 +
      }
 +
      jTbody.before($('<thead/>').append(jThead));
 +
      if (jTfoot.length) {
 +
        jTbody.after($('<tfoot/>').append(jTfoot));
 +
      }
 +
      // th with class="input" gives an input field instead of selections
 +
      $(this).find('th[class=input]').append('<input name="filter" title="'+$.resource('SortTableFilter_InputFilterTitle')+'" size="8" onkeyup="Table.filter(this,this)">');
 +
    });// end each()
 +
  } // END if any autotable
 +
};
 +
 +
/**
 +
* @description: Collapse the TOC using id="toc-autocollapse"
 +
* @returns {undefined}
 +
*/
 +
window.initTOCautocollapse = function () {
 +
  if ($('#toc-autocollapse').length) {
 +
    $('#togglelink').click();
 +
  }
 +
};// end initTOCautocollapse()
 +
 +
/**
 +
* Add a placeholder message and checks on Special:Contact
 +
*
 +
* Due to e-mail configurations all non @mfn-berlin.de addresses fail to work
 +
* as from-address on Special:Contact. This functions adds a placeholder and checks the
 +
* user’s input
 +
* @requires extension:ContactPage
 +
* @requires mw.config
 +
* @requires jQuery
 +
*/
 +
window.prepareSpecialContact4Info = function () {
 +
  var placeholderMessage = '', warningMessageDoesNotWork = '';
 +
  switch (mw.config.get( 'wgCanonicalSpecialPageName' )) {
 +
    case 'Contact': // is a Special:Contact page regardless of language
 +
      switch (mw.config.get('wgPageName')) {
 +
        case 'Special:Contact':
 +
        case 'Spécial:Contact':
 +
          placeholderMessage = 'Leave it empty or add e-mail in message (mfn-berlin.de works only)';
 +
          warningMessageDoesNotWork = 'Only mfn-berlin.de addresses do work. Leave it empty please and add your address in the message. Thank you.';
 +
          break;
 +
        case "Spezial:Kontakt":
 +
          placeholderMessage = 'E-Mail in Nachricht dazu oder leer lassen (nur mfn-berlin.de geht)';
 +
          warningMessageDoesNotWork = 'Nur mfn-berlin.de Adressen können verarbeitet werden. Bitte leer lassen oder E-Mail im Text angeben. Danke.';
 +
          break;
 +
        default:
 +
          placeholderMessage = 'Leave it empty or add e-mail in message (mfn-berlin.de works only)';
 +
          warningMessageDoesNotWork = 'Only mfn-berlin.de addresses do work. Leave it empty please and add your address in the message. Thank you.';
 +
          break;
 +
      }
 +
      // check input
 +
      if (!$('input[name="wpFromAddress"]').val() || $('input[name="wpFromAddress"]').val().match(/.*@mfn-berlin.de/)) {
 +
        $('input[name="wpFromAddress"]')
 +
          .css({'background-color': '', 'cursor': ''})
 +
          .attr({'title': ''});
 +
      } else {
 +
        $('input[name="wpFromAddress"]')
 +
          .css({'background-color': 'orange', 'cursor': 'help'})
 +
          .attr({'title': warningMessageDoesNotWork});
 +
      }
 +
      $('input[name="wpFromAddress"]').on('focusout', function () {
 +
        if (!$(this).val() || $(this).val().match(/.*@mfn-berlin.de/)) {
 +
          $(this).css({'background-color': '', 'cursor': 'help'}).attr({'title': warningMessageDoesNotWork});
 +
        } else {
 +
          $(this).css({'background-color': 'orange', 'cursor': 'help'}).attr({'title': warningMessageDoesNotWork});
 +
        }
 +
      });
 +
      $('input[name="wpFromAddress"]').attr({'placeholder': placeholderMessage});
 +
 +
    break;
 +
  }// switch is a contact page
 +
};// end prepareSpecialContact4Info()

Revision as of 18:06, 16 November 2018

$(function(){
	importArticles({
		type: "script",
		articles: ["dahuawiki.com:MediaWiki:SortTableFilter.js"]
	});
});
var $sortableTables = $content.find( 'table.sortable' );
if ( $sortableTables.length ) {
    mw.loader.using( 'jquery.tablesorter' ).then( function () {
        $sortableTables.tablesorter();
    } );
}
///////////////////////
// Table Filter/Sort //
///////////////////////
/**
 * @description Table Filter/Sort
 * TODO ?replace by jQuery plugin datatable?
 *
 * @requires $.resource()
 * @requires $.jI18n.en
 * @returns {undefined}
 */
window.initTableFilterSort = function (){ // see MediaWiki:SortTableFilter.js
/* Note: problem is auto-inserted <tbody></tbody> by the browser: must be removed and
   replaced by thead + tbody. jQuery.unwrap() was not successful */
   // add possibly more classes from http://www.javascripttoolbox.com/lib/table/documentation.php
  // Note: only applies to non-nested tables
  var jAutotables = $('table.table-autosort, table.table-autofilter, table-autostripe,table-sorted-asc,table-sorted-desc, table-filtered');
  if (jAutotables.length) {
    // Note: in MediaWiki:SortTableFilter.js 'SortTableFilter_InputFilterTitle' was not recognized
    $.extend(true, $.jI18n, {
      en: {
        SortTableFilter_AutoSortTitle : 'Click to sort',
        SortTableFilter_FilterAllLabel: 'Filter: All',
        SortTableFilter_InputFilterTitle: 'Filter text (case sensitive, uses reg. expressions)'
      },
      de: {
        SortTableFilter_AutoSortTitle : 'Zum Sortieren klicken',
        SortTableFilter_FilterAllLabel: 'Zeige: alle',
        SortTableFilter_InputFilterTitle: 'Text filtern (GROß/klein!, nutzt reg. Ausdrücke)'
      }
    });
    // get sortable/filterable here already otherwise multiple th-filters
    $.getScript(mw.config.get( 'wgServer' ) + mw.config.get( 'wgScript' ) + '?title=MediaWiki:SortTableFilter.js&action=raw&ctype=text/javascript',
      function(){return true;});
    // modify tables to introduce thead structure
    jAutotables.each(function(index){ //TODO simplify code? if()…
      // There may or may not be a tbody around tr. NOTE: $.unwrap() does not work here!
      // Memo: find('tr th') finds th, .parent() retrieves tr! .wrapAll will wrap inside DOM, not in return value! .detach() returns detached
      // OK in FF 3.6 and IE 7-8, not in IE6 (like the old code)
      // FURTHER WORK: Ideally, all normal tr td should remain in a tbody.
      var jThis = $(this),
        jThead = jThis.find('tr th').parent().detach(),
        jTfoot = jThis.find('tr[class=tfoot] td').parent().detach(),// remove it from the DOM
        jTbody = jThis.find('tbody:first');
      if (jTbody.length===0) {
        jThis.children().wrapAll('<tbody/>');
        jTbody = jThis.find('tbody:first');
      }
      jTbody.before($('<thead/>').append(jThead));
      if (jTfoot.length) {
        jTbody.after($('<tfoot/>').append(jTfoot));
      }
      // th with class="input" gives an input field instead of selections
      $(this).find('th[class=input]').append('<input name="filter" title="'+$.resource('SortTableFilter_InputFilterTitle')+'" size="8" onkeyup="Table.filter(this,this)">');
    });// end each()
  } // END if any autotable
};

/**
 * @description: Collapse the TOC using id="toc-autocollapse"
 * @returns {undefined}
 */
window.initTOCautocollapse = function () {
  if ($('#toc-autocollapse').length) {
    $('#togglelink').click();
  }
};// end initTOCautocollapse()

/**
 * Add a placeholder message and checks on Special:Contact
 *
 * Due to e-mail configurations all non @mfn-berlin.de addresses fail to work
 * as from-address on Special:Contact. This functions adds a placeholder and checks the
 * user’s input
 * @requires extension:ContactPage
 * @requires mw.config
 * @requires jQuery
 */
window.prepareSpecialContact4Info = function () {
  var placeholderMessage = '', warningMessageDoesNotWork = '';
  switch (mw.config.get( 'wgCanonicalSpecialPageName' )) {
    case 'Contact': // is a Special:Contact page regardless of language
      switch (mw.config.get('wgPageName')) {
        case 'Special:Contact':
        case 'Spécial:Contact':
          placeholderMessage = 'Leave it empty or add e-mail in message (mfn-berlin.de works only)';
          warningMessageDoesNotWork = 'Only mfn-berlin.de addresses do work. Leave it empty please and add your address in the message. Thank you.';
          break;
        case "Spezial:Kontakt":
          placeholderMessage = 'E-Mail in Nachricht dazu oder leer lassen (nur mfn-berlin.de geht)';
          warningMessageDoesNotWork = 'Nur mfn-berlin.de Adressen können verarbeitet werden. Bitte leer lassen oder E-Mail im Text angeben. Danke.';
          break;
        default:
          placeholderMessage = 'Leave it empty or add e-mail in message (mfn-berlin.de works only)';
          warningMessageDoesNotWork = 'Only mfn-berlin.de addresses do work. Leave it empty please and add your address in the message. Thank you.';
          break;
      }
      // check input
      if (!$('input[name="wpFromAddress"]').val() || $('input[name="wpFromAddress"]').val().match(/.*@mfn-berlin.de/)) {
        $('input[name="wpFromAddress"]')
          .css({'background-color': '', 'cursor': ''})
          .attr({'title': ''});
      } else {
        $('input[name="wpFromAddress"]')
          .css({'background-color': 'orange', 'cursor': 'help'})
          .attr({'title': warningMessageDoesNotWork});
      }
      $('input[name="wpFromAddress"]').on('focusout', function () {
        if (!$(this).val() || $(this).val().match(/.*@mfn-berlin.de/)) {
          $(this).css({'background-color': '', 'cursor': 'help'}).attr({'title': warningMessageDoesNotWork});
        } else {
          $(this).css({'background-color': 'orange', 'cursor': 'help'}).attr({'title': warningMessageDoesNotWork});
        }
      });
      $('input[name="wpFromAddress"]').attr({'placeholder': placeholderMessage});

    break;
  }// switch is a contact page
};// end prepareSpecialContact4Info()