-
Notifications
You must be signed in to change notification settings - Fork 1
/
cats.js
100 lines (81 loc) · 2.43 KB
/
cats.js
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
'use strict';
/* exported makeCat, cats, isCatInPlayground, addCatToPlayground, removeCatFromPlayground */
const cats = [
{ name: 'Claude', pic: 'i/1.png' },
{ name: 'Colin', pic: 'i/2.png' },
{ name: 'Fluffy', pic: 'i/3.png' },
{ name: 'Genghis', pic: 'i/4.png' },
{ name: 'Madge', pic: 'i/5.png' },
{ name: 'Penny', pic: 'i/6.png' },
{ name: 'Roger', pic: 'i/7.png' },
{ name: 'Top', pic: 'i/8.png' },
];
// give every cat an index that reflects its position in the array
cats.forEach((c, idx) => c.index = idx);
/**
* Makes a kitten element.
* @param {object} cat contains details on the cat to be added
* @param {function} dragStartHandler what to call when dragging starts
* @return {Element}
*/
function makeCat(cat, dragStartHandler) {
const kitty = document.createElement('figure');
const pic = document.createElement('img');
const nom = document.createElement('figcaption');
cat.id = `cat${cat.index}`;
kitty.id = cat.id;
kitty.className = 'cat';
kitty.draggable = true;
kitty.dataset.cat = JSON.stringify(cat);
kitty.addEventListener('dragstart', dragStartHandler);
nom.textContent = cat.name;
pic.src = cat.pic;
pic.alt = 'A cat';
pic.draggable = false; // images are by default draggable, but we want to drag the whole kitty figure
kitty.appendChild(pic);
kitty.appendChild(nom);
return kitty;
}
/**
* @param {number} catIndex
* @return {boolean}
*/
function isCatInPlayground(catIndex) {
return listCatsInPlayground().includes(catIndex);
}
/**
* @param {number} catIndex cat number
* @param {number} pen which playground pen the cat is in (0..2)
*/
function addCatToPlayground(catIndex, pen) {
const catsInPlayground = listCatsInPlayground();
const oldPos = catsInPlayground.indexOf(catIndex);
if (oldPos !== -1) {
catsInPlayground[oldPos] = null;
}
catsInPlayground[pen] = catIndex;
saveCatsInPlayground(catsInPlayground);
}
/**
* @param {number} catIndex cat number
*/
function removeCatFromPlayground(catIndex) {
const catsInPlayground = listCatsInPlayground();
const pos = catsInPlayground.indexOf(catIndex);
if (pos !== -1) {
catsInPlayground[pos] = null;
saveCatsInPlayground(catsInPlayground);
}
}
/**
* @return {boolean}
*/
function listCatsInPlayground() {
return JSON.parse(localStorage.catsInPlayground || '[]');
}
/**
* @param {Array} cats
*/
function saveCatsInPlayground(cats) {
localStorage.catsInPlayground = JSON.stringify(cats);
}