@itrocks/table 0.1.2 → 0.1.3

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.
Files changed (3) hide show
  1. package/link.d.ts +10 -12
  2. package/link.js +29 -15
  3. package/package.json +1 -1
package/link.d.ts CHANGED
@@ -1,30 +1,28 @@
1
1
  import { Plugin } from '../plugin/plugin.js';
2
2
  import { PluginOptions } from '../plugin/plugin.js';
3
3
  import { Table } from './table.js';
4
- type ValueCallback = (element: Element) => ({
4
+ type AttributeValue = string | string[] | ValueCallback;
5
+ type ElementValue = {
5
6
  element: Element;
6
7
  value: string;
7
- } | undefined);
8
+ };
9
+ type ValueCallback = (element: Element) => (ElementValue | undefined);
8
10
  declare class Options extends PluginOptions {
9
11
  call: (url: string) => void;
10
- href: string | ValueCallback;
11
- id: string | ValueCallback;
12
+ href: AttributeValue;
13
+ id: AttributeValue;
12
14
  link: (href: string, id?: string) => string;
13
15
  }
14
16
  export declare class TableLink extends Plugin<Table, Options> {
15
17
  constructor(options?: Partial<Options>);
16
18
  defaultOptions(): Options;
17
19
  protected getCell(row: HTMLTableRowElement, columnIndex: number): HTMLTableCellElement | undefined;
20
+ protected getClosestElement(target: Element, attributes: string[]): Element | undefined;
18
21
  protected getCol(columnIndex: number): HTMLElement | undefined;
22
+ protected getElementValue(element: Element | undefined, attributes: string[]): ElementValue | undefined;
19
23
  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;
24
+ protected getHref(target: Element, attributes: string | string[], cell: HTMLTableCellElement, idElement?: Element): ElementValue | undefined;
25
+ protected getId(target: Element, attributes: string | string[]): ElementValue | undefined;
28
26
  init(): void;
29
27
  onClick(event: MouseEvent): void;
30
28
  }
package/link.js CHANGED
@@ -2,7 +2,7 @@ import { Plugin } from '../plugin/plugin.js';
2
2
  import { PluginOptions } from '../plugin/plugin.js';
3
3
  class Options extends PluginOptions {
4
4
  call = function (url) { window.location.href = url; };
5
- href = 'data-href';
5
+ href = ['data-href', 'data-link'];
6
6
  id = 'data-id';
7
7
  link = function (href, id) {
8
8
  return id ? (href + (href.endsWith('/') ? '' : '/') + id) : href;
@@ -26,31 +26,45 @@ export class TableLink extends Plugin {
26
26
  current = end;
27
27
  }
28
28
  }
29
+ getClosestElement(target, attributes) {
30
+ return target.closest('[' + attributes.join('],[') + ']') ?? undefined;
31
+ }
29
32
  getCol(columnIndex) {
30
33
  return this.of.element.querySelector('colgroup')?.querySelectorAll('col')?.[columnIndex];
31
34
  }
35
+ getElementValue(element, attributes) {
36
+ if (!element)
37
+ return;
38
+ for (const attribute of attributes) {
39
+ if (element?.hasAttribute(attribute)) {
40
+ return { element, value: element.getAttribute(attribute) ?? '' };
41
+ }
42
+ }
43
+ }
32
44
  getHeadCell(columnIndex) {
33
45
  const row = this.of.element.tHead?.rows[0];
34
46
  return row ? this.getCell(row, columnIndex) : undefined;
35
47
  }
36
- getHref(target, attribute, cell, idElement) {
37
- let element = target.closest('[' + attribute + ']') ?? undefined;
48
+ getHref(target, attributes, cell, idElement) {
49
+ if (typeof attributes === 'string')
50
+ attributes = [attributes];
51
+ let element = this.getClosestElement(target, attributes);
38
52
  if (element && idElement?.contains(element))
39
53
  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
54
  if (element)
47
- return { element, value: element.getAttribute(attribute) ?? '' };
55
+ return this.getElementValue(element, attributes);
56
+ for (const getFunc of [this.getHeadCell, this.getCol]) {
57
+ element = getFunc.call(this, cell.cellIndex);
58
+ const value = this.getElementValue(element, attributes);
59
+ if (value)
60
+ return value;
61
+ }
48
62
  }
49
- getId(target, attribute) {
50
- const element = target.closest('[' + attribute + ']');
51
- const value = element?.getAttribute(attribute);
52
- if (element && value)
53
- return { element, value };
63
+ getId(target, attributes) {
64
+ if (typeof attributes === 'string')
65
+ attributes = [attributes];
66
+ const element = this.getClosestElement(target, attributes);
67
+ return this.getElementValue(element, attributes);
54
68
  }
55
69
  init() {
56
70
  for (const tBody of Array.from(this.of.element.tBodies)) {
package/package.json CHANGED
@@ -56,5 +56,5 @@
56
56
  },
57
57
  "type": "module",
58
58
  "types": "./table.d.ts",
59
- "version": "0.1.2"
59
+ "version": "0.1.3"
60
60
  }