@creative-web-solution/front-library 7.1.48 → 7.1.50

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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 7.1.50
4
+
5
+ - [Autocomplete]: Rework module
6
+ - [DOM]: Update Index function
7
+ - [DOM]: Add type to strToDOM
8
+ - [QuickTemplate]: Add params for opening and closing tag
9
+
10
+ ## 7.1.49
11
+
12
+ - [Accordion]: Fix bug when a tab is opened at start
13
+
3
14
  ## 7.1.48
4
15
 
5
16
  - [MediaQueriesEvents]: Add position check function
package/DOM/Index.ts CHANGED
@@ -6,22 +6,10 @@
6
6
  *
7
7
  * @returns - Position (starting at 0) of the element in the DOM list is belong to. -1 if there is no parentNode.
8
8
  */
9
- export function index( $element: Element ): number {
10
- let $child: ChildNode | null, idx: number;
11
-
12
- if ( !$element.parentNode ) {
9
+ export function index($element: Element): number {
10
+ if (!$element.parentElement) {
13
11
  return -1;
14
12
  }
15
13
 
16
- idx = 0;
17
- $child = $element.parentNode.firstChild;
18
-
19
- while ( $child && $element !== $child ) {
20
- if ( $child.nodeType === 1 ) {
21
- idx++;
22
- }
23
- $child = $child.nextSibling;
24
- }
25
-
26
- return idx;
14
+ return Array.from($element.parentElement.children).indexOf($element);
27
15
  }
package/DOM/StrToDOM.ts CHANGED
@@ -12,24 +12,25 @@
12
12
  *
13
13
  * @returns The created DOM element
14
14
  */
15
- export function strToDOM( html: string, tag = 'DIV' ): Node | DocumentFragment {
15
+ export function strToDOM<T extends HTMLElement>(html: string, tag = "DIV"): T {
16
16
  let $child;
17
17
 
18
18
  const $frag: DocumentFragment = document.createDocumentFragment();
19
- const $elem: HTMLElement = document.createElement( tag );
19
+ const $elem: HTMLElement = document.createElement(tag);
20
20
 
21
21
  $elem.innerHTML = html;
22
22
 
23
- while ( $elem.childNodes.length ) {
24
- $child = $elem.childNodes[ 0 ];
23
+ while ($elem.childNodes.length) {
24
+ $child = $elem.childNodes[0];
25
25
 
26
- if ( $child && $child.nodeType === 1 ) {
27
- $frag.appendChild( $child );
28
- }
29
- else {
30
- $elem.removeChild( $child );
26
+ if ($child && $child.nodeType === 1) {
27
+ $frag.appendChild($child);
28
+ } else {
29
+ $elem.removeChild($child);
31
30
  }
32
31
  }
33
32
 
34
- return $frag.childNodes.length === 1 ? $frag.childNodes[ 0 ] : $frag;
33
+ const $result = $frag.childNodes.length === 1 ? $frag.childNodes[0] : $frag;
34
+
35
+ return $result as T;
35
36
  }
@@ -1,13 +1,12 @@
1
- import { next } from '../../DOM/Traversing';
2
-
1
+ import { next } from "../../DOM/Traversing";
3
2
 
4
3
  /**
5
4
  * Tab of an accordion
6
5
  */
7
6
  export default class Tab {
8
- #isOpen: boolean;
7
+ #isOpen: boolean;
9
8
  #originalOpenedState: boolean;
10
- #$TAB_PANNEL: HTMLElement | null;
9
+ #$TAB_PANNEL: HTMLElement | null;
11
10
  #options;
12
11
  #$TAB;
13
12
 
@@ -15,75 +14,82 @@ export default class Tab {
15
14
  return this.#isOpen;
16
15
  }
17
16
 
18
- constructor( $TAB: HTMLElement, options: FLib.Accordion.TabOptions ) {
19
- this.#options = options;
20
- this.#$TAB = $TAB ;
21
-
22
- const ID = $TAB.getAttribute( 'aria-controls' );
23
- this.#$TAB_PANNEL = ID ? document.getElementById( ID ) : next( $TAB ) as HTMLElement;
17
+ constructor($TAB: HTMLElement, options: FLib.Accordion.TabOptions) {
18
+ this.#options = options;
19
+ this.#$TAB = $TAB;
24
20
 
25
- this.#isOpen = this.#originalOpenedState = $TAB.getAttribute( 'aria-expanded' ) === 'true';
21
+ const ID = $TAB.getAttribute("aria-controls");
22
+ this.#$TAB_PANNEL = ID
23
+ ? document.getElementById(ID)
24
+ : (next($TAB) as HTMLElement);
26
25
 
26
+ this.#isOpen = this.#originalOpenedState =
27
+ $TAB.getAttribute("aria-expanded") === "true";
27
28
 
28
- if ( this.#isOpen ) {
29
- this.#openTab( true );
29
+ if (this.#isOpen) {
30
+ this.#openTab(true);
30
31
  }
31
32
  }
32
33
 
33
-
34
34
  #changeTabState = (): void => {
35
- this.#$TAB.setAttribute( 'aria-expanded', this.#isOpen ? 'true' : 'false' );
36
- }
37
-
35
+ this.#$TAB.setAttribute(
36
+ "aria-expanded",
37
+ this.#isOpen ? "true" : "false",
38
+ );
39
+ };
38
40
 
39
- #openTab = ( isOpenAtStart?: boolean ): void => {
41
+ #openTab = (isOpenAtStart?: boolean): void => {
40
42
  this.#options.animations
41
- .open( this.#$TAB, this.#$TAB_PANNEL )
42
- .then( () => {
43
-
44
- if ( isOpenAtStart && this.#options.onOpenAtStart ) {
45
- this.#options.onOpenAtStart( this.#$TAB, this.#$TAB_PANNEL );
46
- }
47
- else if ( !isOpenAtStart && this.#options.onOpen ) {
48
- this.#options.onOpen( this.#$TAB, this.#$TAB_PANNEL );
49
- }
50
- } );
43
+ .open(this.#$TAB, this.#$TAB_PANNEL)
44
+ .then(() => {
45
+ if (isOpenAtStart && this.#options.onOpenAtStart) {
46
+ this.#options.onOpenAtStart(this.#$TAB, this.#$TAB_PANNEL);
47
+ } else if (!isOpenAtStart && this.#options.onOpen) {
48
+ this.#options.onOpen(this.#$TAB, this.#$TAB_PANNEL);
49
+ }
50
+ });
51
51
 
52
52
  this.#isOpen = true;
53
53
  this.#changeTabState();
54
- }
54
+ };
55
55
 
56
- #closeTab = ( autoClose?: boolean ): void => {
56
+ #closeTab = (autoClose?: boolean): void => {
57
57
  this.#options.animations
58
- .close( this.#$TAB, this.#$TAB_PANNEL )
59
- .then( () => {
60
- this.#options.onClose?.( this.#$TAB, this.#$TAB_PANNEL, autoClose );
61
- } );
58
+ .close(this.#$TAB, this.#$TAB_PANNEL)
59
+ .then(() => {
60
+ this.#options.onClose?.(
61
+ this.#$TAB,
62
+ this.#$TAB_PANNEL,
63
+ autoClose,
64
+ );
65
+ });
62
66
 
63
67
  this.#isOpen = false;
64
68
  this.#changeTabState();
65
- }
69
+ };
66
70
 
67
71
  open(): this {
68
- if( !this.#isOpen ) {
69
- this.#openTab( false );
72
+ if (!this.#isOpen) {
73
+ this.#openTab(false);
70
74
  }
71
75
  return this;
72
76
  }
73
77
 
74
- close( autoClose?: boolean ): this {
75
- if( this.#isOpen ) {
76
- this.#closeTab( autoClose );
78
+ close(autoClose?: boolean): this {
79
+ if (this.#isOpen) {
80
+ this.#closeTab(autoClose);
77
81
  }
78
82
 
79
83
  return this;
80
84
  }
81
85
 
82
-
83
86
  destroy(): this {
84
- this.#options.animations.destroy( this.#$TAB, this.#$TAB_PANNEL );
87
+ this.#options.animations.destroy(this.#$TAB, this.#$TAB_PANNEL);
85
88
 
86
- this.#$TAB.setAttribute( 'aria-expanded', this.#originalOpenedState ? 'true' : 'false' );
89
+ this.#$TAB.setAttribute(
90
+ "aria-expanded",
91
+ this.#originalOpenedState ? "true" : "false",
92
+ );
87
93
 
88
94
  return this;
89
95
  }
@@ -1,33 +1,31 @@
1
- import { aClass, rClass } from '../../DOM/Class';
2
- import { extend } from '../../Helpers/Extend';
3
- import { on, off } from '../../Events/EventsManager';
4
- import Tab from './Tab';
5
-
1
+ import { aClass, rClass } from "../../DOM/Class";
2
+ import { extend } from "../../Helpers/Extend";
3
+ import { on, off } from "../../Events/EventsManager";
4
+ import Tab from "./Tab";
6
5
 
7
6
  const DEFAULT_OPTIONS = {
8
- "tabSelector": "button[aria-expanded]",
9
- "allowMultipleTab": false,
10
- "atLeastOneOpen": false,
11
- "animations": {
12
- "open": function( $TAB, $TAB_PANNEL ) {
13
- aClass( [ $TAB, $TAB_PANNEL ], 'on' );
7
+ tabSelector: "button[aria-expanded]",
8
+ allowMultipleTab: false,
9
+ atLeastOneOpen: false,
10
+ animations: {
11
+ open: function ($TAB, $TAB_PANNEL) {
12
+ aClass([$TAB, $TAB_PANNEL], "on");
14
13
 
15
14
  return Promise.resolve();
16
15
  },
17
- "close": function( $TAB, $TAB_PANNEL ) {
18
- rClass( [ $TAB, $TAB_PANNEL ], 'on' );
16
+ close: function ($TAB, $TAB_PANNEL) {
17
+ rClass([$TAB, $TAB_PANNEL], "on");
19
18
 
20
19
  return Promise.resolve();
21
20
  },
22
- "destroy": function( $TAB, $TAB_PANNEL ) {
23
- rClass( [ $TAB, $TAB_PANNEL ], 'on' );
21
+ destroy: function ($TAB, $TAB_PANNEL) {
22
+ rClass([$TAB, $TAB_PANNEL], "on");
24
23
 
25
24
  return Promise.resolve();
26
- }
27
- }
25
+ },
26
+ },
28
27
  };
29
28
 
30
-
31
29
  /**
32
30
  * Accordion
33
31
  *
@@ -86,70 +84,76 @@ const DEFAULT_OPTIONS = {
86
84
  */
87
85
  export default class Accordion {
88
86
  #$accordionWrapper: HTMLElement;
89
- #options: FLib.Accordion.Options;
90
- #$tabs: NodeListOf<HTMLElement>;
91
- #tablist = new Map<HTMLElement, Tab>();
92
- #status: string;
87
+ #options: FLib.Accordion.Options;
88
+ #$tabs: NodeListOf<HTMLElement>;
89
+ #tablist = new Map<HTMLElement, Tab>();
90
+ #status: string;
93
91
  #lastOpenedTabs = new Set<Tab>();
94
92
 
93
+ #STATUS_ON = "STATUS_ON";
94
+ #STATUS_OFF = "STATUS_OFF";
95
95
 
96
- #STATUS_ON = 'STATUS_ON';
97
- #STATUS_OFF = 'STATUS_OFF';
98
-
99
-
100
- constructor( $accordionWrapper: HTMLElement, userOptions: FLib.Accordion.OptionsInit ) {
96
+ constructor(
97
+ $accordionWrapper: HTMLElement,
98
+ userOptions: FLib.Accordion.OptionsInit,
99
+ ) {
101
100
  this.#$accordionWrapper = $accordionWrapper;
102
101
 
103
- this.#options = extend( DEFAULT_OPTIONS, userOptions );
102
+ this.#options = extend(DEFAULT_OPTIONS, userOptions);
104
103
 
105
- this.#$tabs = $accordionWrapper.querySelectorAll( this.#options.tabSelector );
106
- this.#status = this.#STATUS_OFF;
104
+ this.#$tabs = $accordionWrapper.querySelectorAll(
105
+ this.#options.tabSelector,
106
+ );
107
+ this.#status = this.#STATUS_OFF;
107
108
 
108
109
  this.#on();
109
110
  }
110
111
 
111
112
  #on = (): void => {
112
- if( this.#status === this.#STATUS_ON ){
113
+ if (this.#status === this.#STATUS_ON) {
113
114
  return;
114
115
  }
115
116
 
116
117
  this.#status = this.#STATUS_ON;
117
118
 
118
- this.#$tabs.forEach( ( $tab, index ) => {
119
- this.#tablist.set($tab, new Tab( $tab, {
119
+ this.#$tabs.forEach(($tab, index) => {
120
+ const tab = new Tab($tab, {
120
121
  ...this.#options,
121
122
  index,
122
- } ) );
123
- } );
124
-
125
- on( this.#$accordionWrapper, {
126
- "eventsName": "click",
127
- "selector": this.#options.tabSelector,
128
- "callback": this.#toggleTab
129
- } );
130
- }
131
-
123
+ });
124
+ this.#tablist.set($tab, tab);
125
+ if (tab.isOpen) {
126
+ this.#lastOpenedTabs.add(tab);
127
+ }
128
+ });
129
+
130
+ on(this.#$accordionWrapper, {
131
+ eventsName: "click",
132
+ selector: this.#options.tabSelector,
133
+ callback: this.#toggleTab,
134
+ });
135
+ };
132
136
 
