MediaWiki:Skin/Adventure/Stateful.js: Unterschied zwischen den Versionen

aus Kamelopedia, der wüsten Enzyklopädie
Zur Navigation springen Zur Suche springen
Zeile 12: Zeile 12:
 
     this.update = function(args) {
 
     this.update = function(args) {
 
         for (var i in args)
 
         for (var i in args)
             this.args[i] = args[i];
+
             if (args[i] == "")
 +
                delete this.args[i];
 +
            else
 +
                this.args[i] = args[i];
 
     }
 
     }
  
Zeile 46: Zeile 49:
 
     for (var i in pairs) {
 
     for (var i in pairs) {
 
         var kvs = pairs[i].split("=");
 
         var kvs = pairs[i].split("=");
         if (!advOnly || kvs[0].substr(0,1) == "_")
+
         if (kvs.length > 1 && (!advOnly || kvs[0].substr(0,1) == "_"))
 
             this.args[kvs[0]] = kvs[1];
 
             this.args[kvs[0]] = kvs[1];
 
     }
 
     }

Version vom 1. Juni 2012, 09:54 Uhr

/**
 * Stellt Statusfeatures für das Adventure bereit (noch experimentell).
 * 
 * Bekannte Bugs:
 * - Tod führt nicht zu einem Reset der Stati
 * - man sollte die Stati irgendwie wieder loswerden können
 */

function UrlArgs(url, advOnly){
    this.args = {};

    this.update = function(args) {
        for (var i in args)
            if (args[i] == "")
                delete this.args[i];
            else
                this.args[i] = args[i];
    }

    this.serialize = function(){
        var s = "";
        for (var i in this.args)
            s += "&" + i + "=" + this.args[i];
        return s.substr(1);
    }

    this.keep = function() {
        var other = new UrlArgs(location.href, true);
        other.update(this.args);
        var s = other.serialize();
        return this.base + (s ? "?"+s : "") + this.anchor;
    }

    this.base = /^[^?#]+/.exec(url)[0];
    
    var anchor = /#(.+)/.exec(url);

    if (anchor == null)
        this.anchor = "";
    else
        this.anchor = anchor[0];

    var script = /\?([^#]+)/.exec(url);
 
    if (script == null)
        return;

    var pairs = script[1].split("&");
    for (var i in pairs) {
        var kvs = pairs[i].split("=");
        if (kvs.length > 1 && (!advOnly || kvs[0].substr(0,1) == "_"))
            this.args[kvs[0]] = kvs[1];
    }
}

function keepState(link) {
    link.href = new UrlArgs(link.href).keep();
}

addOnloadHook(function () {
    $("#bodyContent a").each(function (a,b) {
        keepState(b);
    });
});