Zum Inhalt springen

MediaWiki:Mw-customcollapsible.js: Unterschied zwischen den Versionen

Aus Flora von Bayern
imported>Andreas Plank
K resource keys prefixed by plugin
imported>Andreas Plank
K fix toggling: based on('afterExpand.mw-collapsible') or on('afterCollapse.mw-collapsible') from jquery.makeCollapsible.js
 
(2 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
/*global jQuery, $, mw, console */
/*global jQuery, $, mw, console, mediaWiki */
/*jslint maxerr: 50, indent: 2 */
/*jslint maxerr: 50, indent: 2 */


Zeile 10: Zeile 10:
  * somewhere: span.mw-customtoggle-**myKey**
  * somewhere: span.mw-customtoggle-**myKey**
  *
  *
  * div ─ .mw-collapsible  id="mw-customcollapsible-**myKey**"
  * div ─ .mw-collapsible  id='mw-customcollapsible-**myKey**'
  * │    └ [.mw-collapsed]
  * │    └ [.mw-collapsed]
  * │
  * │
  * └─ div.mw-collapsible-content
  * └─ div.mw-collapsible-content
  *
  *
  * @dependecy: $.resource() "CollapseBox_captionExpand", "CollapseBox_captionCollapse", "CollapseBox_toolTipCollapse" : "CollapseBox_toolTipExpand"
  * @dependecy: $.resource() 'CollapseBox_captionExpand', 'CollapseBox_captionCollapse', 'CollapseBox_toolTipCollapse' : 'CollapseBox_toolTipExpand'
  * @dependeciy:
  * @dependeciy:
  * CSS - pseudoclass:
  * CSS - pseudoclass:
Zeile 26: Zeile 26:
  * }
  * }
  *
  *
  * @todo click in player does not toggle on the first click
  * Toggle all
*
* $('.decisiontree').each (function (i, element) { $(element).find('.mw-customtoggle.is-collapsed').trigger('click') });
* $('.decisiontree').each (function (i, element) { $(element).find('.mw-customtoggle').not('.is-collapsed').trigger('click') });
*
  */
  */
