@c8y/sample-plugin 1023.48.0 → 1023.48.2

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/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "@c8y/sample-plugin",
3
- "version": "1023.48.0",
3
+ "version": "1023.48.2",
4
4
  "description": "",
5
5
  "dependencies": {
6
- "@c8y/style": "1023.48.0",
7
- "@c8y/ngx-components": "1023.48.0",
8
- "@c8y/client": "1023.48.0",
9
- "@c8y/bootstrap": "1023.48.0",
6
+ "@c8y/style": "1023.48.2",
7
+ "@c8y/ngx-components": "1023.48.2",
8
+ "@c8y/client": "1023.48.2",
9
+ "@c8y/bootstrap": "1023.48.2",
10
10
  "@angular/cdk": "^20.2.14",
11
11
  "monaco-editor": "~0.53.0",
12
12
  "ngx-bootstrap": "20.0.2",
13
13
  "rxjs": "7.8.2"
14
14
  },
15
15
  "devDependencies": {
16
- "@c8y/options": "1023.48.0",
17
- "@c8y/devkit": "1023.48.0"
16
+ "@c8y/options": "1023.48.2",
17
+ "@c8y/devkit": "1023.48.2"
18
18
  },
19
19
  "peerDependencies": {
20
20
  "@angular/common": ">=20 <21"
@@ -1,7 +1,26 @@
1
- import { Component, inject, Input, OnInit, TemplateRef, ViewChild } from '@angular/core';
2
- import { ControlContainer, NgForm } from '@angular/forms';
3
- import { AlertService, CommonModule, DynamicComponent, FormsModule } from '@c8y/ngx-components';
1
+ import { AsyncPipe } from '@angular/common';
2
+ import {
3
+ Component,
4
+ DestroyRef,
5
+ inject,
6
+ Input,
7
+ OnInit,
8
+ TemplateRef,
9
+ ViewChild
10
+ } from '@angular/core';
11
+ import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
12
+ import {
13
+ ControlContainer,
14
+ FormBuilder,
15
+ FormControl,
16
+ FormGroup,
17
+ NgForm,
18
+ ReactiveFormsModule,
19
+ Validators
20
+ } from '@angular/forms';
21
+ import { AlertService, DynamicComponent, FormGroupComponent } from '@c8y/ngx-components';
4
22
  import { WidgetConfigService } from '@c8y/ngx-components/context-dashboard';
23
+ import { BehaviorSubject } from 'rxjs';
5
24
  import { SamplePluginConfig } from '../sample-plugin.model';
6
25
  import { SamplePluginComponent } from './sample-widget.component';
7
26
 
@@ -11,41 +30,53 @@ import { SamplePluginComponent } from './sample-widget.component';
11
30
  <div class="form-group">
12
31
  <c8y-form-group>
13
32
  <label>Text</label>
14
- <textarea
15
- style="width:100%"
16
- name="text"
17
- [(ngModel)]="config.text"
18
- [required]="true"
19
- ></textarea>
33
+ <textarea style="width: 100%" [formControl]="formGroup.controls.text"></textarea>
20
34
  </c8y-form-group>
21
35
  </div>
22
36
 
23
37
  <ng-template #sampleWidgetPreview>
24
- <c8y-sample-plugin [config]="config"></c8y-sample-plugin>
38
+ <c8y-sample-plugin [config]="config$ | async"></c8y-sample-plugin>
25
39
  </ng-template>
26
40
  `,
27
41
  viewProviders: [{ provide: ControlContainer, useExisting: NgForm }],
28
42
  standalone: true,
29
- imports: [CommonModule, FormsModule, SamplePluginComponent]
43
+ imports: [FormGroupComponent, ReactiveFormsModule, SamplePluginComponent, AsyncPipe]
30
44
  })
31
45
  export class SamplePluginConfigComponent implements DynamicComponent, OnInit {
32
46
  @Input() config: SamplePluginConfig = {};
33
47
 
48
+ formGroup: FormGroup<{ text: FormControl<string | null> }>;
49
+ config$ = new BehaviorSubject<SamplePluginConfig>({});
50
+
34
51
  private readonly alert = inject(AlertService);
35
52
  private readonly widgetConfigService = inject(WidgetConfigService);
53
+ private readonly formBuilder = inject(FormBuilder);
54
+ private readonly form = inject(NgForm);
55
+ private readonly destroyRef = inject(DestroyRef);
36
56
 
37
57
  @ViewChild('sampleWidgetPreview')
38
- set previewMapSet(template: TemplateRef<any>) {
39
- if (template) {
40
- this.widgetConfigService.setPreview(template);
41
- return;
42
- }
43
- this.widgetConfigService.setPreview(null);
58
+ set preview(template: TemplateRef<any>) {
59
+ this.widgetConfigService.setPreview(template ?? null);
44
60
  }
45
61
 
46
62
  ngOnInit(): void {
63
+ this.formGroup = this.formBuilder.group({
64
+ text: [this.config?.text || '', Validators.required]
65
+ });
66
+
67
+ this.form.form.addControl('widgetConfig', this.formGroup);
68
+ this.config$.next(this.config);
69
+
70
+ this.formGroup.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(value => {
71
+ this.config$.next({ ...this.config, ...value });
72
+ });
73
+
47
74
  this.widgetConfigService.addOnBeforeSave(config => {
48
- this.alert.success('Widget added successfully', JSON.stringify(config, null, 2));
75
+ if (this.formGroup.invalid) {
76
+ this.alert.warning('Please enter a valid text.');
77
+ return false;
78
+ }
79
+ Object.assign(config, this.formGroup.value);
49
80
  return true;
50
81
  });
51
82
  }
@@ -1,6 +1,4 @@
1
- import { CommonModule } from '@angular/common';
2
- import { Component, Input } from '@angular/core';
3
- import { FormsModule } from '@c8y/ngx-components';
1
+ import { Component, computed, input } from '@angular/core';
4
2
  import { SamplePluginConfig } from '../sample-plugin.model';
5
3
 
6
4
  @Component({
@@ -8,14 +6,16 @@ import { SamplePluginConfig } from '../sample-plugin.model';
8
6
  template: `
9
7
  <div class="p-16">
10
8
  <h1>Sample-plugin</h1>
11
- <p class="text">{{ config?.text || 'No text' }}</p>
12
- <small>My context is: {{ config?.device?.name || 'No context' }}</small>
9
+ <p class="text">{{ displayText() }}</p>
10
+ <small>My context is: {{ deviceName() }}</small>
13
11
  </div>
14
12
  `,
15
13
  styleUrls: ['./sample-widget.component.css'],
16
- standalone: true,
17
- imports: [CommonModule, FormsModule]
14
+ standalone: true
18
15
  })
19
16
  export class SamplePluginComponent {
20
- @Input() config: SamplePluginConfig;
17
+ readonly config = input<SamplePluginConfig>();
18
+
19
+ readonly displayText = computed(() => this.config()?.text || 'No text');
20
+ readonly deviceName = computed(() => this.config()?.device?.['name'] || 'No context');
21
21
  }