-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhr-script.js
325 lines (287 loc) · 8.83 KB
/
hr-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
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
/*
* Sugar
*/
const getElId = document.getElementById.bind(document);
const c = console.log.bind(window);
/*
* Util
*/
class Util {
// trims and converts a string to dash separated a-z 0-9
static dasherize(str) {
let val = str.toLowerCase();
val = val.replace(/[^a-z0-9 ]/gi, ''); // strip non-alphanumeric
val = val.replace(/\s\s+/g, ' ').trim(); // compress spaces and trim
val = val.replace(/\s+/g, '-'); // replace spaces with dash
return val;
}
// overloaded, returns a time or logs a timing
static timing(...args) {
const time = new Date().getTime();
if (args.length > 0) {
const start = args[0]; // first optional arg is a Date
const message = args[1]; // second optional arg is a message
const end = time;
c(message, 'start:', start, 'end:', end, (end - start), 'ms');
return;
}
return time;
}
// dom element from template string
static makeEl(templateString, mimeType = "text/html") {
return (new DOMParser)
.parseFromString(templateString, mimeType)
.body
.firstElementChild;
}
// log and return, useful for debugging
static logRet(message, obj) {
c(message, obj);
return null;
}
// uniquify array of hashes by keyname
static uniqueVals(arr, key) {
const vals = arr.map(a => a[key]);
return [...new Set(vals)];
}
}
/*
* Employee
* - constructor takes dasherized name slug
*/
class Employee {
constructor(pid) {
this.record = EMPLOYEES.find(emp => emp.pid === pid)
if (!this.record) return null
const r = this.record
this.pid = r.pid
this.mid = r.mid
this.slug = r.slug
this.displayName = r.displayName
this.jobTitle = r.jobTitle
this.unitSlug = r.unitSlug
this.unitDisplayName = r.unitDisplayName
this.city = r.city
this.state = r.state
this.email = r.email
this.reports = r.reports
}
getManagers = () => {
if (!this.mid.trim()) {
return Util.logRet('getManagers: no managers', this.record);
}
const managers = EMPLOYEES.filter((emp) => {
if (emp.pid.trim() || this.mid.trim() !== '') {
return emp.pid === this.mid;
}
});
let result = [];
managers.forEach((mgr) => {
result.push(new Employee(mgr.pid));
});
return result;
}
getStaffs = () => {
const staffs = EMPLOYEES.filter(emp => this.pid === emp.mid)
let result = [];
staffs.forEach((staff) => {
result.push(new Employee(staff.pid));
});
return result;
}
// renders EMPLOYEE node recursively
renderNodeTree = (node = this, childEl) => {
let nodeEl = this.makeNodeEl(node);
if (node === this) {
nodeEl = this.makeNodeEl(node, "node employee-node");
getElId('console-body').append(nodeEl);
}
else childEl.append(nodeEl);
if (node.reports.length === 0) return;
node.reports.forEach((report) => {
const reportEl = this.makeNodeEl(report);
if (node.pid === report.pid) reportEl.append(nodeEl);
this.renderNodeTree(report, nodeEl);
})
}
makeNodeEl = (node, cssClass = "node") => {
const markup = `
<div class="${cssClass}" id="${'tree-' + node.pid}">
<a href="hr-tree.html?p=${node.pid}">
<div class="inner">
<span class="name">${node.displayName}</span>
<small>${node.jobTitle}</small>
</div>
</a>
</div>
`
return Util.makeEl(markup);
}
}
/*
* Page
* - initialize
* - render managers
* - render employee
* - render staffs
*/
// expose global "constants" ¯\_(ツ)_/¯
let EMPLOYEES;
let EMPLOYEE;
const START = Util.timing();
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const p = urlParams.get('p'); // slug
const Page = ({
init: function(employees) {
EMPLOYEES = this.constructEmployees(employees);
EMPLOYEES = this.addReports(EMPLOYEES);
Util.timing(START, 'add reports');
// person query string check
if (!p) return this.coldStart('No person');
// employee validation check
EMPLOYEE = new Employee(p);
if (!EMPLOYEE.record) return this.coldStart('Employee not found');
const managers = EMPLOYEE.getManagers();
const staffs = EMPLOYEE.getStaffs();
this.renderManagers(managers);
this.renderEmployee();
this.renderStaffs(staffs);
this.instrumentPage();
Util.timing(START, 'finish page init');
},
constructEmployees: function(employees) {
const restructure = (emp) => {
// dasherize keys
const pid = Util.dasherize(emp["Position ID"]);
const mid = Util.dasherize(emp["Reports To Position ID"]);
const slug = Util.dasherize(emp["HR First Name Lower Case"]
+ ' '
+ emp["HR Last Name Lower Case"]);
const unitSlug = Util.dasherize(emp["HR Company Code Division"]);
const fname = emp["Preferred First Name"]
? emp["Preferred First Name"]
: emp["HR First Name Lower Case"];
const lname = emp["Preferred Last Name"]
? emp["Preferred Last Name"]
: emp["HR Last Name Lower Case"];
const displayName = fname + ' ' + lname;
const reports = []
// destructure to js conforming key names
const {
"HR First Name Lower Case": firstName,
"HR Last Name Lower Case": lastName,
"Job Title Description": jobTitle,
"HR Company Code Division": unitDisplayName,
"Work Contact: Work Email": email,
...zzz
} = emp
// construct result
return {
pid,
mid,
slug,
firstName,
lastName,
displayName,
jobTitle,
unitSlug,
unitDisplayName,
email,
reports
}
}
return employees.map(restructure);
},
// invert "Reports To Position ID"
addReports: function(employees) {
let emps = employees;
emps.forEach((emp) => {
if (emp.mid === "") return;
emps.forEach((mgr) => {
if (emp.mid == mgr.pid) { mgr.reports.push(emp) }
})
})
return emps;
},
renderManagers: function(managers) {
if (!managers) return;
managers.forEach((manager) => {
let markup = '';
markup += this.renderEmployeeCard(manager);
getElId('managers-block').classList.remove('hide');
getElId('managers-container').insertAdjacentHTML('beforeend', markup);
});
//c('renderManagers: done', managers);
},
renderEmployee: function() {
const emailMarkup = `
<a href="mailto:${EMPLOYEE.email}">${EMPLOYEE.email}</a>
`
const markup = `
<div id="poi-block" class="block flex">
<div class="poi-card">
<h1 id="poi">${EMPLOYEE.displayName}</h1>
<div id="console-toggle" class="console-toggle">
<small class="tooltip">View org tree for ${EMPLOYEE.displayName}</small>
</div>
<p id="poi-job-title">${EMPLOYEE.jobTitle}</p>
<small>${emailMarkup}</small>
</div>
</div>
`
getElId('employee-container').insertAdjacentHTML('beforeend', markup);
//c('renderEmployee: done', EMPLOYEE);
},
renderStaffs: function(staffs) {
if (!staffs) return;
staffs.forEach((staff) => {
markup = this.renderEmployeeCard(staff);
getElId('reports-block').classList.remove('hide');
getElId('reports-container').insertAdjacentHTML('beforeend', markup);
});
},
renderEmployeeCard: function(employee) {
return markup = `
<div id="${employee.pid}">
<a href="hr-tree.html?p=${employee.pid}" class="card">
<div class="name">${employee.displayName}</div>
<small>${employee.jobTitle}</small>
</a>
</div>
`
},
coldStart: function(...stuff) {
c(stuff);
getElId('html').classList.add('cold-start-mode');
//const random = Math.floor(Math.random() * EMPLOYEES.length);
//const emp = EMPLOYEES[random];
//const a = getElId('random-employee-link')
},
instrumentPage: function() {
// click logo to go home
const homeLogo = getElId("logo");
homeLogo.addEventListener("click", () => {
window.location.replace("hr-tree.html");
})
// don't instrument anything below if no employee
if (!EMPLOYEE) return;
// pre-render node tree
EMPLOYEE.renderNodeTree()
// toggle node tree console
const htmlEl = getElId("html");
const consoleToggle = getElId("console-toggle");
const consoleClose = getElId("console-close");
const modalCurtain = getElId("modal-curtain");
[consoleToggle, consoleClose, modalCurtain].forEach(item => {
item.addEventListener("click", () => {
htmlEl.classList.toggle("console-hide");
})
})
// add console title
getElId("console-title").prepend(Util.makeEl(`
<h2>Org Tree for ${EMPLOYEE.displayName}</h2>
`
))
}
})