Difference between revisions of "Common.js"

From Sega Retro

(No you shut up MediaWiki)
 
 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
addOnloadHook(createToggleLinks);
+
// PootTabs - it poots tabs on pages.
 +
var pootTabsHere = {
 +
animationsEnabled: $.support.opacity,
 +
getTab:function(poot, index) {
 +
return $(poot.children('.poot-tabs').children('ul').children('li')[parseInt(index)]);
 +
},
 +
changeTab:function(poot, index, duration, force) {
 +
if(index == parseInt(poot.attr('pootSelected')) && !force && duration) return;
 +
if(!pootTabsHere.animationsEnabled) {
 +
duration = 0;
 +
}
 +
poot.attr('pootSelected', index.toString());
 +
var babies = poot.children('.poot-tabs-content').children();
 +
babies.each(function() {
 +
$(this).fadeOut(duration, function(){
 +
$(this).removeClass('poot-tabs-selected');
 +
                                $(this).attr( "aria-selected", "false" );
 +
});
 +
});
 +
$(babies[index]).each(function() {
 +
$(this).fadeIn(duration, function(){
 +
$(this).addClass('poot-tabs-selected');
 +
                                $(this).attr( "aria-selected", "true" );
  
function createToggleLinks() {
 
  
var stateArray = new Array();
+
});
var allObjects = document.getElementsByTagName('span');
+
});
var rCollapsingObject = new RegExp("^co;(.+?);(.+?);(.+?);(.+?)(;(.+?))?$");
+
var cowtabs = poot.children('.poot-tabs').children('ul').children('li');
+
cowtabs.removeClass('poot-tabs-selected');
for ( var i = 0; i < allObjects.length; i++ ) {
+
                cowtabs.attr("aria-selected", "false");
+
$(cowtabs[index]).addClass('poot-tabs-selected');
if ( rCollapsingObject.test(allObjects[i].className) ) {
+
                $(cowtabs[index]).attr( "aria-selected", "true" );
+
pootTabsHere.updatePoot(poot, $(babies[index]).height());
var collapsingInformation = rCollapsingObject.exec(allObjects[i].className);
+
},
+
updatePoot:function(poot, babysize) {
var collapseText = collapsingInformation[1];
+
poot.find('.poot-tabs-titletext').html(poot.attr('originalTitle') + ' &mdash; ' + pootTabsHere.getTab(poot, poot.attr('pootSelected')).html());
var expandText = collapsingInformation[2];
+
var bestHeight = Math.max(poot.children('.poot-tabs-content').height(), Math.max(poot.children('.poot-tabs').height(), babysize)).toString() + 'px';
var initialState = collapsingInformation[3];
+
poot.children('.poot-tabs-content').css('height', bestHeight);
var targetClass = collapsingInformation[4];
+
if(poot.attr('vertical')) {
var linkColor = collapsingInformation[6];
+
poot.children('.poot-tabs').css('height', bestHeight);
+
}
var toggleLink = document.createElement("a");
+
},
+
toggleCollapse:function(poot) {
if ( initialState == "0" ) {
+
var pootLinkText = poot.children('.poot-tabs-showhide').text().split(';');
+
var duration = pootTabsHere.animationsEnabled ? parseInt(poot.attr('pootslideduration')) : 0;
toggleLink.appendChild(document.createTextNode(expandText));
+
if(poot.attr('pootcollapse') != 'true') {
stateArray[targetClass] = "none";
+
poot.attr('pootcollapse', 'true');
 +
poot.find('.poot-tabs-hidelink a').text(pootLinkText[0]);
 +
poot.children('.poot-tabs, .poot-tabs-content').slideUp(duration);
 +
}
 +
else {
 +
poot.attr('pootcollapse', '');
 +
poot.find('.poot-tabs-hidelink a').text(pootLinkText[1]);
 +
poot.children('.poot-tabs, .poot-tabs-content').slideDown(duration);
 +
}
 +
},
 +
delayHeight:function(poot, selected) {
 +
setTimeout(function() {
 +
poot.attr('pootselected', selected.toString());
 +
pootTabsHere.changeTab(poot, selected, 0, true);
 +
if(poot.hasClass('poot-tabs-collapsed')) {
 +
pootTabsHere.toggleCollapse(poot);
 
}
 
}
+
}, 100);
else {
+
},
+
poot:function() {
toggleLink.appendChild(document.createTextNode(collapseText));
+
var dis = $(this);
stateArray[targetClass] = "inline";
+
var ind = 0;
}
+
dis.attr('originalTitle', dis.find('.poot-tabs-titletext').html());
+
var selected = /poot-tabs-selected-(\d+)/i.exec(dis.attr('class'));
toggleLink.setAttribute("href", "javascript:toggleCollapse('" + targetClass + "','" + collapseText + "','" + expandText + "')");
+
if(selected) {
+
pootTabsHere.delayHeight(dis, parseInt(selected[1])-1);
if ( linkColor != undefined && linkColor != "undefined" && linkColor != "" )
+
}
toggleLink.style.color = linkColor;
+
else {
+
pootTabsHere.delayHeight(dis, 0);
allObjects[i].innerHTML = "";
 
allObjects[i].appendChild(toggleLink);
 
 
}
 
}
+
var duration = dis.hasClass('poot-tabs-noanimations') ? 0 : 200;
else if ( allObjects[i].className == "morphMaster" ) {
+
dis.attr('pootslideduration', dis.hasClass('poot-tabs-noanimations') ? '0' : '75');
+
dis.children('.poot-tabs').children('ul').children('li').each(function(){
var spanID = allObjects[i].getAttribute("id");
+
var thisInd = ind;
var targetID = spanID.substr(0, spanID.length - 6);
+
$(this).click(function(){
var counter = 1;
+
pootTabsHere.changeTab(dis, thisInd, duration, false);
+
$(this).blur();
// Create forward and backward paging if the paging elements exist
+
$(this).find('*').blur();
if ( returnObjById(targetID + "LinkNext") && returnObjById(targetID + "LinkPrev") && returnObjById(targetID + "Content1") ) {
+
return false;
+
});
// Create the forward link
+
ind++;
var nextLink = document.createElement("a");
+
});
nextLink.appendChild(document.createTextNode(returnObjById(targetID + "LinkNext").innerHTML));
+
var isVertical = dis.hasClass('poot-tabs-vertical');
nextLink.setAttribute("href", "javascript:morphForward('" + targetID + "')");
+
dis.attr('pootvertical', isVertical ? 'true' : '');
+
if(isVertical) {
returnObjById(targetID + "LinkNext").innerHTML = "";
+
var teenie = dis.children('.poot-tabs').width().toString() + 'px';
returnObjById(targetID + "LinkNext").appendChild(nextLink, 0);
+
dis.children('.poot-tabs-content').css('margin-left', teenie);
 
// Create the backward link
 
var prevLink = document.createElement("a");
 
prevLink.appendChild(document.createTextNode(returnObjById(targetID + "LinkPrev").innerHTML));
 
prevLink.setAttribute("href", "javascript:morphBackward('" + targetID + "')");
 
 
returnObjById(targetID + "LinkPrev").innerHTML = "";
 
returnObjById(targetID + "LinkPrev").appendChild(prevLink, 0);
 
 
// Initialize content panes
 
while ( returnObjById(targetID + "Content" + counter) ) {
 
 
 
counter++;
 
}
 
}
 
 
 
counter = 1;
 
 
// Whether or not there is paging, generate normal links
 
while ( returnObjById(targetID + "Link" + counter) && returnObjById(targetID + "Content" + counter) ) {
 
 
var morphLink = document.createElement("a");
 
morphLink.appendChild(document.createTextNode(returnObjById(targetID + "Link" + counter).innerHTML));
 
morphLink.setAttribute("href", "javascript:performMorph('" + targetID + "','" + counter + "')");
 
 
returnObjById(targetID + "Link" + counter).innerHTML = "";
 
returnObjById(targetID + "Link" + counter).appendChild(morphLink, 0);
 
 
// Initialize content panes
 
 
counter++;
 
}
 
 
allObjects[i].innerHTML = "1";
 
allObjects[i].style.display = "none";
 
 
}
 
}
 +
dis.attr('pootcollapse', ''); // False
 +
dis.find('.poot-tabs-hidelink a').click(function(){
 +
pootTabsHere.toggleCollapse(dis);
 +
return false;
 +
});
 +
},
 +
init:function() {
 +
$('.poot-tabs-container').each(pootTabsHere.poot);
 
}
 
}
 +
};
 +
$(pootTabsHere.init);
 +
// End of PootTabs
 +
 +
// Responsive table behavior
 +
$(document).ready(function() {
 +
  var switched = false;
 +
  var updateTables = function() {
 +
    if (($(window).width() < 767) && !switched ){
 +
      switched = true;
 +
      $("table.responsive").each(function(i, element) {
 +
        splitTable($(element));
 +
      });
 +
      return true;
 +
    }
 +
    else if (switched && ($(window).width() > 767)) {
 +
      switched = false;
 +
      $("table.responsive").each(function(i, element) {
 +
        unsplitTable($(element));
 +
      });
 +
    }
 +
  };
 +
 
 +
  $(window).load(updateTables);
 +
  $(window).on("redraw",function(){switched=false;updateTables();}); // An event to listen for
 +
  $(window).on("resize", updateTables);
 +
 
 
 
// Set state of appropriate objects
+
function splitTable(original)
allObjects = document.getElementsByTagName('*');
+
{
 +
original.wrap("<div class='table-wrapper' />");
 
 
for ( var i = 0; i < allObjects.length; i++ ) {
+
var copy = original.clone();
 +
copy.find("td:not(:first-child), th:not(:first-child)").css("display", "none");
 +
copy.removeClass("responsive");
 
 
if ( stateArray[allObjects[i].className] )
+
original.closest(".table-wrapper").append(copy);
allObjects[i].style.display = stateArray[allObjects[i].className];
+
copy.wrap("<div class='pinned' />");
}
+
original.wrap("<div class='scrollable' />");
}
 
  
/* Function that toggles collapsing objects.
+
    setCellHeights(original, copy);
* Added 7/13/2008 by WhiteMystery ([email protected]) */
 
 
 
function toggleCollapse(targetClass, collapseText, expandText) {
 
 
 
var allObjects = document.getElementsByTagName('*');
 
var rCollapsingObject = new RegExp("^co;(.+?);(.+?);(.+?);" + targetClass + "(;(.+?))?$");
 
 
var linkType;
 
 
for ( var i = 0; i < allObjects.length; i++ ) {
 
 
if ( allObjects[i].className == targetClass ) {
 
 
if ( allObjects[i].style.display == "none" ) {
 
 
allObjects[i].style.display = "inline";
 
linkType = "Collapse";
 
}
 
 
else {
 
 
allObjects[i].style.display = "none";
 
linkType = "Expand";
 
}
 
}
 
 
}
 
}
 
 
allObjects = document.getElementsByTagName('span');
+
function unsplitTable(original) {
+
    original.closest(".table-wrapper").find(".pinned").remove();
for ( var i = 0; i < allObjects.length; i++ ) {
+
    original.unwrap();
+
    original.unwrap();
if ( rCollapsingObject.test(allObjects[i].className) ) {
 
 
var collapsingInformation = rCollapsingObject.exec(allObjects[i].className);
 
 
var collapseText = collapsingInformation[1];
 
var expandText = collapsingInformation[2];
 
var linkColor = collapsingInformation[5];
 
 
var toggleLink = document.createElement("a");
 
 
if ( linkType == "Expand" )
 
 
toggleLink.appendChild(document.createTextNode(expandText));
 
 
else if ( linkType == "Collapse" )
 
 
toggleLink.appendChild(document.createTextNode(collapseText));
 
 
toggleLink.setAttribute("href", "javascript:toggleCollapse('" + targetClass + "','" + collapseText + "','" + expandText + "')");
 
 
if ( linkColor != undefined && linkColor != "undefined" && linkColor != "" )
 
toggleLink.style.color = linkColor;
 
 
allObjects[i].innerHTML = "";
 
allObjects[i].appendChild(toggleLink);
 
}
 
 
}
 
}
}
 
  
/* Functions that perform the morph operations.
+
  function setCellHeights(original, copy) {
* Added 8/13/2008 by WhiteMystery ([email protected]) */
+
    var tr = original.find('tr'),
 +
        tr_copy = copy.find('tr'),
 +
        heights = [];
  
function performMorph(targetID, targetNumber) {
+
    tr.each(function (index) {
 +
      var self = $(this),
 +
          tx = self.find('th, td');
  
var counter = 1;
+
      tx.each(function () {
+
        var height = $(this).outerHeight(true);
while ( returnObjById(targetID + "Content" + counter) ) {
+
        heights[index] = heights[index] || 0;
+
        if (height > heights[index]) heights[index] = height;
if ( counter == targetNumber )
+
      });
returnObjById(targetID + "Content" + counter).style.display = "block";
 
else
 
returnObjById(targetID + "Content" + counter).style.display = "none";
 
 
counter++;
 
}
 
 
returnObjById(targetID + "Master").innerHTML = targetNumber;
 
}
 
 
 
function morphForward(targetID) {
 
 
 
var nextPane = parseInt(returnObjById(targetID + "Master").innerHTML) + 1;
 
 
if ( returnObjById(targetID + "Content" + nextPane) )
 
performMorph(targetID, nextPane);
 
 
else
 
performMorph(targetID, "1");
 
}
 
 
 
function morphBackward(targetID) {
 
 
 
var prevPane = parseInt(returnObjById(targetID + "Master").innerHTML) - 1;
 
 
if ( prevPane > 0 )
 
performMorph(targetID, prevPane);
 
 
else {
 
 
var maxIndex = 1;
 
 
while ( returnObjById(targetID + "Content" + maxIndex) )
 
maxIndex++;
 
 
performMorph(targetID, maxIndex - 1);
 
}
 
}
 
  
/* Function that returns an object by ID for various browsers
+
    });
* Taken from http://www.netlobo.com/javascript_get_element_id.html */
 
  
function returnObjById( id ) {
+
    tr_copy.each(function (index) {
+
      $(this).height(heights[index]);
    if (document.getElementById)  
+
     });
        var returnVar = document.getElementById(id);
+
  }
       
 
    else if (document.all)
 
        var returnVar = document.all[id];
 
       
 
     else if (document.layers)  
 
        var returnVar = document.layers[id];
 
       
 
    return returnVar;  
 
}
 
  
/*</pre>*/
+
});

Latest revision as of 12:29, 12 September 2020

// PootTabs - it poots tabs on pages.
var pootTabsHere = {
	animationsEnabled: $.support.opacity,
	getTab:function(poot, index) {
		return $(poot.children('.poot-tabs').children('ul').children('li')[parseInt(index)]);
	},
	changeTab:function(poot, index, duration, force) {
		if(index == parseInt(poot.attr('pootSelected')) && !force && duration) return;
		if(!pootTabsHere.animationsEnabled) {
			duration = 0;
		}
		poot.attr('pootSelected', index.toString());
		var babies = poot.children('.poot-tabs-content').children();
		babies.each(function() {
			$(this).fadeOut(duration, function(){
				$(this).removeClass('poot-tabs-selected');
                                $(this).attr( "aria-selected", "false" );
			});
		});
		$(babies[index]).each(function() {
			$(this).fadeIn(duration, function(){
				$(this).addClass('poot-tabs-selected');
                                $(this).attr( "aria-selected", "true" );


			});
		});
		var cowtabs = poot.children('.poot-tabs').children('ul').children('li');
		cowtabs.removeClass('poot-tabs-selected');
                cowtabs.attr("aria-selected", "false");
		$(cowtabs[index]).addClass('poot-tabs-selected');
                $(cowtabs[index]).attr( "aria-selected", "true" );
		pootTabsHere.updatePoot(poot, $(babies[index]).height());
	},
	updatePoot:function(poot, babysize) {
		poot.find('.poot-tabs-titletext').html(poot.attr('originalTitle') + ' &mdash; ' + pootTabsHere.getTab(poot, poot.attr('pootSelected')).html());
		var bestHeight = Math.max(poot.children('.poot-tabs-content').height(), Math.max(poot.children('.poot-tabs').height(), babysize)).toString() + 'px';
		poot.children('.poot-tabs-content').css('height', bestHeight);
		if(poot.attr('vertical')) {
			poot.children('.poot-tabs').css('height', bestHeight);
		}
	},
	toggleCollapse:function(poot) {
		var pootLinkText = poot.children('.poot-tabs-showhide').text().split(';');
		var duration = pootTabsHere.animationsEnabled ? parseInt(poot.attr('pootslideduration')) : 0;
		if(poot.attr('pootcollapse') != 'true') {
			poot.attr('pootcollapse', 'true');
			poot.find('.poot-tabs-hidelink a').text(pootLinkText[0]);
			poot.children('.poot-tabs, .poot-tabs-content').slideUp(duration);
		}
		else {
			poot.attr('pootcollapse', '');
			poot.find('.poot-tabs-hidelink a').text(pootLinkText[1]);
			poot.children('.poot-tabs, .poot-tabs-content').slideDown(duration);
		}
	},
	delayHeight:function(poot, selected) {
		setTimeout(function() {
			poot.attr('pootselected', selected.toString());
			pootTabsHere.changeTab(poot, selected, 0, true);
			if(poot.hasClass('poot-tabs-collapsed')) {
				pootTabsHere.toggleCollapse(poot);
			}
		}, 100);
	},
	poot:function() {
		var dis = $(this);
		var ind = 0;
		dis.attr('originalTitle', dis.find('.poot-tabs-titletext').html());
		var selected = /poot-tabs-selected-(\d+)/i.exec(dis.attr('class'));
		if(selected) {
			pootTabsHere.delayHeight(dis, parseInt(selected[1])-1);
		}
		else {
			pootTabsHere.delayHeight(dis, 0);
		}
		var duration = dis.hasClass('poot-tabs-noanimations') ? 0 : 200;
		dis.attr('pootslideduration', dis.hasClass('poot-tabs-noanimations') ? '0' : '75');
		dis.children('.poot-tabs').children('ul').children('li').each(function(){
			var thisInd = ind;
			$(this).click(function(){
				pootTabsHere.changeTab(dis, thisInd, duration, false);
				$(this).blur();
				$(this).find('*').blur();
				return false;
			});
			ind++;
		});
		var isVertical = dis.hasClass('poot-tabs-vertical');
		dis.attr('pootvertical', isVertical ? 'true' : '');
		if(isVertical) {
			var teenie = dis.children('.poot-tabs').width().toString() + 'px';
			dis.children('.poot-tabs-content').css('margin-left', teenie);
		}
		dis.attr('pootcollapse', ''); // False
		dis.find('.poot-tabs-hidelink a').click(function(){
			pootTabsHere.toggleCollapse(dis);
			return false;
		});
	},
	init:function() {
		$('.poot-tabs-container').each(pootTabsHere.poot);
	}
};
$(pootTabsHere.init);
// End of PootTabs

// Responsive table behavior
$(document).ready(function() {
  var switched = false;
  var updateTables = function() {
    if (($(window).width() < 767) && !switched ){
      switched = true;
      $("table.responsive").each(function(i, element) {
        splitTable($(element));
      });
      return true;
    }
    else if (switched && ($(window).width() > 767)) {
      switched = false;
      $("table.responsive").each(function(i, element) {
        unsplitTable($(element));
      });
    }
  };
   
  $(window).load(updateTables);
  $(window).on("redraw",function(){switched=false;updateTables();}); // An event to listen for
  $(window).on("resize", updateTables);
   
	
	function splitTable(original)
	{
		original.wrap("<div class='table-wrapper' />");
		
		var copy = original.clone();
		copy.find("td:not(:first-child), th:not(:first-child)").css("display", "none");
		copy.removeClass("responsive");
		
		original.closest(".table-wrapper").append(copy);
		copy.wrap("<div class='pinned' />");
		original.wrap("<div class='scrollable' />");

    setCellHeights(original, copy);
	}
	
	function unsplitTable(original) {
    original.closest(".table-wrapper").find(".pinned").remove();
    original.unwrap();
    original.unwrap();
	}

  function setCellHeights(original, copy) {
    var tr = original.find('tr'),
        tr_copy = copy.find('tr'),
        heights = [];

    tr.each(function (index) {
      var self = $(this),
          tx = self.find('th, td');

      tx.each(function () {
        var height = $(this).outerHeight(true);
        heights[index] = heights[index] || 0;
        if (height > heights[index]) heights[index] = height;
      });

    });

    tr_copy.each(function (index) {
      $(this).height(heights[index]);
    });
  }

});