Ajout de try/catch autour de chaque fonction, pour tenter de récupérer toutes les erreurs.

This commit is contained in:
Georges Dupéron 2011-04-28 16:12:06 +02:00
parent c5415d3718
commit 52a0821586
2 changed files with 122 additions and 19 deletions

View File

@ -1,8 +1,11 @@
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);}
};
function dichotomy(start, isBigger) {
try {
var i = 0, min = 0, max, half;
for (max = start || 1; ++i < 10 && !isBigger(max); max *= 2);
@ -12,26 +15,34 @@ function dichotomy(start, isBigger) {
}
while (half > 1 && isBigger(half)) { --half; }
return half;
} catch(e) {alert("Error dichotomy");alert(e);}
}
$.fn.maxWidth = function() {
try {
max = 0;
this.each(function(i,e){ max = Math.max(max, $(e).width()); });
return max;
} catch(e) {alert("Error $.fn.maxWidth");alert(e);}
}
$.fn.maxHeight = function() {
try {
max = 0;
this.each(function(i,e){ max = Math.max(max, $(e).height()); });
return max;
} catch(e) {alert("Error $.fn.maxHeight");alert(e);}
}
$.fn.sumHeight = function() {
try {
sum = 0;
this.each(function(i,e){ sum += $(e).height(); });
return sum;
} catch(e) {alert("Error sumHeight");alert(e);}
}
$.fn.fitFont = function(w, h, minFont, maxFont) {
try {
var oldpos = this.css("position");
this.css({
position: "absolute",
@ -41,18 +52,22 @@ $.fn.fitFont = function(w, h, minFont, maxFont) {
var that = this;
this.css("font-size", dichotomy(parseInt(this.css("font-size"), 10), function(x) {
try {
that.css("fontSize", x);
fubar = wrappers;
return (wrappers.maxHeight() > h || wrappers.maxWidth() > w);
} catch(e) {alert("Error anonymous in $.fn.fitFont");alert(e);}
}).clip(minFont || 0, maxFont || Infinity));
// Restore stuff
this.css("position", oldpos);
//wrappers.children().unwrap();
return this;
} catch(e) {alert("Error $.fn.fitFont");alert(e);}
}
$.fn.fitIn = function(e, t, r, b, l) {
try {
e = $(e);
if (isNaN(+t)) t = 0;
if (isNaN(+r)) r = t;
@ -66,9 +81,11 @@ $.fn.fitIn = function(e, t, r, b, l) {
l *= w;
this.fitFont(w - r - l, h - t - b, 20).center(e.center());
return this;
} catch(e) {alert("Error $.fn.fitIn");alert(e);}
}
function queueize(method) {
try {
return function() {
var $this = this;
return this.queue(function(next) {
@ -76,16 +93,20 @@ function queueize(method) {
next();
});
};
} catch(e) {alert("Error queueize");alert(e);}
}
$.fn.qAddClass = queueize("addClass");
$.fn.qRemoveClass = queueize("removeClass");
$.fn.wh = function(w, h) {
try {
return this.width(w).height(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;
@ -102,6 +123,7 @@ $.fn.relativePos = function(xAnchor, yAnchor, to, justCss) {
pos.top += deltaY;
return pos;
}
} catch(e) {alert("Error $.fn.relativePos");alert(e);}
};
$.each({
@ -115,11 +137,19 @@ $.each({
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) { return this.relativePos(x, y, to, justCss); };
$.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);}
};

View File

@ -1,23 +1,29 @@
if (typeof(console) == 'undefined')
console = {log: function() {}};
// ==== URL persistante
function State(init) {
try {
$.extend(this, init || {});
if (!this.screen) this.screen = 'frontpage';
} catch(e) {alert("Error State");alert(e);}
};
State.prototype.commit = function() {
try {
location.hash="#"+encodeURI($.JSON.encode(this));
return this;
} catch(e) {alert("Error State.prototype.commit");alert(e);}
};
State.prototype.get = function(key) {
try {
return this[key];
} catch(e) {alert("Error State.prototype.get");alert(e);}
};
State.prototype.set = function(key, value) {
try {
this[key] = value;
return this;
} catch(e) {alert("Error State.prototype.set");alert(e);}
};
State.prototype.validate = function () {
try {
state = this;
UI().setScreen(this.screen);
if (oldScreen != this.screen) {
@ -26,6 +32,7 @@ State.prototype.validate = function () {
}
if (window[this.screen] && window[this.screen].enter) window[this.screen].enter();
return this;
} catch(e) {alert("Error State.prototype.validate");alert(e);}
};
var runstate = {};
@ -33,12 +40,15 @@ var state;
var oldScreen = '';
var ui = {};
function hashchange() {
try {
var stateJSON = decodeURI(location.hash.substring(location.hash.indexOf("#") + 1));
state = new State($.parseJSON(stateJSON)).validate();
} catch(e) {alert("Error hashchange");alert(e);}
}
// ==== JavaScript Style général
function jss() {
try {
var w = $(window).width();
var h = $(window).height();
var iconSize;
@ -62,10 +72,11 @@ function jss() {
$("#"+state.screen+".screen").show();
if (window[state.screen] && window[state.screen].jss) window[state.screen].jss(w, h, iconSize);
} catch(e) {alert("Error jss");alert(e);}
}
// ==== Interface Android
function UI () {
try {
if (typeof(PtiClicAndroid) != "undefined") {
return PtiClicAndroid;
} else {
@ -73,39 +84,46 @@ function UI () {
isAndroid: function() { return false; },
setPreference: function() {},
getPreference: function() {return "";},
show: function(title, text) {},// { if (typeof console != 'undefined') console.log(title, text);},
dismiss: function() {},// {if (typeof console != 'undefined') console.log('dismiss');},
show: function(title, text) {},
dismiss: function() {},
exit: function() {},
log: function() {},
setScreen: function() {}
};
}
} catch(e) {alert("Error UI");alert(e);}
}
// ==== Code métier général
$(function() {
try {
$(window).resize(jss);
$(window).hashchange(hashchange);
hashchange();
} catch(e) {alert("Error main function");alert(e);}
});
function ajaxError(x) {
UI().dismiss();
var msg = "Erreur fatale. Merci de nous envoyer ce message : ";
msg += x.status+" - "+x.statusText+"\n"+x.responseText.substring(0,20)+((x.responseText == '') ? '': '…');
if (UI().isAndroid)
true;
else
alert(msg);
UI().exit();
try {
UI().dismiss();
var msg = "Erreur fatale. Merci de nous envoyer ce message : ";
msg += x.status+" - "+x.statusText+"\n"+x.responseText.substring(0,20)+((x.responseText == '') ? '': '…');
alert(msg);
UI().exit();
} catch(e) {alert("Error ajaxError");alert(e);}
}
// ==== Code métier pour la frontpage
frontpage = {};
frontpage.jss = function(w, h, iconSize) {
try {
var fp = $("#frontpage.screen");
var $fp = function() { return fp.find.apply(fp,arguments); };
var $fp = function() {
try {
return fp.find.apply(fp,arguments);
} catch(e) {alert("Error anonymous in frontpage.jss");alert(e);}
};
var nbIcons = $fp(".icon").size();
var nbRows = Math.ceil(nbIcons / 2)
var ww = w - 2 * iconSize;
@ -132,6 +150,7 @@ frontpage.jss = function(w, h, iconSize) {
$fp(".frontpage-button > div").css('display', 'block');
$fp(".frontpage-button").each(function(i,e){
try {
e=$(e);
var currentRow = Math.floor(i/2);
var currentColumn = i % 2;
@ -142,27 +161,38 @@ frontpage.jss = function(w, h, iconSize) {
} else {
e.northWest({left:w/2+ww*0.05,top:iconOffset});
}
} catch(e) {alert("Error anonymous in frontpage.jss");alert(e);}
});
} catch(e) {alert("Error frontpage.jss");alert(e);}
};
frontpage.enter = function () {
try {
if (location.hash != '') state.commit();
$("#frontpage .frontpage-button.game").clickOnce(frontpage.click.game);
jss();
UI().dismiss();
} catch(e) {alert("Error frontpage.enter");alert(e);}
};
frontpage.click = {};
frontpage.click.game = function(){
try {
state.set('screen', 'game').validate();
} catch(e) {alert("Error frontpage.click.game");alert(e);}
};
// ==== Code métier pour le jeu
game = {};
game.jss = function(w, h, iconSize) {
try {
var g = $("#game.screen");
var $g = function() { return g.find.apply(g,arguments); };
var $g = function() {
try {
return g.find.apply(g,arguments);
} catch(e) {alert("Error anonymous in game.jss");alert(e);}
};
var mch = h/8, mnh = h*0.075;
$g("#mc-caption-block")
@ -203,17 +233,21 @@ game.jss = function(w, h, iconSize) {
$g(".relations")
.south(g.south());
} catch(e) {alert("Error game.jss");alert(e);}
};
game.enter = function () {
try {
if (!state.game) {
var notAlreadyFetching = !runstate.gameFetched;
runstate.gameFetched = function(data) {
try {
state.game = data;
state.currentWordNb = 0;
state.game.answers = [];
state.commit();
game.buildUi();
} catch(e) {alert("Error anonymous 1 in game.enter");alert(e);}
};
if (notAlreadyFetching) {
UI().show("PtiClic", "Récupération de la partie");
@ -222,26 +256,33 @@ game.enter = function () {
passwd:"bar",
nonce:Math.random()
}, function(data) {
try {
var fn = runstate.gameFetched;
runstate.gameFetched = false;
fn(data);
} catch(e) {alert("Error anonymous 2 in game.enter");alert(e);}
}).error(ajaxError);
}
} else {
game.buildUi();
}
jss();
} catch(e) {alert("Error game.enter");alert(e);}
};
game.leave = function () {
try {
$("#game .relations").empty();
$('#game #mn-caption').stop().clearQueue();
if (runstate.gameFetched) runstate.gameFetched = function() {};
} catch(e) {alert("Error game.leave");alert(e);}
};
game.buildUi = function () {
try {
$("#game .relations").empty();
$.each(state.game.relations, function(i, relation) {
try {
$('#templates .relationBox')
.clone()
.data("rid", relation.id)
@ -255,18 +296,23 @@ game.buildUi = function () {
game.nextWord({left:e.pageX, top:e.pageY}, this);
})
.appendTo("#game .relations");
} catch(e) {alert("Error anonymous in game.buildUi");alert(e);}
});
game.updateText();
} catch(e) {alert("Error game.buildUi");alert(e);}
}
game.updateText = function() {
try {
$("#game .mn").text(state.game.cloud[state.currentWordNb].name);
$("#game .mc").text(state.game.center.name);
jss();
UI().dismiss();
} catch(e) {alert("Error game.updateText");alert(e);}
}
game.animateNext = function (click, button) {
try {
var duration = 700;
var mn = $("#game #mn-caption");
@ -281,7 +327,11 @@ game.animateNext = function (click, button) {
.appendTo("body") // Append to body so we can animate the offset (instead of top/left).
.offset(mn.offset())
.animate({left:click.left, top:click.top, fontSize: 0}, duration)
.queue(function() { $(this).remove(); });
.queue(function() {
try {
$(this).remove();
} catch(e) {alert("Error anonymous 1 in game.animateNext");alert(e);}
});
game.updateText();
var fs = mn.css("fontSize");
@ -289,10 +339,16 @@ game.animateNext = function (click, button) {
(mn)
.css("fontSize", 0)
.animate({fontSize: fs}, {duration:duration, step:function(){mn.center(mncbCenter);}});
.animate({fontSize: fs}, {duration:duration, step:function(){
try {
mn.center(mncbCenter);
} catch(e) {alert("Error anonymous 2 in game.animateNext");alert(e);}
}});
} catch(e) {alert("Error game.animateNext");alert(e);}
}
game.nextWord = function(click, button) {
try {
state.game.answers[state.currentWordNb++] = $(button).data("rid");
if (state.currentWordNb < state.game.cloud.length) {
game.animateNext(click, button);
@ -300,20 +356,25 @@ game.nextWord = function(click, button) {
} else {
state.set('screen','score').validate();
}
} catch(e) {alert("Error game.nextWord");alert(e);}
}
// ==== Code métier pour les scores
score = {};
score.jss = function(w, h, iconSize) {
try {
$(".screen")
.css('text-align', 'center');
} catch(e) {alert("Error score.jss");alert(e);}
};
score.enter = function () {
try {
if (!state.hasScore) {
var notAlreadyFetching = !runstate.scoreFetched;
runstate.scoreFetched = function(data) {
try {
for (var i = 0; i < data.scores.length; ++i) {
state.game.cloud[i].score = data.scores[i];
}
@ -322,6 +383,7 @@ score.enter = function () {
state.hasScore = true;
state.commit();
score.ui();
} catch(e) {alert("Error anonymous 1 in score.enter");alert(e);}
};
if (notAlreadyFetching) {
UI().show("PtiClic", "Calcul de votre score");
@ -334,24 +396,31 @@ score.enter = function () {
answers: state.game.answers,
nonce: Math.random()
}, function(data){
try {
var fn = runstate.scoreFetched;
runstate.scoreFetched = false;
fn(data);
} catch(e) {alert("Error anonymous 2 in score.enter");alert(e);}
}).error(ajaxError);
}
} else {
score.ui();
}
jss();
} catch(e) {alert("Error score.enter");alert(e);}
}
score.leave = function () {
try {
if (runstate.scoreFetched) runstate.scoreFetched = function() {};
} catch(e) {alert("Error score.leave");alert(e);}
};
score.ui = function () {
try {
$("#score .scores").empty();
$.each(state.game.cloud, function(i,e) {
try {
var percentScore = (e.score - state.game.minScore) / (state.game.maxScore - state.game.minScore);
u = $("#templates .scoreLine");
ee = e;
@ -365,10 +434,14 @@ score.ui = function () {
.css("color","rgb("+(255 - 255*percentScore).clip(0,255)+","+(191*percentScore).clip(0,255,true)+",0)")
.end()
.appendTo("#score .scores");
} catch(e) {alert("Error anonymous 1 in score.ui");alert(e);}
});
$("#score #jaivu").clickOnce(function() {
try {
state = new State().validate();
} catch(e) {alert("Error anonymous 2 in score.ui");alert(e);}
});
jss();
UI().dismiss();
} catch(e) {alert("Error score.ui");alert(e);}
}