tkeron 3.1.1 → 3.2.1

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,3 +1,15 @@
1
+ # v3.2.1
2
+
3
+ - removed path entry to @tkeron.
4
+
5
+ # v3.2.0
6
+
7
+ ## Breaking changes
8
+
9
+ - Added a simple library to support component development.
10
+ - Modified the tsconfig.json file to use the root directory as a base.
11
+ - Also, an index.ts file should be included in the root of the "comps" directory and components should be exported from there. This is to improve the length of component imports.
12
+
1
13
  # v3.1.1
2
14
 
3
15
  - placed the extensions.d.ts file in the root directory so that vscode recognizes css files, images, html, among others, as modules.
package/dist/cicd.js CHANGED
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const fs_1 = require("fs");
4
3
  const path_1 = require("path");
5
4
  const libFiles_ts_1 = require("./libFiles.ts");
6
- for (const file of libFiles_ts_1.libFiles) {
7
- (0, fs_1.copyFileSync)((0, path_1.join)("src", file), (0, path_1.join)("distFiles", file));
8
- }
5
+ const copyFile_js_1 = require("./copyFile.js");
6
+ (async () => {
7
+ const copiedFiles = [];
8
+ for (const file of libFiles_ts_1.libFiles) {
9
+ copiedFiles.push((0, copyFile_js_1.copyFile)((0, path_1.join)("src", file), (0, path_1.join)("distFiles", file)));
10
+ }
11
+ await Promise.all(copiedFiles);
12
+ })();
package/dist/cmdInit.js CHANGED
@@ -7,6 +7,7 @@ const buildLoaders_1 = require("./buildLoaders");
7
7
  const createTsConfigFile_1 = require("./createTsConfigFile");
8
8
  const fileExist_1 = require("./fileExist");
9
9
  const getOptions_1 = require("./getOptions");
