-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
130 lines (114 loc) · 3.51 KB
/
script.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
class Board {
constructor (name) {
this.name = 'Kanban Board';
}
static addElement(child, parent=document.querySelector('#board .column-container')) {
parent.appendChild(child)
initSortable();
}
static removeElement(e) {e.remove()}
static createDeleteButton() {
let deleteButton = document.createElement("button");
deleteButton.className = "btn-delete";
deleteButton.textContent = "x";
return deleteButton;
}
};
class Column extends Board {
constructor (name) {
super(name);
this.id = randomString();
this.name = name;
this.element = this.createColumn();
}
createColumn() {
let column = document.createElement("div");
column.className = "column";
column.appendChild(Board.createDeleteButton());
column.appendChild(this.createColumnTitle());
column.appendChild(this.createColumnAddCardButton());
column.appendChild(this.createColumnCardList());
return column;
}
createColumnTitle() {
let columnTitle = document.createElement("h2");
columnTitle.className = "column-title";
columnTitle.textContent = this.name;
return columnTitle;
}
createColumnCardList() {
let columnCardList = document.createElement("ul");
columnCardList.className = "column-card-list";
return columnCardList;
}
createColumnAddCardButton() {
let columnAddCard = document.createElement("button");
columnAddCard.className = "add-card";
columnAddCard.textContent = "+";
return columnAddCard;
}
}
class Card extends Board {
constructor (name) {
super(name);
this.id = randomString();
this.name = name;
this.element = this.createCard();
}
createCard() {
let card = document.createElement("li");
card.className = "card";
card.appendChild(Board.createDeleteButton());
card.appendChild(this.createCardDescription());
return card;
}
createCardDescription() {
let cardDescription = document.createElement("p");
cardDescription.className = "card-description";
cardDescription.textContent = this.name;
return cardDescription;
}
}
function initSortable() {
$('.column-card-list').sortable({
connectWith: '.column-card-list',
placeholder: 'card-placeholder',
opacity: 0.8,
tolerance: "intersect"
}).disableSelection();
}
function randomString() {
const chars = '0123456789abcdefghiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXTZ';
let str = '';
for (let i = 0; i < 10; i++) {
str += chars[Math.floor(Math.random() * chars.length)];
}
return str;
}
(function setEventListeneres() {
const mainBoard = document.querySelector('.board');
mainBoard.addEventListener('click', (e) => {
if (e.target.matches('.btn-delete')) {
const elementClicked = e.target;
Board.removeElement(elementClicked.parentNode);
} else if (e.target.matches('.add-card')) {
const elementClicked = e.target;
const card = new Card(prompt("Enter the name of the card"));
Board.addElement(card.element, elementClicked.parentNode.children[3]);
} else if (e.target.matches('.create-column')) {
const column = new Column(prompt('Enter a column name'));
Board.addElement(column.element);
}
});
})()
function setup(columnName, cardName) {
const newColumn = new Column(columnName);
Board.addElement(newColumn.element)
if (cardName) {
const card = new Card(cardName);
Board.addElement(card.element, newColumn.element.childNodes[3]);
}
}
setup('To do', 'New task');
setup('Doing', 'Create kanban boards');
setup('Done');