133
137
  #off = (): void => {
134
- if( this.#status === this.#STATUS_OFF ){
138
+ if (this.#status === this.#STATUS_OFF) {
135
139
  return;
136
140
  }
137
141
 
138
142
  this.#status = this.#STATUS_OFF;
139
143
 
140
- this.#tablist.forEach( tab => {
144
+ this.#tablist.forEach((tab) => {
141
145
  tab.destroy();
142
- } );
146
+ });
143
147
 
144
148
  this.#tablist.clear();
145
149
 
146
- off( this.#$accordionWrapper, {
147
- "eventsName": "click",
148
- "callback": this.#toggleTab
149
- } );
150
- }
150
+ off(this.#$accordionWrapper, {
151
+ eventsName: "click",
152
+ callback: this.#toggleTab,
153
+ });
154
+ };
151
155
 
152
- #toggleTab = ( e: Event, $target: HTMLElement ): void => {
156
+ #toggleTab = (e: Event, $target: HTMLElement): void => {
153
157
  e.preventDefault();
154
158
 
155
159
  const tab = this.#tablist.get($target);
@@ -159,12 +163,12 @@ export default class Accordion {
159
163
  }
160
164
 
161
165
  if (tab.isOpen) {
162
- this.closeTab(tab)
166
+ this.closeTab(tab);
163
167
  return;
164
168
  }
