Skip to content

Commit 679bd18

Browse files
committed
version 3.2.0-alpha.41
1 parent 505278b commit 679bd18

File tree

7 files changed

+218
-71
lines changed

7 files changed

+218
-71
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cample",
3-
"version": "3.2.0-alpha.40",
3+
"version": "3.2.0-alpha.41",
44
"description": "Cample.js - fast modern javascript framework. Reactivity without virtual DOM!",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/config/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const TEXT_REGEX = /\{\{\s*([^}]+)\s*\}\}|([^{}]+)/g;
77
export const SPACE_REGEX = /\s+/g;
88
export const EXCLAMATION_POINT = /(\!)/g;
99
export const EACH_INDEX_NAME = "__campleEachIndex";
10+
export const CLICK_FUNCTION_NAME = "__campleFn";
1011
export const { appendChild, insertBefore, removeChild, replaceChild } =
1112
Node.prototype;
1213
export const updText = (

src/core/cample.ts

+33-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import {
88
} from "../types/types";
99
import { renderTemplate } from "./functions/render/render-template";
1010
import { checkFunction, getExportData } from "../shared/utils";
11+
import { CLICK_FUNCTION_NAME } from "../config/config";
1112

1213
export class Cample {
1314
public selector: SelectorType;
1415
public template: string;
1516
public style: string;
1617
public trimHTML?: boolean;
1718
public exportData: ExportCampleDataType;
19+
public _isListener: boolean;
20+
public _el: Element | null;
1821

1922
constructor(
2023
selector: SelectorType,
@@ -27,13 +30,39 @@ export class Cample {
2730
this.trimHTML = options.trimHTML !== undefined ? options.trimHTML : false;
2831
this.exportData = {};
2932
this.style = "";
33+
this._isListener = false;
34+
this._el = null;
3035
}
3136
render(template = "", options: OptionsType = {}): void {
3237
this.template = renderTemplate(template, options);
33-
3438
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+
};
3555
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+
};
3766
Object.keys(options).forEach((e) => {
3867
if (options[e]._getStyle) {
3968
this.style += options[e]._getStyle;
@@ -54,6 +83,7 @@ export class Cample {
5483
for (let i = 0; i < this.exportData[selector].components.length; i++) {
5584
const e = this.exportData[selector].components[i];
5685
e.render(
86+
setEventListener,
5787
this.trimHTML,
5888
this.exportData[selector].value,
5989
undefined,
@@ -65,6 +95,7 @@ export class Cample {
6595
Object.keys(options).forEach((e, i) => {
6696
const selector = options[e]._getSelector;
6797
options[e].render(
98+
setEventListener,
6899
this.trimHTML,
69100
selector && this.exportData.hasOwnProperty(selector)
70101
? this.exportData[selector].value

src/core/components/component/component.ts

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export class Component extends DataComponent {
9595
}
9696

9797
render(
98+
setEventListener: () => void,
9899
trimHTML?: boolean,
99100
exportData?: ExportDataType,
100101
importId?: ExportIdType,
@@ -955,6 +956,7 @@ export class Component extends DataComponent {
955956
this.functions
956957
);
957958
const { obj: newTemplateObj } = parseTemplate(
959+
setEventListener,
958960
renderDynamic,
959961
[
960962
renderFn1,

src/core/components/each/each.ts

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export class Each extends DataComponent {
110110
}
111111

112112
render(
113+
setEventListener: () => void,
113114
trimHTML?: boolean,
114115
exportData?: ExportDataType,
115116
importId?: ExportIdType,
@@ -891,6 +892,7 @@ export class Each extends DataComponent {
891892
};
892893

893894
const { obj: newTemplateObj } = parseTemplate(
895+
setEventListener,
894896
renderDynamic,
895897
[
896898
renderFn1,

0 commit comments

Comments
 (0)