@razaman2/reactive-view 0.0.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/dist/index.js ADDED
@@ -0,0 +1,536 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __getProtoOf = Object.getPrototypeOf;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols)
18
+ for (var prop of __getOwnPropSymbols(b)) {
19
+ if (__propIsEnum.call(b, prop))
20
+ __defNormalProp(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __objRest = (source, exclude) => {
26
+ var target = {};
27
+ for (var prop in source)
28
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
29
+ target[prop] = source[prop];
30
+ if (source != null && __getOwnPropSymbols)
31
+ for (var prop of __getOwnPropSymbols(source)) {
32
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
33
+ target[prop] = source[prop];
34
+ }
35
+ return target;
36
+ };
37
+ var __export = (target, all) => {
38
+ for (var name2 in all)
39
+ __defProp(target, name2, { get: all[name2], enumerable: true });
40
+ };
41
+ var __copyProps = (to, from, except, desc) => {
42
+ if (from && typeof from === "object" || typeof from === "function") {
43
+ for (let key of __getOwnPropNames(from))
44
+ if (!__hasOwnProp.call(to, key) && key !== except)
45
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
46
+ }
47
+ return to;
48
+ };
49
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
50
+ // If the importer is in node compatibility mode or this is not an ESM
51
+ // file that has been converted to a CommonJS file using a Babel-
52
+ // compatible transform (i.e. "__esModule" has not been set), then set
53
+ // "default" to the CommonJS "module.exports" for node compatibility.
54
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
55
+ mod
56
+ ));
57
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
58
+ var __async = (__this, __arguments, generator) => {
59
+ return new Promise((resolve, reject) => {
60
+ var fulfilled = (value) => {
61
+ try {
62
+ step(generator.next(value));
63
+ } catch (e) {
64
+ reject(e);
65
+ }
66
+ };
67
+ var rejected = (value) => {
68
+ try {
69
+ step(generator.throw(value));
70
+ } catch (e) {
71
+ reject(e);
72
+ }
73
+ };
74
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
75
+ step((generator = generator.apply(__this, __arguments)).next());
76
+ });
77
+ };
78
+
79
+ // src/index.ts
80
+ var src_exports = {};
81
+ __export(src_exports, {
82
+ AsyncComponent: () => AsyncComponent_default,
83
+ access: () => access,
84
+ default: () => src_default,
85
+ expose: () => expose,
86
+ getDate: () => getDate,
87
+ getProps: () => getProps,
88
+ getRef: () => getRef,
89
+ getRoot: () => getRoot,
90
+ getSubscription: () => getSubscription,
91
+ safeRequest: () => safeRequest,
92
+ setup: () => setup,
93
+ ucf: () => ucf,
94
+ ucfat: () => ucfat
95
+ });
96
+ module.exports = __toCommonJS(src_exports);
97
+ var import_date_fns_tz = require("date-fns-tz");
98
+
99
+ // src/Subscription.ts
100
+ var Subscription = class _Subscription {
101
+ constructor() {
102
+ this.subscriptions = [];
103
+ this.data = {};
104
+ }
105
+ static create() {
106
+ return new _Subscription();
107
+ }
108
+ subscribe(name2, handler, data) {
109
+ if (this.isNameAvailable(name2)) {
110
+ this.subscriptions.push({
111
+ name: name2,
112
+ handler
113
+ });
114
+ this.data[name2] = data;
115
+ }
116
+ return this;
117
+ }
118
+ replace(name2, handler, data) {
119
+ this.unsubscribe(name2);
120
+ return this.subscribe(name2, handler, data);
121
+ }
122
+ unsubscribe(subscriptions) {
123
+ if (!Array.isArray(subscriptions)) {
124
+ subscriptions = subscriptions ? [subscriptions] : [];
125
+ }
126
+ if (subscriptions.length) {
127
+ subscriptions.forEach((name2) => {
128
+ const subscription = this.find(name2);
129
+ if (subscription) {
130
+ subscription.handler();
131
+ this.remove(subscription);
132
+ console.log(`%cUnsubscribed From Subscription (${name2})`, "background-color: yellow; color: green; font-weight: bold; padding: 3px;");
133
+ }
134
+ });
135
+ } else {
136
+ this.subscriptions.forEach((subscription) => {
137
+ subscription.handler();
138
+ console.log(`%cUnsubscribed From Subscription (${subscription.name})`, "background-color: yellow; color: red; font-weight: bold; padding: 3px;");
139
+ });
140
+ this.subscriptions = [];
141
+ }
142
+ return this;
143
+ }
144
+ size() {
145
+ return this.subscriptions.length;
146
+ }
147
+ hasSubscription(name2) {
148
+ return Boolean(this.find(name2));
149
+ }
150
+ remove(subscription) {
151
+ this.subscriptions.splice(this.subscriptions.indexOf(subscription), 1);
152
+ }
153
+ find(name2) {
154
+ return this.subscriptions.find((subscription) => {
155
+ return subscription.name === name2;
156
+ });
157
+ }
158
+ isNameAvailable(name2) {
159
+ if (this.hasSubscription(name2)) {
160
+ throw new Error(`There is already a subscription called "${name2}".`);
161
+ } else {
162
+ return true;
163
+ }
164
+ }
165
+ registrations() {
166
+ return this.subscriptions;
167
+ }
168
+ get(name2) {
169
+ const subscription = this.find(name2);
170
+ if (subscription) {
171
+ return subscription;
172
+ } else {
173
+ throw new Error(`Subscription "${name2}" doesn't exist!`);
174
+ }
175
+ }
176
+ };
177
+
178
+ // src/Subscriptions.ts
179
+ var _Subscriptions = class _Subscriptions extends Subscription {
180
+ static get() {
181
+ return this.subscriptions;
182
+ }
183
+ };
184
+ _Subscriptions.subscriptions = _Subscriptions.create();
185
+ var Subscriptions = _Subscriptions;
186
+
187
+ // src/ReactiveView.ts
188
+ var import_object_manager = __toESM(require("@razaman2/object-manager"));
189
+ var import_data_manager = __toESM(require("@razaman2/data-manager"));
190
+ var import_vue = require("vue");
191
+
192
+ // package.json
193
+ var name = "@razaman2/reactive-view";
194
+ var version = "0.0.1";
195
+
196
+ // src/ReactiveView.ts
197
+ var import_uuid = require("uuid");
198
+ var props = {
199
+ defaultData: {},
200
+ getDefaultData: {
201
+ type: Function,
202
+ default: (data = {}) => data
203
+ },
204
+ logging: {
205
+ validator: (logging) => {
206
+ return typeof logging === "boolean";
207
+ }
208
+ },
209
+ model: {},
210
+ modelName: {
211
+ type: String,
212
+ default: "ReactiveView"
213
+ },
214
+ notifications: { type: Object },
215
+ root: { type: Function },
216
+ state: {},
217
+ subscriptions: {
218
+ type: Object,
219
+ default: getSubscription()
220
+ }
221
+ };
222
+ var setup = {
223
+ setup: {
224
+ type: Function,
225
+ default: (param1 = {}, param2 = {}) => {
226
+ return Object.assign(param1, param2);
227
+ }
228
+ }
229
+ };
230
+ var ReactiveView_default = {
231
+ props: __spreadValues(__spreadValues({}, setup), props),
232
+ setup(props2, context) {
233
+ var _a, _b, _c, _d;
234
+ const template = (vue, options2) => {
235
+ var _a2, _b2, _c2;
236
+ const vnode = context.slots.default ? (0, import_vue.h)(
237
+ "div",
238
+ context.attrs,
239
+ context.slots.default({ vue, options: options2, props: props2, context })
240
+ ) : (0, import_vue.h)("div", __spreadValues({
241
+ style: {
242
+ color: "red",
243
+ textAlign: "center"
244
+ }
245
+ }, context.attrs), `${props2.modelName}: ${name}@${version}`);
246
+ return (_c2 = (_b2 = (_a2 = context.slots).template) == null ? void 0 : _b2.call(_a2, { vue, vnode })) != null ? _c2 : vnode;
247
+ };
248
+ const isValid = (0, import_vue.ref)(false);
249
+ const defaultData = props2.getDefaultData(
250
+ Array.isArray(props2.state) ? (_a = props2.defaultData) != null ? _a : [] : (_b = props2.defaultData) != null ? _b : {}
251
+ );
252
+ const defaultType = Array.isArray(defaultData) ? [] : {};
253
+ const stateRef = (0, import_vue.ref)(import_data_manager.default.transform((_c = props2.state) != null ? _c : defaultType));
254
+ const config = {
255
+ defaultData,
256
+ data: stateRef.value,
257
+ name: props2.modelName,
258
+ logging: props2.logging,
259
+ notifications: props2.notifications
260
+ };
261
+ const getState = props2.model ? typeof props2.model === "function" ? props2.model(config) : props2.model : new import_data_manager.default(config);
262
+ const options = {
263
+ parent: { self: props2 },
264
+ self: { template, isValid, getState, stateRef }
265
+ };
266
+ const setup2 = (_d = props2.setup(options)) != null ? _d : options;
267
+ const _e = setup2, { parent = {}, self = {} } = _e, rest = __objRest(_e, ["parent", "self"]);
268
+ let sync = false;
269
+ if (context.attrs["onUpdate:modelState"]) {
270
+ const config2 = typeof context.attrs["onUpdate:modelState"] === "function" ? { callback: context.attrs["onUpdate:modelState"] } : context.attrs["onUpdate:modelState"];
271
+ const subscriptionName = `
272
+ ${props2.modelName}
273
+ onUpdate:modelState
274
+ ${(0, import_uuid.v4)()}`;
275
+ const subscription = (0, import_vue.watch)(() => import_object_manager.default.on(stateRef.value).clone(), (after, before) => {
276
+ var _a2;
277
+ const transform = (_a2 = config2.transform) != null ? _a2 : access(setup2).$transform;
278
+ const diff = {
279
+ before: (before == null ? void 0 : before.hasOwnProperty("")) ? before[""] : before,
280
+ after: (after == null ? void 0 : after.hasOwnProperty("")) ? after[""] : after
281
+ };
282
+ if (sync) {
283
+ sync = false;
284
+ } else {
285
+ config2.callback(transform ? transform(diff) : diff, getState);
286
+ }
287
+ }, config2.options);
288
+ props2.subscriptions.addSubscription(subscriptionName, subscription);
289
+ (0, import_vue.onBeforeUnmount)(() => props2.subscriptions.removeSubscription(subscriptionName, false));
290
+ }
291
+ if (context.attrs["onUpdate:propsState"]) {
292
+ const config2 = typeof context.attrs["onUpdate:propsState"] === "function" ? { callback: context.attrs["onUpdate:propsState"] } : context.attrs["onUpdate:propsState"];
293
+ const subscriptionName = `
294
+ ${props2.modelName}
295
+ onUpdate:propsState
296
+ ${(0, import_uuid.v4)()}`;
297
+ const subscription = (0, import_vue.watch)(() => props2.state, (after, before) => {
298
+ config2.callback({ before, after }, getState);
299
+ sync = true;
300
+ }, config2.options);
301
+ props2.subscriptions.addSubscription(subscriptionName, subscription);
302
+ (0, import_vue.onBeforeUnmount)(() => props2.subscriptions.removeSubscription(subscriptionName, false));
303
+ }
304
+ return ($vue) => {
305
+ const setup3 = { $vue, options: parent };
306
+ while (setup3.options) {
307
+ Object.defineProperties(setup3.$vue, Object.assign({
308
+ access: {
309
+ configurable: true,
310
+ value: () => {
311
+ return access({ parent, self });
312
+ }
313
+ }
314
+ }, rest));
315
+ setup3.options = setup3.options.parent;
316
+ setup3.$vue = setup3.$vue.$parent;
317
+ }
318
+ return access($vue).template($vue, { parent, self });
319
+ };
320
+ }
321
+ };
322
+
323
+ // src/AsyncComponent.ts
324
+ var import_vue2 = require("vue");
325
+ var AsyncComponent_default = {
326
+ props: __spreadProps(__spreadValues({}, setup), {
327
+ resolve: {
328
+ required: true
329
+ },
330
+ delay: {
331
+ type: Number,
332
+ default: 200
333
+ },
334
+ timeout: {
335
+ type: Number,
336
+ default: 2e3
337
+ },
338
+ loading: {
339
+ default: (0, import_vue2.h)("div")
340
+ },
341
+ options: {
342
+ type: Object
343
+ },
344
+ error: {
345
+ required: false
346
+ }
347
+ }),
348
+ setup() {
349
+ return ($vue) => {
350
+ return (0, import_vue2.h)(
351
+ src_default,
352
+ {
353
+ setup: (parent) => {
354
+ const componentRef = (0, import_vue2.ref)($vue.loading);
355
+ const template = () => {
356
+ var _a, _b, _c;
357
+ const vnode = componentRef.value;
358
+ return (_c = (_b = (_a = $vue.$slots).template) == null ? void 0 : _b.call(_a, { $vue, vnode })) != null ? _c : vnode;
359
+ };
360
+ const self = { template };
361
+ (0, import_vue2.onMounted)(() => __async(this, null, function* () {
362
+ try {
363
+ if ($vue.resolve) {
364
+ const target = yield $vue.resolve;
365
+ target.hasOwnProperty("property") ? (0, import_vue2.watch)((0, import_vue2.isRef)(target.property) || (0, import_vue2.isReactive)(target.property) ? target.property : () => __async(this, null, function* () {
366
+ return (yield $vue.resolve).property;
367
+ }), (after, before) => {
368
+ setTimeout(() => __async(this, null, function* () {
369
+ return componentRef.value = yield target.onChange({ before: yield before, after: yield after });
370
+ }), $vue.delay);
371
+ }, $vue.options) : setTimeout(() => __async(this, null, function* () {
372
+ return componentRef.value = target;
373
+ }), $vue.delay);
374
+ }
375
+ } catch (error) {
376
+ if ($vue.error) {
377
+ componentRef.value = (0, import_vue2.h)(yield $vue.error, { error });
378
+ }
379
+ }
380
+ }));
381
+ return { parent, self };
382
+ }
383
+ }
384
+ );
385
+ };
386
+ }
387
+ };
388
+
389
+ // src/index.ts
390
+ var src_default = ReactiveView_default;
391
+ function ucf(string) {
392
+ return string.split(/\s+/).map((string2) => {
393
+ return string2.toLowerCase().replace(/^(\w)/, ($1) => $1.toUpperCase());
394
+ }).join(" ");
395
+ }
396
+ function ucfat(string, tokens) {
397
+ const pattern = RegExp(`(?<=(?:${tokens.join("|")}))(\\w)(\\w*)`, "g");
398
+ return string.replace(pattern, ($1, $2, $3) => `${$2.toUpperCase()}${$3.toLowerCase()}`);
399
+ }
400
+ function safeRequest(request) {
401
+ return new Promise((resolve) => __async(this, null, function* () {
402
+ var _a, _b, _c, _d;
403
+ const { message } = (_a = request.loading) != null ? _a : {};
404
+ if (request.loading) {
405
+ request.loading.status = true;
406
+ if (request.message) {
407
+ request.loading.message = request.message;
408
+ }
409
+ }
410
+ try {
411
+ resolve(yield request.try());
412
+ } catch (e) {
413
+ if ((_b = request.alternative) != null ? _b : true) {
414
+ resolve(request.catch ? yield request.catch(e) : console.log(e));
415
+ }
416
+ } finally {
417
+ yield (_c = request.finally) == null ? void 0 : _c.call(request);
418
+ if (request.loading) {
419
+ request.loading.status = false;
420
+ }
421
+ if (request.loading && message) {
422
+ request.loading.message = message;
423
+ }
424
+ yield (_d = request.complete) == null ? void 0 : _d.call(request);
425
+ }
426
+ }));
427
+ }
428
+ function getRoot(vue) {
429
+ var _a, _b, _c;
430
+ return (_c = (_a = typeof vue.$attrs.root === "function" ? vue.$attrs.root() : vue.$attrs.root) != null ? _a : vue.$root) != null ? _c : (_b = vue.$parent) == null ? void 0 : _b.$root;
431
+ }
432
+ function getProps(props2, param2) {
433
+ var _a;
434
+ const exclude = Array.isArray(param2) || typeof param2 === "string" ? param2 : param2.exclude;
435
+ const exclusions = (Array.isArray(exclude) ? exclude : [exclude]).join("|");
436
+ const include = (_a = param2.include) != null ? _a : {};
437
+ return Object.entries(include).reduce((props3, [key, val]) => {
438
+ props3[key] = val;
439
+ return props3;
440
+ }, Object.entries(props2).reduce((props3, [key, val]) => {
441
+ if (!RegExp(`(^|\\|)${key}($|\\|)`, "i").test(exclusions)) {
442
+ props3[key] = val;
443
+ }
444
+ return props3;
445
+ }, {}));
446
+ }
447
+ function getDate(param1, param2) {
448
+ var _a, _b;
449
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
450
+ const format = "MM/dd/yyyy h:mm a";
451
+ const options = typeof param2 === "string" ? { format: param2, timezone } : { format: (_a = param2 == null ? void 0 : param2.format) != null ? _a : format, timezone: (_b = param2 == null ? void 0 : param2.timezone) != null ? _b : timezone };
452
+ const datetime = () => {
453
+ try {
454
+ return param1 instanceof Date ? param1 : param1.toDate();
455
+ } catch (e) {
456
+ return /* @__PURE__ */ new Date();
457
+ }
458
+ };
459
+ return (0, import_date_fns_tz.formatInTimeZone)(datetime(), options.timezone, options.format);
460
+ }
461
+ function getRef(component, ref3) {
462
+ safeRequest({
463
+ try: () => __async(this, null, function* () {
464
+ return component.$props.ref.value = component;
465
+ }),
466
+ alternative: false
467
+ });
468
+ return ref3.value = component;
469
+ }
470
+ function expose(vue, props2) {
471
+ Object.entries(Object.getOwnPropertyDescriptors(props2)).forEach(([key, val]) => {
472
+ Object.defineProperty(vue, key, val);
473
+ });
474
+ return props2;
475
+ }
476
+ function getSubscription() {
477
+ const subscriptions = [];
478
+ const subscription = Subscriptions.get();
479
+ return {
480
+ addSubscription(name2, handler = () => false, data) {
481
+ subscription.subscribe(name2, handler, data);
482
+ subscriptions.push(() => subscription.unsubscribe(name2));
483
+ },
484
+ replaceSubscription(name2, handler = () => false, data) {
485
+ subscription.replace(name2, handler, data);
486
+ subscriptions.push(() => subscription.unsubscribe(name2));
487
+ },
488
+ removeSubscriptions() {
489
+ subscriptions.forEach((subscription2) => safeRequest({
490
+ try: () => subscription2()
491
+ }));
492
+ },
493
+ removeSubscription(name2) {
494
+ subscription.unsubscribe(name2);
495
+ },
496
+ hasSubscription(name2) {
497
+ return subscription.hasSubscription(name2);
498
+ },
499
+ subscriptions,
500
+ subscription
501
+ };
502
+ }
503
+ function access($vue = {}) {
504
+ var _a, _b;
505
+ const target = typeof $vue.access === "function" ? $vue.access() : (_b = (_a = $vue.value && typeof $vue.value.access === "function" ? $vue.value.access() : $vue.value) != null ? _a : $vue) != null ? _b : {};
506
+ return new Proxy(target, {
507
+ get(target2, key) {
508
+ var _a2, _b2;
509
+ const component = { $vue: target2 };
510
+ do {
511
+ if ((_a2 = component.$vue) == null ? void 0 : _a2[key]) {
512
+ return component.$vue[key];
513
+ } else if ((_b2 = component.$vue.self) == null ? void 0 : _b2[key]) {
514
+ return component.$vue.self[key];
515
+ } else {
516
+ component.$vue = component.$vue.parent;
517
+ }
518
+ } while (component.$vue);
519
+ }
520
+ });
521
+ }
522
+ // Annotate the CommonJS export names for ESM import in node:
523
+ 0 && (module.exports = {
524
+ AsyncComponent,
525
+ access,
526
+ expose,
527
+ getDate,
528
+ getProps,
529
+ getRef,
530
+ getRoot,
531
+ getSubscription,
532
+ safeRequest,
533
+ setup,
534
+ ucf,
535
+ ucfat
536
+ });