|
1 | 1 | import { Component } from '@angular/core'; |
2 | | -import { GridStackOptions } from 'gridstack'; |
3 | | -import { elementCB, nodesCB } from './gridstack.component'; |
| 2 | +import { GridStackOptions, GridStackWidget } from 'gridstack'; |
| 3 | +import { GridstackComponent, elementCB, nodesCB } from './gridstack.component'; |
4 | 4 |
|
| 5 | +// unique ids sets for each item for correct ngFor updating |
| 6 | +let ids = 1; |
5 | 7 | @Component({ |
6 | 8 | selector: 'app-root', |
7 | 9 | templateUrl: './app.component.html', |
8 | 10 | styleUrls: ['./app.component.css'] |
9 | 11 | }) |
10 | 12 | export class AppComponent { |
11 | 13 | // which sample to show |
12 | | - show = 1; |
| 14 | + show = 3; |
13 | 15 |
|
14 | | - public gridstackConfig: GridStackOptions = { |
| 16 | + /** sample grid options and items to load... */ |
| 17 | + public gridOptions: GridStackOptions = { |
15 | 18 | margin: 5, |
16 | 19 | float: true, |
17 | 20 | } |
| 21 | + public items: GridStackWidget[] = [ |
| 22 | + {x: 0, y: 0, minW: 2}, |
| 23 | + {x: 1, y: 1}, |
| 24 | + {x: 2, y: 2}, |
| 25 | + ]; |
18 | 26 |
|
19 | | - public onChange(h: nodesCB) { |
20 | | - console.log('change ', h.nodes.length > 1 ? h.nodes : h.nodes[0]); |
| 27 | + constructor() { |
| 28 | + // give them content and unique id to make sure we track them during changes below... |
| 29 | + this.items.forEach(w => { |
| 30 | + w.content = `item ${ids}`; |
| 31 | + w.id = String(ids++); |
| 32 | + }) |
21 | 33 | } |
22 | 34 |
|
23 | | - public onResizeStop(h: elementCB) { |
24 | | - console.log('resizestop ', h.el.gridstackNode); |
| 35 | + /** called whenever items change size/position/etc.. */ |
| 36 | + public onChange(data: nodesCB) { |
| 37 | + console.log('change ', data.nodes.length > 1 ? data.nodes : data.nodes[0]); |
| 38 | + // TODO: update our list to match ? |
| 39 | + } |
| 40 | + |
| 41 | + public onResizeStop(data: elementCB) { |
| 42 | + console.log('resizestop ', data.el.gridstackNode); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * CRUD TEST operations |
| 47 | + */ |
| 48 | + public add(comp: GridstackComponent) { |
| 49 | + // new array isn't required as Angular seem to detect changes to content |
| 50 | + // this.items = [...this.items, { x:3, y:0, w:3, content:`item ${ids}`, id:String(ids++) }]; |
| 51 | + this.items.push({ x:3, y:0, w:3, content:`item ${ids}`, id:String(ids++)}); |
| 52 | + } |
| 53 | + |
| 54 | + public delete(comp: GridstackComponent) { |
| 55 | + this.items.pop(); |
| 56 | + } |
| 57 | + |
| 58 | + public change(comp: GridstackComponent) { |
| 59 | + // this will not update the DOM nor trigger gridstackItems.changes for GS to auto-update, so call GS update() instead |
| 60 | + // this.items[0].w = 3; |
| 61 | + // comp.updateAll(); |
| 62 | + const n = comp.grid?.engine.nodes[0]; |
| 63 | + if (n?.el) comp.grid?.update(n.el, {w:3}); |
| 64 | + } |
| 65 | + |
| 66 | + /** test updating existing and creating new one */ |
| 67 | + public newLayout(comp: GridstackComponent) { |
| 68 | + this.items = [ |
| 69 | + {x:0, y:1, id:'1', minW:1, w:1}, // new size/constrain |
| 70 | + {x:1, y:1, id:'2'}, |
| 71 | + // {x:2, y:1, id:'3'}, // delete item |
| 72 | + {x:3, y:0, w:3, content:'new item'}, // new item |
| 73 | + ]; |
| 74 | + } |
| 75 | + |
| 76 | + // ngFor unique node id to have correct match between our items used and GS |
| 77 | + public identify(index: number, w: GridStackWidget) { |
| 78 | + return w.id; |
25 | 79 | } |
26 | 80 | } |
0 commit comments