elegance-js 1.9.9 → 1.9.11

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/dist/index.d.ts CHANGED
@@ -0,0 +1,3 @@
1
+ export * from "./server/state";
2
+ export * from "./server/loadHook";
3
+ export * from "./server/observe";
package/dist/index.mjs CHANGED
@@ -0,0 +1,213 @@
1
+ // src/internal/deprecate.ts
2
+ var ShowDeprecationWarning = (msg) => {
3
+ console.warn("\x1B[31m", msg, "\x1B[0m");
4
+ console.trace("Stack Trace:");
5
+ };
6
+
7
+ // src/server/loadHook.ts
8
+ var resetLoadHooks = () => globalThis.__SERVER_CURRENT_LOADHOOKS__ = [];
9
+ var getLoadHooks = () => globalThis.__SERVER_CURRENT_LOADHOOKS__;
10
+ var loadHook = (deps, fn, bind) => {
11
+ const stringFn = fn.toString();
12
+ const depsArray = (deps || []).map((dep) => ({
13
+ id: dep.id,
14
+ bind: dep.bind
15
+ }));
16
+ let dependencyString = "[";
17
+ for (const dep of depsArray) {
18
+ dependencyString += `{id:${dep.id}`;
19
+ if (dep.bind) dependencyString += `,bind:${dep.bind}`;
20
+ dependencyString += `},`;
21
+ }
22
+ dependencyString += "]";
23
+ const isAsync = fn.constructor.name === "AsyncFunction";
24
+ const wrapperFn = isAsync ? `async (state) => await (${stringFn})(state, ...state.getAll(${dependencyString}))` : `(state) => (${stringFn})(state, ...state.getAll(${dependencyString}))`;
25
+ globalThis.__SERVER_CURRENT_LOADHOOKS__.push({
26
+ fn: wrapperFn,
27
+ bind: bind || ""
28
+ });
29
+ };
30
+ var createLoadHook = (options) => {
31
+ ShowDeprecationWarning("WARNING: createLoadHook() is a deprecated function. Use loadHook() from elegance-js/loadHook instead.");
32
+ const stringFn = options.fn.toString();
33
+ const deps = (options.deps || []).map((dep) => ({
34
+ id: dep.id,
35
+ bind: dep.bind
36
+ }));
37
+ let dependencyString = "[";
38
+ for (const dep of deps) {
39
+ dependencyString += `{id:${dep.id}`;
40
+ if (dep.bind) dependencyString += `,bind:${dep.bind}`;
41
+ dependencyString += `},`;
42
+ }
43
+ dependencyString += "]";
44
+ const isAsync = options.fn.constructor.name === "AsyncFunction";
45
+ const wrapperFn = isAsync ? `async (state) => await (${stringFn})(state, ...state.getAll(${dependencyString}))` : `(state) => (${stringFn})(state, ...state.getAll(${dependencyString}))`;
46
+ globalThis.__SERVER_CURRENT_LOADHOOKS__.push({
47
+ fn: wrapperFn,
48
+ bind: options.bind || ""
49
+ });
50
+ };
51
+
52
+ // src/server/state.ts
53
+ if (!globalThis.__SERVER_CURRENT_STATE_ID__) {
54
+ globalThis.__SERVER_CURRENT_STATE_ID__ = 0;
55
+ }
56
+ var currentId = globalThis.__SERVER_CURRENT_STATE_ID__;
57
+ var state = (value, options) => {
58
+ const serverStateEntry = {
59
+ id: currentId += 1,
60
+ value,
61
+ type: 1 /* STATE */,
62
+ bind: options?.bind
63
+ };
64
+ globalThis.__SERVER_CURRENT_STATE__.push(serverStateEntry);
65
+ if (Array.isArray(value)) {
66
+ serverStateEntry.reactiveMap = reactiveMap;
67
+ }
68
+ return serverStateEntry;
69
+ };
70
+ var reactiveMap = function(template, deps) {
71
+ const subject = this;
72
+ const dependencies = state(deps || []);
73
+ const templateFn = state(template);
74
+ loadHook(
75
+ [subject, templateFn, dependencies],
76
+ (state2, subject2, templateFn2, dependencies2) => {
77
+ const el = document.querySelector(
78
+ `[map-id="${subject2.id}"]`
79
+ );
80
+ if (!el) throw new Error(`Couldn't find map tag with map-id=${subject2.id}`);
81
+ const parentElement = el.parentElement;
82
+ const nextSibling = el.nextSibling;
83
+ el.remove();
84
+ const value = subject2.value;
85
+ const deps2 = state2.getAll(dependencies2.value.map((dep) => ({ id: dep.id, bind: dep.bind })));
86
+ const attributes = [];
87
+ const currentlyWatched = [];
88
+ const createElements = () => {
89
+ const state3 = pd[client.currentPage].stateManager;
90
+ for (let i = 0; i < value.length; i += 1) {
91
+ const htmlElement = client.renderRecursively(templateFn2.value(value[i], i, ...deps2), attributes);
92
+ htmlElement.setAttribute("map-id", subject2.id.toString());
93
+ const elementKey = ((i - 1) * -1).toString();
94
+ htmlElement.setAttribute("key", elementKey);
95
+ for (const attribute of attributes) {
96
+ let values = {};
97
+ const type = attribute.type;
98
+ switch (type) {
99
+ case 2 /* OBSERVER */: {
100
+ const { field, subjects, updateCallback } = attribute;
101
+ for (const reference of subjects) {
102
+ const subject3 = state3.get(reference.id, reference.bind);
103
+ const updateFunction = (value2) => {
104
+ values[subject3.id] = value2;
105
+ try {
106
+ const newValue = updateCallback(...Object.values(values));
107
+ let attribute2 = field === "class" ? "className" : field;
108
+ htmlElement[attribute2] = newValue;
109
+ } catch (e) {
110
+ console.error(e);
111
+ return;
112
+ }
113
+ };
114
+ updateFunction(subject3.value);
115
+ state3.observe(subject3, updateFunction, elementKey);
116
+ currentlyWatched.push({
117
+ key: elementKey,
118
+ subject: subject3
119
+ });
120
+ }
121
+ break;
122
+ }
123
+ case 1 /* STATE */: {
124
+ const { field, element, subjects, eventListener: eventListener2 } = attribute;
125
+ const lc = field.toLowerCase();
126
+ const state4 = pd[client.currentPage].stateManager;
127
+ const fn = (event) => {
128
+ eventListener2(event, ...subjects);
129
+ };
130
+ console.log(element);
131
+ element[lc] = fn;
132
+ break;
133
+ }
134
+ }
135
+ }
136
+ parentElement.insertBefore(htmlElement, nextSibling);
137
+ attributes.splice(0, attributes.length);
138
+ }
139
+ };
140
+ const removeOldElements = () => {
141
+ const list = Array.from(document.querySelectorAll(`[map-id="${subject2.id}"]`));
142
+ for (const el2 of list) {
143
+ el2.remove();
144
+ }
145
+ const pageData = pd[client.currentPage];
146
+ const state3 = pageData.stateManager;
147
+ for (const watched of currentlyWatched) {
148
+ state3.unobserve(watched.subject, watched.key);
149
+ }
150
+ currentlyWatched.splice(0, currentlyWatched.length);
151
+ };
152
+ createElements();
153
+ const uniqueId = `${Date.now()}`;
154
+ state2.observe(subject2, (value2) => {
155
+ removeOldElements();
156
+ createElements();
157
+ }, uniqueId);
158
+ }
159
+ );
160
+ return globalThis.template({
161
+ "map-id": subject.id
162
+ });
163
+ };
164
+ var eventListener = (dependencies, eventListener2) => {
165
+ const deps = dependencies.map((dep) => ({ id: dep.id, bind: dep.bind }));
166
+ let dependencyString = "[";
167
+ for (const dep of deps) {
168
+ dependencyString += `{id:${dep.id}`;
169
+ if (dep.bind) dependencyString += `,bind:${dep.bind}`;
170
+ dependencyString += `},`;
171
+ }
172
+ dependencyString += "]";
173
+ const value = {
174
+ id: currentId += 1,
175
+ type: 1 /* STATE */,
176
+ value: new Function(
177
+ "state",
178
+ "event",
179
+ `(${eventListener2.toString()})(event, ...state.getAll(${dependencyString}))`
180
+ )
181
+ };
182
+ globalThis.__SERVER_CURRENT_STATE__.push(value);
183
+ return value;
184
+ };
185
+ var initializeState = () => globalThis.__SERVER_CURRENT_STATE__ = [];
186
+ var getState = () => {
187
+ return globalThis.__SERVER_CURRENT_STATE__;
188
+ };
189
+
190
+ // src/server/observe.ts
191
+ var observe = (refs, update) => {
192
+ const returnValue = {
193
+ type: 2 /* OBSERVER */,
194
+ initialValues: refs.map((ref) => ref.value),
195
+ update,
196
+ refs: refs.map((ref) => ({
197
+ id: ref.id,
198
+ bind: ref.bind
199
+ }))
200
+ };
201
+ return returnValue;
202
+ };
203
+ export {
204
+ createLoadHook,
205
+ eventListener,
206
+ getLoadHooks,
207
+ getState,
208
+ initializeState,
209
+ loadHook,
210
+ observe,
211
+ resetLoadHooks,
212
+ state
213
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elegance-js",
3
- "version": "1.9.9",
3
+ "version": "1.9.11",
4
4
  "description": "Web-Framework",
5
5
  "type": "module",
6
6
  "bin": {
@@ -16,6 +16,11 @@
16
16
  "prepare": "npm run build && node ./dist/compile_docs.mjs --environment=production"
17
17
  },
18
18
  "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.mjs",
22
+ "require": "./dist/index.mjs"
23
+ },
19
24
  "./*": {
20
25
  "types": "./dist/*.d.ts",
21
26
  "import": "./dist/*.mjs",
package/scripts/dev.js CHANGED
@@ -26,8 +26,8 @@ compile({
26
26
  child.kill('SIGKILL');
27
27
  }
28
28
 
29
- const { child: childProcess} =exec("npx @tailwindcss/cli -i \"./pages/index.css\" -o \".elegance/dist/index.css\" --cwd \"./pages\" --watch")
29
+ const childProcess = exec("npx @tailwindcss/cli -i \"./pages/index.css\" -o \".elegance/dist/index.css\" --cwd \"./pages\" --watch")
30
30
 
31
31
  child = childProcess;
32
32
  },
33
- })
33
+ })
package/scripts/prod.js CHANGED
@@ -20,8 +20,8 @@ compile({
20
20
  child.kill('SIGKILL');
21
21
  }
22
22
 
23
- const { child: childProcess} = exec("npx @tailwindcss/cli -i \"./pages/index.css\" -o \".elegance/dist/index.css\" --cwd \"./pages\" --minidy=true")
23
+ const childProcess = exec("npx @tailwindcss/cli -i \"./pages/index.css\" -o \".elegance/dist/index.css\" --cwd \"./pages\" --minidy=true")
24
24
 
25
25
  child = childProcess;
26
26
  },
27
- })
27
+ })