2011-m1s2-ter/code/serveur/php/ressources/my-extensions.js
2011-05-14 18:23:37 +02:00

170 lines
4.9 KiB
JavaScript

Number.prototype.clip = function(min, max, floor) {
try {
return Math.min(Math.max(floor ? Math.floor(this) : this, min), max);
} catch(e) {alert("Error Number.prototype.clip");alert(e);}
};
Number.prototype.mapInterval = function(a,b,x,y) {
return x + ((this-a) / (b-a) * (y-x));
}
function dichotomy(start, isBigger) {
try {
var i = 0, min = 0, max, half;
for (max = start || 1; ++i < 10 && !isBigger(max); max *= 2);
for (half = start; Math.abs(min-max) > 0.1; half = (min + max) / 2) {
if (!isBigger(half)) min = half;
else max = half;
}
while (half > 1 && isBigger(half)) { --half; }
return half;
} catch(e) {alert("Error dichotomy");alert(e);}
}
$.fn.fold = function(acc, fn) {
try {
this.each(function(i,e) { acc = fn(acc, i, e); });
return acc;
} catch(e) {alert("Error $.fn.fold");alert(e);}
};
$.fn.maxWidth = function() { return this.fold(0, function(acc,i,e){ return Math.max(acc, $(e).width()); }); };
$.fn.maxHeight = function() { return this.fold(0, function(acc,i,e){ return Math.max(acc, $(e).height()); }); };
$.fn.sumHeight = function() { return this.fold(0, function(acc,i,e){ return acc + $(e).height(); }); };
$.fn.sumOuterHeight = function() { return this.fold(0, function(acc,i,e){ return acc + $(e).outerHeight(); }); };
$.fn.hasScroll = function() {
var e = this.get(0);
return (e.clientHeight != e.scrollHeight) || (e.clientWidth != e.scrollWidth);
}
$.fn.fitFont = function() {
try {
var that = this;
var setFont = this.find('.setFont').andSelf();
var size = dichotomy(parseInt(this.css("font-size"), 10), function(x) {
setFont.css("fontSize", x);
return that.$ormap(function(i,e) { return e.hasScroll(); });
});
this.css("font-size", Math.max(0, size));
return this;
} catch(e) {alert("Error $.fn.fitFont");alert(e);}
};
$.fn.swapCSS = function(k,v) {
var old = this.css(k);
this.css(k, v);
return old;
};
$.fn.$each = function(fn) {
this.each(function(i,e) { return fn(i, $(e)); });
};
$.fn.$ormap = function(fn) {
var ret = false;
this.each(function(i,e) { if (fn(i, $(e))) { ret = true; return false; } });
return ret;
};
function queueize(method) {
try {
return function() {
var $this = this;
var args = arguments;
return this.queue(function(next) {
$this[method].apply($this,args);
next();
});
};
} catch(e) {alert("Error queueize");alert(e);}
}
$.fn.qAddClass = queueize("addClass");
$.fn.qRemoveClass = queueize("removeClass");
$.fn.qShow = queueize("show");
$.fn.qHide = queueize("hide");
$.fn.qCss = queueize("css");
$.fn.wh = function(w, h) {
try {
return this.width(w).height(isNaN(+h) ? w : h);
} catch(e) {alert("Error $.fn.wh");alert(e);}
};
$.fn.relativePos = function(xAnchor, yAnchor, to, justCss) {
try {
if (to) this.css("position", "absolute");
var deltaX = this.outerWidth() * xAnchor;
var deltaY = this.outerHeight() * yAnchor;
if (to) {
var css = {
left: to.left - deltaX,
top: to.top - deltaY
};
return (justCss ? css : this.offset(css));
} else {
var pos = this.offset();
pos.left += deltaX;
pos.top += deltaY;
return pos;
}
} catch(e) {alert("Error $.fn.relativePos");alert(e);}
};
$.each({
center: {x:0.5, y:0.5},
north: {x:0.5, y:0},
northEast: {x:1, y:0},
east: {x:1, y:0.5},
southEast: {x:1, y:1},
south: {x:0.5, y:1},
southWest: {x:0, y:1},
west: {x:0, y:0.5},
northWest: {x:0, y:0},
}, function(i,e) {
try {
var x = e.x;
var y = e.y;
$.fn[i] = function(to, justCss) {
try {
return this.relativePos(x, y, to, justCss);
} catch(e) {alert("Error auto-generated $.fn." + i);alert(e);}
};
} catch(e) {alert("Error top-level anonymous in my-extensions");alert(e);}
});
$.fn.clickOnce = function(fn) {
try {
this.unbind("click",fn).click(fn);
} catch(e) {alert("Error $.fn.clickOnce");alert(e);}
};
/**
* startcolor et endcolor sont de la forme {r:0,g:255,b:127}
*/
$.fn.goodBad = function(min, max, startcolor, endcolor) {
var val = parseInt(this.text(), 10);
if (isNaN(val)) return this;
this.css("color","rgb("
+(val.mapInterval(min,max,startcolor.r,endcolor.r).clip(0, 255, true))+","
+(val.mapInterval(min,max,startcolor.g,endcolor.g).clip(0, 255, true))+","
+(val.mapInterval(min,max,startcolor.b,endcolor.b).clip(0, 255, true))+")");
return this;
};
var PtiClic = $({});
PtiClic.queueJSON = function(url, data, ok, error) {
};
/* Enchaînement des écrans
Aller sur un écran donné.
Recevoir des données avant d'entrer dans un écran.
Envoyer des données avant de quiter un écran.
Vérouiller l'écran courant pendant qu'on attend un transfert ou bien des écrans d'«attente».
Lorsqu'un transfert a échoué car non logué, on va sur l'écran de connexion et on retente le transfert ensuite.
Stocker uniquement les données importantes dans l'url (état, numéro de partie, réponses).
Pouvoir basculer sur un écran et exécuter quelque chose une fois qu'il est chargé (exécuter le commit pour l'url).
*/