User:AT/wikilinks.js

From Guild Wars Wiki
Jump to navigationJump to search

Note: After publishing, 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: Press Ctrl-F5.
function Vector(inc) {
	if (inc == 0) {
		inc = 100;
	}
	
	/* Properties */
	this.data = new Array(inc);
	this.increment = inc;
	this.size = 0;
	
	/* Methods */
	this.getCapacity = getCapacity;
	this.getSize = getSize;
	this.isEmpty = isEmpty;
	this.getLastElement = getLastElement;
	this.getFirstElement = getFirstElement;
	this.getElementAt = getElementAt;
	this.addElement = addElement;
	this.insertElementAt = insertElementAt;
	this.removeElementAt = removeElementAt;
	this.removeAllElements = removeAllElements;
	this.indexOf = indexOf;
	this.contains = contains
	this.resize = resize;
	this.toString = toString;
	this.sort = sort;
	this.trimToSize = trimToSize;
	this.clone = clone;
	this.overwriteElementAt;
}

// getCapacity() -- returns the number of elements the vector can hold
function getCapacity() {
	return this.data.length;
}

// getSize() -- returns the current size of the vector
function getSize() {
	return this.size;
}

// isEmpty() -- checks to see if the Vector has any elements
function isEmpty() {
	return this.getSize() == 0;
}

// getLastElement() -- returns the last element
function getLastElement() {
	if (this.data[this.getSize() - 1] != null) {
		return this.data[this.getSize() - 1];
	}
}

// getFirstElement() -- returns the first element
function getFirstElement() {
	if (this.data[0] != null) {
		return this.data[0];
	}
}

// getElementAt() -- returns an element at a specified index
function getElementAt(i) {
	try {
		return this.data[i];
	} 
	catch (e) {
		return "Exception " + e + " occured when accessing " + i;	
	}	
}

// addElement() -- adds a element at the end of the Vector
function addElement(obj) {
	if(this.getSize() == this.data.length) {
		this.resize();
	}
	this.data[this.size++] = obj;
}

// insertElementAt() -- inserts an element at a given position
function insertElementAt(obj, index) {
	try {
		if (this.size == this.capacity) {
			this.resize();
		}
		
		for (var i=this.getSize(); i > index; i--) {
			this.data[i] = this.data[i-1];
		}
		this.data[index] = obj;
		this.size++;
	}
	catch (e) {
		return "Invalid index " + i;
	}
}

// removeElementAt() -- removes an element at a specific index
function removeElementAt(index) {
	try {
		var element = this.data[index];
		
		for(var i=index; i<(this.getSize()-1); i++) {
			this.data[i] = this.data[i+1];
		}
		
		this.data[getSize()-1] = null;
		this.size--;
		return element;
	}
	catch(e) {
		return "Invalid index " + index;
	}
} 

// removeAllElements() -- removes all elements in the Vector
function removeAllElements() {
	this.size = 0;
	
	for (var i=0; i<this.data.length; i++) {
		this.data[i] = null;
	}
}

// indexOf() -- returns the index of a searched element
function indexOf(obj) {
	for (var i=0; i<this.getSize(); i++) {
		if (this.data[i] == obj) {
			return i;
		}
	}
	return -1;
}

// contains() -- returns true if the element is in the Vector, otherwise false
function contains(obj) {
	for (var i=0; i<this.getSize(); i++) {
		if (this.data[i] == obj) {
			return true;
		}
	}
	return false;
}

// resize() -- increases the size of the Vector
function resize() {
	newData = new Array(this.data.length + this.increment);
	
	for	(var i=0; i< this.data.length; i++) {
		newData[i] = this.data[i];
	}
	
	this.data = newData;
}


// trimToSize() -- trims the vector down to it's size
function trimToSize() {
	var temp = new Array(this.getSize());
	
	for (var i = 0; i < this.getSize(); i++) {
		temp[i] = this.data[i];
	}
	this.size = temp.length - 1;
	this.data = temp;
} 

// sort() - sorts the collection based on a field name - f
function sort(f) {
	var i, j;
	var currentValue;
	var currentObj;
	var compareObj;
	var compareValue;
	
	for(i=1; i<this.getSize();i++) {
		currentObj = this.data[i];
		currentValue = currentObj[f];
		
		j= i-1;
		compareObj = this.data[j];
		compareValue = compareObj[f];
		
		while(j >=0 && compareValue > currentValue) {
			this.data[j+1] = this.data[j];
			j--;
			if (j >=0) {
				compareObj = this.data[j];
				compareValue = compareObj[f];
			}				
		}	
		this.data[j+1] = currentObj;
	}
}

