660f0be9976e56109716fed35aed292a3affb755
[vchess.git] / public / javascripts / components / rules.js
1 // Load rules on variant page
2 Vue.component('my-rules', {
3 data: function() {
4 return { content: "" };
5 },
6 template: `<div v-html="content" class="section-content"></div>`,
7 mounted: function() {
8 // AJAX request to get rules content (plain text, HTML)
9 ajax("/rules/" + variant, "GET", response => {
10 let replaceByDiag = (match, p1, p2) => {
11 const args = self.parseFen(p2);
12 return getDiagram(args);
13 };
14 self.content = response.replace(/(fen:)([^:]*):/g, replaceByDiag);
15 }
16 },
17 methods: {
18 parseFen(fen) {
19 const fenParts = fen.split(" ");
20 return {
21 position: fenParts[0],
22 marks: fenParts[1],
23 orientation: fenParts[2],
24 };
25 },
26 },
27 })