LWWidget.Beta5.Forums = LWWidget.Game.Page.inherits({ switchMode: function () { this.details = !this.details; this.game.draw(); }, setData: function (data) { var _f = data.getChildren('Communications')[0].getChildren('Forums')[0]; // Read general forums var gf = _f.getChildren('GeneralForums'), gcats = new Base.Util.Hashtable(); for (var i in gf) { var c = gf[i], desc = c.getChildren('Description')[0].getText(), cf = c.getChildren('Forum'), forums = new Array(); for (var j in cf) { forums.push([cf[j].getText(), parseInt(cf[j].getAttribute('topics'), 10), parseInt(cf[j].getAttribute('unread'), 10)]); } gcats.put(desc, forums); } // Read alliance forums var af = _f.getChildren('AllianceForums'), aForums = new Array(); if (af.length) { af = af[0].getChildren('Forum'); for (var i in af) { aForums.push([af[i].getText(), parseInt(af[i].getAttribute('topics'), 10), parseInt(af[i].getAttribute('unread'), 10)]); } } // Store data this.general = gcats; this.alliance = aForums; }, show: function () { this.details = false; }, draw: function (_e) { if (this.details) { _e.innerHTML = this.getDetailedHTML(); } else { _e.innerHTML = this.getShortHTML(); } }, getShortHTML: function () { var text = new Array(), gk = this.general.keys(); for (var i in gk) { text.push(this.catSummary(gk[i], this.general.get(gk[i]))); } if (this.alliance.length) { text.push(this.catSummary('Alliance forums', this.alliance)); } return '

' + text.join('
') + '

Show details'; }, catSummary: function (name, list) { var s = '' + name + ': '; var unread = this.countUnread(list); if (unread > 0) { s += '' + Base.Util.formatNumber(unread) + ' unread topic' + (unread > 1 ? 's' : ''); } else { s += 'no unread topics'; } return s; }, countUnread: function (forums) { var s = 0; for (var i in forums) { s += forums[i][2]; } return s; }, getDetailedHTML: function () { var text = new Array(), gk = this.general.keys(); for (var i in gk) { text.push(this.catDetailed(gk[i], this.general.get(gk[i]))); } if (this.alliance.length) { text.push(this.catDetailed('Alliance forums', this.alliance)); } return '

Hide details

' + text.join('

'); }, catDetailed: function (name, forums) { if (!forums.length) { return ""; } var text = new Array(); text.push('' + name + ''); for (var i in forums) { var f = forums[i], s = '' + f[0] + ': '; if (f[1]) { s += '' + Base.Util.formatNumber(f[1]) + ' topic' + (f[1] > 1 ? 's' : ''); if (f[2]) { s += ' (' + Base.Util.formatNumber(f[2]) + ' unread)'; } } else { s += 'empty forum'; } text.push(s); } return text.join('
'); } });