From 44915bd7cd8118998899bcdac4086467deb82a1b Mon Sep 17 00:00:00 2001 From: ghiscoding Date: Fri, 12 May 2023 23:32:03 -0400 Subject: [PATCH] chore: better use of DOM element creation and innerHTML - also remove `passive` mode to certain events that use preventDefault since that is not compatible with `passive` mode --- cypress/integration/example-grid-menu.spec.js | 4 +- examples/example-grid-menu.html | 4 +- slick.grid.js | 54 ++++++++----------- slick.interactions.js | 4 +- 4 files changed, 29 insertions(+), 37 deletions(-) diff --git a/cypress/integration/example-grid-menu.spec.js b/cypress/integration/example-grid-menu.spec.js index 11548ca5..8b33b4c6 100644 --- a/cypress/integration/example-grid-menu.spec.js +++ b/cypress/integration/example-grid-menu.spec.js @@ -3,7 +3,7 @@ import '@4tw/cypress-drag-drop'; describe('Example - Grid Menu', () => { - const fullTitles = ['#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']; + const fullTitles = ['', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']; beforeEach(() => { // create a console.log spy for later use @@ -53,7 +53,7 @@ describe('Example - Grid Menu', () => { }); it('should click on the Grid Menu to hide the column "A"', () => { - const expectedTitleList = ['#', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']; // without "A" + const expectedTitleList = ['', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']; // without "A" cy.get('#myGrid') .find('button.slick-gridmenu-button') diff --git a/examples/example-grid-menu.html b/examples/example-grid-menu.html index 77b6e8ea..9e89e074 100644 --- a/examples/example-grid-menu.html +++ b/examples/example-grid-menu.html @@ -245,7 +245,9 @@

View Source:

] } }; - var columns = [{ id: 'id', field: 'id', name: '#', width: 40, excludeFromGridMenu: true }]; + + // show 1st column with font-awesome hashtag icon + var columns = [{ id: 'id', field: 'id', name: '', width: 40, excludeFromGridMenu: true }]; var columnFilters = {}; var sortcol = "id"; var sortdir = 1; diff --git a/slick.grid.js b/slick.grid.js index 85b7e85a..1fd3138a 100644 --- a/slick.grid.js +++ b/slick.grid.js @@ -1080,8 +1080,9 @@ if (typeof Slick === "undefined") { }); header.setAttribute("title", toolTip || ""); - if(title !== undefined) - header.children[0].innerHTML = title; + if (title !== undefined) { + header.children[0].innerHTML = sanitizeHtmlString(title); + } trigger(self.onHeaderCellRendered, { "node": header, @@ -1221,13 +1222,14 @@ if (typeof Slick === "undefined") { frozenColumnsValid = true; } - const el = utils.createDomElement('div', { className: 'ui-state-default slick-group-header-column' }, hasFrozenColumns() && (columnsLength - 1) > options.frozenColumn ? _groupHeadersR[index] : _groupHeadersL[index]); - el.innerHTML = "" + m.name + ""; - el.setAttribute("id", "" + uid + m.id); - el.setAttribute("title", m.toolTip || ""); - el.classList.add(m.headerCssClass || "") - el.classList.add(hasFrozenColumns() && (columnsLength - 1) > options.frozenColumn? 'frozen': ''); - utils.storage.put(el, "column", m); + const el = utils.createDomElement('div', + { id: `${uid + m.id}`, className: 'ui-state-default slick-group-header-column', title: m.toolTip || '' }, + hasFrozenColumns() && (columnsLength - 1) > options.frozenColumn ? _groupHeadersR[index] : _groupHeadersL[index] + ); + utils.createDomElement('span', { className: 'slick-column-name', innerHTML: sanitizeHtmlString(m.name) }, el); + el.classList.add(m.headerCssClass || "") + el.classList.add(hasFrozenColumns() && (columnsLength - 1) > options.frozenColumn ? 'frozen' : ''); + utils.storage.put(el, "column", m); } if (hasFrozenColumns() && index === 0 && !frozenColumnsValid) { @@ -1325,13 +1327,9 @@ if (typeof Slick === "undefined") { const headerTarget = hasFrozenColumns() ? ((i <= options.frozenColumn) ? _headerL : _headerR) : _headerL; const headerRowTarget = hasFrozenColumns() ? ((i <= options.frozenColumn) ? _headerRowL : _headerRowR) : _headerRowL; - const header = utils.createDomElement('div', { className: 'ui-state-default slick-header-column' }, headerTarget); - - header.innerHTML = "" + m.name + ""; + const header = utils.createDomElement('div', { id: `${uid + m.id}`, dataset: { id: m.id }, className: 'ui-state-default slick-header-column', title: m.toolTip || '' }, headerTarget); + utils.createDomElement('span', { className: 'slick-column-name', innerHTML: sanitizeHtmlString(m.name) }, header); utils.width(header, m.width - headerColumnWidthDiff); - header.setAttribute("id", "" + uid + m.id); - header.setAttribute("title", m.toolTip || ""); - header.setAttribute("data-id", m.id); let classname = m.headerCssClass || null; if (classname) { @@ -2141,8 +2139,7 @@ if (typeof Slick === "undefined") { } function createCssRules() { - const template = document.createElement('template'); - template.innerHTML = '