-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpower.js
413 lines (377 loc) · 13.2 KB
/
power.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
* ============================= CLASS DEFS ================================
* =========================================================================
*/
class InputLog {
constructor() {
this.queryInputLog = [];
console.log("InputLog created:", this);
}
updateQueryInputLog() {
const fields = [
"nameInput",
"phone",
"linkedin",
"github",
"email",
"summaryStatement",
"education",
"skills",
"projects",
"experience",
"certifications",
];
const inputData = {};
fields.forEach(
(field) => (inputData[field] = document.getElementById(field).value)
);
this.queryInputLog.push(inputData);
console.log("InputLog updated:", this.queryInputLog);
}
generateSections() {
const inputData = this.queryInputLog[0];
return Object.keys(inputData).map((key, index) => {
const header = index < 5 ? "" : this.formatHeader(key);
return new Section(header, `<div class="${key}">${inputData[key]}</div>`);
});
}
formatHeader(key) {
return key
.replace(/([A-Z])/g, " $1")
.replace(/^./, (str) => str.toUpperCase());
}
}
class User {
constructor(name) {
this.isNewUser = true;
this.resumes = [];
this.name = name;
this.isViewer = false;
console.log("User created:", this);
}
changeName(newName) {
this.name = newName;
console.log("User name changed:", this.name);
}
getName() {
return this.name;
}
}
class StoredLog {
constructor() {
this.Users = [];
this.resumeIds = [];
this.resumes = [];
console.log("StoredLog created:", this);
}
updateLog(user, resume) {
if (!this.Users.some((u) => u.name === user.name)) {
this.Users.push(user);
}
this.resumes.push(resume);
this.resumeIds.push(resume.resumeId);
console.log("StoredLog updated:", this);
}
}
class Resume {
constructor(owner, sections) {
this.resumeName;
this.resumeId = this.resumeIDGenerator();
this.pdf = null;
this.comments = [];
this.allowedViewers = [];
this.sections = sections;
this.owner = owner;
console.log("Resume created:", this);
}
resumeIDGenerator() {
return Math.floor(10000 + Math.random() * 90000);
}
}
class Section {
constructor(header, content) {
this.header = header;
this.content = content;
console.log("Section created:", this);
}
generateHTML() {
return this.header
? // <h4>${this.header}</h4> <hr> <p>${this.content}</p>
`<h4>${this.header}</h4> <p>${this.content}</p>`
: `<div>${this.content}</div>`;
}
}
class Comment {
constructor(user, text) {
this.User = user;
this.text = text;
this.textColor = "black";
console.log("Comment created:", this);
}
}
/**
* =========================== INITIAL SETUP ===============================
* =========================================================================
*/
const dbTemp = new StoredLog();
document.addEventListener("DOMContentLoaded", () => {
document.getElementById("nextButton").addEventListener("click", () => {
const inputLog = new InputLog();
inputLog.updateQueryInputLog();
const newUser = new User(inputLog.queryInputLog[0].nameInput);
const newResume = new Resume(newUser, inputLog.generateSections());
dbTemp.updateLog(newUser, newResume);
console.log("New user and resume created:", newUser, newResume);
});
/**
* =========================== MAIN BUTTONS ==============================
* =========================================================================
*/
// main --> generate resume
function generateResumePreview(resumeSections, resumeComments) {
const previewArea = document.getElementById("resumeContent");
const commentArea = document.getElementById("commentDisplayArea");
previewArea.innerHTML = "";
commentArea.innerHTML = "";
resumeSections.forEach((section) => {
previewArea.innerHTML += section.generateHTML();
});
if (resumeComments.length > 0) {
const commentsHTML = resumeComments
.map(
(comment) =>
`<p><strong>${comment.User.getName()}:</strong> ${comment.text}</p>`
)
.join("");
commentArea.innerHTML = `<div class="section"><h4>Comments:</h4>${commentsHTML}</div> <span class="close" onclick="closeCommentDisplay()">×</span>
`;
commentArea.style.display = "block";
} else {
commentArea.style.display = "none";
}
}
// main --> view comments
document
.getElementById("viewCommentsButton")
.addEventListener("click", () => {
if (dbTemp.resumes.length > 0) {
const firstResume = dbTemp.resumes[0];
const commentText =
document.getElementById("comments").value ||
"Please let me know how I can make my resume better";
const newComment = new Comment(
new User(firstResume.owner.name),
commentText
);
firstResume.comments.push(newComment);
generateResumePreview(firstResume.sections, firstResume.comments);
console.log("Added comment to resume:", newComment);
} else {
alert("Need to save a resume first!");
}
});
// sub --> generate --> allowed viewers pg
document.getElementById("subGenerateButton").addEventListener("click", () => {
if (dbTemp.resumes.length > 0) {
const latestResume = dbTemp.resumes[dbTemp.resumes.length - 1];
const currentUser = new User(latestResume.owner.name);
dbTemp.Users.push(currentUser);
dbTemp.resumeIds.push(latestResume.resumeId);
dbTemp.resumes.push(latestResume);
// get allowed viewers from input field and add them to resume
const allowedViewersInput = document.getElementById(
"allowedViewersInput"
).value;
const allowedViewers = allowedViewersInput
.split(",")
.map((viewer) => viewer.trim());
latestResume.allowedViewers.push(...allowedViewers);
generateResumePreview(latestResume.sections, latestResume.comments);
document.getElementById("resumeDisplayArea").style.display = "block";
console.log("Generated resume:", latestResume);
} else {
// if no resume saved, then pop up saying save resume
// first when trying to generate a resume
alert("Need to save a resume first!");
document.getElementById("resumeContent").textContent =
"No resumes found.";
document.getElementById("resumeDisplayArea").style.display = "none";
}
});
// sub --> generate --> within resume display --> add comments
document
.getElementById("addCommentToResumeButton")
.addEventListener("click", () => {
if (dbTemp.resumes.length > 0) {
const firstResume = dbTemp.resumes[0];
const commentText = prompt("Enter your comment:");
if (commentText) {
const newComment = new Comment(
new User(firstResume.owner.name),
commentText
);
firstResume.comments.push(newComment);
generateResumePreview(firstResume.sections, firstResume.comments);
console.log("Added comment to resume:", newComment);
}
}
});
// sub --> generate --> within resume display --> edit sections
document
.getElementById("editSectionsButton")
.addEventListener("click", () => {
if (dbTemp.resumes.length > 0) {
const firstResume = dbTemp.resumes[0];
openEditModal(firstResume.sections, firstResume.resumeId);
}
});
// sub --> generate --> within resume display --> download PDF
const downloadButton = document.getElementById("downloadPdfButton");
if (downloadButton) {
downloadButton.addEventListener("click", downloadPDF);
} else {
console.error("Download PDF button not found.");
}
});
/**
* =========================== EDIT SECTION ===============================
* =========================================================================
*/
// generate --> resume display --> edit
let currentEditingResumeId = null;
function openEditModal(resumeSections, resumeId) {
currentEditingResumeId = resumeId;
const modal = document.getElementById("editModal");
const modalContent = document.getElementById("modalContent");
modalContent.innerHTML = "";
resumeSections.forEach((section, index) => {
const editableContent = extractTextContent(section.content);
const sectionDiv = document.createElement("div");
sectionDiv.classList.add("modal-section");
sectionDiv.innerHTML =
index < 5
? `<div class="non-editable-content">${editableContent}</div>`
: `<label>${section.header}</label><textarea id="edit-${section.header}" rows="4" cols="50">${editableContent}</textarea>`;
modalContent.appendChild(sectionDiv);
});
modal.style.display = "block";
attachModalEventHandlers();
}
function extractTextContent(htmlString) {
const tempDiv = document.createElement("div");
tempDiv.innerHTML = htmlString;
return tempDiv.textContent || tempDiv.innerText || "";
}
function attachModalEventHandlers() {
const saveButton = document.getElementById("saveButton");
if (saveButton) {
saveButton.removeEventListener("click", saveModalChanges);
saveButton.addEventListener("click", saveModalChanges);
}
}
// SAVE CHANGES ==============================================================
function saveModalChanges() {
const editingResume = dbTemp.resumes.find(
(resume) => resume.resumeId === currentEditingResumeId
);
if (editingResume) {
editingResume.sections.forEach((section, index) => {
if (index >= 5) {
const editedContentElement = document.getElementById(
`edit-${section.header}`
);
if (editedContentElement) {
const className = section.content.match(/class="([^"]+)"/)[1];
section.content = `<div class="${className}">${editedContentElement.value}</div>`;
}
}
});
generateResumePreview(editingResume.sections, editingResume.comments);
}
document.getElementById("editModal").style.display = "none";
currentEditingResumeId = null;
}
// CLOSING ==========================================================
function closeResumeDisplay() {
document.getElementById("resumeDisplayArea").style.display = "none";
}
function closeEditDisplay() {
document.getElementById("editModal").style.display = "none";
}
function closeCommentDisplay() {
document.getElementById("commentDisplayArea").style.display = "none";
}
/**
* ============================= ADD ROASTER ===============================
* =========================================================================
*/
// generate --> resume display --> add roaster
// Event listener for opening Add Roaster modal
document.addEventListener("DOMContentLoaded", () => {
document.getElementById("addRoasterButton").addEventListener("click", () => {
document.getElementById("addRoasterModal").style.display = "block";
});
// Event listener for closing Add Roaster modal
document
.querySelector("#addRoasterModal .close")
.addEventListener("click", function () {
document.getElementById("addRoasterModal").style.display = "none";
});
// Event listener for saving roaster
document
.getElementById("saveRoasterButton")
.addEventListener("click", function () {
var roasterName = document.getElementById("roasterNameInput").value;
if (roasterName) {
// Assuming you want to add roaster to first resume
if (dbTemp.resumes.length > 0) {
const firstResume = dbTemp.resumes[0];
firstResume.allowedViewers.push(roasterName);
console.log(`Added roaster: ${roasterName}`);
// Update resume preview to display new roaster
generateResumePreview(firstResume.sections, firstResume.comments);
}
}
// Clear input field and close modal
document.getElementById("roasterNameInput").value = "";
document.getElementById("addRoasterModal").style.display = "none";
});
});
/**
* =========================== PDF GENERATING ==============================
* =========================================================================
*/
function downloadPDF() {
const jsPDF = window.jspdf.jsPDF;
const resumeDisplayArea = document.getElementById("resumeDisplayArea");
resumeDisplayArea.style.display = "block";
html2canvas(resumeDisplayArea, {
onclone: (clonedDoc) => {
clonedDoc
.querySelectorAll(
"#downloadPdfButton, #addCommentToResumeButton, #addRoasterButton, #editSectionsButton, #closeResumeButton"
)
.forEach((elem) => (elem.style.display = "none"));
},
scale: window.devicePixelRatio,
windowWidth: resumeDisplayArea.scrollWidth,
windowHeight: resumeDisplayArea.scrollHeight,
})
.then((canvas) => {
const imgData = canvas.toDataURL("image/png");
const pdf = new jsPDF({
orientation: "portrait",
unit: "in",
format: "letter",
});
// calculate scaling to fit canvas image within 8.5 x 11 inches
const imgWidth = 8.5;
const imgHeight = (canvas.height * imgWidth) / canvas.width;
pdf.addImage(imgData, "PNG", 0, 0, imgWidth, imgHeight);
pdf.save("resume.pdf");
resumeDisplayArea.style.display = "none";
})
.catch((error) => {
console.error("Error generating PDF: ", error);
});
}