10
+ const copyDir_1 = require("./copyDir");
10
11
  const cmdInit = async ({ sourceDir, outputDir } = { sourceDir: "", outputDir: "" }) => {
11
12
  const options = (0, getOptions_1.getOptions)({ sourceDir, outputDir });
12
13
  sourceDir = options.sourceDir;
@@ -20,5 +21,8 @@ const cmdInit = async ({ sourceDir, outputDir } = { sourceDir: "", outputDir: ""
20
21
  const extModules = (0, buildLoaders_1.getExtModules)();
21
22
  const extModName = (0, path_1.join)(dotTkeron, "..", "extensions.d.ts");
22
23
  await (0, promises_1.writeFile)(extModName, extModules, { encoding: "utf-8" });
24
+ const tkeronLibSrc = (0, path_1.join)(__dirname, "..", "tkeron_library");
25
+ const tkeronLibDir = (0, path_1.join)(dotTkeron, "tkeron_library");
26
+ await (0, copyDir_1.copyDir)(tkeronLibSrc, tkeronLibDir);
23
27
  };
24
28
  exports.cmdInit = cmdInit;
@@ -0,0 +1,2 @@
1
+ import { CopyFileResponse } from "./copyFile";
2
+ export declare const copyDir: (sourceDir: string, targetDir: string) => Promise<CopyFileResponse[]>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.copyDir = void 0;
4
+ const path_1 = require("path");
5
+ const getFilesRecursive_1 = require("./getFilesRecursive");
6
+ const copyFile_1 = require("./copyFile");
7
+ const copyDir = async (sourceDir, targetDir) => {
8
+ const files = (0, getFilesRecursive_1.getFilesRecursive)(sourceDir);
9
+ const allFilesPromises = [];
10
+ for (const file of files) {
11
+ const targetFileName = (0, path_1.normalize)(file).slice((0, path_1.normalize)(sourceDir).length);
12
+ const target = (0, path_1.join)(targetDir, targetFileName);
13
+ allFilesPromises.push((0, copyFile_1.copyFile)(file, target));
14
+ }
15
+ return await Promise.all(allFilesPromises);
16
+ };
17
+ exports.copyDir = copyDir;
@@ -0,0 +1,8 @@
1
+ export interface CopyFileResponse {
2
+ status: boolean;
3
+ message: string;
4
+ from: string;
5
+ to: string;
6
+ err?: Error;
7
+ }
8
+ export declare const copyFile: (from: string, to: string) => Promise<CopyFileResponse>;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.copyFile = void 0;
4
+ const fs_1 = require("fs");
5
+ const promises_1 = require("fs/promises");
6
+ const path_1 = require("path");
7
+ const copyFile = async (from, to) => new Promise(async (resume) => {
8
+ const toDirectory = (0, path_1.dirname)(to);
9
+ await (0, promises_1.mkdir)(toDirectory, { recursive: true });
10
+ const rs = (0, fs_1.createReadStream)(from);
11
+ const ws = (0, fs_1.createWriteStream)(to);
12
+ rs.on("error", (err) => {
13
+ ws.close();
14
+ resume({ status: false, message: "error reading file", err, from, to });
15
+ });
16
+ ws.on("error", (err) => {
17
+ rs.close();
18
+ resume({ status: false, message: "error writing file", err, from, to });
19
+ });
20
+ rs.on("end", () => {
21
+ rs.close();
22
+ ws.close();
23
+ resume({ status: true, message: "file copied", from, to });
24
+ });
25
+ rs.pipe(ws);
26
+ });
27
+ exports.copyFile = copyFile;
@@ -14,10 +14,12 @@ const createTsConfigFile = async () => {
14
14
  "moduleResolution": "Node",
15
15
  "declaration": false,
16
16
  "skipLibCheck": true,
17
- "baseUrl": "${sourceDir}",
18
17
  "paths": {
19
- "@comps/*": [
20
- "comps/*"
18
+ "@comps": [
19
+ "./${sourceDir}/comps",
20
+ ],
21
+ "@tkeron": [
22
+ "./.tkeron/tkeron_library"
21
23
  ],
22
24
  }
23
25
  },
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.libFiles = void 0;
4
4
  exports.libFiles = [
5
- "tkeron.ts",
6
5
  "eventEmitter.ts",
7
6
  "getType.ts",
8
7
  "runOncePerTime.ts",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tkeron",
3
- "version": "3.1.1",
3
+ "version": "3.2.1",
4
4
  "description": "Micro framework for developing web user interfaces with typescript.",
5
5
  "bin": {
6
6
  "tkeron": "dist/index.js",
@@ -0,0 +1,48 @@
1
+ import { TkeronElement } from "./element";
2
+ import { settings } from "./settings";
3
+
4
+ export const appendIn = (com: TkeronElement) => {
5
+ const appendInElement = (querySelector) => {
6
+ const element = document.querySelector(querySelector);
7
+ if (!element) return;
8
+ element.appendChild(com.htmlElement);
9
+ };
10
+
11
+ com.appendIn = {
12
+ body: <TkeronElement>{},
13
+ head: <TkeronElement>{},
14
+ element: (querySelector) => {
15
+ if (!settings.loaded) {
16
+ settings.runOnLoad.push(() => appendInElement(querySelector));
17
+ return com;
18
+ }
19
+ appendInElement(querySelector);
20
+ return com;
21
+ },
22
+ };
23
+
24
+ Object.defineProperty(com.appendIn, "body", {
25
+ get() {
26
+ if (!settings.loaded) {
27
+ settings.runOnLoad.push(() =>
28
+ document.body.appendChild(com.htmlElement)
29
+ );
30
+ return com;
31
+ }
32
+ document.body.appendChild(com.htmlElement);
33
+ return com;
34
+ },
35
+ });
36
+ Object.defineProperty(com.appendIn, "head", {
37
+ get() {
38
+ if (!settings.loaded) {
39
+ settings.runOnLoad.push(() =>
40
+ document.head.appendChild(com.htmlElement)
41
+ );
42
+ return com;
43
+ }
44
+ document.head.appendChild(com.htmlElement);
45
+ return com;
46
+ },
47
+ });
48
+ };
@@ -0,0 +1,49 @@
1
+ import { appendIn } from "./appendIn";
2
+ import { setAttribute } from "./setAttribute";
3
+ import { setHtml, setText } from "./setHtml_setText";
4
+
5
+ export interface TkeronElement {
6
+ htmlElement: HTMLElement;
7
+ appendIn: {
8
+ body: TkeronElement;
9
+ head: TkeronElement;
10
+ element: (querySelector: string) => TkeronElement;
11
+ };
12
+ setHtml: (html: string) => TkeronElement;
13
+ setText: (text: string) => TkeronElement;
14
+ setAttribute: (attribute: string, value: string) => TkeronElement;
15
+ }
16
+
17
+ export interface TkeronElementArguments {
18
+ tag: string;
19
+ childs: TkeronElement[];
20
+ }
21
+
22
+ export const tk = (
23
+ args?: Partial<TkeronElementArguments> | string
24
+ ): TkeronElement => {
25
+ if (!args) args = {};
26
+
27
+ let { tag, childs } = typeof args === "object" ? args : <any>{};
28
+
29
+ if (typeof args === "string") tag = args;
30
+
31
+ if (!tag) tag = "div";
32
+
33
+ const com = <TkeronElement>{
34
+ htmlElement: document.createElement(tag),
35
+ };
36
+
37
+ //init chaining methods
38
+ appendIn(com);
39
+ setHtml(com);
40
+ setText(com);
41
+ setAttribute(com);
42
+
43
+ if (childs)
44
+ for (const child of childs) {
45
+ com.htmlElement.appendChild(child.htmlElement);
46
+ }
47
+
48
+ return com;
49
+ };
@@ -0,0 +1,20 @@
1
+ export * from "./element";
2
+ import { tk } from "./element";
3
+ import { settings } from "./settings";
4
+
5
+ /**
6
+ * @param css {string} css code to append in style element
7
+ */
8
+ export const addCss = (css: string) => {
9
+ tk({ tag: "style" });
10
+ };
11
+
12
+ /**
13
+ * Run on document load to render elements waiting for body element.
14
+ */
15
+ document.addEventListener("DOMContentLoaded", () => {
16
+ settings.loaded = true;
17
+ for (const fn of settings.runOnLoad) {
18
+ fn();
19
+ }
20
+ });
@@ -0,0 +1,8 @@
1
+ import { TkeronElement } from "./element";
2
+
3
+ export const setAttribute = (com: TkeronElement) => {
4
+ com.setAttribute = (attribute, value) => {
5
+ com.htmlElement.setAttribute(attribute, value);
6
+ return com;
7
+ };
8
+ };
@@ -0,0 +1,15 @@
1
+ import { TkeronElement } from "./element";
2
+
3
+ export const setHtml = (com: TkeronElement) => {
4
+ com.setHtml = (html) => {
5
+ com.htmlElement.innerHTML = html;
6
+ return com;
7
+ };
8
+ };
9
+
10
+ export const setText = (com: TkeronElement) => {
11
+ com.setText = (text) => {
12
+ com.htmlElement.innerHTML = text;
13
+ return com;
14
+ };
15
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Tkeron general settings object
3
+ */
4
+ export const settings = {
5
+ loaded: false,
6
+ runOnLoad: <CallableFunction[]>[],
7
+ };
package/dist/tkeron.d.ts DELETED
@@ -1,44 +0,0 @@
1
- export interface tkeronOptions {
2
- type?: string;
3
- value?: string;
4
- }
5
- export interface Component {
6
- readonly id: string;
7
- value: string;
8
- setValue(v: string): Component;
9
- readonly parent: Component | null;
10
- _setParent(c: Component | null): Component;
11
- childs: Component[];
12
- add(...c: Component[]): Component;
13
- remove(c: Component): Component;
14
- renderIn(query: string | HTMLElement): Component;
15
- appendIn(query: string | HTMLElement): Component;
16
- getHTMLElement(f: (el: HTMLElement) => void): Component;
17
- getHTML(): string;
18
- readonly classList: string[];
19
- addClass(cls: string): Component;
20
- removeClass(cls: string): Component;
21
- readonly attributes: any;
22
- addAttribute(attr: string, value: string): Component;
23
- removeAttribute(attr: string): Component;
24
- getElement(): HTMLElement;
25
- setElement(el: HTMLElement): Component;
26
- css(css: string): Component;
27
- getCss(): string;
28
- on(f: (_com: Component, _el: HTMLElement) => void): Component;
29
- onRender(fn: () => void): Component;
30
- onAppend(fn: () => void): Component;
31
- send(...msg: any[]): Component;
32
- onMessage(fn: (...msg: any[]) => void): Component;
33
- }
34
- export declare const tkeron: {
35
- (opt?: tkeronOptions | string, ...classes: string[]): Component;
36
- css(name: string, cssText: string): void;
37
- script(anonFunc: CallableFunction, options?: scriptOptions): void;
38
- };
39
- export interface scriptOptions {
40
- appendIn?: string;
41
- runOnDOMContentLoaded?: boolean;
42
- globalFunctions?: CallableFunction[];
43
- }
44
- export declare const version = "1.9.0";
package/dist/tkeron.js DELETED
@@ -1,255 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.version = exports.tkeron = void 0;
4
- const handlers = {};
5
- const addHandler = (id, event, fn) => {
6
- if (typeof handlers[event] === "undefined")
7
- handlers[event] = {};
8
- if (typeof handlers[event][id] === "undefined")
9
- handlers[event][id] = [];
10
- handlers[event][id].push(fn);
11
- };
12
- const runHandlers = (id, event, ...args) => {
13
- if (typeof handlers[event] === "undefined")
14
- return;
15
- if (typeof handlers[event][id] === "undefined")
16
- return;
17
- handlers[event][id].forEach((fn) => fn(...args));
18
- };
19
- const toHexString = (bytes) => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
20
- const rnds = (n) => toHexString(crypto.getRandomValues(new Uint8Array(n))).slice(0, n);
21
- const IDs = [];
22
- const getID = () => {
23
- const id = "tk_" + rnds(20);
24
- //@ts-ignore
25
- if (IDs.includes(id)) {
26
- return getID();
27
- }
28
- return id;
29
- };
30
- const tkeron = (opt, ...classes) => {
31
- if (typeof opt === "string") {
32
- classes.push(opt);
33
- opt = { type: "div" };
34
- }
35
- if (!opt)
36
- opt = { type: "div" };
37
- const valueProp = ["input", "textarea"].some((n) => {
38
- if ("type" in opt)
39
- return opt.type === n;
40
- });
41
- let el = document.createElement(opt.type || "div");
42
- const priv = {
43
- parent: {},
44
- childs: [],
45
- css: "",
46
- };
47
- priv.parent = null;
48
- const com = {
49
- id: getID(),
50
- parent: priv.parent,
51
- _setParent: (c) => {
52
- priv.parent = c;
53
- return com;
54
- },
55
- value: "",
56
- setValue: (v) => {
57
- com.value = v;
58
- return com;
59
- },
60
- childs: priv.childs,
61
- add: (...c) => {
62
- c.forEach((_) => {
63
- _._setParent(com);
64
- priv.childs.push(_);
65
- _.getHTMLElement((ele) => {
66
- el.appendChild(ele);
67
- });
68
- });
69
- return com;
70
- },
71
- remove: (c) => {
72
- c._setParent(null);
73
- priv.childs = priv.childs.filter((_c) => {
74
- if (_c.id === c.id)
75
- return false;
76
- return true;
77
- });
78
- c.getHTMLElement((_el) => {
79
- el.removeChild(_el);
80
- });
81
- return com;
82
- },
83
- renderIn: (query) => {
84
- if (typeof query === "string") {
85
- const domel = document.querySelector(query);
86
- if (!domel) {
87
- document.addEventListener("DOMContentLoaded", () => {
88
- com.renderIn(query);
89
- });
90
- return com;
91
- }
92
- domel.innerHTML = "";
93
- domel.appendChild(el);
94
- runHandlers(com.id, 0 /* event.render */);
95
- return com;
96
- }
97
- query.innerHTML = "";
98
- query.appendChild(el);
99
- runHandlers(com.id, 0 /* event.render */);
100
- return com;
101
- },
102
- appendIn: (query) => {
103
- if (typeof query === "string") {
104
- const domel = document.querySelector(query);
105
- if (!domel) {
106
- document.addEventListener("DOMContentLoaded", () => {
107
- com.appendIn(query);
108
- });
109
- return com;
110
- }
111
- domel.appendChild(el);
112
- runHandlers(com.id, 1 /* event.append */);
113
- return com;
114
- }
115
- query.appendChild(el);
116
- runHandlers(com.id, 1 /* event.append */);
117
- return com;
118
- },
119
- getHTMLElement: (f) => {
120
- f(el);
121
- return com;
122
- },
123
- getHTML: () => {
124
- return el.outerHTML;
125
- },
126
- classList: [],
127
- addClass: (cls) => {
128
- el.classList.add(cls);
129
- return com;
130
- },
131
- removeClass: (cls) => {
132
- el.classList.remove(cls);
133
- return com;
134
- },
135
- attributes: {},
136
- addAttribute: (attr, value) => {
137
- el.setAttribute(attr, value);
138
- return com;
139
- },
140
- removeAttribute: (attr) => {
141
- el.removeAttribute(attr);
142
- return com;
143
- },
144
- getElement: () => {
145
- return el;
146
- },
147
- setElement: (ele) => {
148
- if (!ele)
149
- throw Error("Component.setElement: Element must not be null.");
150
- el = ele;
151
- return com;
152
- },
153
- css: (css) => {
154
- priv.css = css;
155
- exports.tkeron.css(com.id, `#${com.id} {${css}}`);
156
- com.addAttribute("id", com.id);
157
- return com;
158
- },
159
- getCss: () => priv.css,
160
- on: (f) => {
161
- f(com, el);
162
- return com;
163
- },
164
- onRender: (fn) => {
165
- addHandler(com.id, 0 /* event.render */, fn);
166
- return com;
167
- },
168
- onAppend: (fn) => {
169
- addHandler(com.id, 1 /* event.append */, fn);
170
- return com;
171
- },
172
- send: (...msg) => {
173
- runHandlers(com.id, 2 /* event.msg */, ...msg);
174
- return com;
175
- },
176
- onMessage: (fn) => {
177
- addHandler(com.id, 2 /* event.msg */, fn);
178
- return com;
179
- },
180
- };
181
- Object.defineProperty(com, "attributes", {
182
- get() {
183
- return el.attributes;
184
- },
185
- });
186
- Object.defineProperty(com, "value", {
187
- get() {
188
- if (valueProp) {
189
- //@ts-ignore
190
- return el.value;
191
- }
192
- return el.innerHTML;
193
- },
194
- set(v) {
195
- if (valueProp) {
196
- //@ts-ignore
197
- el.value = v;
198
- return true;
199
- }
200
- priv.childs.forEach((c) => c._setParent(null));
201
- priv.childs.length = 0;
202
- el.innerHTML = v;
203
- return true;
204
- },
205
- });
206
- Object.defineProperty(com, "classList", {
207
- get() {
208
- //@ts-ignore
209
- return Array.from(el.classList);
210
- },
211
- });
212
- if (typeof opt.value !== "undefined")
213
- com.setValue(opt.value);
214
- if (classes.length) {
215
- classes.forEach((c) => {
216
- com.addClass(c);
217
- });
218
- }
219
- return com;
220
- };
221
- exports.tkeron = tkeron;
222
- exports.tkeron.css = (name, cssText) => {
223
- const selctr = `style[name=${name}]`;
224
- const cstyle = document.head.querySelector(selctr);
225
- if (cstyle) {
226
- cstyle.innerHTML = cssText;
227
- return;
228
- }
229
- const style = document.createElement("style");
230
- style.setAttribute("name", name);
231
- style.innerHTML = cssText;
232
- //@ts-ignore
233
- style.selctr = selctr;
234
- document.head.appendChild(style);
235
- };
236
- exports.tkeron.script = (anonFunc, options) => {
237
- const { appendIn, runOnDOMContentLoaded, globalFunctions } = options || {};
238
- let value = `(${anonFunc.toString()})();`;
239
- if (runOnDOMContentLoaded)
240
- value = `document.addEventListener("DOMContentLoaded",(${anonFunc.toString()}));`;
241
- if (globalFunctions) {
242
- let functions = "";
243
- for (const func of globalFunctions) {
244
- const name = func.name;
245
- functions += `
246
- const ${name} = ${func.toString()};
247
- window.${name} = ${name};
248
- `;
249
- }
250
- value = `${functions} ${value}`;
251
- }
252
- value = `/*tkeron-front-script ${value} tkeron-front-script*/`;
253
- (0, exports.tkeron)({ type: "script", value }).appendIn(appendIn || "body");
254
- };
255
- exports.version = "1.9.0";
@@ -1,319 +0,0 @@
1
- export interface tkeronOptions {
2
- type?: string;
3
- value?: string;
4
- }
5
-
6
- export interface Component {
7
- readonly id: string;
8
-
9
- value: string;
10
- setValue(v: string): Component;
11
-
12
- readonly parent: Component | null;
13
- _setParent(c: Component | null): Component;
14
- childs: Component[];
15
- add(...c: Component[]): Component;
16
- remove(c: Component): Component;
17
-
18
- renderIn(query: string | HTMLElement): Component;
19
- appendIn(query: string | HTMLElement): Component;
20
-
21
- getHTMLElement(f: (el: HTMLElement) => void): Component;
22
- getHTML(): string;
23
-
24
- readonly classList: string[];
25
- addClass(cls: string): Component;
26
- removeClass(cls: string): Component;
27
-
28
- readonly attributes: any;
29
- addAttribute(attr: string, value: string): Component;
30
- removeAttribute(attr: string): Component;
31
-
32
- getElement(): HTMLElement;
33
- setElement(el: HTMLElement): Component;
34
-
35
- css(css: string): Component;
36
- getCss(): string;
37
-
38
- on(f: (_com: Component, _el: HTMLElement) => void): Component;
39
-
40
- onRender(fn: () => void): Component;
41
- onAppend(fn: () => void): Component;
42
-
43
- send(...msg: any[]): Component;
44
- onMessage(fn: (...msg: any[]) => void): Component;
45
- }
46
-
47
- const enum event {
48
- render,
49
- append,
50
- msg,
51
- }
52
- const handlers: any = {};
53
- const addHandler = (id: string, event: event, fn: (...args: any[]) => void) => {
54
- if (typeof handlers[event] === "undefined") handlers[event] = {} as any;
55
- if (typeof handlers[event][id] === "undefined")
56
- handlers[event][id] = [] as any[];
57
- handlers[event][id].push(fn);
58
- };
59
- const runHandlers = (id: string, event: event, ...args: any[]) => {
60
- if (typeof handlers[event] === "undefined") return;
61
- if (typeof handlers[event][id] === "undefined") return;
62
- handlers[event][id].forEach((fn: (...args: any[]) => void) => fn(...args));
63
- };
64
-
65
- const toHexString = (bytes: Uint8Array): string =>
66
- bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
67
- const rnds = (n: number): string =>
68
- toHexString(crypto.getRandomValues(new Uint8Array(n))).slice(0, n);
69
-
70
- const IDs: string[] = [];
71
- const getID = (): string => {
72
- const id = "tk_" + rnds(20);
73
- //@ts-ignore
74
- if (IDs.includes(id)) {
75
- return getID();
76
- }
77
- return id;
78
- };
79
-
80
- export const tkeron = (
81
- opt?: tkeronOptions | string,
82
- ...classes: string[]
83
- ): Component => {
84
- if (typeof opt === "string") {
85
- classes.push(opt);
86
- opt = { type: "div" };
87
- }
88
- if (!opt) opt = { type: "div" };
89
- const valueProp: boolean = ["input", "textarea"].some((n) => {
90
- if ("type" in (opt as any)) return (opt as any).type === n;
91
- });
92
- let el = document.createElement(opt.type || "div");
93
- const priv = {
94
- parent: {} as Component | null,
95
- childs: [] as Component[],
96
- css: "",
97
- };
98
- priv.parent = null;
99
- const com: Component = {
100
- id: getID(),
101
- parent: priv.parent,
102
- _setParent: (c: Component | null) => {
103
- priv.parent = c;
104
- return com;
105
- },
106
- value: "",
107
- setValue: (v: string) => {
108
- com.value = v;
109
- return com;
110
- },
111
- childs: priv.childs,
112
- add: (...c: Component[]) => {
113
- c.forEach((_) => {
114
- _._setParent(com);
115
- priv.childs.push(_);
116
- _.getHTMLElement((ele) => {
117
- el.appendChild(ele);
118
- });
119
- });
120
- return com;
121
- },
122
- remove: (c: Component) => {
123
- c._setParent(null);
124
- priv.childs = priv.childs.filter((_c) => {
125
- if (_c.id === c.id) return false;
126
- return true;
127
- });
128
- c.getHTMLElement((_el) => {
129
- el.removeChild(_el);
130
- });
131
- return com;
132
- },
133
- renderIn: (query: string | HTMLElement) => {
134
- if (typeof query === "string") {
135
- const domel = document.querySelector(query);
136
- if (!domel) {
137
- document.addEventListener("DOMContentLoaded", () => {
138
- com.renderIn(query);
139
- });
140
- return com;
141
- }
142
- domel.innerHTML = "";
143
- domel.appendChild(el);
144
- runHandlers(com.id, event.render);
145
- return com;
146
- }
147
- (query as HTMLElement).innerHTML = "";
148
- (query as HTMLElement).appendChild(el);
149
- runHandlers(com.id, event.render);
150
- return com;
151
- },
152
- appendIn: (query: string | HTMLElement) => {
153
- if (typeof query === "string") {
154
- const domel = document.querySelector(query);
155
- if (!domel) {
156
- document.addEventListener("DOMContentLoaded", () => {
157
- com.appendIn(query);
158
- });
159
- return com;
160
- }
161
- domel.appendChild(el);
162
- runHandlers(com.id, event.append);
163
- return com;
164
- }
165
- (query as HTMLElement).appendChild(el);
166
- runHandlers(com.id, event.append);
167
- return com;
168
- },
169
- getHTMLElement: (f) => {
170
- f(el);
171
- return com;
172
- },
173
- getHTML: () => {
174
- return el.outerHTML;
175
- },
176
- classList: [],
177
- addClass: (cls: string) => {
178
- el.classList.add(cls);
179
- return com;
180
- },
181
- removeClass: (cls: string) => {
182
- el.classList.remove(cls);
183
- return com;
184
- },
185
- attributes: {},
186
- addAttribute: (attr: string, value: string) => {
187
- el.setAttribute(attr, value);
188
- return com;
189
- },
190
- removeAttribute: (attr: string) => {
191
- el.removeAttribute(attr);
192
- return com;
193
- },
194
- getElement: () => {
195
- return el;
196
- },
197
- setElement: (ele: HTMLElement) => {
198
- if (!ele) throw Error("Component.setElement: Element must not be null.");
199
- el = ele;
200
- return com;
201
- },
202
- css: (css: string) => {
203
- priv.css = css;
204
- tkeron.css(com.id, `#${com.id} {${css}}`);
205
- com.addAttribute("id", com.id);
206
- return com;
207
- },
208
- getCss: () => priv.css,
209
- on: (f) => {
210
- f(com, el);
211
- return com;
212
- },
213
- onRender: (fn) => {
214
- addHandler(com.id, event.render, fn);
215
- return com;
216
- },
217
- onAppend: (fn) => {
218
- addHandler(com.id, event.append, fn);
219
- return com;
220
- },
221
- send: (...msg: any[]) => {
222
- runHandlers(com.id, event.msg, ...msg);
223
- return com;
224
- },
225
- onMessage: (fn) => {
226
- addHandler(com.id, event.msg, fn);
227
- return com;
228
- },
229
- };
230
-
231
- Object.defineProperty(com, "attributes", {
232
- get() {
233
- return el.attributes;
234
- },
235
- });
236
- Object.defineProperty(com, "value", {
237
- get() {
238
- if (valueProp) {
239
- //@ts-ignore
240
- return el.value;
241
- }
242
- return el.innerHTML;
243
- },
244
- set(v: string) {
245
- if (valueProp) {
246
- //@ts-ignore
247
- el.value = v;
248
- return true;
249
- }
250
- priv.childs.forEach((c) => c._setParent(null));
251
- priv.childs.length = 0;
252
- el.innerHTML = v;
253
- return true;
254
- },
255
- });
256
- Object.defineProperty(com, "classList", {
257
- get() {
258
- //@ts-ignore
259
- return Array.from(el.classList);
260
- },
261
- });
262
-
263
- if (typeof opt.value !== "undefined") com.setValue(opt.value);
264
-
265
- if (classes.length) {
266
- classes.forEach((c) => {
267
- com.addClass(c);
268
- });
269
- }
270
-
271
- return com;
272
- };
273
-
274
- tkeron.css = (name: string, cssText: string) => {
275
- const selctr = `style[name=${name}]`;
276
- const cstyle = document.head.querySelector(selctr);
277
- if (cstyle) {
278
- cstyle.innerHTML = cssText;
279
- return;
280
- }
281
- const style = document.createElement("style");
282
- style.setAttribute("name", name);
283
- style.innerHTML = cssText;
284
- //@ts-ignore
285
- style.selctr = selctr;
286
-
287
- document.head.appendChild(style);
288
- };
289
-
290
- export interface scriptOptions {
291
- appendIn?: string;
292
- runOnDOMContentLoaded?: boolean;
293
- globalFunctions?: CallableFunction[];
294
- }
295
- tkeron.script = (anonFunc: CallableFunction, options?: scriptOptions) => {
296
- const { appendIn, runOnDOMContentLoaded, globalFunctions } =
297
- options || ({} as scriptOptions);
298
- let value = `(${anonFunc.toString()})();`;
299
- if (runOnDOMContentLoaded)
300
- value = `document.addEventListener("DOMContentLoaded",(${anonFunc.toString()}));`;
301
-
302
- if (globalFunctions) {
303
- let functions = "";
304
- for (const func of globalFunctions) {
305
- const name = func.name;
306
- functions += `
307
- const ${name} = ${func.toString()};
308
- window.${name} = ${name};
309
- `;
310
- }
311
- value = `${functions} ${value}`;
312
- }
313
-
314
- value = `/*tkeron-front-script ${value} tkeron-front-script*/`;
315
-
316
- tkeron({ type: "script", value }).appendIn(appendIn || "body");
317
- };
318
-
319
- export const version = "1.9.0";