@itrocks/table 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/link.d.ts +31 -0
- package/link.js +77 -0
- package/package.json +1 -1
package/link.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Plugin } from '../plugin/plugin.js';
|
|
2
|
+
import { PluginOptions } from '../plugin/plugin.js';
|
|
3
|
+
import { Table } from './table.js';
|
|
4
|
+
type ValueCallback = (element: Element) => ({
|
|
5
|
+
element: Element;
|
|
6
|
+
value: string;
|
|
7
|
+
} | undefined);
|
|
8
|
+
declare class Options extends PluginOptions {
|
|
9
|
+
call: (url: string) => void;
|
|
10
|
+
href: string | ValueCallback;
|
|
11
|
+
id: string | ValueCallback;
|
|
12
|
+
link: (href: string, id?: string) => string;
|
|
13
|
+
}
|
|
14
|
+
export declare class TableLink extends Plugin<Table, Options> {
|
|
15
|
+
constructor(options?: Partial<Options>);
|
|
16
|
+
defaultOptions(): Options;
|
|
17
|
+
protected getCell(row: HTMLTableRowElement, columnIndex: number): HTMLTableCellElement | undefined;
|
|
18
|
+
protected getCol(columnIndex: number): HTMLElement | undefined;
|
|
19
|
+
protected getHeadCell(columnIndex: number): HTMLElement | undefined;
|
|
20
|
+
protected getHref(target: Element, attribute: string, cell: HTMLTableCellElement, idElement?: Element): {
|
|
21
|
+
element: Element;
|
|
22
|
+
value: string;
|
|
23
|
+
} | undefined;
|
|
24
|
+
protected getId(target: Element, attribute: string): {
|
|
25
|
+
element: Element;
|
|
26
|
+
value: string;
|
|
27
|
+
} | undefined;
|
|
28
|
+
init(): void;
|
|
29
|
+
onClick(event: MouseEvent): void;
|
|
30
|
+
}
|
|
31
|
+
export {};
|
package/link.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Plugin } from '../plugin/plugin.js';
|
|
2
|
+
import { PluginOptions } from '../plugin/plugin.js';
|
|
3
|
+
class Options extends PluginOptions {
|
|
4
|
+
call = function (url) { window.location.href = url; };
|
|
5
|
+
href = 'data-href';
|
|
6
|
+
id = 'data-id';
|
|
7
|
+
link = function (href, id) {
|
|
8
|
+
return id ? (href + (href.endsWith('/') ? '' : '/') + id) : href;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export class TableLink extends Plugin {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
super(options);
|
|
14
|
+
}
|
|
15
|
+
defaultOptions() {
|
|
16
|
+
return new Options();
|
|
17
|
+
}
|
|
18
|
+
getCell(row, columnIndex) {
|
|
19
|
+
let current = -1;
|
|
20
|
+
for (const cell of Array.from(row.cells)) {
|
|
21
|
+
const start = current + 1;
|
|
22
|
+
const end = start + Math.max(1, cell.colSpan || 1) - 1;
|
|
23
|
+
if (columnIndex >= start && columnIndex <= end) {
|
|
24
|
+
return cell;
|
|
25
|
+
}
|
|
26
|
+
current = end;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
getCol(columnIndex) {
|
|
30
|
+
return this.of.element.querySelector('colgroup')?.querySelectorAll('col')?.[columnIndex];
|
|
31
|
+
}
|
|
32
|
+
getHeadCell(columnIndex) {
|
|
33
|
+
const row = this.of.element.tHead?.rows[0];
|
|
34
|
+
return row ? this.getCell(row, columnIndex) : undefined;
|
|
35
|
+
}
|
|
36
|
+
getHref(target, attribute, cell, idElement) {
|
|
37
|
+
let element = target.closest('[' + attribute + ']') ?? undefined;
|
|
38
|
+
if (element && idElement?.contains(element))
|
|
39
|
+
element = undefined;
|
|
40
|
+
if (!element)
|
|
41
|
+
for (const getFunc of [this.getHeadCell, this.getCol]) {
|
|
42
|
+
element = getFunc.call(this, cell.cellIndex);
|
|
43
|
+
if (element?.hasAttribute(attribute))
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
if (element)
|
|
47
|
+
return { element, value: element.getAttribute(attribute) ?? '' };
|
|
48
|
+
}
|
|
49
|
+
getId(target, attribute) {
|
|
50
|
+
const element = target.closest('[' + attribute + ']');
|
|
51
|
+
const value = element?.getAttribute(attribute);
|
|
52
|
+
if (element && value)
|
|
53
|
+
return { element, value };
|
|
54
|
+
}
|
|
55
|
+
init() {
|
|
56
|
+
for (const tBody of Array.from(this.of.element.tBodies)) {
|
|
57
|
+
tBody.addEventListener('click', event => this.onClick(event));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
onClick(event) {
|
|
61
|
+
const target = event.target;
|
|
62
|
+
if (!(target instanceof Element))
|
|
63
|
+
return;
|
|
64
|
+
const cell = target.closest('td, th');
|
|
65
|
+
if (!cell || cell.querySelector('input, select, textarea'))
|
|
66
|
+
return;
|
|
67
|
+
const id = (typeof this.options.id === 'function')
|
|
68
|
+
? this.options.id.call(this, target)
|
|
69
|
+
: this.getId(target, this.options.id);
|
|
70
|
+
const href = (typeof this.options.href === 'function')
|
|
71
|
+
? this.options.href.call(this, target)
|
|
72
|
+
: this.getHref(target, this.options.href, cell, id?.element);
|
|
73
|
+
if (!href)
|
|
74
|
+
return;
|
|
75
|
+
this.options.call(this.options.link(href.value, id?.value));
|
|
76
|
+
}
|
|
77
|
+
}
|
package/package.json
CHANGED