From 62d926e022aaf35be029f20568e55533caee3bf7 Mon Sep 17 00:00:00 2001 From: Ian C Date: Sat, 7 Jul 2018 13:26:02 +0000 Subject: Added up/down links to reorder entries. --- www/scripts/main.js | 123 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 113 insertions(+), 10 deletions(-) (limited to 'www/scripts') diff --git a/www/scripts/main.js b/www/scripts/main.js index 0ed2cf5..79fb0b7 100644 --- a/www/scripts/main.js +++ b/www/scripts/main.js @@ -170,6 +170,21 @@ function AddSmallLink(node, text, func, id) node.appendChild(anchor); } +function AddSmallLinkPrevNext(node, text, func, id, prev, next) +{ + var anchor = document.createElement("a"); + + anchor.href = "#"; + anchor.onclick = func; + anchor.text = text; + anchor.className = "smalltext"; + anchor.dataset.pmId = id; + anchor.dataset.pmPrevId = prev; + anchor.dataset.pmNextId = next; + + node.appendChild(anchor); +} + function AddSmallLinkUpDown(node, text, down_func, up_func, id) { var anchor = document.createElement("a"); @@ -197,7 +212,7 @@ function AddElement(node, elementname) node.appendChild(elem); } -function AddRow(table, id, desc, username, password, rowcount) +function AddRow(table, id, prev_id, next_id, desc, username, password, rowcount) { var tr = document.createElement("tr"); @@ -232,7 +247,26 @@ function AddRow(table, id, desc, username, password, rowcount) AddText(td, "\u00a0"); AddText(td, "\u00a0"); AddSmallLinkUpDown(td, "Show", DoShow, DoHide, id); + + if (next_id != -1 || prev_id != -1) + { + if (prev_id != -1) + { + AddText(td, "\u00a0"); + AddText(td, "\u00a0"); + AddSmallLinkPrevNext(td, "Up", DoUp, id, prev_id, next_id); + } + + if (next_id != -1) + { + AddText(td, "\u00a0"); + AddText(td, "\u00a0"); + AddSmallLinkPrevNext(td, "Down", DoDown, id, prev_id, next_id); + } + } + AddElement(td, "br"); + AddSmallLink(td, "Copy Username", DoCopyUsername, id); AddText(td, "\u00a0"); AddText(td, "\u00a0"); @@ -244,6 +278,21 @@ function AddRow(table, id, desc, username, password, rowcount) table.appendChild(tr); } +function GetGroupRows() +{ + var result = []; + + for(var f = 0; f < globalDb.length; f++) + { + if (globalDb[f].group == globalCurrentGroup) + { + result.push(globalDb[f]); + } + } + + return result; +} + function LoadTable() { var table = document.getElementById("DataTable"); @@ -252,20 +301,34 @@ function LoadTable() var rowcount = 0; - for(var f = 0; f < globalDb.length; f++) + var rows = GetGroupRows(); + + for(var f = 0; f < rows.length; f++) { - if (globalDb[f].group == globalCurrentGroup) + var prevId = -1; + var nextId = -1; + + if (f > 0) { - AddRow(table, - globalDb[f].id, - globalDb[f].description, - globalDb[f].username, - Decrypt(globalDb[f].password), - rowcount++); + prevId = rows[f-1].id; } + + if (f < rows.length - 1) + { + nextId = rows[f+1].id; + } + + AddRow(table, + rows[f].id, + prevId, + nextId, + rows[f].description, + rows[f].username, + Decrypt(rows[f].password), + rowcount++); } - AddRow(table, -1, "", "", "", rowcount++); + AddRow(table, -1, -1, -1, "", "", "", rowcount++); } function RefreshDataAsync() @@ -466,4 +529,44 @@ function DoHide() elem.type = "password"; } +function DoReorderAsync() +{ + var response = JSON.parse(this.responseText); + + if (response.error) + { + Error(response.error); + } + else + { + RefreshData(); + } +} + +function DoUp() +{ + var id = this.dataset.pmId; + var prev = this.dataset.pmPrevId; + + WebRequest("reorder.php", DoReorderAsync, + [ + "from_id", id, + "to_id", prev + ] + ); +} + +function DoDown() +{ + var id = this.dataset.pmId; + var next = this.dataset.pmNextId; + + WebRequest("reorder.php", DoReorderAsync, + [ + "from_id", id, + "to_id", next + ] + ); +} + // vim: sw=4 ts=4 -- cgit v1.2.3