Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In Django SQL dashboard, hyperlink BBLs to WoW #2107

Open
toolness opened this issue May 20, 2021 · 0 comments
Open

In Django SQL dashboard, hyperlink BBLs to WoW #2107

toolness opened this issue May 20, 2021 · 0 comments
Labels
code snippet Temporary code that we only need to run once and don't need to merge for long-term use.

Comments

@toolness
Copy link
Collaborator

toolness commented May 20, 2021

The following snippet of code, if added to a django-sql-dashboard view, will do the trick whenever it sees a pad_bbl column:

/**
 * @param element {Element}
 * @returns {number|null}
 */
function findIndexInParent(element) {
  const parent = element.parentNode;

  if (parent) {
    for (let i = 0; i < parent.children.length; i++) {
      const child = parent.children[i];
      if (child === element) return i;
    }
  }

  return null;
}

/**
 * @param element {Element}
 * @returns {HTMLTableElement|null}
 */
function getTableAncestor(element) {
  let parent = element.parentNode;

  while (parent) {
    if (parent instanceof HTMLTableElement) {
      return parent;
    }
    parent = parent.parentNode;
  }

  return null;
}

/**
 * @param name {String}
 */
function* iterColumnValues(name) {
  const headers = document.querySelectorAll(`th[alt="${name}"]`);

  for (let header of headers) {
    const table = getTableAncestor(header);
    const index = findIndexInParent(header);
    if (index !== null && table) {
      const cells = table.querySelectorAll(`td:nth-child(${index + 1})`);
      for (let cell of cells) {
        yield cell;
      }
    }
  }
}

window.addEventListener("load", () => {
  for (let td of iterColumnValues("pad_bbl")) {
    const a = document.createElement("a");
    const bbl = td.textContent;
    // TODO: Replace origin w/ settings.WOW_ORIGIN.
    a.href = `http://whoownswhat.justfix.nyc/en/bbl/${bbl}`;
    a.target = "_blank";
    a.rel = "noopener noreferrer";
    a.textContent = td.textContent;
    td.textContent = "";
    td.appendChild(a);
  }
});

Note that we could take this same concept and extend it to other types of fields. For instance, we could make it so that any column called user_id was automatically hyperlinked to the Django admin user change view.

@toolness toolness added the code snippet Temporary code that we only need to run once and don't need to merge for long-term use. label May 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code snippet Temporary code that we only need to run once and don't need to merge for long-term use.
Projects
None yet
Development

No branches or pull requests

1 participant