function init_MwCustomCollapse_clickText() {
 
   "use strict";
window.init_MwCustomCollapse_clickText = function () {
   'use strict';
   // search first for id of mw-customcollapsible-**myKey** and then initiate the elements
   // search first for id of mw-customcollapsible-**myKey** and then initiate the elements
   // that trigger the media wiki based customcollapsible boxes
   // that trigger the media wiki based customcollapsible boxes
Zeile 35: Zeile 40:
   if (jMwCollapsible.length) {
   if (jMwCollapsible.length) {
     jMwCollapsible.each(function () {
     jMwCollapsible.each(function () {
       if ($(this).attr('id').indexOf('mw-customcollapsible-') === 0) {
       var $thisCustomCollapsible = $(this),
      idThisCollapsible = $thisCustomCollapsible.attr('id'),
        /**
        * toggle the corresponding custom toggler
        *
        * adds class is-collapsed (then showing expandible text)
        * @param {selector} element the element to target
        * @param {sting} showCase 'show expandible' or 'show collapsible'
        * @returns {undefined}
        */
      toggleCustomToggler = function (element, showCase) {
        var $thisToggler = $(element)
        , thisHasDataAttr = Boolean($thisToggler.attr('data-expandtext')) || Boolean($thisToggler.attr('data-collapsetext')) || false
        , thisExpandText = $thisToggler.attr('data-expandtext') || $.resource('CollapseBox_captionExpand')
        , thisExpandTooltip = $.resource('CollapseBox_toolTipExpand')
        , thisCollapseText = $thisToggler.attr('data-collapsetext') || $.resource('CollapseBox_captionCollapse')
        , thisCollapseTooltip = $.resource('CollapseBox_toolTipExpand')
        , thisTitle = (showCase === 'show expandible' ? thisExpandTooltip : thisCollapseTooltip)
        , thisToggleText = (showCase  === 'show expandible'? thisExpandText : thisCollapseText)
        , thisHtml = $thisToggler.html()
        ;
       
       
        if (!$thisToggler.hasClass('pseudolink')) {
            $thisToggler.addClass('pseudolink');
        }
        if(showCase === 'show expandible') {
          $thisToggler.addClass('is-collapsed');
        } else {
          $thisToggler.removeClass('is-collapsed');
        }
        $thisToggler
          .attr('title', thisTitle)
          /* keep html when there is no data-expandtext or data-collapsetext */
          .html(thisHasDataAttr ? undefined : thisToggleText);
        return;
      };
     
     
      if (idThisCollapsible.indexOf('mw-customcollapsible-') === 0) {
       // id of collapsible box
       // id of collapsible box
         var thisId = $(this).attr('id'),
         var wasCollapsed = true
           collapsibleCustomTriggerClass = thisId.replace("mw-customcollapsible", "mw-customtoggle"),
          , classCustomToggler = idThisCollapsible.replace('mw-customcollapsible', 'mw-customtoggle'),
           isCollapsed = $('#' + thisId).hasClass('mw-collapsed');
          // wasCollapsed = $thisCustomCollapsible.hasClass('mw-collapsed'),
          $collapseToggler = $('.' + classCustomToggler);
       
        /**
        * Listen on triggered events of the collapsible content (from jquery.makeCollapsible.js)
        */
        $thisCustomCollapsible.on('afterExpand.mw-collapsible', function () {
          classCustomToggler = $(this).attr('id').replace('mw-customcollapsible', 'mw-customtoggle');
          $('.' + classCustomToggler).each( function (i, element) {
            toggleCustomToggler(element, 'show collapsible');
          });
        });
           
        /**
        * Listen on triggered events of the collapsible content (from jquery.makeCollapsible.js)
        */
        $thisCustomCollapsible.on('afterCollapse.mw-collapsible', function () {
           classCustomToggler = $(this).attr('id').replace('mw-customcollapsible', 'mw-customtoggle');
           $('.' + classCustomToggler).each( function (i, element) {
            toggleCustomToggler(element, 'show expandible');
          });
        });
        wasCollapsed = $(this).hasClass('mw-collapsed') ;
        if( Boolean(mw.util.getParamValue('debug'))) {
          console.log('DEBUG this class: '
          + $(this).attr('class')
          + ', wasCollapsed ' + (wasCollapsed ? 'yes' : 'no')
          );
        }
         // bind each corresponding class and also toggle text (can have attribute data-expandtext/data-collapsetext)
         // bind each corresponding class and also toggle text (can have attribute data-expandtext/data-collapsetext)
         $('.' + collapsibleCustomTriggerClass)
         $collapseToggler.each (function (i, element) {
          //live does not bind the click
          if(Boolean(mw.util.getParamValue('debug'))) {
          //delegate does not bind the click; does delegate() apply only when an outer wrapper element is there?
             console.log('DEBUG this collapseToggler: '  
          // try namespaced click, do not name it mw-… it is used by MediaWiki
            + $(this).attr('class')
          .each(function () {
            + ', wasCollapsed ' + (wasCollapsed ? 'yes' : 'no')
            var thisExpandText = $(this).attr('data-expandtext') ? $(this).attr('data-expandtext') : $.resource("CollapseBox_captionExpand"),
            );
              thisCollapseText = $(this).attr('data-collapsetext') ? $(this).attr('data-collapsetext') : $.resource("CollapseBox_captionCollapse");
          }
 
          toggleCustomToggler(element, wasCollapsed ? 'show expandible' : 'show collapsible' );
             $(this).on('click.customtoggle', function (event) {
        });
              // this click event fires before click.mw-collapse of jquery.makeCollapsible.js
        /*
              // so CSS class mw-collapsed is not yet removed or added
        */
              //
              // get the corresponding id to this mw-customtoggle class
              var thisClassList = $(this).attr('class').split(" "),
                thisClassMwCustomtoggle = $.grep(
                  thisClassList,
                  // loop through the classes
                  function (classValue, i) {
                    if (classValue.indexOf('mw-customtoggle') === 0) {
                      return classValue;
                    }
                  }
                ).toString(),
                thisIdToToggle = thisClassMwCustomtoggle.replace("mw-customtoggle", "mw-customcollapsible"),
              // check status of class mw-collapsed when clicked
                wasCollapsedBeforeThisClick = $('#' + thisIdToToggle).hasClass('mw-collapsed');
              // Debugging
              // console.log("Mw-customcollapsible.js on()"
              //  + ", wasCollapsedBeforeClick: " + wasCollapsedBeforeThisClick
              //  + ", htmltext: " + (wasCollapsedBeforeThisClick ? thisCollapseText : thisExpandText)
              //  + ", thisExpandText: " + thisExpandText
              //  + ", thisCollapseText: " + thisCollapseText);
              // toggle text accordingly, set title attribute
              $(this)
                .attr("title", $.resource(wasCollapsedBeforeThisClick ? "CollapseBox_toolTipCollapse" : "CollapseBox_toolTipExpand"))
                .html(wasCollapsedBeforeThisClick ? thisCollapseText : thisExpandText);
            })
            // format the triggering HTML element
              .addClass("pseudolink")
              .attr("title", $.resource(isCollapsed ? "CollapseBox_toolTipExpand" : "CollapseBox_toolTipCollapse"))
              .html(isCollapsed ? thisExpandText : thisCollapseText);
            // Debugging
            // console.log("Mw-customcollapsible.js triggering HTML"
            //  + ", wasCollapsedBeforeClick: " + isCollapsed
            //  + ", htmltext: " + (isCollapsed ? thisExpandText : thisCollapseText)
            //  + ", thisExpandText: " + thisExpandText
            //  + ", thisCollapseText: " + thisCollapseText);
          });// each()
       }// if mw-customcollapsible
       }// if mw-customcollapsible
     });
     });
   }
   }
   return null;
   return null;
}// init_MwCustomCollapse_clickText()
};// init_MwCustomCollapse_clickText()


