Koha Test Wiki MW Canasta on Koha Portainer

Test major Koha Wiki changes or bug fixes here without fear of breaking the production wiki.

For the current Koha Wiki, visit https://wiki.koha-community.org .

User:Frietjes/infoboxgap.js

From Koha Test Wiki MW Canasta on Koha Portainer
Jump to navigation Jump to search

Note: After saving, 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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
jQuery(document).ready(function($) {

var myContent = document.getElementsByName('wpTextbox1')[0];

if(mw.config.get('wgNamespaceNumber') != -1 && myContent) {
	mw.loader.using(['mediawiki.util']).done( function() {
	var portletlink1 = mw.util.addPortletLink('p-tb', '#', 'Infobox gap', 't-gap', 'Insert a numbering gap in an infobox', '');
	$(portletlink1).click(function(e) {
      e.preventDefault();
      wpInfoboxGap();
    });
    var portletlink2 = mw.util.addPortletLink('p-tb', '#', 'Infobox renumber', 't-ren', 'Remove numbering gaps in an infobox', '');
    $(portletlink2).click(function(e) {
      e.preventDefault();
      wpInfoboxRenumber();
    });
	});
}

// -------------------------------------------------------------------------------- //
function wpInfoboxGap()
{
 var i = 0;
 var nums = myContent.value.match(/(?:label|data|[^a-z0-9]header|class)[0-9]+(?:[a-z][ ]*=|[ ]*=)/g);
 for(i = 0; i < nums.length; i++) {
   nums[i] = nums[i].replace(/[^0-9]*([0-9]+)[^0-9]*/, '$1');
 }
 nums = nums.sort(function(a,b){return a-b});
 var firstnum = Number(nums.shift());
 var lastnum = Number(nums.pop());

 var s = Number(prompt("Gap number (between " + String(firstnum) + " and " + String(lastnum) + ")?"));
 if ( (s >= firstnum) && (s <= lastnum + 1) ) {
 	var firstgap = 0;
 	for(i = s; i < lastnum; i++) {
 		var re = new RegExp('(label|data|[^a-z0-9]header|class|rowstyle)' + String(i) + '([a-z][ ]*=|[ ]*=)', 'g');
 		if(myContent.value.search(re) < 0) {
 			if(firstgap > 0) {
	 			lastnum = i - 1;
 			}
 		} else {
 			firstgap = 1;
 		}
 	}
 	for(i = lastnum; i >= s; i--) {
 		var re = new RegExp('(label|data|[^a-z0-9]header|class|rowstyle)' + String(i) + '([a-z][ ]*=|[ ]*=)', 'g');
 		myContent.value = myContent.value.replace(re,'$1' + String(i+1) + '$2');
   }
 }
}

function wpInfoboxRenumber()
{
 var i = 0;
 var nums = myContent.value.match(/(?:label|data|[^a-z0-9]header|class|rowstyle)[0-9]+(?:[a-z][ ]*=|[ ]*=)/g);
 for(i = 0; i < nums.length; i++) {
   nums[i] = parseInt(nums[i].replace(/[^0-9]*([0-9]+)[^0-9]*/, '$1'));
 }

 var k = 1;
 var currnum  = nums[0];
 var lastnum = nums[0];
 for(i = 0; i < nums.length; i++) {
   currnum = nums[i];
   if (currnum != lastnum) {
     k = k + 1;
     lastnum = currnum;
   }
   var re = new RegExp('(label|data|[^a-z0-9]header|class|rowstyle)' + String(nums[i]) + '([a-z][ ]*=|[ ]*=)', 'g');
   myContent.value = myContent.value.replace(re,'$1 WPFOOMARK ' + String(k) + '$2');
 }
 myContent.value = myContent.value.replace(/ WPFOOMARK /g, '');
}

});