Giter Club home page Giter Club logo

Comments (5)

simonw avatar simonw commented on June 11, 2024
from datasette import hookimpl

JS = """
document.addEventListener('datasette_init', function(ev) {
  ev.detail.registerPlugin('column-name-plugin', {
    version: 0.1,
    makeColumnActions: (columnDetails) => {
      console.log({columnDetails});
      const numericColumns = Array.from(
        document.querySelectorAll('[data-column-type="float"],[data-column-type="integer"]')
      ).filter(el => el.dataset.isPk != "1").map((el) => el.dataset.column);
      console.log({numericColumns});
      if (columnDetails.columnType == 'float' || columnDetails.columnType == 'integer') {
        const sql = `select sum(${columnDetails.columnName}) from {table}`;
        return [
          {
            label: 'Sum column',
            onClick: () => {
              location.href = '/{database}?sql=' + encodeURIComponent(sql);
            }
          }
        ];
      }
      if (columnDetails.columnType == 'text' && numericColumns.length) {
        // Add options for every other numeric column
        return numericColumns.map((name) => ({
          label: 'Sum by this: ' + name,
          onClick: () => {
            const sql = `select ${columnDetails.columnName}, sum(${name}) from {table} group by ${columnDetails.columnName}`;
            location.href = '/{database}?sql=' + encodeURIComponent(sql);
          }
        }));
      }
      return [];
    }
  });
});
"""

@hookimpl
def extra_body_script(database, table):
    if database and table:
        return {
            "script": JS.replace("{database}", database).replace("{table}", table)
        }

the menu seems to be reused, so if you click a text column first it shows up incorrectly on the following clicks to float columns.

cog-bug

from datasette.

simonw avatar simonw commented on June 11, 2024

Bug is in here somewhere:

