summaryrefslogtreecommitdiff
path: root/scripts/cards/card.js
blob: 3ca435c682984fab6dd6a79b667073bd0bf3d91f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';

// Possible positions of content in a card
const CardPos = ["top", "topl", "topr", "mid", "midt", "midb", "bot", "botl", "botr", "all"];

// Card class represents one card.
// Every card should have a deck.
// Use deck.appendCard, deck.prependCard, or deck.addCardAt to make a card visible
class Card {
	constructor (id, data)
	{
		this.e = document.createElement("card");
		this.generateElements(data);
		this.e.style.left = "0px";
		this.e.style.top = "0px";
		this.e.card = this;

		this.getID = function() {
			return id;
		}
	}

	// Generate a card with basic text only
	static generateBasicCard (data, el)
	{
		let t = document.createElement("carea");
		t.className = "mid";
		t.innerText = data;
		el.appendChild(t);
	}

	// Generate a card with a simple error message.
	static generateErrorCard (el)
	{
		Card.generateBasicCard("Card Error: data", el);
	}

	// Generate an area of a card
	static generateCArea (data, carea, assetURL)
	{
		// Create and set area
		let area = document.createElement("carea");
		area.className = carea;

		// Create inner area text and images
		for (let i in data)
		{
			if (i == "style")
			{
				for (j in data.style) 
					area.style.setProperty(j, data[i].style[j]);
			}
			
			if (data[i].type == "text")
			{
				let e = document.createElement("ctext");

				e.innerText = data[i].text;

				for (let j in data[i].style)
					e.style.setProperty(j, data[i].style[j]);

				area.appendChild(e);

			}
			else if (data[i].type == "image")
			{
				let e = document.createElement("cimage");

				e.style.backgroundImage = "url(\"" + assetURL + data[i].image + "\")";
				
				for (let j in data[i].style)
					e.style.setProperty(j, data[i].style[j]);

				area.appendChild(e);
			}
		}

		return area;
	}

	// Generate a card with rich visuals
	static generateObjectCard (data, el)
	{
		// Generate card areas.
		for (let i of CardPos)
		{
			if (typeof data[i] == "object")
				el.appendChild(this.generateCArea(data[i], i, data.assetURL));
		}
	}

	generateElements (data)
	{
		while(this.e.firstElementChild != null)
			this.e.firstElementChild.remove();

		switch (typeof data)
		{
			case "object":
				Card.generateObjectCard(data, this.e);
				break;
			case "string":
				Card.generateBasicCard(data, this.e);
				break;
			default:
				Card.generateErrorCard(this.e);
		}
	}

	setPos (p)
	{
		this.e.style.setProperty("--cpos", p);
	}

	resetPos()
	{
		this.e.style.left = "0px";
		this.e.style.top = "0px";
	}

	getDeck() {
		return this.e.parentElement.deck;
	}
}