init_MwCustomCollapse_clickText();
jQuery( document ).ready( function( $ ) {
  init_MwCustomCollapse_clickText();
});

Aktuelle Version vom 14. August 2017, 10:52 Uhr

/*global jQuery, $, mw, console, mediaWiki */
/*jslint maxerr: 50, indent: 2 */

/*
 * change click-text based on MediaWiki's collapsible-element-mechanism (jQuery)
 * that are triggered from outside a mw-collapsible-class, using the socalled
 * 'mw-customtoggle-something' CSS class. This class of the triggering element defines
 * what id will be collapsible. Example:
 *
 * somewhere: span.mw-customtoggle-**myKey**
 *
 * div ─ .mw-collapsible  id='mw-customcollapsible-**myKey**'
 * │     └ [.mw-collapsed]
 * │
 * └─ div.mw-collapsible-content
 *
 * @dependecy: $.resource() 'CollapseBox_captionExpand', 'CollapseBox_captionCollapse', 'CollapseBox_toolTipCollapse' : 'CollapseBox_toolTipExpand'
 * @dependeciy:
 * CSS - pseudoclass:
 * span.pseudolink {
 *   color:#0645AD;
 * }
 * span.pseudolink:hover, span.pseudolink:focus {
 *   text-decoration: underline;
 *   cursor:pointer;
 * }
 *
 * Toggle all
 * 
 * $('.decisiontree').each (function (i, element) { $(element).find('.mw-customtoggle.is-collapsed').trigger('click') });
 * $('.decisiontree').each (function (i, element) { $(element).find('.mw-customtoggle').not('.is-collapsed').trigger('click') });
 * 
 */

