+ cleanBeforeDestroy: function() {
+ clearInterval(this.socketCloseListener);
+ document.removeEventListener('visibilitychange', this.visibilityChange);
+ window.removeEventListener('focus', this.onFocus);
+ window.removeEventListener('blur', this.onBlur);
+ window.removeEventListener("beforeunload", this.cleanBeforeDestroy);
+ this.conn.removeEventListener("message", this.socketMessageListener);
+ this.send("disconnect");
+ this.conn = null;
+ },
+ getRandomnessClass: function(pc) {
+ return {
+ ["random-" + pc.randomness]: true
+ };
+ },
+ openModalPeople: function() {
+ window.doClick("modalPeople");
+ document.getElementById("inputChat").focus();
+ },
+ anonymousCount: function() {
+ let count = 0;
+ Object.values(this.people).forEach(p => {
+ // Do not cound people who did not send their identity yet:
+ count += (!p.name && p.id === 0) ? 1 : 0;
+ });
+ return count;
+ },
+ visibilityChange: function() {
+ // TODO: Use document.hidden? https://webplatform.news/issues/2019-03-27
+ this.focus = (document.visibilityState == "visible");
+ this.send(this.focus ? "getfocus" : "losefocus");
+ },
+ onFocus: function() {
+ this.focus = true;
+ this.send("getfocus");
+ },
+ onBlur: function() {
+ this.focus = false;
+ this.send("losefocus");
+ },
+ partialResetNewchallenge: function() {
+ // Reset potential target and custom FEN:
+ this.newchallenge.to = "";
+ this.newchallenge.fen = "";
+ this.newchallenge.diag = "";
+ this.newchallenge.memorize = false;
+ },
+ showNewchallengeForm: function() {
+ this.partialResetNewchallenge();
+ window.doClick("modalNewgame");
+ },
+ addPresetChall: function(chall) {
+ // Add only if not already existing:
+ if (this.presetChalls.some(c =>
+ c.vid == chall.vid &&
+ c.cadence == chall.cadence &&
+ c.randomness == chall.randomness
+ )) {
+ return;
+ }
+ const L = this.presetChalls.length;
+ this.presetChalls.push({
+ index: L,
+ vid: chall.vid,
+ vname: chall.vname, //redundant, but easier
+ cadence: chall.cadence,
+ randomness: chall.randomness
+ });
+ localStorage.setItem("presetChalls", JSON.stringify(this.presetChalls));
+ },
+ removePresetChall: function(e, pchall) {
+ e.stopPropagation();
+ const pchallIdx =
+ this.presetChalls.findIndex(pc => pc.index == pchall.index);
+ this.presetChalls.splice(pchallIdx, 1);
+ localStorage.setItem("presetChalls", JSON.stringify(this.presetChalls));
+ },
+ tchallButtonsMargin: function() {
+ if (!!this.curChallToAccept.fen) return { "margin-top": "10px" };
+ return {};
+ },
+ changeChallTarget: function() {
+ if (!this.newchallenge.to) {
+ // Reset potential FEN + diagram
+ this.newchallenge.fen = "";
+ this.newchallenge.diag = "";
+ }
+ },