function onTableHeaderClick(ev) {
ev.preventDefault();
ev.stopPropagation();
var th = ev.target;
while (th.nodeName != "TH") {
th = th.parentNode;
}
var rect = th.getBoundingClientRect();
var menuTop = rect.bottom + window.scrollY;
var menuLeft = rect.left + window.scrollX;
var column = th.getAttribute("data-column");
var params = getParams();
var sort = menu.querySelector("a.dropdown-sort-asc");
var sortDesc = menu.querySelector("a.dropdown-sort-desc");
var facetItem = menu.querySelector("a.dropdown-facet");
var notBlank = menu.querySelector("a.dropdown-not-blank");
var hideColumn = menu.querySelector("a.dropdown-hide-column");
var showAllColumns = menu.querySelector("a.dropdown-show-all-columns");
if (params.get("_sort") == column) {
sort.parentNode.style.display = "none";
} else {
sort.parentNode.style.display = "block";
sort.setAttribute("href", sortAscUrl(column));
}
if (params.get("_sort_desc") == column) {
sortDesc.parentNode.style.display = "none";
} else {
sortDesc.parentNode.style.display = "block";
sortDesc.setAttribute("href", sortDescUrl(column));
}
/* Show hide columns options */
if (params.get("_nocol") || params.get("_col")) {
showAllColumns.parentNode.style.display = "block";
showAllColumns.setAttribute("href", showAllColumnsUrl());
} else {
showAllColumns.parentNode.style.display = "none";
}
if (th.getAttribute("data-is-pk") != "1") {
hideColumn.parentNode.style.display = "block";
hideColumn.setAttribute("href", hideColumnUrl(column));
} else {
hideColumn.parentNode.style.display = "none";
}
/* Only show "Facet by this" if it's not the first column, not selected,
not a single PK and the Datasette allow_facet setting is True */
var displayedFacets = Array.from(
document.querySelectorAll(".facet-info")
).map((el) => el.dataset.column);
var isFirstColumn =
th.parentElement.querySelector("th:first-of-type") == th;
var isSinglePk =
th.getAttribute("data-is-pk") == "1" &&
document.querySelectorAll('th[data-is-pk="1"]').length == 1;
if (
!DATASETTE_ALLOW_FACET ||
isFirstColumn ||
displayedFacets.includes(column) ||
isSinglePk
) {
facetItem.parentNode.style.display = "none";
} else {
facetItem.parentNode.style.display = "block";
facetItem.setAttribute("href", facetUrl(column));
}
/* Show notBlank option if not selected AND at least one visible blank value */
var tdsForThisColumn = Array.from(
th.closest("table").querySelectorAll("td." + th.className)
);
if (
params.get(`${column}__notblank`) != "1" &&
tdsForThisColumn.filter((el) => el.innerText.trim() == "").length
) {
notBlank.parentNode.style.display = "block";
notBlank.setAttribute("href", notBlankUrl(column));
} else {
notBlank.parentNode.style.display = "none";
}
var columnTypeP = menu.querySelector(".dropdown-column-type");
var columnType = th.dataset.columnType;
var notNull = th.dataset.columnNotNull == 1 ? " NOT NULL" : "";
if (columnType) {
columnTypeP.style.display = "block";
columnTypeP.innerText = `Type: ${columnType.toUpperCase()}${notNull}`;
} else {
columnTypeP.style.display = "none";
}
var columnDescriptionP = menu.querySelector(".dropdown-column-description");
if (th.dataset.columnDescription) {
columnDescriptionP.innerText = th.dataset.columnDescription;
columnDescriptionP.style.display = "block";
} else {
columnDescriptionP.style.display = "none";
}
menu.style.position = "absolute";
menu.style.top = menuTop + 6 + "px";
menu.style.left = menuLeft + "px";
menu.style.display = "block";
menu.classList.add("anim-scale-in");
// Custom menu items on each render
// Plugin hook: allow adding JS-based additional menu items
const columnActionsPayload = {
columnName: th.dataset.column,
columnNotNull: th.dataset.columnNotNull === '1',
columnType: th.dataset.columnType,
isPk: th.dataset.isPk === '1'
};
const columnItemConfigs = manager.makeColumnActions(columnActionsPayload);
const menuList = menu.querySelector('ul');
columnItemConfigs.forEach(itemConfig => {
// Remove items from previous render. We assume entries have unique labels.
const existingItems = menuList.querySelectorAll(`li`);
Array.from(existingItems).filter(item => item.innerText === itemConfig.label).forEach(node => {
node.remove();
});
const newLink = document.createElement('a');
newLink.textContent = itemConfig.label;
newLink.href = itemConfig.href ?? '#';
if (itemConfig.onClick) {
newLink.onclick = itemConfig.onClick;
}
// Attach new elements to DOM
const menuItem = document.createElement('li');
menuItem.appendChild(newLink);
menuList.appendChild(menuItem);
});
// Measure width of menu and adjust position if too far right
const menuWidth = menu.offsetWidth;
const windowWidth = window.innerWidth;
if (menuLeft + menuWidth > windowWidth) {
menu.style.left = windowWidth - menuWidth - 20 + "px";
}
// Align menu .hook arrow with the column cog icon
const hook = menu.querySelector('.hook');
const icon = th.querySelector('.dropdown-menu-icon');
const iconRect = icon.getBoundingClientRect();
const hookLeft = (iconRect.left - menuLeft + 1) + 'px';
hook.style.left = hookLeft;
// Move the whole menu right if the hook is too far right
const menuRect = menu.getBoundingClientRect();
if (iconRect.right > menuRect.right) {
menu.style.left = (iconRect.right - menuWidth) + 'px';
// And move hook tip as well
hook.style.left = (menuWidth - 13) + 'px';
}
}

from datasette.

simonw avatar simonw commented on June 11, 2024

Here is the code that creates that menu only once:

var menu = document.createElement("div");
menu.innerHTML = DROPDOWN_HTML;
menu = menu.querySelector("*");
menu.style.position = "absolute";
menu.style.display = "none";
document.body.appendChild(menu);

It should be created on every click to open the menu.

from datasette.

simonw avatar simonw commented on June 11, 2024

I just reset it with menu.innerHTML = DROPDOWN_HTML.

from datasette.

simonw avatar simonw commented on June 11, 2024

Plugin is now released that uses this fix: https://pypi.org/project/datasette-column-sum/

from datasette.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.