-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
277 lines (277 loc) · 9.67 KB
/
app.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
// Project Type
var ProjectStatus;
(function (ProjectStatus) {
ProjectStatus[ProjectStatus["Active"] = 0] = "Active";
ProjectStatus[ProjectStatus["Finished"] = 1] = "Finished";
})(ProjectStatus || (ProjectStatus = {}));
class Project {
constructor(id, title, description, people, status) {
this.id = id;
this.title = title;
this.description = description;
this.people = people;
this.status = status;
}
}
class State {
constructor() {
this.listeners = [];
}
addListener(listenerFn) {
this.listeners.push(listenerFn);
}
}
class ProjectState extends State {
constructor() {
super();
this.projects = [];
}
static getInstance() {
if (this.instance)
return this.instance;
this.instance = new ProjectState();
return this.instance;
}
addListener(listenerFn) {
this.listeners.push(listenerFn);
}
addProject(title, description, numberOfPeople) {
const newProject = new Project(Math.random().toString(), title, description, numberOfPeople, ProjectStatus.Active);
this.projects.push(newProject);
this.notifyListeners();
}
moveProject(projectId, newStatus) {
const project = this.projects.find((proj) => proj.id === projectId);
if (project && project.status !== newStatus) {
project.status = newStatus;
this.notifyListeners();
}
}
notifyListeners() {
for (const listenerFn of this.listeners) {
listenerFn(this.projects.slice());
}
}
}
const projectState = ProjectState.getInstance();
function validate(validatableInput) {
let isValid = true;
if (validatableInput.required) {
isValid = isValid && validatableInput.value.toString().trim().length !== 0;
}
if (validatableInput.minLength != null &&
typeof validatableInput.value === "string") {
isValid =
isValid && validatableInput.value.length >= validatableInput.minLength;
}
if (validatableInput.maxLength != null &&
typeof validatableInput.value === "string") {
isValid =
isValid && validatableInput.value.length <= validatableInput.maxLength;
}
if (validatableInput.min != null &&
typeof validatableInput.value === "number") {
isValid = isValid && +validatableInput.value >= validatableInput.min;
}
if (validatableInput.max != null &&
typeof validatableInput.value === "number") {
isValid = isValid && +validatableInput.value <= validatableInput.max;
}
return isValid;
}
// autobind decorator
function autobind(target, methodName, descriptor) {
const originalMethod = descriptor.value;
const adjDescriptor = {
configurable: true,
get() {
const boundFn = originalMethod.bind(this);
return boundFn;
},
};
return adjDescriptor;
}
// Component Base Class
class Component {
constructor(templateID, hostElementId, insertAtStart, newElementId) {
this.templateElement = document.getElementById(templateID);
this.hostElement = document.getElementById(hostElementId);
const importedNode = document.importNode(this.templateElement.content, true);
this.element = importedNode.firstElementChild;
if (newElementId)
this.element.id = newElementId;
this.attach(insertAtStart);
}
attach(insertAtStart) {
this.hostElement.insertAdjacentElement(insertAtStart ? "afterbegin" : "beforeend", this.element);
}
}
// ProjectItem Class
class ProjectItem extends Component {
get persons() {
if (this.project.people === 1)
return "1 person";
else
return `${this.project.people} persons`;
}
constructor(hostId, project) {
super("single-project", hostId, false, project.id);
this.project = project;
this.configure();
this.renderContent();
}
dragStartHandler(event) {
event.dataTransfer.setData("text/plain", this.project.id);
event.dataTransfer.effectAllowed = "move";
}
dragEndHandler(_) {
console.log("END");
}
configure() {
this.element.addEventListener("dragstart", this.dragStartHandler);
this.element.addEventListener("dragend", this.dragEndHandler);
}
renderContent() {
this.element.querySelector("h2").textContent = this.project.title;
this.element.querySelector("h3").textContent = this.persons + " assigned";
this.element.querySelector("p").textContent = this.project.description;
}
}
__decorate([
autobind
], ProjectItem.prototype, "dragStartHandler", null);
__decorate([
autobind
], ProjectItem.prototype, "dragEndHandler", null);
// ProjectList class
class ProjectList extends Component {
constructor(type) {
super("project-list", "app", false, `${type}-projects`);
this.type = type;
this.assignedProjects = [];
this.configure();
this.renderContent();
}
dragOverHandler(event) {
if (event.dataTransfer && event.dataTransfer.types[0] === "text/plain") {
event.preventDefault();
const listEl = this.element.querySelector("ul");
listEl.classList.add("droppable");
}
}
dropHandler(event) {
const projectId = event.dataTransfer.getData("text/plain");
projectState.moveProject(projectId, this.type === "active" ? ProjectStatus.Active : ProjectStatus.Finished);
}
dragLeaveHandler(event) {
const listEl = this.element.querySelector("ul");
listEl.classList.remove("droppable");
}
renderContent() {
const listId = `${this.type}-projects-list`;
this.element.querySelector("ul").id = listId;
this.element.querySelector("h2").textContent =
this.type.toUpperCase() + " PROJECTS";
}
configure() {
this.element.addEventListener("dragover", this.dragOverHandler);
this.element.addEventListener("dragleave", this.dragLeaveHandler);
this.element.addEventListener("drop", this.dropHandler);
projectState.addListener((projects) => {
const relevantProjects = projects.filter((project) => {
if (this.type === "active")
return project.status === ProjectStatus.Active;
else
return project.status === ProjectStatus.Finished;
});
this.assignedProjects = relevantProjects;
this.renderProjects();
});
}
renderProjects() {
const listEl = document.getElementById(`${this.type}-projects-list`);
listEl.innerHTML = "";
for (const projectItem of this.assignedProjects) {
new ProjectItem(this.element.querySelector("ul").id, projectItem);
}
}
}
__decorate([
autobind
], ProjectList.prototype, "dragOverHandler", null);
__decorate([
autobind
], ProjectList.prototype, "dropHandler", null);
__decorate([
autobind
], ProjectList.prototype, "dragLeaveHandler", null);
// ProjectInput class
class ProjectInput extends Component {
constructor() {
super("project-input", "app", true, "user-input");
this.titleInputElement = this.element.querySelector("#title");
this.descriptionInputElement = this.element.querySelector("#description");
this.peopleInputElement = this.element.querySelector("#people");
this.configure();
}
configure() {
this.element.addEventListener("submit", this.submitHandler);
}
renderContent() { }
gatherUserInput() {
const enteredTitle = this.titleInputElement.value;
const enteredDescription = this.descriptionInputElement.value;
const enteredPeople = this.peopleInputElement.value;
const titleValidatable = {
value: enteredTitle,
required: true,
};
const descriptionValidatable = {
value: enteredDescription,
required: true,
minLength: 5,
};
const peopleValidatable = {
value: +enteredPeople,
required: true,
min: 1,
max: 5,
};
if (!validate(titleValidatable) ||
!validate(descriptionValidatable) ||
!validate(peopleValidatable)) {
alert("Invalid input, please try again");
return;
}
else {
return [enteredTitle, enteredDescription, +enteredPeople];
}
}
clearInputs() {
this.titleInputElement.value = "";
this.descriptionInputElement.value = "";
this.peopleInputElement.value = "";
}
submitHandler(event) {
event.preventDefault();
const userInput = this.gatherUserInput();
if (Array.isArray(userInput)) {
const [title, desc, people] = userInput;
projectState.addProject(title, desc, people);
this.clearInputs();
}
}
}
__decorate([
autobind
], ProjectInput.prototype, "submitHandler", null);
const projInput = new ProjectInput();
const activeProjectList = new ProjectList("active");
const finishedProjectList = new ProjectList("finished");