Skip to content

Commit

Permalink
Make tableData dropdown function
Browse files Browse the repository at this point in the history
  • Loading branch information
psvenk committed Apr 30, 2020
1 parent 79785be commit d2f8170
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 26 deletions.
22 changes: 22 additions & 0 deletions public/js/buttonFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ let importTableData = async function(obj) {
tableData[currentTableDataIndex] = {};
currentTableData = tableData[currentTableDataIndex];
currentTableData.imported = true;
currentTableData.name = obj.name;

currentTerm = "";
termConverter.forEach(term => {
Expand Down Expand Up @@ -373,4 +374,25 @@ let importTableData = async function(obj) {
});

currentTerm = firstTerm;

for (let i in tableData) {
if (!$(`#tableData_select option[value='${i}']`)[0]) {
let option = document.createElement("option");
option.value = `${i}`;
option.textContent = tableData[i].name;
$("#tableData_select")[0].insertBefore(
option, $("#tableData_select option[value='import']")[0]
);

let div = document.createElement("div");
div.id = `tableData_select-items-${i}`;
div.textContent = tableData[i].name;
div.addEventListener("click", tableData_option_onclick);
$(".tableData_select-items")[0].insertBefore(
div, $("#tableData_select-items-import")[0]
);
}
}

$(`#tableData_select-items-${currentTableDataIndex}`).click();
};
59 changes: 35 additions & 24 deletions public/js/extraFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,39 @@ let initialize_quarter_dropdown = function() {
}
};

let tableData_option_onclick = function() {
if (this.id === "tableData_select-items-import") {
importModal.style.display = 'inline-block';
return;
}

let index_temp = parseInt(this.id.substring("tableData_select-items-".length))
currentTableDataIndex =
isNaN(index_temp) ? currentTableDataIndex : index_temp;
currentTableData = tableData[currentTableDataIndex];

/* When an item is clicked, update the original select box,
and the selected item: */
let y, i, k, s, h;
s = this.parentNode.parentNode.getElementsByTagName("select")[0];
h = this.parentNode.previousSibling;
for (i = 0; i < s.length; i++) {
if (s.options[i].innerHTML === this.innerHTML) {
s.selectedIndex = i;
h.innerHTML = this.innerHTML;
y = this.parentNode.getElementsByClassName("tableData_same-as-selected");
for (k = 0; k < y.length; k++) {
y[k].removeAttribute("class");
}
this.setAttribute("class", "tableData_same-as-selected");

classesTable.setData(currentTableData.currentTermData.classes);
break;
}
}
h.click();
}

let initialize_tableData_dropdown = function() {
let x, i, j, selElmnt, a, b, c;
/* Look for any elements with the class "pdf_custom-select": */
Expand All @@ -898,31 +931,9 @@ let initialize_tableData_dropdown = function() {
/* For each option in the original select element,
create a new DIV that will act as an option item: */
c = document.createElement("DIV");
c.id = `tableData_select-items-${selElmnt.options[j].value}`;
c.innerHTML = selElmnt.options[j].innerHTML;
c.addEventListener("click", function(e) {
if (this.innerHTML === "Import Data...") {
importModal.style.display = 'inline-block';
return;
}
/* When an item is clicked, update the original select box,
and the selected item: */
let y, i, k, s, h;
s = this.parentNode.parentNode.getElementsByTagName("select")[0];
h = this.parentNode.previousSibling;
for (i = 0; i < s.length; i++) {
if (s.options[i].innerHTML === this.innerHTML) {
s.selectedIndex = i;
h.innerHTML = this.innerHTML;
y = this.parentNode.getElementsByClassName("tableData_same-as-selected");
for (k = 0; k < y.length; k++) {
y[k].removeAttribute("class");
}
this.setAttribute("class", "tableData_same-as-selected");
break;
}
}
h.click();
});
c.addEventListener("click", tableData_option_onclick);
b.appendChild(c);
}
x[i].appendChild(b);
Expand Down
6 changes: 4 additions & 2 deletions public/js/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let exportModal = document.getElementById('export_modal');
let importModal = document.getElementById('import_modal');
let term_dropdown_active = true;
let currentTerm = "current";
let tableData = [{}];
let tableData = [{ name: "Current Year" }];
let currentTableDataIndex = 0;
let currentTableData = tableData[currentTableDataIndex];
let selected_class_i;
Expand Down Expand Up @@ -1018,7 +1018,9 @@ $("#import_button").click(async () => {
const reader = new FileReader();
reader.readAsText(file);
reader.addEventListener("load", async () => {
let response = await importTableData(JSON.parse(reader.result));
let obj = JSON.parse(reader.result);
obj.name = file.name;
let response = await importTableData(obj);
if (response) {
$("#import_error").text(response);
} else {
Expand Down

0 comments on commit d2f8170

Please sign in to comment.