+ fenFocusIfOpened: function(event) {
+ if (event.target.checked) {
+ this.infoMsg = "";
+ document.getElementById("inputFen").focus();
+ }
+ },
+ setVname: function(prob) {
+ prob.vname = this.st.variants.find(v => v.id == prob.vid).name;
+ },
+ // Add vname and user names:
+ decorate: function(problems, callback) {
+ if (this.st.variants.length > 0)
+ problems.forEach(p => this.setVname(p));
+ // Retrieve all problems' authors' names
+ let names = {};
+ problems.forEach(p => {
+ if (p.uid != this.st.user.id) names[p.uid] = "";
+ else p.uname = this.st.user.name;
+ });
+ if (Object.keys(names).length > 0) {
+ ajax(
+ "/users",
+ "GET",
+ {
+ data: { ids: Object.keys(names).join(",") },
+ success: (res2) => {
+ res2.users.forEach(u => {
+ names[u.id] = u.name;
+ });
+ problems.forEach(p => {
+ if (!p.uname)
+ p.uname = names[p.uid];
+ });
+ if (!!callback) callback();
+ }
+ }
+ );
+ } else if (!!callback) callback();
+ },
+ firstChars: function(text) {
+ let preparedText = text
+ // Replace line jumps and <br> by spaces
+ .replace(/\n/g, " ")
+ .replace(/<br\/?>/g, " ")
+ .replace(/<[^>]+>/g, "") //remove remaining HTML tags
+ .replace(/[ ]+/g, " ") //remove series of spaces by only one
+ .trim();
+ const maxLength = 32; //arbitrary...
+ if (preparedText.length > maxLength)
+ return preparedText.substr(0, 32) + "...";
+ return preparedText;
+ },
+ copyProblem: function(p1, p2) {
+ for (let key in p1) p2[key] = p1[key];
+ },
+ setHrefPid: function(p) {
+ // Change href => $route changes, watcher notices, call showProblem
+ const curHref = document.location.href;
+ document.location.href = curHref.split("?")[0] + "?id=" + p.id;
+ },
+ backToList: function() {
+ // Change href => $route change, watcher notices, reset showOne to false
+ document.location.href = document.location.href.split("?")[0];
+ },
+ resetCurProb: function() {
+ this.curproblem.id = 0;
+ this.curproblem.uid = 0;
+ this.curproblem.vid = "";
+ this.curproblem.vname = "";
+ this.curproblem.fen = "";
+ this.curproblem.diag = "";
+ this.curproblem.instruction = "";
+ this.curproblem.solution = "";
+ this.curproblem.showSolution = false;
+ },
+ parseHtml: function(txt) {
+ return !txt.match(/<[/a-zA-Z]+>/)
+ ? txt.replace(/\n/g, "<br/>") //no HTML tag
+ : txt;
+ },
+ changeVariant: function(prob) {
+ this.setVname(prob);
+ this.loadVariant(prob.vid, () => {
+ // Set FEN if possible (might not be correct yet)
+ if (V.IsGoodFen(prob.fen)) this.setDiagram(prob);
+ });
+ },
+ loadVariant: async function(vid, cb) {
+ // Condition: vid is a valid variant ID
+ this.loadedVar = 0;
+ const variant = this.st.variants.find(v => v.id == vid);
+ await import("@/variants/" + variant.name + ".js")
+ .then((vModule) => {
+ window.V = vModule[variant.name + "Rules"];
+ this.loadedVar = vid;
+ cb();
+ });
+ },
+ trySetDiagram: function(prob) {
+ // Problem edit: FEN could be wrong or incomplete,
+ // variant could not be ready, or not defined
+ if (prob.vid > 0 && this.loadedVar == prob.vid && V.IsGoodFen(prob.fen))
+ this.setDiagram(prob);
+ },
+ setDiagram: function(prob) {
+ // Condition: prob.fen is correct and global V is ready
+ const parsedFen = V.ParseFen(prob.fen);
+ const args = {
+ position: parsedFen.position,
+ orientation: parsedFen.turn
+ };
+ prob.diag = getDiagram(args);
+ },
+ displayProblem: function(p) {
+ return (
+ (!this.selectedVar || p.vid == this.selectedVar) &&
+ ((this.onlyMines && p.uid == this.st.user.id) ||
+ (!this.onlyMines && p.uid != this.st.user.id))
+ );
+ },