MediaWiki:Gadget-autosort.js: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
Tag: Manual revert
No edit summary
Line 1: Line 1:
"use strict";

/**
/**
* For autosorting sortable tables
* For autosorting sortable tables
* @example <>
* @example <>
*/
*/
(function($,mw,rs){
(function ($, mw, rs) {
var $sortables = $('.sortable[class*="autosort="]');
var $sortables = $('.sortable[class*="autosort="]');
if (!$sortables.length) return;
if (!$sortables.length) return;
rs.autosort = function () {
rs.autosort = function () {
mw.loader.using('jquery.tablesorter', function () {
mw.loader.using('jquery.tablesorter', function () {
$sortables.each(function () {
$sortables.each(function () {
var $this = $(this),
var $this = $(this),
matched = (' ' + $(this).attr('class') + ' ').match(/autosort=(\d+)[,-]{1}(a|d)/),
matched = (' ' + $(this).attr( 'class') + ' ')
$sortCol = $this.find('> thead th:nth-child(' + matched[1] + ')');
if (matched[2] === 'd') {
.match(/autosort=(\d+)[,-]{1}(a|d)/),
$sortCol = $this
// descending
$sortCol.click().click();
.find('> thead th:nth-child(' + matched[1] + ')');
} else {

if (matched[2] === 'd') {
// ascending
// descending
$sortCol.click();
}
$sortCol.click().click();
} else {
});
mw.hook('gadget.autosort.sorted').fire($sortables);
// ascending
$sortCol.click();
}
});
mw.hook('gadget.autosort.sorted').fire($sortables);
});
};
mw.hook('wikipage.content').add(function init() {
if ($('.jquery-tablesorter').length) {
rs.autosort();
} else {
// tablesorter plugin has not run yet; observe the first
// sortable table element and wait for the plugin to add
// the 'jquery-tablesorter' class to it
new MutationObserver(function(muts, obs) {
if (muts[0].target.classList.contains('jquery-tablesorter')) {
window.rs.autosort();
obs.disconnect();
}
}).observe($sortables[0], {
attributes: true,
attributeFilter: ['class'],
});
}
// only run once
mw.hook('wikipage.content').remove(init);
});
});
};
mw.hook('wikipage.content').add(function init() {
if ($('.jquery-tablesorter').length) {
rs.autosort();
} else {
// tablesorter plugin has not run yet; observe the first
// sortable table element and wait for the plugin to add
// the 'jquery-tablesorter' class to it
new MutationObserver(function (muts, obs) {
if (muts[0].target.classList.contains('jquery-tablesorter')) {
window.rs.autosort();
obs.disconnect();
}
}).observe($sortables[0], {
attributes: true,
attributeFilter: ['class']
});
}


// only run once
}(jQuery, mediaWiki, rswiki));
mw.hook('wikipage.content').remove(init);
});
})(jQuery, mediaWiki, rswiki);

Revision as of 12:06, 20 October 2024

"use strict";

/**
 * For autosorting sortable tables
 * @example <>
 */
(function ($, mw, rs) {
  var $sortables = $('.sortable[class*="autosort="]');
  if (!$sortables.length) return;
  rs.autosort = function () {
    mw.loader.using('jquery.tablesorter', function () {
      $sortables.each(function () {
        var $this = $(this),
          matched = (' ' + $(this).attr('class') + ' ').match(/autosort=(\d+)[,-]{1}(a|d)/),
          $sortCol = $this.find('> thead th:nth-child(' + matched[1] + ')');
        if (matched[2] === 'd') {
          // descending
          $sortCol.click().click();
        } else {
          // ascending
          $sortCol.click();
        }
      });
      mw.hook('gadget.autosort.sorted').fire($sortables);
    });
  };
  mw.hook('wikipage.content').add(function init() {
    if ($('.jquery-tablesorter').length) {
      rs.autosort();
    } else {
      // tablesorter plugin has not run yet; observe the first
      // sortable table element and wait for the plugin to add
      // the 'jquery-tablesorter' class to it
      new MutationObserver(function (muts, obs) {
        if (muts[0].target.classList.contains('jquery-tablesorter')) {
          window.rs.autosort();
          obs.disconnect();
        }
      }).observe($sortables[0], {
        attributes: true,
        attributeFilter: ['class']
      });
    }

    // only run once
    mw.hook('wikipage.content').remove(init);
  });
})(jQuery, mediaWiki, rswiki);