@@ -8,13 +8,16 @@ import {
8
8
} from "../types/types" ;
9
9
import { renderTemplate } from "./functions/render/render-template" ;
10
10
import { checkFunction , getExportData } from "../shared/utils" ;
11
+ import { CLICK_FUNCTION_NAME } from "../config/config" ;
11
12
12
13
export class Cample {
13
14
public selector : SelectorType ;
14
15
public template : string ;
15
16
public style : string ;
16
17
public trimHTML ?: boolean ;
17
18
public exportData : ExportCampleDataType ;
19
+ public _isListener : boolean ;
20
+ public _el : Element | null ;
18
21
19
22
constructor (
20
23
selector : SelectorType ,
@@ -27,13 +30,39 @@ export class Cample {
27
30
this . trimHTML = options . trimHTML !== undefined ? options . trimHTML : false ;
28
31
this . exportData = { } ;
29
32
this . style = "" ;
33
+ this . _isListener = false ;
34
+ this . _el = null ;
30
35
}
31
36
render ( template = "" , options : OptionsType = { } ) : void {
32
37
this . template = renderTemplate ( template , options ) ;
33
-
34
38
if ( typeof this . selector === "string" ) {
39
+ const eventListener : EventListenerOrEventListenerObject = (
40
+ current : Event
41
+ ) => {
42
+ let node : any =
43
+ current . composedPath !== undefined
44
+ ? current . composedPath ( ) [ 0 ]
45
+ : current . target ;
46
+ while ( node !== null ) {
47
+ const eventListener = node [ CLICK_FUNCTION_NAME ] ;
48
+ if ( eventListener !== undefined && ! node . disabled ) {
49
+ eventListener ( current ) ;
50
+ if ( current . cancelBubble ) return ;
51
+ }
52
+ node = node . parentNode ;
53
+ }
54
+ } ;
35
55
const el : Element | null = document . querySelector ( this . selector ) ;
36
- if ( el !== null ) el . innerHTML = this . template ;
56
+ if ( el !== null ) {
57
+ el . innerHTML = this . template ;
58
+ this . _el = el ;
59
+ }
60
+ const setEventListener = ( ) => {
61
+ if ( ! this . _isListener && this . _el !== null ) {
62
+ document . addEventListener ( "click" , eventListener ) ;
63
+ this . _isListener = true ;
64
+ }
65
+ } ;
37
66
Object . keys ( options ) . forEach ( ( e ) => {
38
67
if ( options [ e ] . _getStyle ) {
39
68
this . style += options [ e ] . _getStyle ;
@@ -54,6 +83,7 @@ export class Cample {
54
83
for ( let i = 0 ; i < this . exportData [ selector ] . components . length ; i ++ ) {
55
84
const e = this . exportData [ selector ] . components [ i ] ;
56
85
e . render (
86
+ setEventListener ,
57
87
this . trimHTML ,
58
88
this . exportData [ selector ] . value ,
59
89
undefined ,
@@ -65,6 +95,7 @@ export class Cample {
65
95
Object . keys ( options ) . forEach ( ( e , i ) => {
66
96
const selector = options [ e ] . _getSelector ;
67
97
options [ e ] . render (
98
+ setEventListener ,
68
99
this . trimHTML ,
69
100
selector && this . exportData . hasOwnProperty ( selector )
70
101
? this . exportData [ selector ] . value
0 commit comments