Private/Angular/Component/List/ServerGrid/New-NgServerGridComponentTsToString.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
Function New-NgServerGridComponentTsToString([TableInfo]$tableInfo) {

    # local variables to make rendering easier
    [string] $serviceName = "$($tableInfo.tableLowerCamel)Service"
    [string] $serviceTypeName = "$($tableInfo.tableCapitalCamel)Service"
    [string] $dataTypeName = "$($tableInfo.tableCapitalCamel)"
    [string] $dataKebab = "$($tableInfo.tableLowerKebab)"
    [string] $primaryKeyName = "PK"
    [string] $itemUrl = "someurl"

<##########################################################################################>
return @"
import 'rxjs/add/operator/switchMap';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, Params } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Observable } from 'rxjs/Rx';
import { GridOptions } from 'ag-grid';
import * as moment from 'moment';

import { $serviceTypeName } from '../service/$($dataKebab).service';
import { $dataTypeName } from '../model/$($dataKebab)';
import { StringService } from '../service/string.service';

@Component({
    selector: '$($dataKebab)-list',
    templateUrl: './$($dataKebab)-list.component.html',
    styleUrls: ['./$($dataKebab)-list.component.css']
})
export class $($dataTypeName)ListComponent implements OnInit {
    private gridOptions: GridOptions;

    constructor(
        private $($serviceName): $serviceTypeName,
        private route: ActivatedRoute,
        private router: Router,
        private stringService: StringService
    ) {
        // Some weird thing with load order, must explicitly supply
        // services here and not rely on this.statusReportService
        this.loadGridOptions($($serviceName));
    }

    ngOnInit(): void {
        this.loadData();
    }

    loadGridOptions($($serviceName): $serviceTypeName): void {
        this.gridOptions = <GridOptions>{};
        this.gridOptions.columnDefs = [
            {
                headerName: "Server",
                field: "server",
                width: 120,
                filter: 'text',
                tooltipField: 'server',
                cellRenderer: function (params: any) {
                    // If we do a true hyperlink, then angular gets
                    // all messed up and reloads the entire SPA page.
                    // Instead, put in a dummy link, and we'll plug
                    // code later to handle the onCellClick
                    return "<a href='javascript:void(0)' >" + params.value + "</a>";
                }
            },
            {
                headerName: "Active",
                field: "active",
                width: 70
            },
            {
                headerName: "Up",
                field: "serverUp",
                width: 50
            },
            {
                headerName: "Reboot",
                field: "lastReboot",
                width: 160,
                suppressFilter: true,
                cellRenderer: function (params:any) {
                    return params.value ? moment(params.value).format("M/D/YY h:mm a") : '';
                }
            },
            {
                headerName: "Count",
                field: "fileCount",
                width: 65,
                filter: 'number',
                cellStyle: { 'text-align': 'right' }
            },
            {
                headerName: "Size",
                field: "fileSize",
                width: 100,
                cellStyle: { 'text-align': 'right' },
                cellRenderer: function (params) {
                    return Intl.NumberFormat().format(params.value);
                },
                filter: 'number',
                tooltipField: 'fileSize'
            },
            {
                headerName: "Pkg Name",
                field: "pkgName",
                width: 120,
                tooltipField: 'pkgName'
            },
            {
                headerName: "Pkg Desc",
                field: "pkgDesc",
                width: 200,
                tooltipField: 'pkgDesc'
            },
            {
                headerName: "Comments",
                field: "comments",
                width: 100,
                tooltipField: 'comments'
            },
            {
                headerName: "Status Age",
                field: "statusAge",
                suppressFilter: true,
                width: 100
            },
            {
                headerName: "Last Action",
                field: "lastAction",
                width: 100
            },
            {
                headerName: "Last Act Stat",
                field: "lastActionStatus",
                width: 100
            },
            {
                headerName: "Last Act Date",
                field: "lastActionDate",
                width: 160,
                //filter: 'date',
                suppressFilter: true,
                suppressMenu: true,
                cellRenderer: function (params) {
                    return params.value ? moment(params.value).format("M/D/YY h:mm a") : '';
                }
            },
        ];

        // The list of servers is about 200
        // and we want real time refresh,
        // and ag-grid won't let us put checkboxes
        // on an infinite scroll, so just load the
        // data direct into memory
        this.gridOptions.floatingFilter = true;
        this.gridOptions.debug = false;
        this.gridOptions.enableColResize = true;
        this.gridOptions.rowSelection = 'multiple';
        this.gridOptions.suppressRowClickSelection = true;
        this.gridOptions.rowModelType = 'normal';
        this.gridOptions.maxConcurrentDatasourceRequests = 2;
        this.gridOptions.deltaRowDataMode = true;
        this.gridOptions.getRowNodeId = function (item) {
            return item.key;
        };

        this.gridOptions.onGridReady = function (event:any) {
            $($serviceName).list().then(
                myData => {
                    event.api.setRowData(myData);
                });
        }
        // Don't do vertical scrollbars, just show everything
        this.gridOptions.domLayout = 'autoHeight';

        this.gridOptions.onCellClicked = function (params:any) {
            if (params.colDef.field == "server") {
                let angularComponent: any = params.api.gridCore.gridOptions.AngularComponent;
                angularComponent.router.navigate([``/$($itemUrl)/`${params.data.$($primaryKeyName)}``]);
            }
        };

        // Hijack GridOptions to add a pointer back to this object
        // that can be used during ag-grid callbacks.
        // Because GridOptions is strongly typed, cheat with an "any" pointer
        let thisTemp: any = this.gridOptions;
        thisTemp.AngularComponent = this;
    }

    loadData(): void {
        this.$($($serviceName)).list().then(
            myData => {
                let myApi = this.gridOptions.api;
                if (myApi) {
                    myApi.setRowData(myData);
                }
            });
    }

}

"@

<##########################################################################################>
}