// clone() -- copies the contents of a Vector to another Vector returning the new Vector.
function clone() {
	var newVector = new Vector(this.size);
	
	for (var i=0; i<this.size; i++) {
		newVector.addElement(this.data[i]);
	}
	
	return newVector;
}

// toString() -- returns a string rep. of the Vector
function toString() {
	var str = "Vector Object properties:\n" +
	          "Increment: " + this.increment + "\n" +
	          "Size: " + this.size + "\n" +
	          "Elements:\n";
	
	for (var i=0; i<getSize(); i++) {
		for (var prop in this.data[i]) {
			var obj = this.data[i];
			str += "\tObject." + prop + " = " + obj[prop] + "\n";
		}
	}
	return str;	
}

// overwriteElementAt() - overwrites the element with an object at the specific index.
function overwriteElementAt(obj, index) {
	this.data[index] = obj;
}



var expdate = new Date();
expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 

function getCookie(name) {
var dcookie = document.cookie; 
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
                if (dcookie.substring(cbegin, vbegin) == cname) { 
                var vend = dcookie.indexOf (";", vbegin);
                        if (vend == -1) vend = clen;
                return unescape(dcookie.substring(vbegin, vend));
                }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
                if (cbegin == 0) break;
        }
return null;
}

function setCookie (name, value, expires) {
  if (!expires) expires = new Date();
  document.cookie = name + '=' + escape (value) + '; expires=' + expires.toGMTString() +  '; path=/';
}

function delCookie (name) {
var expireNow = new Date();
document.cookie = name + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}

function setCookieArray(name){
this.length = setCookieArray.arguments.length - 1;
        for (var i = 0; i < this.length; i++) {
        this[i + 1] = setCookieArray.arguments[i + 1]
        setCookie (name + i, this[i + 1], expdate);
        }        
}

function getCookieArray(name){
var i = 0;
        while (getCookie(name + i) != null) {
        this[i + 1] = getCookie(name + i);
        i++; this.length = i; 
        }
}

function delCookieArray(name){
var i = 0;
        while (getCookie(name + i) != null) {
        var newname = name + i;  
        delCookie(newname);
        i++; this.length = i; 
        }
}

function cookieVector(name) {
this.vector = Vector(0);
this.vecName = name;
this.addCookie = addCookie;
}

function getTitle() {
  return this.vecName;
}
 
function addCookie(value) {
  addElement(value);
  setCookie (this.vecName + indexOf(value), value, expdate);
}

function getURL(uri) {
  var sizeOf = location.href.length;
  uri.dir = location.href.substring(0, sizeOf);
  uri.array = uri.dir.split("/");
  var arraySize = uri.array.length;
  uri.pagename = "";
  uri.namespace = new String();
  for(var i = 3; i <= arraySize-1; i++) {
    if(i==arraySize-1){
      uri.pagename = uri.pagename+uri.array[i];
    }
    else {
      uri.pagename = uri.pagename+uri.array[i]+"/";
    }
  }
  var isGWW = uri.array[2].indexOf("wiki.guildwars.com");
  var isGWIKI = uri.array[2].indexOf("gw.gamewikis.org");
  if (isGWW != -1 ) { uri.thisSite = "GWW"  };
  if (isGWIKI != -1 ) {uri.thisSite = "GWIKI" };
  return uri;
}

//MyWikiLinks - adapted from Rein of Terror's original tool at [[User:Rein Of Terror/MyWikiLinks]]
cookieVector('links');
var uri = new Object();
getURL(uri);


function MWL() {
 var links = '';
 for(var i = 0; i < getSize(); i++) {
  links +=  '<li id=\><a href=\"/wiki/'+getElementAt(i)+'\">'+getElementAt(i)+'</a></li>'
 }
 
 document.getElementById('p-navigation').innerHTML += 
 '</div>'+'<p>'+
 '<div id=p-my wiki links=portlet>'+
 '<h5>'+getTitle()+'</h5>'+
 '<div class=pBody><ul>'+
 '--<a href=javascript:addCookie(uri.pagename)>TEST</a>'+
 links+
 '</ul></div></div>';
 // Adding link to user js for convenience.
 addPortletLink('p-personal', 'http:/wiki/Special:Mypage/monobook.js', 'javascript', '', '', '');
}
addOnloadHook(MWL);