-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStore.js
104 lines (95 loc) · 2.48 KB
/
Store.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
101
102
103
104
// Store Class: Handles Storage
class Store
{
static getId()
{
// Ideally: would be generating a uuid here
let id = localStorage.getItem('id');
if (id === null)
{
id = -1;
} else
{
id = parseInt(id);
}
id += 1;// next consecutive id
localStorage.setItem('id', id);
return id;
}
static getQueue(id)
{
let queues = Store.getQueues();
for (let i = 0; i < queues.length; ++i)
{
if (queues[i].id === parseInt(id))
{
return queues[i];
}
}
return null;
}
static getQueues()
{
let queues = localStorage.getItem('queues');
if (queues === null)
{
queues = [];
} else
{
let temp = localStorage.getItem('queues');
queues = JSON.parse(temp);
}
return queues;
}
static addQueue(queue)
{
const queues = Store.getQueues();
queues.push(queue);
localStorage.setItem('queues', JSON.stringify(queues));
}
static updateQueue(queue)
{
let queues = Store.getQueues();
for (let i = 0; i < queues.length; ++i)
{
if (queues[i].id === parseInt(queue.id))
{
queues[i] = queue;
break;
}
}
localStorage.setItem('queues', JSON.stringify(queues));
}
static removeQueue(id)
{
const queues = Store.getQueues();
queues.forEach((queue, index) =>
{
if (queue.id === parseInt(id))
{
queues.splice(index, 1);
}
});
localStorage.setItem('queues', JSON.stringify(queues));
}
static moveQueue(id, change)
{
let queues = Store.getQueues();
let queue = null;
let ndx = -1;
for (let i = 0; i < queues.length; ++i)
{
if (queues[i].id === parseInt(id))
{
ndx = i;
break;
}
}
if (ndx == -1 || (queues.length == 1) || (ndx === 0 && change === -1) || (ndx === queues.length - 1 && change === 1))
{
return;
}
Util.arrayMove(queues, ndx, ndx + change);
localStorage.setItem('queues', JSON.stringify(queues));
}
}