|
| 1 | +import { WebMcpService, type SlickGridState } from '@slickgrid-universal/web-mcp'; |
| 2 | +import { bindable } from 'aurelia'; |
| 3 | +import { type AureliaGridInstance, type Column, type GridOption } from 'aurelia-slickgrid'; |
| 4 | + |
| 5 | +const NB_ITEMS = 2000; |
| 6 | +const PRIORITIES = ['Low', 'Medium', 'High', 'Critical']; |
| 7 | +const STATUSES = ['Todo', 'In Progress', 'Done', 'Blocked']; |
| 8 | + |
| 9 | +export class Example54 { |
| 10 | + @bindable() textResult = ''; |
| 11 | + aureliaGrid!: AureliaGridInstance; |
| 12 | + columns: Column[] = []; |
| 13 | + gridContainerElm!: HTMLDivElement; |
| 14 | + gridOptions!: GridOption; |
| 15 | + dataset: any[] = []; |
| 16 | + hideSubTitle = false; |
| 17 | + mcpService = new WebMcpService(); |
| 18 | + |
| 19 | + constructor() { |
| 20 | + this.defineGrid(); |
| 21 | + this.showOutput( |
| 22 | + '// Click a button above to inspect or manipulate the grid via the MCP service API.\n// In a real WebMCP-capable browser, an AI assistant calls these same methods automatically.' |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + attached() { |
| 27 | + this.dataset = this.loadData(NB_ITEMS); |
| 28 | + } |
| 29 | + |
| 30 | + aureliaGridReady(aureliaGrid: AureliaGridInstance) { |
| 31 | + this.aureliaGrid = aureliaGrid; |
| 32 | + } |
| 33 | + |
| 34 | + defineGrid() { |
| 35 | + this.columns = [ |
| 36 | + { id: 'id', name: '#', field: 'id', sortable: true, width: 50 }, |
| 37 | + { id: 'title', name: 'Title', field: 'title', sortable: true, filterable: true, width: 200 }, |
| 38 | + { id: 'priority', name: 'Priority', field: 'priority', sortable: true, filterable: true, width: 110 }, |
| 39 | + { id: 'status', name: 'Status', field: 'status', sortable: true, filterable: true, width: 120 }, |
| 40 | + { id: 'duration', name: 'Duration (days)', field: 'duration', sortable: true, filterable: true, type: 'number', width: 140 }, |
| 41 | + { id: 'completed', name: 'Completed %', field: 'completed', sortable: true, filterable: true, type: 'number', width: 130 }, |
| 42 | + ]; |
| 43 | + |
| 44 | + this.gridOptions = { |
| 45 | + enableFiltering: true, |
| 46 | + enableSorting: true, |
| 47 | + gridHeight: 300, |
| 48 | + gridWidth: 800, |
| 49 | + externalResources: [this.mcpService], |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + // --------------------------------------------------------------------------- |
| 54 | + // Button handlers — simulating what an LLM would call via WebMCP tools |
| 55 | + // --------------------------------------------------------------------------- |
| 56 | + |
| 57 | + showSchema() { |
| 58 | + const schema = this.mcpService.getStructuredSchema(); |
| 59 | + this.showOutput(JSON.stringify(schema, null, 2)); |
| 60 | + } |
| 61 | + |
| 62 | + showState() { |
| 63 | + const state = this.mcpService.getGridState(); |
| 64 | + this.showOutput(JSON.stringify(state, null, 2)); |
| 65 | + } |
| 66 | + |
| 67 | + /** Simulate a typical LLM response: filter to High/Critical priority, sort by duration desc */ |
| 68 | + async applyAiState() { |
| 69 | + const aiGeneratedState: Partial<SlickGridState> = { |
| 70 | + filters: [{ columnId: 'priority', searchTerms: ['High'], operator: 'EQ' }], |
| 71 | + sorters: [{ columnId: 'duration', direction: 'DESC' }], |
| 72 | + }; |
| 73 | + this.showOutput(`// Simulated LLM response — applying state:\n${JSON.stringify(aiGeneratedState, null, 2)}`); |
| 74 | + await this.mcpService.applyGridState(aiGeneratedState); |
| 75 | + } |
| 76 | + |
| 77 | + async resetGrid() { |
| 78 | + await this.mcpService.applyGridState({ filters: [], sorters: [] }); |
| 79 | + this.showOutput('// Grid state reset.'); |
| 80 | + } |
| 81 | + |
| 82 | + toggleSubTitle() { |
| 83 | + this.hideSubTitle = !this.hideSubTitle; |
| 84 | + const action = this.hideSubTitle ? 'add' : 'remove'; |
| 85 | + document.querySelector('.subtitle')?.classList[action]('hidden'); |
| 86 | + this.aureliaGrid.resizerService.resizeGrid(0); |
| 87 | + } |
| 88 | + |
| 89 | + // --------------------------------------------------------------------------- |
| 90 | + // Helpers |
| 91 | + // --------------------------------------------------------------------------- |
| 92 | + |
| 93 | + private showOutput(text: string) { |
| 94 | + this.textResult = text; |
| 95 | + } |
| 96 | + |
| 97 | + private loadData(count: number): any[] { |
| 98 | + const data: any[] = []; |
| 99 | + for (let i = 0; i < count; i++) { |
| 100 | + data.push({ |
| 101 | + id: i, |
| 102 | + title: `Task ${i}`, |
| 103 | + priority: PRIORITIES[Math.floor(Math.random() * PRIORITIES.length)], |
| 104 | + status: STATUSES[Math.floor(Math.random() * STATUSES.length)], |
| 105 | + duration: Math.floor(Math.random() * 90) + 1, |
| 106 | + completed: Math.floor(Math.random() * 100), |
| 107 | + }); |
| 108 | + } |
| 109 | + return data; |
| 110 | + } |
| 111 | +} |
0 commit comments