window.init_MwCustomCollapse_clickText = function () {
  'use strict';
  // search first for id of mw-customcollapsible-**myKey** and then initiate the elements
  // that trigger the media wiki based customcollapsible boxes
  var jMwCollapsible = $('.mw-collapsible[id^="mw-customcollapsible"]');
  if (jMwCollapsible.length) {
    jMwCollapsible.each(function () {
      var $thisCustomCollapsible = $(this), 
      idThisCollapsible = $thisCustomCollapsible.attr('id'),
        /**
        * toggle the corresponding custom toggler
        * 
        * adds class is-collapsed (then showing expandible text)
        * @param {selector} element the element to target
        * @param {sting} showCase 'show expandible' or 'show collapsible'
        * @returns {undefined}
        */
       toggleCustomToggler = function (element, showCase) {
         var $thisToggler = $(element)
         , thisHasDataAttr = Boolean($thisToggler.attr('data-expandtext')) || Boolean($thisToggler.attr('data-collapsetext')) || false
         , thisExpandText = $thisToggler.attr('data-expandtext') || $.resource('CollapseBox_captionExpand')
         , thisExpandTooltip = $.resource('CollapseBox_toolTipExpand')
         , thisCollapseText = $thisToggler.attr('data-collapsetext') || $.resource('CollapseBox_captionCollapse')
         , thisCollapseTooltip = $.resource('CollapseBox_toolTipExpand')
         , thisTitle = (showCase === 'show expandible' ? thisExpandTooltip : thisCollapseTooltip)
         , thisToggleText = (showCase  === 'show expandible'? thisExpandText : thisCollapseText)
         , thisHtml = $thisToggler.html()
         ;
         
         
         if (!$thisToggler.hasClass('pseudolink')) {
             $thisToggler.addClass('pseudolink');
         }
         if(showCase === 'show expandible') {
           $thisToggler.addClass('is-collapsed');
         } else {
           $thisToggler.removeClass('is-collapsed');
         }
         $thisToggler
           .attr('title', thisTitle)
           /* keep html when there is no data-expandtext or data-collapsetext */ 
           .html(thisHasDataAttr ? undefined : thisToggleText);
         return;
       };
       
       
      if (idThisCollapsible.indexOf('mw-customcollapsible-') === 0) {
      // id of collapsible box
        var wasCollapsed = true
          , classCustomToggler = idThisCollapsible.replace('mw-customcollapsible', 'mw-customtoggle'),
          // wasCollapsed = $thisCustomCollapsible.hasClass('mw-collapsed'),
          $collapseToggler = $('.' + classCustomToggler);
        
        /**
         * Listen on triggered events of the collapsible content (from jquery.makeCollapsible.js)
         */
        $thisCustomCollapsible.on('afterExpand.mw-collapsible', function () {
          classCustomToggler = $(this).attr('id').replace('mw-customcollapsible', 'mw-customtoggle');
          $('.' + classCustomToggler).each( function (i, element) {
            toggleCustomToggler(element, 'show collapsible');
          });
        });
            
        /**
         * Listen on triggered events of the collapsible content (from jquery.makeCollapsible.js)
         */
        $thisCustomCollapsible.on('afterCollapse.mw-collapsible', function () {
          classCustomToggler = $(this).attr('id').replace('mw-customcollapsible', 'mw-customtoggle');
          $('.' + classCustomToggler).each( function (i, element) {
            toggleCustomToggler(element, 'show expandible');
          });
        });
        wasCollapsed = $(this).hasClass('mw-collapsed') ;
        if( Boolean(mw.util.getParamValue('debug'))) {
          console.log('DEBUG this class: ' 
          + $(this).attr('class')
          + ', wasCollapsed ' + (wasCollapsed ? 'yes' : 'no')
          );
        }
        // bind each corresponding class and also toggle text (can have attribute data-expandtext/data-collapsetext)
        $collapseToggler.each (function (i, element) {
          if(Boolean(mw.util.getParamValue('debug'))) {
            console.log('DEBUG this collapseToggler: ' 
            + $(this).attr('class')
            + ', wasCollapsed ' + (wasCollapsed ? 'yes' : 'no')
            );
          }
          toggleCustomToggler(element, wasCollapsed ? 'show expandible' : 'show collapsible' );
        });
        /*
        */
      }// if mw-customcollapsible
    });
  }
  return null;
};// init_MwCustomCollapse_clickText()

jQuery( document ).ready( function( $ ) {
  init_MwCustomCollapse_clickText();
});