This repository was archived by the owner on Sep 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg-chart.js
181 lines (162 loc) · 6.05 KB
/
org-chart.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
/** Location of SOAR's Position Database on Google Sheets. */
const POSITION_DB_URL = 'https://docs.google.com/spreadsheets/d/16K7XM1Q1_Ps4sU1KsbU2xdkuY-7JjMlh9wt5wEAcPS4/gviz/tq?gid=Positions&headers=1';
/** Indexes of columns in the positions sheet. */
const COL_INDICES = {
TEAM: 0,
POSITION: 1,
UNIQUE_NAME: 2,
HELD_BY: 3,
RESPORTS_TO: 4,
ACCEPTING_APPLICATIONS: 5,
POSITION_DESCRIPTION: 6,
COMMENTS: 7,
};
/** HTML ID of the org chart container. */
const CONTAINER_ID = 'orgChartContainer';
/** URL to direct prospective applicants to. */
const ACCEPTING_APPLICATIONS_URL = 'http://usfsoar.com/positions';
google.charts.load('current', {packages: ['orgchart']});
google.charts.setOnLoadCallback(loadData);
/** Load the positions data from Google Sheets. */
function loadData() {
let query = new google.visualization.Query(POSITION_DB_URL);
query.send(handleDataResponse);
}
/**
* Process and filter the response data.
* @param {google.visualization.QueryResponse} response The response object.
*/
function handleDataResponse(response) {
let data = response.getDataTable();
let newColumnIndex = data.getNumberOfColumns();
data.addColumn('string', 'OrgChartNodeContent');
for (let row = 0; row < data.getNumberOfRows(); row++) {
let positionName = data.getValue(row, COL_INDICES.POSITION);
let positionTeam = data.getValue(row, COL_INDICES.TEAM);
let positionHeldBy = data.getValue(row, COL_INDICES.HELD_BY);
let positionOpen =
data.getValue(row, COL_INDICES.ACCEPTING_APPLICATIONS) === 'Yes';
let formatted =
`<div class="orgChart-node-flex">
<span class="orgChart-node-team">${positionTeam}</span>
<span class="orgChart-node-position">${positionName}</span>
${positionHeldBy
? `<span class="orgChart-node-heldBy">${positionHeldBy}</span>`
: ''}
${positionOpen
? `<span class="orgChart-node-open">
<a href="${ACCEPTING_APPLICATIONS_URL}">Open - Apply Now!</a>
</span>`
: ''}
${(!positionHeldBy && !positionOpen)
? `<span class="orgChart-node-spaceFiller"> </span>`
: ``}
</div>`;
data.setCell(
row,
newColumnIndex,
data.getValue(row, COL_INDICES.UNIQUE_NAME), formatted);
}
let unfilteredDataView = new google.visualization.DataView(data);
unfilteredDataView.setColumns([
newColumnIndex,
COL_INDICES.RESPORTS_TO,
COL_INDICES.HELD_BY,
COL_INDICES.TEAM, // For filtering; ignored by org chart
]);
initOrgChart(unfilteredDataView);
}
/**
* Create the org chart object and then draw the chart and initialize the filter
* buttons.
* @param {google.visualization.DataView} unfilteredData
*/
function initOrgChart(unfilteredData) {
const container = document.getElementById(CONTAINER_ID);
const orgChart = new google.visualization.OrgChart(container);
drawOrgChart(orgChart, unfilteredData);
applyLinkedFilterIfPresent(orgChart, unfilteredData);
initFilterButtons(orgChart, unfilteredData);
}
/**
* Draw the chart using the data from the filter.
* @param {google.visualization.OrgChart} chart The chart object.
* @param {google.visualization.DataView} data
*/
function drawOrgChart(chart, data) {
chart.draw(data, {
allowCollapse: true,
allowHtml: true,
nodeClass: 'orgChart-node',
selectedNodeClass: 'orgChart-node--selected',
});
}
/**
* Detect if the url contains a filter, and if so, apply that filter. Allows for
* linking directly to filtered org charts.
* @param {google.visualization.OrgChart} chart The chart object.
* @param {google.visualization.DataView} unfilteredData The chart's
* unfiltered data view.
*/
function applyLinkedFilterIfPresent(chart, unfilteredData) {
let hash = window.location.hash;
if (hash) {
let filter = hash.substr(1).replace('+', ' '); // Remove '#', replace spaces
console.log(filter);
/** All allowed filter strings, pulled from the filter buttons. */
let filters = [...document.querySelectorAll('.filterButton')]
.map(button => button.dataset.filter);
if (filters.indexOf(filter) !== -1) {
// Is a valid filter string
filterChartData(chart, unfilteredData, filter);
clearFilterButtonsActiveClass();
document.querySelectorAll(`.filterButton[data-filter='${filter}']`)
.forEach(button => button.classList.add('filterButton--active'));
}
}
}
/**
* Filter the data by team name and redraw chart.
* @param {google.visualization.OrgChart} chart The chart object.
* @param {google.visualization.DataView} unfilteredData The chart's
* unfiltered data view.
* @param {?string} [teamName] The exact name of the team to show, or a falsy
* value to show all positions.
*/
function filterChartData(chart, unfilteredData, teamName) {
let filteredDataView = new google.visualization.DataView(unfilteredData);
if (teamName) {
let matchingRows = filteredDataView.getFilteredRows([{
column: 3,
value: teamName,
}]);
filteredDataView.setRows(matchingRows);
}
drawOrgChart(chart, filteredDataView);
}
/**
* Initalize the filter buttons by binding event listeners.
* @param {google.visualization.OrgChart} chart The chart object.
* @param {google.visualization.DataView} unfilteredData The chart's unfiltered
* data view.
*/
function initFilterButtons(chart, unfilteredData) {
let filterButtons = document.querySelectorAll('.filterButton');
filterButtons.forEach(filterButton => {
filterButton.addEventListener('click', event => {
let filter = event.target.dataset.filter;
filterChartData(chart, unfilteredData, filter);
clearFilterButtonsActiveClass();
filterButton.classList.add('filterButton--active');
window.location.hash = filter.replace(' ', '+'); // For linking
});
});
}
/**
* Remove the `--active` modifier from all filter buttons.
*/
function clearFilterButtonsActiveClass() {
let filterButtons = document.querySelectorAll('.filterButton');
filterButtons.forEach(
filterButton => filterButton.classList.remove('filterButton--active'));
}