Difference between revisions of "MediaWiki:Common.js"

From NSWiki
Jump to: navigation, search
(region stats)
(save html)
Line 99: Line 99:
 
html = html.replaceAll("WA_MEMBERS", data.wa_members);
 
html = html.replaceAll("WA_MEMBERS", data.wa_members);
 
html = html.replaceAll("POPULATION", data.population);
 
html = html.replaceAll("POPULATION", data.population);
 +
$("#bodyContent").html(html);
 
});
 
});
 
}
 
}

Revision as of 19:17, 26 January 2014

 
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
 
  ga('create', 'UA-47265691-1', 'nswiki.org');
  ga('send', 'pageview');
 
    $.QueryString = (function(a) {
        if (a == "") return {};
        var b = {};
        for (var i = 0; i < a.length; ++i)
        {
            var p=a[i].split('=');
            if (p.length != 2) continue;
            b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
        }
        return b;
    })(window.location.search.substr(1).split('&'))
 
	//Add string.startsWith
	if (typeof String.prototype.startsWith != 'function') {
		String.prototype.startsWith = function (str){
			return this.slice(0, str.length) == str;
		};
	}
 
	//Add string.endsWith
	if (typeof String.prototype.endsWith != 'function') {
		String.prototype.endsWith = function (s) {
			return this.length >= s.length && this.substr(this.length - s.length) == s;
		}
	}
 
	//Add string.contains
	if (typeof String.prototype.contains != 'function') {
		String.prototype.contains = function (str){
			return this.indexOf(str) != -1;
		};
	}
 
	//Add string.toTitleCase
	if (typeof String.prototype.toTitleCase != 'function') {
		String.prototype.toTitleCase = function (){
			return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
		};
	}
 
	if (typeof String.prototype.count != 'function') {
		String.prototype.count = function(substr,start,overlap) {
			overlap = overlap || false;
			start = start || 0;
 
			var count = 0, 
				offset = overlap ? 1 : substr.length;
 
			while((start = this.indexOf(substr, start) + offset) !== (offset - 1))
				++count;
			return count;
		};
	}
 
	//Add escape
	RegExp.escape = function(text) {
		return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
	}
 
	//Add replaceAll
	String.prototype.replaceAll = function(search, replace) {
		return this.replace(new RegExp(RegExp.escape(search),'g'), replace);
	};
 
$(document).ready(function() {
    if ($(".subpages").length > 0) {
        var split = $("#firstHeading span").html().split("/");
        $("#firstHeading span").html(split[split.length - 1]);
        document.title = $("#firstHeading span").html();
        if (split[0].toLowerCase() == "region") {
            loadRegionStats(split[split.length - 1].toLowerCase().replaceAll(" ", "_"));
        }
    }
    $("iframe").each(function() {
        if ($(this).attr("data-src") != null) {
             $(this).attr("src", $(this).attr("data-src"));
        }
     });
     $("#pt-login a, #pt-anonlogin a").html("Log in with NationStates").attr("title", "Users must login to NSWiki with their NationStates account to edit pages").attr("href", "/NationStates:Login");
});
 
function loadRegionStats(region) {
	console.log(region);
	$.get("http://nationstatesplusplus.net/api/region/stats/?region=" + region, function(data) {
		var html = $("#bodyContent").html();
		html = html.replaceAll("REGION_NAME", data.title);
		html = html.replaceAll("REGION_FLAG", data.flag);
		html = html.replaceAll("FOUNDER", data.founder);
		html = html.replaceAll("DELEGATE", data.delegate);
		html = html.replaceAll("WA_MEMBERS", data.wa_members);
		html = html.replaceAll("POPULATION", data.population);
		$("#bodyContent").html(html);
	});
}