-
Notifications
You must be signed in to change notification settings - Fork 0
/
in-memory-board-store.js
82 lines (72 loc) · 1.76 KB
/
in-memory-board-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
var board = {
boardId: 1,
items: []
};
exports.getBoard = function() {
return board;
};
exports.upsertItem = function(item) {
if (typeof item.id === 'undefined') {
item.id = generateUUID();
item.status = 'NEW_ITEM';
if (typeof item.zindex === 'undefined') {
item.zindex = getHighestZIndex() + 1;
}
if (typeof item.position === 'undefined') {
item.position = getPositionForNewItem();
}
board.items.push(item);
} else {
for (var i = 0; i < board.items.length; i += 1) {
var oldItem = board.items[i];
if (oldItem.id === item.id) {
item.status = 'OLD_ITEM';
board.items[i] = item;
break;
}
}
}
return item;
};
exports.getItem = function(itemId) {
for (var i = 0; i < board.items.length; i += 1) {
var item = board.items[i];
if (item.id === itemId) {
return item;
}
}
};
exports.deleteItem = function(itemId) {
};
var getPositionForNewItem = function() {
var newItemCount = 0;
for (var i = 0; i < board.items.length; i += 1) {
var item = board.items[i];
if (item.status === 'NEW_ITEM') {
newItemCount += 1;
}
}
return {
left: 100 + newItemCount * 60,
top: 100 + newItemCount * 60
};
};
var getHighestZIndex = function() {
var highestZIndex = 2;
for (var i = 0; i < board.items.length; i += 1) {
var item = board.items[i];
if (item.zindex > highestZIndex) {
highestZIndex = item.zindex;
}
}
return highestZIndex;
};
var generateUUID = function() {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x7|0x8)).toString(16);
});
return uuid;
};