@rugal.tu/vuemodel3 1.5.9 → 1.5.10

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.
@@ -0,0 +1,2049 @@
1
+ //#endregion
2
+ //#region FuncBase
3
+ export class FuncBase {
4
+ //#region Protected Property
5
+ $NavigateToFunc;
6
+ $DefaultDateJoinChar;
7
+ //#endregion
8
+ constructor() {
9
+ this.$NavigateToFunc = null;
10
+ this.WithDateTextJoinChar('/');
11
+ }
12
+ //#region Public With Method
13
+ WithNavigateTo(NavigateToFunc) {
14
+ this.$NavigateToFunc = NavigateToFunc;
15
+ return this;
16
+ }
17
+ WithDateTextJoinChar(JoinChar) {
18
+ this.$DefaultDateJoinChar = JoinChar;
19
+ return this;
20
+ }
21
+ //#endregion
22
+ //#region Public Method
23
+ GenerateId() {
24
+ let NewId = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (char) => {
25
+ let RandomValue = crypto.getRandomValues(new Uint8Array(1))[0] & 15;
26
+ let Id = char === 'x' ? RandomValue : (RandomValue & 0x3) | 0x8;
27
+ return Id.toString(16);
28
+ });
29
+ return NewId;
30
+ }
31
+ GenerateIdReplace(FillString) {
32
+ let Id = this.GenerateId().replaceAll('-', FillString);
33
+ return Id;
34
+ }
35
+ GenerateUrl(Url, UrlParam = null) {
36
+ let UrlPaths = this.Paths(Url);
37
+ if (UrlPaths == null || UrlPaths.length == 0 || UrlPaths[0].length == 0)
38
+ this.$Throw('Url can not be null or empty');
39
+ UrlPaths = UrlPaths.map(Item => Item.replace(/\/+$/g, '').replace(/^\/+/g, '/'));
40
+ let CombineUrl = this.ToJoin(UrlPaths, '/');
41
+ if (CombineUrl == null || CombineUrl == '')
42
+ CombineUrl = '/';
43
+ if (UrlParam != null) {
44
+ UrlParam = this.ConvertTo_UrlQuery(UrlParam);
45
+ CombineUrl += `?${UrlParam}`;
46
+ }
47
+ return CombineUrl;
48
+ }
49
+ $BaseNavigateTo(Url) {
50
+ if (this.$NavigateToFunc)
51
+ this.$NavigateToFunc(Url);
52
+ else
53
+ window.location.href = Url;
54
+ }
55
+ NavigateToRoot() {
56
+ let RootUrl = '/';
57
+ this.$BaseNavigateTo(RootUrl);
58
+ return this;
59
+ }
60
+ NavigateTo(Url, UrlParam = null) {
61
+ let TargetUrl = this.GenerateUrl(Url, UrlParam);
62
+ this.$BaseNavigateTo(TargetUrl);
63
+ return this;
64
+ }
65
+ $BaseNavigateBlank(Url) {
66
+ let Link = document.createElement('a');
67
+ Link.href = Url;
68
+ Link.target = '_blank';
69
+ Link.rel = 'noopener noreferrer';
70
+ Link.click();
71
+ }
72
+ NavigateBlank(Url, UrlParam = null) {
73
+ let TargetUrl = this.GenerateUrl(Url, UrlParam);
74
+ this.$BaseNavigateBlank(TargetUrl);
75
+ return this;
76
+ }
77
+ ForEachObject(Param, Func) {
78
+ for (let Key of Object.getOwnPropertyNames(Param)) {
79
+ if (Key.match(/^$/g))
80
+ continue;
81
+ let Value = Param[Key];
82
+ if (Func != null)
83
+ Func.call(this, Key, Value);
84
+ }
85
+ }
86
+ DeepObjectExtend(Target, Source, MaxDepth = 10) {
87
+ if (MaxDepth == 0)
88
+ return {
89
+ ...Target,
90
+ ...Source,
91
+ };
92
+ let AllKeys = Object.keys(Source);
93
+ for (let i = 0; i < AllKeys.length; i++) {
94
+ let Key = AllKeys[i];
95
+ if (!(Key in Target))
96
+ Target[Key] = Source[Key];
97
+ else if (typeof Source[Key] != "object")
98
+ Target[Key] = Source[Key];
99
+ else {
100
+ let NewObject = {
101
+ ...this.DeepObjectExtend(Target[Key], Source[Key], MaxDepth - 1),
102
+ };
103
+ Target[Key] = NewObject;
104
+ }
105
+ }
106
+ return Target;
107
+ }
108
+ ToDateInfo(QueryDate) {
109
+ QueryDate ??= new Date();
110
+ let Info = this.$CreateDateInfo(QueryDate);
111
+ return Info;
112
+ }
113
+ ToDateText(QueryDate, Option) {
114
+ QueryDate = this.$CreateDateInfo(QueryDate);
115
+ Option ??= {};
116
+ if (typeof Option == 'string')
117
+ Option = { Format: Option };
118
+ Option.DateJoinChar ??= this.$DefaultDateJoinChar;
119
+ Option.Format ??= `yyyy${Option.DateJoinChar}MM${Option.DateJoinChar}dd`;
120
+ let Result = Option.Format;
121
+ Result = Result.replaceAll('yyyy', QueryDate.Year.toString().padStart(4, '0'));
122
+ Result = Result.replaceAll('MM', QueryDate.Month.toString().padStart(2, '0'));
123
+ Result = Result.replaceAll('dd', QueryDate.Day.toString().padStart(2, '0'));
124
+ Option.Format = Result;
125
+ return Result;
126
+ }
127
+ ToDateTimeText(QueryDate, Option) {
128
+ Option ??= {};
129
+ if (typeof Option == 'string')
130
+ Option = { Format: Option };
131
+ Option.DateJoinChar ??= this.$DefaultDateJoinChar;
132
+ Option.Format ??= `yyyy${Option.DateJoinChar}MM${Option.DateJoinChar}dd HH:mm:ss`;
133
+ Option = { ...Option };
134
+ QueryDate = this.$CreateDateInfo(QueryDate);
135
+ this.ToDateText(QueryDate, Option);
136
+ let Result = Option.Format;
137
+ Result = Result.replaceAll('HH', QueryDate.Hour.toString().padStart(2, '0'));
138
+ Result = Result.replaceAll('mm', QueryDate.Minute.toString().padStart(2, '0'));
139
+ Result = Result.replaceAll('ss', QueryDate.Second.toString().padStart(2, '0'));
140
+ return Result;
141
+ }
142
+ $CreateDateInfo(DateOrText) {
143
+ DateOrText ??= new Date();
144
+ if (typeof DateOrText === 'string')
145
+ DateOrText = new Date(DateOrText);
146
+ if (DateOrText instanceof Date == false)
147
+ return DateOrText;
148
+ let Result = {
149
+ Date: DateOrText,
150
+ Year: DateOrText.getFullYear(),
151
+ Month: DateOrText.getMonth() + 1,
152
+ Day: DateOrText.getDate(),
153
+ Hour: DateOrText.getHours(),
154
+ Minute: DateOrText.getMinutes(),
155
+ Second: DateOrText.getSeconds(),
156
+ };
157
+ return Result;
158
+ }
159
+ //#endregion
160
+ //#region Process
161
+ ConvertTo_UrlQuery(Param) {
162
+ if (typeof Param === 'string')
163
+ return Param;
164
+ let AllParam = [];
165
+ this.ForEachObject(Param, (Key, Value) => {
166
+ AllParam.push(`${Key}=${Value}`);
167
+ });
168
+ let QueryString = AllParam.join('&');
169
+ return QueryString;
170
+ }
171
+ ClearUrl(ApiUrl) {
172
+ let ClearUrl = ApiUrl.replace(/^\/+|\/+$/g, '');
173
+ return ClearUrl;
174
+ }
175
+ ToJoin(Value, Separator = '.') {
176
+ let ConvertArray = this.Paths(Value);
177
+ let Result = ConvertArray.join(Separator);
178
+ return Result;
179
+ }
180
+ Paths(...Value) {
181
+ if (!Array.isArray(Value))
182
+ return [Value];
183
+ let Result = [];
184
+ for (let Item of Value) {
185
+ if (Item == null)
186
+ continue;
187
+ if (!Array.isArray(Item)) {
188
+ Result.push(Item);
189
+ continue;
190
+ }
191
+ if (Item.length == 0)
192
+ continue;
193
+ Result.push(...this.Paths(...Item));
194
+ }
195
+ return Result;
196
+ }
197
+ IsPathType(CheckPathType) {
198
+ if (Array.isArray(CheckPathType)) {
199
+ for (let Item of CheckPathType) {
200
+ let IsTrue = this.IsPathType(Item);
201
+ if (!IsTrue)
202
+ return false;
203
+ }
204
+ return true;
205
+ }
206
+ else if (typeof (CheckPathType) == 'string') {
207
+ return true;
208
+ }
209
+ return false;
210
+ }
211
+ //#endregion
212
+ //#region Console And Throw
213
+ $Throw(Message) {
214
+ throw new Error(Message);
215
+ }
216
+ $Error(Data) {
217
+ console.error(Data);
218
+ }
219
+ }
220
+ export class QueryNode extends FuncBase {
221
+ NodeId;
222
+ Dom;
223
+ DomName = null;
224
+ Parent = null;
225
+ Children = [];
226
+ //DomChildren:
227
+ ElementDeep = 0;
228
+ NodeDeep = 0;
229
+ constructor(Dom) {
230
+ super();
231
+ this.Dom = Dom;
232
+ this.NodeId = this.GenerateIdReplace('');
233
+ }
234
+ Query(DomName, Option) {
235
+ Option ??= { Mode: 'Multi' };
236
+ return this.$RCS_QueryChildrens(this, DomName, Option);
237
+ }
238
+ QueryCss(Selector, Option) {
239
+ Option ??= { Mode: 'Multi' };
240
+ return this.$RCS_QueryCssChildrens(this, Selector, Option);
241
+ }
242
+ Selector(Selector) {
243
+ return this.Dom.querySelector(Selector);
244
+ }
245
+ SelectorAll(Selector) {
246
+ return this.Dom.querySelectorAll(Selector);
247
+ }
248
+ $RCS_QueryChildrens(TargetNode, DomName, Option) {
249
+ if (DomName == null)
250
+ return null;
251
+ DomName = this.Paths(DomName);
252
+ if (DomName.length == 1)
253
+ DomName = DomName[0];
254
+ let Results = [];
255
+ for (let NodeItem of TargetNode.Children) {
256
+ if (Array.isArray(DomName)) {
257
+ let Names = [...DomName];
258
+ let FirstName = Names.shift();
259
+ const pathRegex = FirstName.match(/^(?<key>[^:]+):(?<name>.+)$/);
260
+ if (pathRegex) {
261
+ const key = pathRegex.groups.key;
262
+ const name = pathRegex.groups.name;
263
+ switch (key) {
264
+ case 'tag':
265
+ //TargetNode.Dom.
266
+ //Todo:
267
+ break;
268
+ default:
269
+ FirstName = name;
270
+ break;
271
+ }
272
+ }
273
+ if (NodeItem.DomName == FirstName) {
274
+ if (Names.length == 1)
275
+ Names = Names[0];
276
+ let FindChildren = this.$RCS_QueryChildrens(NodeItem, Names, Option);
277
+ if (FindChildren != null) {
278
+ Results.push(...FindChildren);
279
+ continue;
280
+ }
281
+ }
282
+ }
283
+ else if (NodeItem.DomName == DomName) {
284
+ Results.push(NodeItem);
285
+ if (Option.Mode == 'Multi')
286
+ continue;
287
+ }
288
+ let ChildrenResult = this.$RCS_QueryChildrens(NodeItem, DomName, Option);
289
+ if (ChildrenResult != null)
290
+ Results.push(...ChildrenResult);
291
+ }
292
+ return Results;
293
+ }
294
+ $RCS_QueryCssChildrens(TargetNode, Selector, Option) {
295
+ let Results = [];
296
+ for (let NodeItem of TargetNode.Children) {
297
+ if (NodeItem.Dom.matches(Selector)) {
298
+ Results.push(NodeItem);
299
+ if (Option.Mode == 'Multi')
300
+ continue;
301
+ }
302
+ let ChildrenResult = this.$RCS_QueryCssChildrens(NodeItem, Selector, Option);
303
+ if (ChildrenResult != null)
304
+ Results.push(...ChildrenResult);
305
+ }
306
+ return Results;
307
+ }
308
+ }
309
+ export class DomQueryer {
310
+ $Root = null;
311
+ $RootNode = null;
312
+ $Nodes = [];
313
+ $QueryDomName = null;
314
+ IsInited = false;
315
+ WithRoot(Filter) {
316
+ this.$Root = document.querySelector(Filter);
317
+ return this;
318
+ }
319
+ WithDomName(QueryDomName) {
320
+ this.$QueryDomName = QueryDomName;
321
+ return this;
322
+ }
323
+ Init(IsReInited = false) {
324
+ if (this.IsInited && !IsReInited)
325
+ return this;
326
+ this.$Root ??= document.body;
327
+ this.$RootNode = new QueryNode(this.$Root);
328
+ this.$RCS_Visit(this.$Root, this.$RootNode, 0);
329
+ this.$Nodes = this.$Nodes.sort((A, B) => A.NodeDeep - B.NodeDeep);
330
+ this.IsInited = true;
331
+ return this;
332
+ }
333
+ Query(DomName, Option) {
334
+ if (!Queryer.IsInited)
335
+ Queryer.Init();
336
+ if (Option == null) {
337
+ Option = {
338
+ Mode: 'Multi',
339
+ };
340
+ }
341
+ else if (Option instanceof QueryNode) {
342
+ Option = {
343
+ Mode: 'Multi',
344
+ TargetNode: Option,
345
+ };
346
+ }
347
+ if (Option.TargetNode == null)
348
+ Option.TargetNode = this.$RootNode;
349
+ return Option.TargetNode.Query(DomName, Option);
350
+ }
351
+ QueryCss(Selector, Option) {
352
+ if (!Queryer.IsInited)
353
+ Queryer.Init();
354
+ if (Option == null) {
355
+ Option = {
356
+ Mode: 'Multi',
357
+ };
358
+ }
359
+ else if (Option instanceof QueryNode) {
360
+ Option = {
361
+ Mode: 'Multi',
362
+ TargetNode: Option,
363
+ };
364
+ }
365
+ if (Option.TargetNode == null)
366
+ Option.TargetNode = this.$RootNode;
367
+ return Option.TargetNode.QueryCss(Selector, Option);
368
+ }
369
+ Using(DomName, UsingFunc, TargetNode) {
370
+ let QueryNodes = this.Query(DomName, {
371
+ Mode: 'Multi',
372
+ TargetNode: TargetNode,
373
+ });
374
+ if (QueryNodes != null && QueryNodes.length > 0) {
375
+ UsingFunc({
376
+ QueryNodes,
377
+ });
378
+ }
379
+ return this;
380
+ }
381
+ $RCS_Visit(DomNode, Parent, ElementDeep) {
382
+ let NextNode = this.$AddNode(DomNode, Parent, ElementDeep);
383
+ NextNode ??= Parent;
384
+ let Children = DomNode.children;
385
+ if (DomNode instanceof HTMLTemplateElement)
386
+ Children = DomNode.content.children;
387
+ if (Children == null || Children.length == 0)
388
+ return;
389
+ for (let i = 0; i < Children.length; i++) {
390
+ let Item = Children[i];
391
+ if (Item instanceof HTMLElement)
392
+ this.$RCS_Visit(Item, NextNode, ElementDeep + 1);
393
+ }
394
+ }
395
+ $AddNode(Dom, Parent, ElementDeep) {
396
+ if (this.$QueryDomName != null && !Dom.matches(`[${this.$QueryDomName}]`))
397
+ return null;
398
+ let DomName = Dom.getAttribute(this.$QueryDomName);
399
+ let NewNode = new QueryNode(Dom);
400
+ NewNode.DomName = DomName;
401
+ NewNode.ElementDeep = ElementDeep;
402
+ this.$Nodes.push(NewNode);
403
+ if (Parent != null) {
404
+ NewNode.Parent = Parent;
405
+ NewNode.NodeDeep = Parent.NodeDeep + 1;
406
+ Parent.Children.push(NewNode);
407
+ }
408
+ return NewNode;
409
+ }
410
+ }
411
+ export var Queryer = new DomQueryer();
412
+ export class FileItem {
413
+ OnChangeBase64;
414
+ OnChangeBuffer;
415
+ $Store;
416
+ constructor(File, ConvertType = 'none') {
417
+ if (File == null)
418
+ this.$Store = reactive({});
419
+ else {
420
+ this.$Store = reactive({
421
+ FileId: new FuncBase().GenerateId(),
422
+ File: File,
423
+ ConvertType: ConvertType,
424
+ Base64: null,
425
+ Buffer: null,
426
+ IsLoaded: false,
427
+ IsLoading: true,
428
+ Error: null,
429
+ });
430
+ this.$ConvertFile();
431
+ }
432
+ }
433
+ get FileId() {
434
+ return this.$Store.FileId;
435
+ }
436
+ set FileId(Value) {
437
+ this.$Store.FileId = Value;
438
+ }
439
+ get File() {
440
+ return this.$Store.File;
441
+ }
442
+ set File(Value) {
443
+ this.$Store.File = Value;
444
+ }
445
+ get ConvertType() {
446
+ return this.$Store.ConvertType;
447
+ }
448
+ set ConvertType(Value) {
449
+ this.$Store.ConvertType = Value;
450
+ }
451
+ get Base64() {
452
+ return this.$Store.Base64;
453
+ }
454
+ set Base64(Value) {
455
+ this.$Store.Base64 = Value;
456
+ this.OnChangeBase64?.call(this, this.$Store.Base64);
457
+ }
458
+ get Buffer() {
459
+ return this.$Store.Buffer;
460
+ }
461
+ set Buffer(Value) {
462
+ this.$Store.Buffer = Value;
463
+ this.OnChangeBuffer?.call(this, this.$Store.Buffer);
464
+ }
465
+ get InnerStore() {
466
+ return this.$Store;
467
+ }
468
+ get IsLoaded() {
469
+ return this.$Store.IsLoaded;
470
+ }
471
+ get IsLoading() {
472
+ return this.$Store.IsLoading;
473
+ }
474
+ get Error() {
475
+ return this.$Store.Error;
476
+ }
477
+ Clear() {
478
+ this.$Store.Base64 = null;
479
+ this.$Store.Buffer = null;
480
+ this.$Store.File = null;
481
+ }
482
+ From(Item) {
483
+ this.$Store = Item.InnerStore;
484
+ }
485
+ CheckLoadAsync() {
486
+ return new Promise((resolve, reject) => {
487
+ try {
488
+ if (this.$Store.IsLoaded || !this.$Store.IsLoading)
489
+ return resolve(this.$Store.IsLoaded);
490
+ const timeout = setTimeout(() => {
491
+ if (timer)
492
+ clearInterval(timer);
493
+ this.$Store.Error = 'timeout';
494
+ console.error("WaitLoadAsync: timeout");
495
+ reject(false);
496
+ }, 60000);
497
+ const timer = setInterval(() => {
498
+ if (this.$Store.IsLoaded || !this.$Store.IsLoading) {
499
+ clearInterval(timer);
500
+ clearTimeout(timeout);
501
+ resolve(this.$Store.IsLoaded);
502
+ }
503
+ }, 100);
504
+ }
505
+ catch (e) {
506
+ this.$Store.Error = e;
507
+ console.error(e);
508
+ reject(false);
509
+ }
510
+ });
511
+ }
512
+ $ConvertFile() {
513
+ if (this.ConvertType == null)
514
+ return;
515
+ let GetConvertType = [];
516
+ if (Array.isArray(this.ConvertType))
517
+ GetConvertType = this.ConvertType;
518
+ else
519
+ GetConvertType = [this.ConvertType];
520
+ for (let i = 0; i < GetConvertType.length; i++) {
521
+ let TypeItem = GetConvertType[i];
522
+ switch (TypeItem) {
523
+ case 'base64':
524
+ this.$Store.IsLoading = true;
525
+ this.$ConvertBase64();
526
+ break;
527
+ case 'buffer':
528
+ this.$Store.IsLoading = true;
529
+ this.$ConvertBuffer();
530
+ break;
531
+ default:
532
+ this.$Store.IsLoaded = true;
533
+ break;
534
+ }
535
+ }
536
+ }
537
+ $ConvertBase64(IsForce = false) {
538
+ if (this.File == null)
539
+ return this;
540
+ if (this.Base64 != null && IsForce == false)
541
+ return this;
542
+ let Reader = new FileReader();
543
+ Reader.readAsDataURL(this.File);
544
+ Reader.onload = () => {
545
+ this.Base64 = Reader.result;
546
+ this.$Store.IsLoaded = true;
547
+ this.$Store.IsLoading = false;
548
+ };
549
+ return this;
550
+ }
551
+ $ConvertBuffer() {
552
+ let Reader = new FileReader();
553
+ Reader.readAsArrayBuffer(this.File);
554
+ Reader.onload = () => {
555
+ this.Buffer = Reader.result;
556
+ this.$Store.IsLoaded = true;
557
+ this.$Store.IsLoading = false;
558
+ };
559
+ }
560
+ }
561
+ //#endregion
562
+ export class ApiStore extends FuncBase {
563
+ //#region Private Property
564
+ #ApiDomain = null;
565
+ #RootRoute = null;
566
+ #AccessToken = null;
567
+ #RefreshToken = null;
568
+ #HeaderFuncs = [];
569
+ #OnEventFunc = {};
570
+ #OnEventName = {
571
+ ApiStore: {
572
+ AddApi: 'AddApi',
573
+ UpdateStore: 'UpdateStore',
574
+ AddStore: 'AddStore',
575
+ SetStore: 'SetStore',
576
+ }
577
+ };
578
+ #OnSuccess;
579
+ #OnError;
580
+ #OnComplete;
581
+ #ExportSuccessStore;
582
+ #Store = {
583
+ FileStore: {},
584
+ };
585
+ #Func_ConvertTo_FormData = [];
586
+ //#endregion
587
+ constructor() {
588
+ super();
589
+ this.SetStore('api', {});
590
+ }
591
+ //#region Get/Set Property
592
+ get ApiDomain() {
593
+ if (this.#ApiDomain == null)
594
+ return null;
595
+ return this.ClearUrl(this.#ApiDomain);
596
+ }
597
+ set ApiDomain(ApiDomain) {
598
+ this.#ApiDomain = this.ClearUrl(ApiDomain);
599
+ }
600
+ get OnEventName() {
601
+ return this.#OnEventName;
602
+ }
603
+ get #EventName() {
604
+ return this.OnEventName.ApiStore;
605
+ }
606
+ get Store() {
607
+ return this.#Store;
608
+ }
609
+ set Store(Store) {
610
+ this.#Store = Store;
611
+ }
612
+ get ApiStore() {
613
+ return this.GetStore('api');
614
+ }
615
+ get FileStore() {
616
+ return this.Store.FileStore;
617
+ }
618
+ //#endregion
619
+ //#region Public With Method
620
+ WithAccessToken(AccessToken) {
621
+ this.#AccessToken = AccessToken;
622
+ return this;
623
+ }
624
+ WithRefreshToken(RefreshToken) {
625
+ this.#RefreshToken = RefreshToken;
626
+ return this;
627
+ }
628
+ WithApiDomain(ApiDomain) {
629
+ this.ApiDomain = ApiDomain;
630
+ return this;
631
+ }
632
+ WithRootRoute(Route) {
633
+ this.#RootRoute = Route;
634
+ return this;
635
+ }
636
+ WithHeader(Func) {
637
+ this.#HeaderFuncs.push(Func);
638
+ return this;
639
+ }
640
+ WithOnSuccess(SuccessFunc) {
641
+ this.#OnSuccess = SuccessFunc;
642
+ return this;
643
+ }
644
+ WithOnError(ErrorFunc) {
645
+ this.#OnError = ErrorFunc;
646
+ return this;
647
+ }
648
+ WithOnComplete(CompleteFunc) {
649
+ this.#OnComplete = CompleteFunc;
650
+ return this;
651
+ }
652
+ WithExportSuccessStore(ExportSuccessStoreFunc) {
653
+ this.#ExportSuccessStore = ExportSuccessStoreFunc;
654
+ return this;
655
+ }
656
+ //#endregion
657
+ //#region ConvertTo Method
658
+ WithConvertTo_FormParam(ConvertToFunc) {
659
+ this.#Func_ConvertTo_FormData.push(ConvertToFunc);
660
+ return this;
661
+ }
662
+ ClearConvertTo_FormParam() {
663
+ this.#Func_ConvertTo_FormData = [];
664
+ return this;
665
+ }
666
+ ConvertTo_ApiUrl(Url, Param = null) {
667
+ let ApiDomainUrl = Url;
668
+ if (this.ApiDomain != null && !ApiDomainUrl.includes('http'))
669
+ ApiDomainUrl = `${this.ApiDomain}/${this.ClearUrl(ApiDomainUrl)}`;
670
+ if (Param != null)
671
+ ApiDomainUrl = `${ApiDomainUrl}?${this.ConvertTo_UrlQuery(Param)}`;
672
+ return ApiDomainUrl;
673
+ }
674
+ //#endregion
675
+ //#region Api Method
676
+ AddApi(AddApi) {
677
+ for (let ApiKey in AddApi) {
678
+ let ApiOption = AddApi[ApiKey];
679
+ let SetApi = {
680
+ ApiKey,
681
+ ...ApiOption,
682
+ };
683
+ this.AddStoreFrom(this.ApiStore, ApiKey, {});
684
+ this.UpdateStoreFrom(this.ApiStore, ApiKey, SetApi);
685
+ this.$EventTrigger(this.#EventName.AddApi, SetApi);
686
+ }
687
+ return this;
688
+ }
689
+ ApiCall(ApiKey, Option = null) {
690
+ this.$BaseApiCall(ApiKey, Option, false);
691
+ return this;
692
+ }
693
+ ApiCall_Form(ApiKey, Option = null) {
694
+ this.$BaseApiCall(ApiKey, Option, true);
695
+ return this;
696
+ }
697
+ $BaseApiCall(ApiKey, Option, IsFormRequest) {
698
+ let Api = this.ApiStore[ApiKey];
699
+ if (Api == null)
700
+ this.$Throw(`Api setting not found of "${ApiKey}"`);
701
+ let ParamQuery = Option?.Query ?? Api.Query;
702
+ let ParamBody = Option?.Body ?? Api.Body;
703
+ let ParamFile = Option?.File ?? Api.File;
704
+ if (typeof (ParamQuery) == 'function')
705
+ ParamQuery = ParamQuery();
706
+ if (typeof (ParamBody) == 'function')
707
+ ParamBody = ParamBody();
708
+ if (typeof (ParamFile) == 'function')
709
+ ParamFile = ParamFile();
710
+ let IsUpdateStore = Option?.IsUpdateStore ?? Api.IsUpdateStore ?? true;
711
+ let Url = this.ConvertTo_ApiUrl(Api.Url, ParamQuery);
712
+ let FetchRequest = this.$GenerateFetchRequest(Api, ParamBody, ParamFile, IsFormRequest);
713
+ this.PubApi(ApiKey, 'IsCalling', true);
714
+ Api.OnCalling?.call(this, FetchRequest);
715
+ Option?.OnCalling?.call(this, FetchRequest);
716
+ fetch(Url, FetchRequest)
717
+ .then(async (ApiResponse) => {
718
+ if (!ApiResponse.ok)
719
+ throw ApiResponse;
720
+ let ConvertResult = await this.$ProcessApiReturn(ApiResponse);
721
+ if (IsUpdateStore) {
722
+ if (Api.Export != false) {
723
+ if (typeof Api.Export === 'function') {
724
+ ConvertResult = Api.Export?.call(this, ConvertResult, ApiResponse);
725
+ }
726
+ else if (this.#ExportSuccessStore != null) {
727
+ ConvertResult = this.#ExportSuccessStore?.call(this, ConvertResult, ApiResponse);
728
+ }
729
+ }
730
+ let StoreKey = Api.ApiKey;
731
+ this.UpdateStore(StoreKey, ConvertResult);
732
+ }
733
+ this.PubApi(ApiKey, 'IsSuccess', true);
734
+ this.PubApi(ApiKey, 'IsError', false);
735
+ Api.OnSuccess?.call(this, ConvertResult, ApiResponse);
736
+ Option?.OnSuccess?.call(this, ConvertResult, ApiResponse);
737
+ this.#OnSuccess?.call(this, ConvertResult, ApiResponse);
738
+ return { ConvertResult, ApiResponse };
739
+ })
740
+ .catch(ex => {
741
+ this.PubApi(ApiKey, 'IsError', true);
742
+ this.PubApi(ApiKey, 'IsSuccess', false);
743
+ this.$Error(ex.message);
744
+ Api.OnError?.call(this, ex);
745
+ Option?.OnError?.call(this, ex);
746
+ this.#OnError?.call(this, ex);
747
+ })
748
+ .then(Result => {
749
+ this.PubApi(ApiKey, 'IsCalling', false);
750
+ this.PubApi(ApiKey, 'IsComplete', true);
751
+ if (Result instanceof Object) {
752
+ Api.OnComplete?.call(this, Result.ConvertResult, Result.ApiResponse);
753
+ Option?.OnComplete?.call(this, Result.ConvertResult, Result.ApiResponse);
754
+ this.#OnComplete?.call(this, Result.ConvertResult, Result.ApiResponse);
755
+ }
756
+ else {
757
+ Api.OnComplete?.call(this);
758
+ Option?.OnComplete?.call(this);
759
+ this.#OnComplete?.call(this, null, null);
760
+ }
761
+ });
762
+ }
763
+ $GenerateFetchRequest(Api, ParamBody, ParamFile, IsFormRequest) {
764
+ let Header = new Headers();
765
+ Header.set('Authorization', `Bearer ${this.#AccessToken}`);
766
+ if (this.#HeaderFuncs.length > 0) {
767
+ for (let Func of this.#HeaderFuncs) {
768
+ Func(Header);
769
+ }
770
+ }
771
+ let FetchRequest = {
772
+ method: Api.Method,
773
+ headers: Header,
774
+ };
775
+ if (IsFormRequest) {
776
+ let SendForm = this.$ConvertTo_FormData(ParamBody, new FormData());
777
+ SendForm = this.$ConvertTo_FormFile(ParamFile, SendForm);
778
+ FetchRequest.body = SendForm;
779
+ FetchRequest.method = 'POST';
780
+ }
781
+ else {
782
+ Header.set('content-type', 'application/json');
783
+ if (Api.Method != 'GET')
784
+ FetchRequest.body = JSON.stringify(ParamBody ?? {});
785
+ }
786
+ return FetchRequest;
787
+ }
788
+ //#endregion
789
+ //#region Api Sub/Pub Method
790
+ AddSubApi(ApiKey, Option) {
791
+ if (typeof Option === 'function')
792
+ Option = {
793
+ NotifyEvent: Option,
794
+ };
795
+ let SubApiStore = {
796
+ ...Option,
797
+ };
798
+ this.AddStoreFrom(this.ApiStore, ApiKey, {});
799
+ let GetApiStore = this.ApiStore[ApiKey];
800
+ GetApiStore.$sub ??= [];
801
+ GetApiStore.$sub.push(SubApiStore);
802
+ return this;
803
+ }
804
+ PubApi(ApiKey, PropertyName, Value) {
805
+ let GetApiStore = this.ApiStore[ApiKey];
806
+ GetApiStore[PropertyName] = Value;
807
+ if (GetApiStore.$sub == null || !Array.isArray(GetApiStore.$sub))
808
+ return this;
809
+ for (let Sub of GetApiStore.$sub) {
810
+ let SubStore = Sub;
811
+ if (SubStore.PropertyName != null && SubStore.PropertyName != PropertyName)
812
+ continue;
813
+ SubStore.NotifyEvent({
814
+ PropertyName: PropertyName,
815
+ ApiStore: GetApiStore,
816
+ Value: GetApiStore[PropertyName],
817
+ });
818
+ }
819
+ return this;
820
+ }
821
+ //#endregion
822
+ //#region Default Use Method
823
+ UseFormJsonBody(JsonBodyKey = 'Body') {
824
+ this.WithConvertTo_FormParam((FormDataBody, Form) => {
825
+ let ConvertParam = {};
826
+ ConvertParam[JsonBodyKey] = JSON.stringify(FormDataBody);
827
+ return ConvertParam;
828
+ });
829
+ return this;
830
+ }
831
+ //#endregion
832
+ //#region Public Event Add
833
+ EventAdd_AddApi(EventFunc) {
834
+ this.$EventAdd(this.#EventName.AddApi, EventFunc);
835
+ return this;
836
+ }
837
+ EventAdd_UpdateStore(EventFunc) {
838
+ this.$EventAdd(this.#EventName.UpdateStore, EventFunc);
839
+ return this;
840
+ }
841
+ EventAdd_AddStore(EventFunc) {
842
+ this.$EventAdd(this.#EventName.AddStore, EventFunc);
843
+ return this;
844
+ }
845
+ EventAdd_SetStore(EventFunc) {
846
+ this.$EventAdd(this.#EventName.SetStore, EventFunc);
847
+ return this;
848
+ }
849
+ //#endregion
850
+ //#region Protected Event Process
851
+ $EventAdd(EventName, OnFunc) {
852
+ if (EventName in this.#OnEventFunc == false)
853
+ this.#OnEventFunc[EventName] = [];
854
+ this.#OnEventFunc[EventName].push(OnFunc);
855
+ }
856
+ $EventTrigger(EventName, EventArg) {
857
+ let EventFuncs = this.#OnEventFunc[EventName];
858
+ if (EventFuncs == null)
859
+ return;
860
+ for (let Item of EventFuncs)
861
+ Item(EventArg);
862
+ }
863
+ //#endregion
864
+ //#region Store Control
865
+ //#region Public Data Store Contorl
866
+ GetStore(StorePath, Option) {
867
+ return this.GetStoreFrom(this.Store, StorePath, Option);
868
+ }
869
+ AddStore(StorePath, StoreData = null) {
870
+ return this.AddStoreFrom(this.Store, StorePath, StoreData);
871
+ }
872
+ SetStore(StorePath, StoreData) {
873
+ return this.SetStoreFrom(this.Store, StorePath, StoreData);
874
+ }
875
+ UpdateStore(StorePath, StoreData) {
876
+ return this.UpdateStoreFrom(this.Store, StorePath, StoreData);
877
+ }
878
+ ClearStore(StorePath, Option) {
879
+ return this.ClearStoreFrom(this.Store, StorePath, Option);
880
+ }
881
+ GetStoreFrom(SourceStore, StorePath, Option) {
882
+ if (typeof Option == 'boolean')
883
+ Option = { Clone: Option };
884
+ Option ??= {};
885
+ Option.Clone ??= false;
886
+ Option.CreateIfNull ??= false;
887
+ if (Option.DefaultValue == null)
888
+ Option.DefaultValue = {};
889
+ StorePath = this.ToJoin(StorePath);
890
+ let FindStore = this.$RCS_GetStore(StorePath, SourceStore, {
891
+ CreateIfNull: Option.CreateIfNull,
892
+ DefaultValue: Option.DefaultValue,
893
+ });
894
+ if (Option.Clone) {
895
+ let CloneResult = {};
896
+ let AllKeys = Object.getOwnPropertyNames(FindStore);
897
+ for (let Key of AllKeys) {
898
+ if (!Key.match(/^\$/g))
899
+ CloneResult[Key] = FindStore[Key];
900
+ }
901
+ return CloneResult;
902
+ }
903
+ return FindStore;
904
+ }
905
+ AddStoreFrom(SourceStore, StorePath, StoreData = null) {
906
+ StorePath = this.ToJoin(StorePath);
907
+ if (this.GetStoreFrom(SourceStore, StorePath) != null)
908
+ return this;
909
+ this.$RCS_SetStore(StorePath, StoreData, SourceStore, {
910
+ IsDeepSet: true,
911
+ });
912
+ this.$EventTrigger(this.#EventName.AddStore, {
913
+ Path: StorePath,
914
+ Data: StoreData,
915
+ });
916
+ return this;
917
+ }
918
+ SetStoreFrom(SourceStore, StorePath, StoreData) {
919
+ if (StorePath != null)
920
+ StorePath = this.ToJoin(StorePath);
921
+ this.$RCS_SetStore(StorePath, StoreData, SourceStore, {
922
+ IsDeepSet: false,
923
+ });
924
+ this.$EventTrigger(this.#EventName.SetStore, {
925
+ Path: StorePath,
926
+ Data: StoreData,
927
+ });
928
+ return this;
929
+ }
930
+ UpdateStoreFrom(SourceStore, StorePath, StoreData) {
931
+ if (StorePath != null)
932
+ StorePath = this.ToJoin(StorePath);
933
+ this.$RCS_SetStore(StorePath, StoreData, SourceStore, {
934
+ IsDeepSet: true,
935
+ });
936
+ this.$EventTrigger(this.#EventName.UpdateStore, {
937
+ Path: StorePath,
938
+ Data: StoreData,
939
+ });
940
+ return this;
941
+ }
942
+ ClearStoreFrom(SourceStore, StorePath, Option) {
943
+ Option ??= {};
944
+ if (typeof Option === 'boolean')
945
+ Option = { DeepClear: Option };
946
+ let TargetStore = StorePath == null ? SourceStore : this.GetStoreFrom(SourceStore, StorePath);
947
+ if (TargetStore == null)
948
+ return this;
949
+ this.$RCS_ClearStore(TargetStore, Option);
950
+ return this;
951
+ }
952
+ //#endregion
953
+ //#region Protected Data Store Process
954
+ $RCS_GetStore(StorePath, FindStore, Option) {
955
+ if (FindStore == null)
956
+ return null;
957
+ StorePath = StorePath.replaceAll(/\[|\]/g, '.').replace(/\.+/g, '.').replace(/\.$/, '');
958
+ let StorePaths = StorePath.split('.');
959
+ let FirstKey = StorePaths.shift();
960
+ if (FindStore[FirstKey] == null && Option.CreateIfNull) {
961
+ if (Array.isArray(Option.DefaultValue))
962
+ FindStore[FirstKey] = [...Option.DefaultValue];
963
+ else if (typeof Option.DefaultValue == 'object')
964
+ FindStore[FirstKey] = { ...Option.DefaultValue };
965
+ else
966
+ FindStore[FirstKey] = Option.DefaultValue;
967
+ }
968
+ let NextStore = FindStore[FirstKey];
969
+ if (StorePaths.length == 0)
970
+ return NextStore;
971
+ let NextKey = StorePaths.join('.');
972
+ return this.$RCS_GetStore(NextKey, NextStore, Option);
973
+ }
974
+ $RCS_SetStore(StorePath, StoreData, FindStore, Option = {
975
+ IsDeepSet: true,
976
+ }) {
977
+ if (StorePath == null) {
978
+ this.$DeepSetObject(StoreData, FindStore);
979
+ return StoreData;
980
+ }
981
+ StorePath = StorePath.replaceAll(/\[|\]/g, '.').replace(/\.+/g, '.').replace(/\.$/, '');
982
+ if (StorePath.includes('.')) {
983
+ let StorePaths = StorePath.split('.');
984
+ let FirstKey = StorePaths.shift();
985
+ if (FindStore[FirstKey] == null)
986
+ FindStore[FirstKey] = {};
987
+ let NextStore = FindStore[FirstKey];
988
+ let NextKey = StorePaths.join('.');
989
+ return this.$RCS_SetStore(NextKey, StoreData, NextStore, Option);
990
+ }
991
+ let IsAwaysSet = StoreData == null ||
992
+ !Option.IsDeepSet ||
993
+ FindStore[StorePath] == null ||
994
+ typeof StoreData != 'object';
995
+ if (IsAwaysSet) {
996
+ FindStore[StorePath] = StoreData;
997
+ return StoreData;
998
+ }
999
+ return this.$RCS_SetStore(null, StoreData, FindStore[StorePath]);
1000
+ }
1001
+ $RCS_ClearStore(TargetStore, Option) {
1002
+ if (typeof TargetStore != 'object')
1003
+ return;
1004
+ if (Array.isArray(TargetStore)) {
1005
+ TargetStore.length = 0;
1006
+ return;
1007
+ }
1008
+ let AllProperty = Object.getOwnPropertyNames(TargetStore);
1009
+ for (let Key of AllProperty) {
1010
+ if (Key.match(/^\$/g))
1011
+ continue;
1012
+ if (typeof TargetStore[Key] === 'function')
1013
+ continue;
1014
+ else if (Option.DeepClear && typeof TargetStore[Key] === 'object')
1015
+ this.$RCS_ClearStore(TargetStore[Key], Option);
1016
+ else
1017
+ TargetStore[Key] = null;
1018
+ }
1019
+ }
1020
+ $DeepSetObject(SetData, FindStore) {
1021
+ if (SetData == null) {
1022
+ this.ClearStoreFrom(FindStore);
1023
+ return;
1024
+ }
1025
+ if (Array.isArray(SetData)) {
1026
+ if (!Array.isArray(FindStore))
1027
+ return;
1028
+ FindStore.length = 0;
1029
+ SetData.forEach(item => FindStore.push(item));
1030
+ return;
1031
+ }
1032
+ this.ForEachObject(SetData, (Key, Value) => {
1033
+ let IsGoNext = false;
1034
+ if (Array.isArray(Value)) {
1035
+ if (FindStore[Key] == null || !Array.isArray(FindStore[Key]))
1036
+ FindStore[Key] = [];
1037
+ IsGoNext = true;
1038
+ }
1039
+ else if (Value != null && typeof Value == 'object') {
1040
+ if (FindStore[Key] == null || typeof FindStore[Key] != 'object')
1041
+ FindStore[Key] = {};
1042
+ IsGoNext = true;
1043
+ }
1044
+ if (IsGoNext)
1045
+ this.$DeepSetObject(Value, FindStore[Key]);
1046
+ else
1047
+ FindStore[Key] = Value;
1048
+ });
1049
+ }
1050
+ //#endregion
1051
+ //#region File Store
1052
+ AddFileStore(FileStoreKey, Option) {
1053
+ Option ??= {};
1054
+ if (this.FileStore[FileStoreKey] == null) {
1055
+ if (Option.Multi == true)
1056
+ this.FileStore[FileStoreKey] = [];
1057
+ else {
1058
+ this.FileStore[FileStoreKey] = new FileItem();
1059
+ }
1060
+ }
1061
+ return this;
1062
+ }
1063
+ Files(FileStoreKey, WhereFunc = null) {
1064
+ let GetFiles = this.FileStore[FileStoreKey];
1065
+ if (GetFiles == null)
1066
+ return [];
1067
+ if (!Array.isArray(GetFiles))
1068
+ GetFiles = [GetFiles];
1069
+ if (WhereFunc != null)
1070
+ GetFiles = GetFiles.filter(Item => WhereFunc(Item));
1071
+ let Result = GetFiles.map(Item => Item.File);
1072
+ return Result;
1073
+ }
1074
+ File(FileStoreKey, WhereFunc = null) {
1075
+ let GetFiles = this.Files(FileStoreKey, WhereFunc);
1076
+ if (GetFiles == null || GetFiles.length == 0)
1077
+ return null;
1078
+ return GetFiles[0];
1079
+ }
1080
+ AddFile(FileStoreKey, AddFile, ConvertType = 'none') {
1081
+ if (AddFile == null)
1082
+ return;
1083
+ this.AddFileStore(FileStoreKey);
1084
+ let GetStore = this.FileStore[FileStoreKey];
1085
+ if (Array.isArray(AddFile)) {
1086
+ if (Array.isArray(GetStore))
1087
+ AddFile.forEach(Item => this.AddFile(FileStoreKey, Item));
1088
+ else
1089
+ this.AddFile(FileStoreKey, AddFile[0]);
1090
+ }
1091
+ else {
1092
+ if (AddFile instanceof FileItem == false)
1093
+ AddFile = new FileItem(AddFile, ConvertType);
1094
+ if (Array.isArray(GetStore)) {
1095
+ GetStore.push(AddFile);
1096
+ }
1097
+ else {
1098
+ GetStore.From(AddFile);
1099
+ }
1100
+ }
1101
+ return this;
1102
+ }
1103
+ RemoveFile(FileStoreKey, DeleteFileId) {
1104
+ let GetStore = this.FileStore[FileStoreKey];
1105
+ if (GetStore == null)
1106
+ return this;
1107
+ if (Array.isArray(DeleteFileId))
1108
+ DeleteFileId.forEach(Item => this.RemoveFile(FileStoreKey, Item));
1109
+ else {
1110
+ if (Array.isArray(GetStore)) {
1111
+ let DeleteIndex = GetStore.findIndex(Item => Item.FileId == DeleteFileId);
1112
+ if (DeleteIndex >= 0)
1113
+ GetStore.splice(DeleteIndex, 1);
1114
+ }
1115
+ else {
1116
+ GetStore.Clear();
1117
+ }
1118
+ }
1119
+ return this;
1120
+ }
1121
+ ClearFile(FileStoreKey) {
1122
+ let GetStore = this.FileStore[FileStoreKey];
1123
+ if (GetStore == null)
1124
+ return this;
1125
+ if (Array.isArray(GetStore)) {
1126
+ GetStore.splice(0, GetStore.length);
1127
+ }
1128
+ else {
1129
+ GetStore.Clear();
1130
+ }
1131
+ return this;
1132
+ }
1133
+ //#endregion
1134
+ //#endregion
1135
+ //#region Protected Process
1136
+ $ProcessApiReturn(ApiResponse) {
1137
+ let GetContentType = ApiResponse.headers.get("content-type");
1138
+ if (GetContentType.includes('application/json')) {
1139
+ return ApiResponse.json();
1140
+ }
1141
+ if (GetContentType.includes('text')) {
1142
+ return ApiResponse.text();
1143
+ }
1144
+ return new Promise(reslove => { reslove(ApiResponse); });
1145
+ }
1146
+ //#endregion
1147
+ //#region Override Method
1148
+ NavigateToRoot() {
1149
+ let RootUrl = this.#RootRoute ?? '/';
1150
+ super.$BaseNavigateTo(RootUrl);
1151
+ return this;
1152
+ }
1153
+ //#endregion
1154
+ //#region Protected ConvertTo
1155
+ $ConvertTo_FormData(ConvertFormData, Form) {
1156
+ Form ??= new FormData();
1157
+ if (ConvertFormData == null)
1158
+ return Form;
1159
+ this.#Func_ConvertTo_FormData.forEach(Func => {
1160
+ ConvertFormData = Func(ConvertFormData, Form);
1161
+ });
1162
+ if (ConvertFormData instanceof FormData)
1163
+ return ConvertFormData;
1164
+ this.ForEachObject(ConvertFormData, (Key, Value) => {
1165
+ Form.append(Key, Value);
1166
+ });
1167
+ return Form;
1168
+ }
1169
+ $ConvertTo_FormFile(FileParam, Form) {
1170
+ Form ??= new FormData();
1171
+ if (FileParam == null)
1172
+ return Form;
1173
+ let DefaultKey = 'Files';
1174
+ if (Array.isArray(FileParam)) {
1175
+ this.$AppendFileToFormData(DefaultKey, Form, FileParam);
1176
+ return Form;
1177
+ }
1178
+ if (FileParam instanceof File || FileParam instanceof FileItem) {
1179
+ this.$AppendFileToFormData(DefaultKey, Form, FileParam);
1180
+ return Form;
1181
+ }
1182
+ let Keys = Object.keys(FileParam);
1183
+ for (let i = 0; i < Keys.length; i++) {
1184
+ let FileKey = Keys[i];
1185
+ let FileValue = FileParam[FileKey];
1186
+ this.$AppendFileToFormData(FileKey, Form, FileValue);
1187
+ }
1188
+ return Form;
1189
+ }
1190
+ $AppendFileToFormData(FileKey, Form, FileData) {
1191
+ if (Array.isArray(FileData)) {
1192
+ for (let i = 0; i < FileData.length; i++)
1193
+ this.$AppendFileToFormData(FileKey, Form, FileData[i]);
1194
+ }
1195
+ else if (FileData instanceof File)
1196
+ Form.append(FileKey, FileData);
1197
+ else
1198
+ Form.append(FileKey, FileData.File);
1199
+ return Form;
1200
+ }
1201
+ }
1202
+ import { createApp, reactive, nextTick } from 'vue';
1203
+ import { watch } from 'vue';
1204
+ export class VueStore extends ApiStore {
1205
+ $VueProxy = null;
1206
+ $VueOption = {
1207
+ methods: {},
1208
+ components: {},
1209
+ computed: {},
1210
+ };
1211
+ $VueApp = null;
1212
+ $VueUse = [];
1213
+ $CoreStore = 'app';
1214
+ $MountedFuncs = [];
1215
+ $Directive = [];
1216
+ constructor() {
1217
+ super();
1218
+ this.#Setup();
1219
+ }
1220
+ //#region Private Setup
1221
+ #Setup() {
1222
+ this
1223
+ .EventAdd_AddApi(Arg => {
1224
+ this.AddStore(Arg.ApiKey);
1225
+ })
1226
+ .EventAdd_UpdateStore(() => {
1227
+ this.ForceUpdate();
1228
+ })
1229
+ .EventAdd_AddStore(() => {
1230
+ this.ForceUpdate();
1231
+ })
1232
+ .EventAdd_SetStore(() => {
1233
+ this.ForceUpdate();
1234
+ })
1235
+ .AddStore(this.$CoreStore, {})
1236
+ .WithMounted(() => {
1237
+ this.UpdateStore([this.$CoreStore, 'IsMounted'], true);
1238
+ });
1239
+ }
1240
+ //#endregion
1241
+ //#region Get/Set Property
1242
+ get Store() {
1243
+ if (this.$VueProxy != null)
1244
+ return this.$VueProxy;
1245
+ return super.Store;
1246
+ }
1247
+ set Store(Store) {
1248
+ super.Store = Store;
1249
+ }
1250
+ //#endregion
1251
+ //#region Public With Method
1252
+ WithVueOption(VueOption = {}) {
1253
+ this.$VueOption = this.DeepObjectExtend(this.$VueOption, VueOption);
1254
+ return this;
1255
+ }
1256
+ WithMounted(MountedFunc = () => { }) {
1257
+ this.$MountedFuncs.push(MountedFunc);
1258
+ return this;
1259
+ }
1260
+ WithComponent(Component = {}) {
1261
+ this.$VueOption.components = this.DeepObjectExtend(this.$VueOption.components, Component);
1262
+ return this;
1263
+ }
1264
+ WithVueUse(...UsePlugin) {
1265
+ for (let Item of UsePlugin) {
1266
+ this.$VueUse.push(Item);
1267
+ }
1268
+ return this;
1269
+ }
1270
+ WithDirective(Name, Directive) {
1271
+ this.$Directive.push({
1272
+ Name,
1273
+ Directive
1274
+ });
1275
+ return this;
1276
+ }
1277
+ //#endregion
1278
+ //#region Public Method
1279
+ ForceUpdate() {
1280
+ this.$VueProxy?.$forceUpdate();
1281
+ return this;
1282
+ }
1283
+ Refs(RefName) {
1284
+ if (!this.$VueProxy)
1285
+ return null;
1286
+ return this.$VueProxy.$refs[Model.ToJoin(RefName)];
1287
+ }
1288
+ }
1289
+ //#endregion
1290
+ export class VueCommand extends VueStore {
1291
+ $IsInited = false;
1292
+ $CommandMap;
1293
+ $QueryDomName = null;
1294
+ constructor() {
1295
+ super();
1296
+ this.$SetupCommandMap();
1297
+ }
1298
+ //#region With Method
1299
+ WithQueryDomName(QueryDomName) {
1300
+ this.$QueryDomName = QueryDomName;
1301
+ Queryer.WithDomName(this.$QueryDomName);
1302
+ return this;
1303
+ }
1304
+ //#endregion
1305
+ //#region Path Command
1306
+ AddV_Text(DomName, Option) {
1307
+ let SetOption = this.$ConvertCommandOption(DomName, Option);
1308
+ if (typeof SetOption.Target != 'function') {
1309
+ let FullPaths = Model.ToJoin(SetOption.Target);
1310
+ if (/^[A-Za-z_$][A-Za-z0-9_$]*(\.[A-Za-z_$][A-Za-z0-9_$]*)*$/.test(FullPaths)) {
1311
+ Model.AddStore(SetOption.Target);
1312
+ }
1313
+ }
1314
+ SetOption.FuncAction = true;
1315
+ this.$AddCommand(DomName, 'v-text', SetOption);
1316
+ return this;
1317
+ }
1318
+ AddV_Model(DomName, StorePath, Option) {
1319
+ let SetOption = this.$ConvertCommandOption(StorePath);
1320
+ Option ??= {};
1321
+ Option.DefaultValue ??= null;
1322
+ SetOption.CommandKey = Option.ModelValue;
1323
+ this.AddStore(StorePath, Option.DefaultValue);
1324
+ this.$AddCommand(DomName, 'v-model', SetOption);
1325
+ return this;
1326
+ }
1327
+ AddV_Slot(DomName, SlotKey, Option) {
1328
+ let SetOption = this.$ConvertCommandOption(DomName, Option);
1329
+ if (SlotKey != null)
1330
+ SetOption.CommandKey = SlotKey;
1331
+ this.$AddCommand(DomName, `v-slot`, SetOption);
1332
+ return this;
1333
+ }
1334
+ //#endregion
1335
+ //#region Path/Function Command
1336
+ AddV_For(DomName, Option, ForKey) {
1337
+ let SetOption = this.$ConvertCommandOption(DomName, Option);
1338
+ if (ForKey) {
1339
+ ForKey = this.ToJoin(ForKey);
1340
+ if (!/^\(/.test(ForKey))
1341
+ ForKey = `(${ForKey}`;
1342
+ if (!/\)$/.test(ForKey))
1343
+ ForKey += ')';
1344
+ SetOption.TargetHead = `${ForKey} in `;
1345
+ }
1346
+ let Target = Model.ToJoin(SetOption.Target);
1347
+ if (!/\b(in|of)\b/.test(Target))
1348
+ SetOption.TargetHead ??= '(item, index) in ';
1349
+ SetOption.FuncAction = true;
1350
+ this.$AddCommand(DomName, 'v-for', SetOption);
1351
+ return this;
1352
+ }
1353
+ AddV_If(DomName, Option) {
1354
+ let SetOption = this.$ConvertCommandOption(DomName, Option);
1355
+ SetOption.FuncAction = true;
1356
+ this.$AddCommand(DomName, 'v-if', SetOption);
1357
+ return this;
1358
+ }
1359
+ AddV_ElseIf(DomName, Option) {
1360
+ let SetOption = this.$ConvertCommandOption(DomName, Option);
1361
+ SetOption.FuncAction = true;
1362
+ this.$AddCommand(DomName, 'v-else-if', SetOption);
1363
+ return this;
1364
+ }
1365
+ AddV_Else(DomName) {
1366
+ let SetOption = this.$ConvertCommandOption(DomName);
1367
+ SetOption.Target = '';
1368
+ this.$AddCommand(DomName, 'v-else', SetOption);
1369
+ return this;
1370
+ }
1371
+ AddV_Show(DomName, Option) {
1372
+ let SetOption = this.$ConvertCommandOption(DomName, Option);
1373
+ SetOption.FuncAction = true;
1374
+ this.$AddCommand(DomName, 'v-show', SetOption);
1375
+ return this;
1376
+ }
1377
+ AddV_Bind(DomName, BindKey, Option, Args) {
1378
+ let SetOption = this.$ConvertCommandOption(DomName, Option, Args);
1379
+ SetOption.CommandKey = BindKey;
1380
+ this.$AddCommand(DomName, 'v-bind', SetOption);
1381
+ return this;
1382
+ }
1383
+ AddV_On(DomName, EventName, Option, Args) {
1384
+ let SetOption = this.$ConvertCommandOption(DomName, Option, Args);
1385
+ SetOption.CommandKey = EventName;
1386
+ this.$AddCommand(DomName, `v-on`, SetOption);
1387
+ return this;
1388
+ }
1389
+ //#endregion
1390
+ //#region Customer Command
1391
+ Watch(WatchPath, Callback, Option = {}) {
1392
+ let Handle;
1393
+ if (typeof WatchPath == 'function')
1394
+ Handle = watch(WatchPath, Callback, Option);
1395
+ else {
1396
+ Model.AddStore(WatchPath);
1397
+ Handle = watch(() => Model.GetStore(WatchPath), Callback, Option);
1398
+ }
1399
+ return Handle;
1400
+ }
1401
+ AddV_Watch(WatchPath, Callback, Option = {}) {
1402
+ Model.WithMounted(() => {
1403
+ this.Watch(WatchPath, Callback, Option);
1404
+ });
1405
+ return this;
1406
+ }
1407
+ AddV_Function(FuncName, Func) {
1408
+ if (this.$IsInited && !Array.isArray(FuncName))
1409
+ this.$VueOption.methods[FuncName] = Func;
1410
+ else
1411
+ Model.UpdateStore(FuncName, Func);
1412
+ return this;
1413
+ }
1414
+ AddV_OnChange(DomName, ChangeFunc, Args) {
1415
+ this.AddV_On(DomName, 'change', ChangeFunc, Args);
1416
+ return this;
1417
+ }
1418
+ AddV_Click(DomName, Option, Args) {
1419
+ let SetOption = this.$ConvertCommandOption(DomName, Option, Args);
1420
+ this.AddV_On(DomName, 'click', SetOption);
1421
+ return this;
1422
+ }
1423
+ AddV_FilePicker(DomName, Option) {
1424
+ let FileStorePath = null;
1425
+ let Accept = null;
1426
+ let ConvertType = 'none';
1427
+ let Multi = false;
1428
+ let OnSuccess;
1429
+ let OnError;
1430
+ if (typeof (Option) == 'string')
1431
+ FileStorePath = Option;
1432
+ else {
1433
+ FileStorePath = Option.Store;
1434
+ ConvertType = Option.ConvertType;
1435
+ Multi = Option.Multiple;
1436
+ OnSuccess = Option.OnSuccess;
1437
+ if (Array.isArray(Option.Accept))
1438
+ Accept = Option.Accept.join(' ');
1439
+ else
1440
+ Accept = Option.Accept;
1441
+ }
1442
+ this.AddFileStore(FileStorePath, {
1443
+ Multi: Multi,
1444
+ });
1445
+ this.AddV_Click(DomName, () => {
1446
+ let TempInput = document.createElement('input');
1447
+ TempInput.type = 'file';
1448
+ if (Accept != null)
1449
+ TempInput.accept = Accept;
1450
+ if (Multi != null)
1451
+ TempInput.multiple = Multi;
1452
+ TempInput.onchange = async (Event) => {
1453
+ if (TempInput.files == null || TempInput.files.length == 0)
1454
+ return;
1455
+ const Files = TempInput.files;
1456
+ const FileItems = [];
1457
+ for (let i = 0; i < Files.length; i++) {
1458
+ let PickFile = Files[i];
1459
+ this.AddFile(FileStorePath, PickFile, ConvertType);
1460
+ let fileData = Model.FileStore[FileStorePath];
1461
+ if (!Array.isArray(fileData)) {
1462
+ fileData = [fileData];
1463
+ }
1464
+ FileItems.push(...fileData);
1465
+ }
1466
+ if (OnSuccess) {
1467
+ let isChecked = true;
1468
+ for (const item of FileItems) {
1469
+ if (item instanceof FileItem) {
1470
+ if (!await item.CheckLoadAsync()) {
1471
+ if (OnError)
1472
+ OnError(item);
1473
+ isChecked = false;
1474
+ break;
1475
+ }
1476
+ }
1477
+ }
1478
+ if (isChecked)
1479
+ OnSuccess(FileItems);
1480
+ }
1481
+ };
1482
+ TempInput.click();
1483
+ });
1484
+ return this;
1485
+ }
1486
+ AddV_Tree(TreeRoot, TreeSet, Option) {
1487
+ let AllSetInfo = [];
1488
+ let RootNode;
1489
+ let UsingRootNode = TreeRoot instanceof QueryNode;
1490
+ if (UsingRootNode)
1491
+ RootNode = TreeRoot;
1492
+ let RootPaths = UsingRootNode ? [] : this.Paths(TreeRoot);
1493
+ this.$ParseTreeSet(RootPaths, TreeSet, AllSetInfo);
1494
+ for (let Info of AllSetInfo) {
1495
+ let ActionSet = this.$CommandMap[Info.Command];
1496
+ if (ActionSet == null) {
1497
+ Model.$Error(`${Info.Command} command is not allowed, path: ${this.ToJoin(Info.DomPaths)}`);
1498
+ continue;
1499
+ }
1500
+ if (Info.StoreValue == '' || Info.StoreValue == null && ActionSet.AcceptNull != true)
1501
+ continue;
1502
+ if (Info.StoreValue == '.' && ActionSet.AcceptSelf != true)
1503
+ continue;
1504
+ let NeedQuery = Info.Command == 'using';
1505
+ let QueryOption = {
1506
+ Mode: 'Multi',
1507
+ };
1508
+ if (UsingRootNode) {
1509
+ NeedQuery = true;
1510
+ QueryOption.TargetNode = RootNode;
1511
+ }
1512
+ if (Option?.UseDeepQuery) {
1513
+ NeedQuery = true;
1514
+ QueryOption.Mode = 'DeepMulti';
1515
+ }
1516
+ if (NeedQuery) {
1517
+ let QueryNodes = [RootNode];
1518
+ if (Info.DomPaths.length > 0)
1519
+ QueryNodes = Queryer.Query(Info.DomPaths, QueryOption);
1520
+ Info.Nodes = QueryNodes;
1521
+ }
1522
+ let TargetDom = NeedQuery ? Info.Nodes : Info.DomPaths;
1523
+ let TargetValue;
1524
+ if (typeof Info.StoreValue === 'function') {
1525
+ TargetValue = {
1526
+ Target: Info.StoreValue,
1527
+ FuncArgs: Info.Args,
1528
+ };
1529
+ }
1530
+ else {
1531
+ if (typeof Info.StoreValue === 'string' || Array.isArray(Info.StoreValue)) {
1532
+ if (Info.StoreValue == '.')
1533
+ TargetValue = this.Paths(Info.TreePaths, Info.DomName);
1534
+ else if (Info.StoreValue != null && Info.StoreValue != '')
1535
+ TargetValue = Info.StoreValue;
1536
+ }
1537
+ else {
1538
+ TargetValue = {
1539
+ Target: Info.StoreValue?.TargetFunc,
1540
+ FuncArgs: Info.StoreValue?.Args,
1541
+ };
1542
+ if (Info.StoreValue?.Args != null) {
1543
+ let TargetArgs = Info.StoreValue.Args;
1544
+ if (Info.Args == null)
1545
+ Info.Args = Model.ToJoin(TargetArgs, ', ');
1546
+ else
1547
+ Info.Args = Model.ToJoin([Info.Args, TargetArgs], ', ');
1548
+ }
1549
+ }
1550
+ }
1551
+ ActionSet.Execute(Info, {
1552
+ TargetDom,
1553
+ TargetValue,
1554
+ });
1555
+ }
1556
+ return this;
1557
+ }
1558
+ $ParseTreeSet(Paths, TreeSet, Result) {
1559
+ const TreeNodeReges = /^:(?<next>.+)$/;
1560
+ const AllKeys = Object.keys(TreeSet);
1561
+ for (let i = 0; i < AllKeys.length; i++) {
1562
+ const Command = AllKeys[i];
1563
+ const SetPair = TreeSet[Command];
1564
+ const DomPaths = [...Paths];
1565
+ const TreePaths = [...Paths];
1566
+ const DomName = TreePaths.pop();
1567
+ const TreeNodeResult = Command.match(TreeNodeReges);
1568
+ if (TreeNodeResult) {
1569
+ const NextDomName = TreeNodeResult.groups.next;
1570
+ if (typeof SetPair === 'function') {
1571
+ Result.push({
1572
+ Command: 'using',
1573
+ StoreValue: SetPair,
1574
+ TreePaths: [...DomPaths],
1575
+ DomPaths: [...DomPaths, NextDomName],
1576
+ DomName: NextDomName,
1577
+ });
1578
+ }
1579
+ else {
1580
+ this.$ParseTreeSet([...Paths, NextDomName], SetPair, Result);
1581
+ }
1582
+ continue;
1583
+ }
1584
+ const GetCommandPart = (FindCommand, StartChar, EndChar) => {
1585
+ if (!FindCommand.includes(StartChar) || !FindCommand.includes(EndChar))
1586
+ return null;
1587
+ const StartIndex = FindCommand.indexOf(StartChar);
1588
+ const EndIndex = FindCommand.lastIndexOf(EndChar);
1589
+ const Result = FindCommand.slice(StartIndex + 1, EndIndex).trim();
1590
+ return Result?.trim();
1591
+ };
1592
+ const GetCommandWithKey = (FindCommand) => {
1593
+ let ArgsStart = null;
1594
+ let ForKeyStart = null;
1595
+ if (FindCommand.includes('('))
1596
+ ArgsStart = FindCommand.indexOf('(');
1597
+ if (FindCommand.includes('<'))
1598
+ ForKeyStart = FindCommand.indexOf('<');
1599
+ let CommandWithKey = null;
1600
+ if (ArgsStart == null && ForKeyStart == null) {
1601
+ CommandWithKey = FindCommand;
1602
+ }
1603
+ else if (ArgsStart == null || ForKeyStart == null) {
1604
+ let MinIndex = ArgsStart ?? ForKeyStart;
1605
+ CommandWithKey = FindCommand.slice(0, MinIndex);
1606
+ }
1607
+ else {
1608
+ let MinIndex = Math.min(ArgsStart, ForKeyStart);
1609
+ CommandWithKey = FindCommand.slice(0, MinIndex);
1610
+ }
1611
+ let Command = CommandWithKey;
1612
+ let CommandKey = null;
1613
+ if (CommandWithKey.includes(':')) {
1614
+ let CommandKeyStart = Command.indexOf(':');
1615
+ Command = CommandWithKey.slice(0, CommandKeyStart);
1616
+ CommandKey = CommandWithKey.slice(CommandKeyStart + 1);
1617
+ }
1618
+ return {
1619
+ Command: Command?.trim(),
1620
+ CommandKey: CommandKey?.trim(),
1621
+ };
1622
+ };
1623
+ const Args = GetCommandPart(Command, '(', ')');
1624
+ const ForKey = GetCommandPart(Command, '<', '>');
1625
+ const CommandWithKey = GetCommandWithKey(Command);
1626
+ Result.push({
1627
+ Command: CommandWithKey?.Command,
1628
+ CommandKey: CommandWithKey?.CommandKey,
1629
+ ForKey: ForKey,
1630
+ Args: Args,
1631
+ StoreValue: SetPair,
1632
+ TreePaths: TreePaths,
1633
+ DomPaths: DomPaths,
1634
+ DomName: DomName,
1635
+ });
1636
+ continue;
1637
+ }
1638
+ }
1639
+ $SetupCommandMap() {
1640
+ this.$CommandMap = {
1641
+ 'v-text': {
1642
+ Execute: (Info, Option) => {
1643
+ Model.AddV_Text(Option.TargetDom, Option.TargetValue);
1644
+ },
1645
+ AcceptSelf: true,
1646
+ },
1647
+ 'v-model': {
1648
+ Execute: (Info, Option) => {
1649
+ if (typeof (Option.TargetValue) == 'function') {
1650
+ Model.$Error(`v-model command value must be a string or string[], path: ${this.ToJoin(Info.DomPaths)}`);
1651
+ return;
1652
+ }
1653
+ Model.AddV_Model(Option.TargetDom, Option.TargetValue, {
1654
+ ModelValue: Info.CommandKey,
1655
+ });
1656
+ },
1657
+ AcceptSelf: true,
1658
+ },
1659
+ 'v-for': {
1660
+ Execute: (Info, Option) => {
1661
+ Model.AddV_For(Option.TargetDom, Option.TargetValue, Info.ForKey);
1662
+ },
1663
+ AcceptSelf: true,
1664
+ },
1665
+ 'v-show': {
1666
+ Execute: (Info, Option) => {
1667
+ Model.AddV_Show(Option.TargetDom, Option.TargetValue);
1668
+ },
1669
+ AcceptSelf: true,
1670
+ },
1671
+ 'v-if': {
1672
+ Execute: (Info, Option) => {
1673
+ Model.AddV_If(Option.TargetDom, Option.TargetValue);
1674
+ },
1675
+ AcceptSelf: true,
1676
+ },
1677
+ 'v-else-if': {
1678
+ Execute: (Info, Option) => {
1679
+ Model.AddV_ElseIf(Option.TargetDom, Option.TargetValue);
1680
+ },
1681
+ AcceptSelf: true,
1682
+ },
1683
+ 'v-else': {
1684
+ Execute: (Info, Option) => {
1685
+ Model.AddV_Else(Option.TargetDom);
1686
+ },
1687
+ AcceptNull: true,
1688
+ },
1689
+ 'v-bind': {
1690
+ Execute: (Info, Option) => {
1691
+ Model.AddV_Bind(Option.TargetDom, Info.CommandKey, Option.TargetValue, Info.Args);
1692
+ },
1693
+ },
1694
+ 'v-on': {
1695
+ Execute: (Info, Option) => {
1696
+ Model.AddV_On(Option.TargetDom, Info.CommandKey, Option.TargetValue, Info.Args);
1697
+ },
1698
+ },
1699
+ 'v-slot': {
1700
+ Execute: (Info, Option) => {
1701
+ Model.AddV_Slot(Option.TargetDom, Info.CommandKey, Option.TargetValue);
1702
+ },
1703
+ },
1704
+ 'v-on-mounted': {
1705
+ Execute: (Info, Option) => {
1706
+ Model.AddV_OnMounted(Option.TargetDom, Option.TargetValue, Info.Args);
1707
+ },
1708
+ },
1709
+ 'v-on-unmounted': {
1710
+ Execute: (Info, Option) => {
1711
+ Model.AddV_OnUnMounted(Option.TargetDom, Option.TargetValue, Info.Args);
1712
+ },
1713
+ },
1714
+ 'v-on-ready': {
1715
+ Execute: (Info, Option) => {
1716
+ Model.AddV_OnReady(Option.TargetDom, Option.TargetValue, Info.Args);
1717
+ },
1718
+ },
1719
+ 'watch': {
1720
+ Execute: (Info, Option) => {
1721
+ let WatchPaths = [Info.DomPaths];
1722
+ if (Info.CommandKey)
1723
+ WatchPaths.push(Info.CommandKey.split(':'));
1724
+ WatchPaths = Model.Paths(WatchPaths);
1725
+ if (typeof (Info.StoreValue) === 'function')
1726
+ Model.AddV_Watch(WatchPaths, Info.StoreValue);
1727
+ else {
1728
+ let Target = Info.StoreValue;
1729
+ Model.AddV_Watch(WatchPaths, Target?.CallBack, Target?.Option);
1730
+ }
1731
+ },
1732
+ },
1733
+ 'func': {
1734
+ Execute: (Info, Option) => {
1735
+ if (typeof (Info.StoreValue) != 'function') {
1736
+ Model.$Error(`func command value must be a function, path: ${this.ToJoin(Info.DomPaths)}`);
1737
+ return;
1738
+ }
1739
+ Model.AddV_Function(Model.Paths(...Info.DomPaths, Info.CommandKey ?? 'func'), Info.StoreValue);
1740
+ },
1741
+ },
1742
+ 'using': {
1743
+ Execute: (Info, Option) => {
1744
+ if (typeof (Info.StoreValue) === 'function') {
1745
+ const nodes = Info.Nodes;
1746
+ const node = nodes != null ? nodes[0] : null;
1747
+ Info.StoreValue(Info.DomPaths, node, nodes);
1748
+ }
1749
+ },
1750
+ },
1751
+ 'store': {
1752
+ Execute: (Info, Option) => {
1753
+ Model.UpdateStore(Info.DomPaths, Info.StoreValue);
1754
+ },
1755
+ }
1756
+ };
1757
+ }
1758
+ //#endregion
1759
+ //#region Property Method
1760
+ AddV_Property(PropertyPath, Option) {
1761
+ return this.AddV_PropertyFrom(this.Store, PropertyPath, Option);
1762
+ }
1763
+ AddV_PropertyFrom(SourceStore, PropertyPath, Option) {
1764
+ if (PropertyPath == null)
1765
+ return;
1766
+ let SetStore = SourceStore;
1767
+ PropertyPath = this.ToJoin(PropertyPath);
1768
+ let PropertyKey = PropertyPath;
1769
+ if (PropertyPath.includes('.')) {
1770
+ let PropertyPaths = PropertyPath.split('.');
1771
+ PropertyKey = PropertyPaths.pop();
1772
+ let FindPath = PropertyPaths.join('.');
1773
+ SetStore = this.GetStoreFrom(SourceStore, FindPath, {
1774
+ CreateIfNull: true,
1775
+ });
1776
+ }
1777
+ let SetProperty = this.$BaseAddProperty(SetStore, PropertyKey, Option);
1778
+ if (Option.Bind) {
1779
+ if (!Array.isArray(Option.Bind))
1780
+ Option.Bind = [Option.Bind];
1781
+ for (let BindPath of Option.Bind) {
1782
+ if (BindPath == null)
1783
+ continue;
1784
+ this.AddV_Property(BindPath, {
1785
+ Target: PropertyPath,
1786
+ });
1787
+ }
1788
+ SetProperty['Bind'] = Option.Bind;
1789
+ }
1790
+ return this;
1791
+ }
1792
+ $BaseAddProperty(PropertyStore, PropertyKey, Option) {
1793
+ let ThisModel = this;
1794
+ let PropertyContent = {
1795
+ get() {
1796
+ if (Option.get)
1797
+ return Option.get();
1798
+ return this.$get(PropertyKey);
1799
+ },
1800
+ set(Value) {
1801
+ if (Option.set) {
1802
+ Option.set(Value);
1803
+ return;
1804
+ }
1805
+ this.$set(PropertyKey, Value);
1806
+ }
1807
+ };
1808
+ if (Option.get)
1809
+ PropertyContent.get = Option.get;
1810
+ if (Option.set != null)
1811
+ PropertyContent.set = Option.set;
1812
+ let OriginalValue = PropertyStore[PropertyKey];
1813
+ let SetProperty = Object.defineProperty(PropertyStore, PropertyKey, PropertyContent);
1814
+ SetProperty.$properties ??= {};
1815
+ SetProperty.$properties[PropertyKey] = { ...Option };
1816
+ SetProperty.$get ??= (PropertyKey) => {
1817
+ let PropertyOption = SetProperty.$properties[PropertyKey];
1818
+ if (PropertyOption?.Target == null)
1819
+ return PropertyOption[`$${PropertyKey}`];
1820
+ return ThisModel.GetStore(PropertyOption.Target);
1821
+ };
1822
+ SetProperty.$set ??= (PropertyKey, Value) => {
1823
+ let PropertyOption = SetProperty.$properties[PropertyKey];
1824
+ if (PropertyOption?.Target)
1825
+ ThisModel.SetStore(PropertyOption.Target, Value);
1826
+ else
1827
+ PropertyOption[`$${PropertyKey}`] = Value;
1828
+ };
1829
+ if (Option.Value != null)
1830
+ SetProperty[PropertyKey] = Option.Value;
1831
+ else if (OriginalValue != null)
1832
+ SetProperty[PropertyKey] = OriginalValue;
1833
+ return SetProperty;
1834
+ }
1835
+ //#endregion
1836
+ //#region Protected Process
1837
+ $ConvertCommandOption(DomName, Option, Args) {
1838
+ let Result;
1839
+ if (Option == null || Option == '.') {
1840
+ if (this.IsPathType(DomName))
1841
+ Result = {
1842
+ Target: DomName
1843
+ };
1844
+ else {
1845
+ let Nodes = DomName;
1846
+ let NodeNames = Nodes.map(Item => Item.DomName);
1847
+ Result = {
1848
+ Target: NodeNames
1849
+ };
1850
+ }
1851
+ }
1852
+ else if (typeof Option == 'string' || typeof Option == 'function' || Array.isArray(Option))
1853
+ Result = {
1854
+ Target: Option
1855
+ };
1856
+ else
1857
+ Result = Option;
1858
+ if (Args)
1859
+ Result.FuncArgs = Args;
1860
+ Result.FuncAction ??= false;
1861
+ if (Result.FuncArgs)
1862
+ Result.FuncAction = true;
1863
+ return Result;
1864
+ }
1865
+ $AddCommand(DomName, Command, Option) {
1866
+ if (DomName == null)
1867
+ return;
1868
+ if (!Array.isArray(DomName))
1869
+ DomName = [DomName];
1870
+ let IsFromQueryNode = DomName[0] instanceof QueryNode;
1871
+ let QueryNodes;
1872
+ if (IsFromQueryNode)
1873
+ QueryNodes = DomName;
1874
+ else
1875
+ QueryNodes = Queryer.Query(DomName);
1876
+ let Target = Option.Target;
1877
+ if (typeof (Target) == 'function') {
1878
+ let FuncDomName = [];
1879
+ if (IsFromQueryNode)
1880
+ FuncDomName = DomName.at(-1).DomName;
1881
+ else
1882
+ FuncDomName = DomName;
1883
+ Target = this.$GenerateEventFunction(FuncDomName, Target, Command);
1884
+ if (Option.FuncArgs) {
1885
+ let Args = this.ToJoin(Option.FuncArgs, ',');
1886
+ Target += `(${Args})`;
1887
+ }
1888
+ else if (Option.FuncAction) {
1889
+ Target += `()`;
1890
+ }
1891
+ }
1892
+ else
1893
+ Target = this.ToJoin(Target);
1894
+ if (Option.TargetHead)
1895
+ Target = Option.TargetHead + Target;
1896
+ if (Option.TargetTail)
1897
+ Target += Option.TargetTail;
1898
+ if (Option.CommandKey)
1899
+ Command += `:${Option.CommandKey}`;
1900
+ for (let i = 0; i < QueryNodes.length; i++) {
1901
+ let NodeItem = QueryNodes[i];
1902
+ let Dom = NodeItem.Dom;
1903
+ this.$SetAttribute(Dom, Command, Target);
1904
+ }
1905
+ }
1906
+ $SetAttribute(Dom, AttrName, AttrValue) {
1907
+ if (Dom == null) {
1908
+ let Message = `Dom Element is null. ${AttrValue}`;
1909
+ console.warn(Message);
1910
+ return;
1911
+ }
1912
+ Dom.setAttribute(AttrName, AttrValue);
1913
+ }
1914
+ //#region Function Control
1915
+ $RandomFuncName(BaseFuncName) {
1916
+ return `${BaseFuncName}${this.GenerateIdReplace('')}`.replace(/[-:.]/g, '_');
1917
+ }
1918
+ $GenerateEventFunction(domName, eventFunc, command) {
1919
+ const funcName = this.$RandomFuncName(`${command}_`);
1920
+ domName = this.Paths(domName);
1921
+ const fullFuncPath = ['event', ...domName, funcName]
1922
+ .filter(item => item != null && item != '');
1923
+ this.AddV_Function(fullFuncPath, eventFunc);
1924
+ return this.ToJoin(fullFuncPath);
1925
+ }
1926
+ }
1927
+ export class VueModel extends VueCommand {
1928
+ $NativeWarn;
1929
+ $IsEnableVueWarn;
1930
+ $MountId = null;
1931
+ Id;
1932
+ constructor() {
1933
+ super();
1934
+ this.Id = this.GenerateId();
1935
+ this.$MountId = 'app';
1936
+ this.WithVueWarn(false);
1937
+ this.WithLifeCycleDirective();
1938
+ }
1939
+ //#region With Method
1940
+ WithMountId(MountId) {
1941
+ this.$MountId = MountId;
1942
+ return this;
1943
+ }
1944
+ WithVueWarn(Enable) {
1945
+ this.$IsEnableVueWarn = Enable;
1946
+ this.$NativeWarn = console.warn;
1947
+ console.warn = (...Message) => {
1948
+ if (Message == null)
1949
+ return;
1950
+ if (Message.length == 0)
1951
+ return;
1952
+ if (Message[0].toLowerCase().includes('[vue warn]') && this.$IsEnableVueWarn == false)
1953
+ return;
1954
+ this.$NativeWarn(Message);
1955
+ };
1956
+ return this;
1957
+ }
1958
+ WithLifeCycleDirective() {
1959
+ this.WithDirective('on-mounted', {
1960
+ mounted(el, binding, vnode) {
1961
+ if (typeof binding.value === 'function')
1962
+ binding.value(el, vnode);
1963
+ }
1964
+ });
1965
+ this.WithDirective('on-unmounted', {
1966
+ unmounted(el, binding, vnode) {
1967
+ if (typeof binding.value === 'function')
1968
+ binding.value(el, vnode);
1969
+ }
1970
+ });
1971
+ this.WithDirective('on-ready', {
1972
+ mounted(el, binding, vnode) {
1973
+ if (typeof binding.value === 'function')
1974
+ nextTick(() => binding.value(el, vnode));
1975
+ }
1976
+ });
1977
+ }
1978
+ //#endregion
1979
+ //#region Public Method
1980
+ Init() {
1981
+ if (this.$IsInited)
1982
+ return this;
1983
+ this.Store = reactive(this.Store);
1984
+ let GetStore = this.Store;
1985
+ let MountedFunc = this.$MountedFuncs;
1986
+ this.$VueApp = createApp({
1987
+ ...this.$VueOption,
1988
+ data() {
1989
+ return GetStore;
1990
+ },
1991
+ mounted: () => {
1992
+ for (let Func of MountedFunc)
1993
+ Func();
1994
+ }
1995
+ });
1996
+ for (let Item of this.$VueUse)
1997
+ this.$VueApp.use(Item);
1998
+ for (let Item of this.$Directive)
1999
+ this.$VueApp.directive(Item.Name, Item.Directive);
2000
+ this.$VueProxy = this.$VueApp.mount(`#${this.$MountId}`);
2001
+ this.$IsInited = true;
2002
+ return this;
2003
+ }
2004
+ Using(UseFunc = () => { }) {
2005
+ UseFunc();
2006
+ return this;
2007
+ }
2008
+ UsingVueApp(UsingFunc) {
2009
+ UsingFunc?.call(this, this.$VueApp);
2010
+ this.$VueApp.directive;
2011
+ return this;
2012
+ }
2013
+ //#endregion
2014
+ //#region Customer Vue Command
2015
+ AddV_OnMounted(DomName, Option, Args) {
2016
+ let SetOption = this.$ConvertCommandOption(DomName, Option);
2017
+ if (Args) {
2018
+ SetOption.FuncArgs = Args;
2019
+ SetOption.TargetHead = '($el, $vnode) => ';
2020
+ }
2021
+ SetOption.FuncAction = false;
2022
+ this.$AddCommand(DomName, `v-on-mounted`, SetOption);
2023
+ return this;
2024
+ }
2025
+ AddV_OnUnMounted(DomName, Option, Args) {
2026
+ let SetOption = this.$ConvertCommandOption(DomName, Option);
2027
+ if (Args) {
2028
+ SetOption.FuncArgs = Args;
2029
+ SetOption.TargetHead = '($el, $vnode) => ';
2030
+ }
2031
+ SetOption.FuncAction = false;
2032
+ this.$AddCommand(DomName, `v-on-unmounted`, SetOption);
2033
+ return this;
2034
+ }
2035
+ AddV_OnReady(DomName, Option, Args) {
2036
+ let SetOption = this.$ConvertCommandOption(DomName, Option);
2037
+ if (Args) {
2038
+ SetOption.FuncArgs = Args;
2039
+ SetOption.TargetHead = '($el, $vnode) => ';
2040
+ }
2041
+ SetOption.FuncAction = false;
2042
+ this.$AddCommand(DomName, `v-on-ready`, SetOption);
2043
+ return this;
2044
+ }
2045
+ }
2046
+ const Model = new VueModel();
2047
+ window.Model = Model;
2048
+ export { Model, };
2049
+ //# sourceMappingURL=VueModel.js.map