165
169
 
166
- this.openTab(tab)
167
- }
170
+ this.openTab(tab);
171
+ };
168
172
 
169
173
  closeTab(tab: Tab): this {
170
174
  if (this.#options.atLeastOneOpen && this.#lastOpenedTabs.size < 2) {
@@ -179,7 +183,7 @@ export default class Accordion {
179
183
 
180
184
  openTab(tab: Tab): this {
181
185
  if (!this.#options.allowMultipleTab && this.#lastOpenedTabs.size > 0) {
182
- this.#lastOpenedTabs.forEach(tab => {
186
+ this.#lastOpenedTabs.forEach((tab) => {
183
187
  tab.close(true);
184
188
  });
185
189
  this.#lastOpenedTabs.clear();
@@ -210,7 +214,6 @@ export default class Accordion {
210
214
  return this;
211
215
  }
212
216
 
213
-
214
217
  /**
215
218
  * Restart the module
216
219
  */
@@ -0,0 +1,24 @@
1
+ export default abstract class AbstractAutocompleteAdapter<ItemDataType> {
2
+ abstract layerRender(): string;
3
+ abstract listRender(): string;
4
+ abstract itemRender(options: { item: ItemDataType; index: number }): string;
5
+ abstract errorRender(errorMessage: string, query: string): string;
6
+ abstract noResultRender(query: string): string;
7
+ abstract queryParamAdapter(
8
+ query: string,
9
+ $input: HTMLElement,
10
+ ): Record<string, any>;
11
+ abstract markValue(options: {
12
+ item: ItemDataType;
13
+ index: number;
14
+ query: string;
15
+ }): ItemDataType;
16
+ abstract updateInputValueFromItem?(options: {
17
+ item: ItemDataType;
18
+ index: number;
19
+ query: string;
20
+ results: ItemDataType[];
21
+ }): string;
22
+ abstract source(query: string): Promise<ItemDataType[]>;
23
+ abstract abortSource?(): void;
24
+ }