Skip to content

Commit d31b75a

Browse files
committed
improved angular code doc
1 parent d3a49aa commit d31b75a

File tree

5 files changed

+323
-42
lines changed

5 files changed

+323
-42
lines changed

angular/projects/lib/src/lib/base-widget.ts

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,86 @@
44
*/
55

66
/**
7-
* Base interface that all widgets need to implement in order for GridstackItemComponent to correctly save/load/delete/..
7+
* Abstract base class that all custom widgets should extend.
8+
*
9+
* This class provides the interface needed for GridstackItemComponent to:
10+
* - Serialize/deserialize widget data
11+
* - Save/restore widget state
12+
* - Integrate with Angular lifecycle
13+
*
14+
* Extend this class when creating custom widgets for dynamic grids.
15+
*
16+
* @example
17+
* ```typescript
18+
* @Component({
19+
* selector: 'my-custom-widget',
20+
* template: '<div>{{data}}</div>'
21+
* })
22+
* export class MyCustomWidget extends BaseWidget {
23+
* @Input() data: string = '';
24+
*
25+
* serialize() {
26+
* return { data: this.data };
27+
* }
28+
* }
29+
* ```
830
*/
931

1032
import { Injectable } from '@angular/core';
1133
import { NgCompInputs, NgGridStackWidget } from './types';
1234

13-
@Injectable()
14-
export abstract class BaseWidget {
35+
/**
36+
* Base widget class for GridStack Angular integration.
37+
*/
38+
@Injectable()
39+
export abstract class BaseWidget {
1540

16-
/** variable that holds the complete definition of this widgets (with selector,x,y,w,h) */
41+
/**
42+
* Complete widget definition including position, size, and Angular-specific data.
43+
* Populated automatically when the widget is loaded or saved.
44+
*/
1745
public widgetItem?: NgGridStackWidget;
1846

1947
/**
20-
* REDEFINE to return an object representing the data needed to re-create yourself, other than `selector` already handled.
21-
* This should map directly to the @Input() fields of this objects on create, so a simple apply can be used on read
48+
* Override this method to return serializable data for this widget.
49+
*
50+
* Return an object with properties that map to your component's @Input() fields.
51+
* The selector is handled automatically, so only include component-specific data.
52+
*
53+
* @returns Object containing serializable component data
54+
*
55+
* @example
56+
* ```typescript
57+
* serialize() {
58+
* return {
59+
* title: this.title,
60+
* value: this.value,
61+
* settings: this.settings
62+
* };
63+
* }
64+
* ```
2265
*/
2366
public serialize(): NgCompInputs | undefined { return; }
2467

2568
/**
26-
* REDEFINE this if your widget needs to read from saved data and transform it to create itself - you do this for
27-
* things that are not mapped directly into @Input() fields for example.
69+
* Override this method to handle widget restoration from saved data.
70+
*
71+
* Use this for complex initialization that goes beyond simple @Input() mapping.
72+
* The default implementation automatically assigns input data to component properties.
73+
*
74+
* @param w The saved widget data including input properties
75+
*
76+
* @example
77+
* ```typescript
78+
* deserialize(w: NgGridStackWidget) {
79+
* super.deserialize(w); // Call parent for basic setup
80+
*
81+
* // Custom initialization logic
82+
* if (w.input?.complexData) {
83+
* this.processComplexData(w.input.complexData);
84+
* }
85+
* }
86+
* ```
2887
*/
2988
public deserialize(w: NgGridStackWidget) {
3089
// save full description for meta data

angular/projects/lib/src/lib/gridstack-item.component.ts

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,34 @@ import { Component, ElementRef, Input, ViewChild, ViewContainerRef, OnDestroy, C
77
import { GridItemHTMLElement, GridStackNode } from 'gridstack';
88
import { BaseWidget } from './base-widget';
99

10-
/** store element to Ng Class pointer back */
10+
/**
11+
* Extended HTMLElement interface for grid items.
12+
* Stores a back-reference to the Angular component for integration.
13+
*/
1114
export interface GridItemCompHTMLElement extends GridItemHTMLElement {
15+
/** Back-reference to the Angular GridStackItem component */
1216
_gridItemComp?: GridstackItemComponent;
1317
}
1418

1519
/**
16-
* HTML Component Wrapper for gridstack items, in combination with GridstackComponent for parent grid
20+
* Angular component wrapper for individual GridStack items.
21+
*
22+
* This component represents a single grid item and handles:
23+
* - Dynamic content creation and management
24+
* - Integration with parent GridStack component
25+
* - Component lifecycle and cleanup
26+
* - Widget options and configuration
27+
*
28+
* Use in combination with GridstackComponent for the parent grid.
29+
*
30+
* @example
31+
* ```html
32+
* <gridstack>
33+
* <gridstack-item [options]="{x: 0, y: 0, w: 2, h: 1}">
34+
* <my-widget-component></my-widget-component>
35+
* </gridstack-item>
36+
* </gridstack>
37+
* ```
1738
*/
1839
@Component({
1940
selector: 'gridstack-item',
@@ -34,16 +55,37 @@ export interface GridItemCompHTMLElement extends GridItemHTMLElement {
3455
})
3556
export class GridstackItemComponent implements OnDestroy {
3657

37-
/** container to append items dynamically */
58+
/**
59+
* Container for dynamic component creation within this grid item.
60+
* Used to append child components programmatically.
61+
*/
3862
@ViewChild('container', { read: ViewContainerRef, static: true}) public container?: ViewContainerRef;
3963

40-
/** ComponentRef of ourself - used by dynamic object to correctly get removed */
64+
/**
65+
* Component reference for dynamic component removal.
66+
* Used internally when this component is created dynamically.
67+
*/
4168
public ref: ComponentRef<GridstackItemComponent> | undefined;
4269

43-
/** child component so we can save/restore additional data to be saved along */
70+
/**
71+
* Reference to child widget component for serialization.
72+
* Used to save/restore additional data along with grid position.
73+
*/
4474
public childWidget: BaseWidget | undefined;
4575

46-
/** list of options for creating/updating this item */
76+
/**
77+
* Grid item configuration options.
78+
* Defines position, size, and behavior of this grid item.
79+
*
80+
* @example
81+
* ```typescript
82+
* itemOptions: GridStackNode = {
83+
* x: 0, y: 0, w: 2, h: 1,
84+
* noResize: true,
85+
* content: 'Item content'
86+
* };
87+
* ```
88+
*/
4789
@Input() public set options(val: GridStackNode) {
4890
const grid = this.el.gridstackNode?.grid;
4991
if (grid) {

0 commit comments

Comments
 (0)