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

Some Refactoring #4

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
490 changes: 245 additions & 245 deletions src/capytable.css

Large diffs are not rendered by default.

43 changes: 14 additions & 29 deletions src/capytable.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { _fnAddColumns } from './core/columns';
import { addColumns } from './core/columns';
import { detectHeaderLength } from './core/draw';
import { _fnInitialise } from './core/init';
import { _fnSortingClasses } from './core/sort';
import { _fnLog } from './core/support';
import { initialize } from './core/init';
import { adjustSortingClasses } from './core/sort';
import { logError } from './core/support';
import { ISettings } from './models/interfaces';
import { IOptions } from './models/options';
import { Settings } from './models/settings';

class Capytable {
settings: ISettings;

constructor(id: string, options?: IOptions) {
constructor(id: string) {
var element = document.getElementById(id);
if (!element) {
console.error('Element with id ' + id + ' not found');
Expand All @@ -19,37 +18,23 @@ class Capytable {

/* Sanity check */
if (element.nodeName.toLowerCase() != 'table') {
_fnLog(
logError(
null,
'Non-table node initialisation (' + element.nodeName + ')',
2,
);
return;
}

if (!options) {
options = {
searching: true,
paging: true,
ordering: true,
};
}

// make col group element
const colGroupElement = document.createElement('colgroup');
element.prepend(colGroupElement);

/* Create the settings object for this table and set some of the default parameters */
var oSettings: ISettings = new Settings(
options.searching,
options.paging,
options.ordering,
id,
{
table: element,
colgroup: colGroupElement,
},
);
var oSettings: ISettings = new Settings(id, {
table: element,
colgroup: colGroupElement,
});

// Need to add the instance after the instance after the settings object has been added
// to the settings array, so we can self reference the table instance if more than one
Expand All @@ -73,12 +58,12 @@ class Capytable {
}

// Add the columns, apply the column definitions
_fnAddColumns(oSettings, initHeaderLength);
addColumns(oSettings, initHeaderLength);

// Do a first pass on the sorting classes (allows any size changes to be taken into
// account, and also will apply sorting disabled classes if disabled
_fnSortingClasses(oSettings);
oSettings.drawCallbacks.push(() => _fnSortingClasses(oSettings));
adjustSortingClasses(oSettings);
oSettings.drawCallbacks.push(() => adjustSortingClasses(oSettings));

/*
* Table HTML init
Expand Down Expand Up @@ -114,7 +99,7 @@ class Capytable {
oSettings.initialized = true;

// Language definitions
_fnInitialise(oSettings);
initialize(oSettings);

this.settings = oSettings;
}
Expand Down
59 changes: 30 additions & 29 deletions src/core/columns.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { Column } from '../models/column';
import { ISettings } from '../models/interfaces';
import { _fnCalculateColumnWidths } from './sizing';
import { _fnCallbackFire } from './support';
import { calculateColumnWidths } from './sizing';
import { callbackFire } from './support';

/**
* Add columns to the list used for the table with default values
* @param {object} oSettings Capytable settings object
* @memberof Capytable#oApi
* @param settings - Capytable settings object
* @param length - Number of columns to add
*/
export function _fnAddColumns(oSettings: ISettings, length: number) {
export function addColumns(settings: ISettings, length: number): void {
for (let i = 0; i < length; i++) {
// Add column to columns array
var iCol = oSettings.columns.length;
var iCol = settings.columns.length;

const colElement = document.createElement('col');
colElement.setAttribute('data-dt-column', iCol.toString());
colElement.setAttribute('data-ct-column', iCol.toString());

var oCol = new Column(iCol, colElement);
oSettings.columns.push(oCol);
settings.columns.push(oCol);

/* Feature sorting overrides column specific when off */
if (!oSettings.features.ordering) {
if (!settings.features.ordering) {
oCol.orderable = false;
}
}
Expand All @@ -29,33 +29,31 @@ export function _fnAddColumns(oSettings: ISettings, length: number) {
/**
* Adjust the table column widths for new data. Note: you would probably want to
* do a redraw after calling this function!
* @param {object} settings Capytable settings object
* @memberof Capytable#oApi
* @param settings - Capytable settings object
*/
export function _fnAdjustColumnSizing(settings) {
_fnCalculateColumnWidths(settings);
export function adjustColumnSizing(settings: ISettings): void {
calculateColumnWidths(settings);

var cols = settings.columns;

for (var i = 0; i < cols.length; i++) {
var width = _fnColumnsSumWidth(settings, [i], false);
var width = sumColumnWidth(settings, [i], false);

// Need to set the min-width, otherwise the browser might try to collapse
// it further
cols[i].colElement.style.width = width;
cols[i].colElement.style.minWidth = width;
}

_fnCallbackFire(settings, null, 'column-sizing', [settings]);
callbackFire(settings, null, 'column-sizing', [settings]);
}

/**
* Get the number of visible columns
* @param {object} oSettings Capytable settings object
* @returns {int} i the number of visible columns
* @memberof Capytable#oApi
* @param settings - Capytable settings object
* @returns the number of visible columns
*/
export function _fnVisibleColumns(settings) {
export function countVisibleColumns(settings: ISettings): number {
var layout = settings.header;
var vis = 0;

Expand All @@ -72,19 +70,22 @@ export function _fnVisibleColumns(settings) {

/**
* Get the width for a given set of columns
*
* @param {*} settings Capytable settings object
* @param {*} targets Columns - comma separated string or array of numbers
* @param {*} original Use the original width (true) or calculated (false)
* @param settings - Capytable settings object
* @param targets - Columns - comma separated string or array of numbers
* @param original - Use the original width (true) or calculated (false)
* @returns Combined CSS value
*/
export function _fnColumnsSumWidth(settings, targets, original) {
export function sumColumnWidth(
settings: ISettings,
targets,
original: boolean,
): string {
if (!Array.isArray(targets)) {
targets = _fnColumnsFromHeader(targets);
targets = getColumnsFromHeader(targets);
}

var sum = 0;
var unit;
var unit: string;
var columns = settings.columns;

for (let i = 0; i < targets.length; i++) {
Expand All @@ -109,12 +110,12 @@ export function _fnColumnsSumWidth(settings, targets, original) {
return sum + unit;
}

export function _fnColumnsFromHeader(cell) {
const attr = cell.closest('[data-dt-column]').getAttribute('data-dt-column');
export function getColumnsFromHeader(cell: HTMLTableCellElement): number[] {
const attr = cell.closest('[data-ct-column]').getAttribute('data-ct-column');

if (!attr) {
return [];
}

return attr.split(',').map((val) => val * 1);
return attr.split(',').map((val) => Number(val));
}
59 changes: 26 additions & 33 deletions src/core/data.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { ISettings } from '../models/interfaces';
import { Row } from '../models/row';
import { _fnCreateTr } from './draw';
import { _search } from './internal';
import { _fnLog } from './support';
import { createRow } from './draw';
import { search } from './internal';
import { logError } from './support';

/**
* Add one or more TR elements to the table. Generally we'd expect to
* use this for reading data from a DOM sourced table, but it could be
* used for an TR element. Note that if a TR is given, it is used (i.e.
* it is not cloned).
* @param {object} settings Capytable settings object
* @param {array|node|jQuery} trs The TR element(s) to add to the table
* @returns {array} Array of indexes for the added rows
* @memberof Capytable#oApi
* @param settings - Capytable settings object
* @returns array of indexes for the added rows
*/
export function _fnAddTr(settings) {
export function addRows(settings: ISettings): number[] {
const trs = [...settings.tBodyElement.querySelectorAll(':scope > tr')];

return trs.map(function (tr) {
const tds: any[] = [];
const data: any[] = [];
const tds: HTMLTableCellElement[] = [];
const data: string[] = [];

let i = 0;
let td = tr.firstChild;
let td = tr.firstChild as HTMLElement;
while (td) {
const name = td.nodeName.toUpperCase();

Expand All @@ -31,10 +30,10 @@ export function _fnAddTr(settings) {
data[i] = contents;
i++;

tds.push(td);
tds.push(td as HTMLTableCellElement);
}

td = td.nextSibling;
td = td.nextSibling as HTMLElement;
}

/* Create the object for storing information about this new row */
Expand All @@ -49,7 +48,7 @@ export function _fnAddTr(settings) {

/* Create the DOM information, or register it if already present */
if (tr) {
_fnCreateTr(settings, rowIdx, tr, tds);
createRow(settings, rowIdx, tr as HTMLTableRowElement, tds);
}

return rowIdx;
Expand All @@ -58,14 +57,18 @@ export function _fnAddTr(settings) {

/**
* Get the data for a given cell from the internal cache, taking into account data mapping
* @param {object} settings Capytable settings object
* @param {int} rowIdx data row id
* @param {int} colIdx Column index
* @param {string} type data get type ('display', 'type' 'filter|search' 'sort|order')
* @returns {*} Cell data
* @memberof Capytable#oApi
* @param settings - Capytable settings object
* @param rowIdx - data row id
* @param colIdx - Column index
* @param type - data get type ('display', 'type' 'filter|search' 'sort|order')
* @returns Cell data
*/
export function _fnGetCellData(settings, rowIdx, colIdx, type) {
export function getCellData(
settings: ISettings,
rowIdx: number,
colIdx: number,
type: string,
): string {
var row = settings.data[rowIdx];
if (!row) {
return undefined;
Expand All @@ -75,18 +78,8 @@ export function _fnGetCellData(settings, rowIdx, colIdx, type) {
var rowData = row._data;
var cellData = rowData[col.idx];

// Allow for a node being returned for non-display types
if (
type !== 'display' &&
cellData &&
typeof cellData === 'object' &&
cellData.nodeName
) {
cellData = cellData.innerHTML;
}

if (cellData === undefined) {
_fnLog(
logError(
settings,
'Requested unknown parameter ' +
(typeof col.idx == 'function' ? '{function}' : "'" + col.idx + "'") +
Expand All @@ -105,7 +98,7 @@ export function _fnGetCellData(settings, rowIdx, colIdx, type) {
}

if (type === 'filter') {
cellData = _search(cellData);
cellData = search(cellData);
}

return cellData;
Expand Down
Loading