Blame view

src/app/data/bus-stop/bus-stop.component.ts 10.4 KB
7085f966   Yarik   Validation try
1
2
3
  import { Component, ViewEncapsulation } from '@angular/core';
  import { TdLoadingService } from '@covalent/core';
  import { GridOptions } from 'ag-grid/main';
c680f5b4   Administrator   first commit
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  
  import { BusStopService } from '../../../services/bus-stop.service';
  import { BusStop } from '../../../models/bus-stop';
  import { EditorComponent } from '../../../helpers/editor.component';
  import { RendererComponent } from '../../../helpers/renderer.component';
  import { BusStopCreateService } from '../../../services/bus-stop-create.service';
  import { BooleanSelectListService } from '../../../services/boolean-select-list.service';
  import { RegionSelectList } from '../../../models/region-select-list';
  import { StateCommonSelectList } from '../../../models/state-common-select-list';
  import { RoadSelectList } from '../../../models/road-select-list';
  import { BooleanSelectList } from '../../../models/boolean-select-list';
  import { SettlementSelectList } from '../../../models/settlement-select-list';
  import { SurfaceTypeSelectList } from '../../../models/surface-type-select-list';
  
  
  // only import this if you are using the ag-Grid-Enterprise
  
  @Component({
74a2441c   Administrator   Add ServiceObject
22
      selector: 'bus-grid',
c680f5b4   Administrator   first commit
23
24
      templateUrl: 'bus-stop.component.html',
      styleUrls: ['bus-stop.scss'],
7085f966   Yarik   Validation try
25
      encapsulation: ViewEncapsulation.None,
c680f5b4   Administrator   first commit
26
27
28
  })
  export class BusStopComponent {
  
7085f966   Yarik   Validation try
29
30
      private columnDefs: any[];
      private gridOptions: GridOptions;
c680f5b4   Administrator   first commit
31
32
33
34
35
36
37
38
39
40
41
42
      public showGrid: boolean;
      public rowData: any[];
      public rowCount: string;
      public regions: RegionSelectList[];
      public states: StateCommonSelectList[];
      public surfaceTypes: SurfaceTypeSelectList[];
      public settlements: SettlementSelectList[];
      public roads: RoadSelectList[];
      public boolean: BooleanSelectList[];
      public isLoading: boolean = false;
      public isBootstrapping: boolean = true;
      public isSelected: boolean = true;
c680f5b4   Administrator   first commit
43
44
45
46
  
      constructor(
          protected service: BusStopService,
          private dataService: BusStopCreateService,
7085f966   Yarik   Validation try
47
48
          private booleanService: BooleanSelectListService,
          private loadingService: TdLoadingService,
c680f5b4   Administrator   first commit
49
50
      ) {
          this.gridOptions = <GridOptions>{};
7085f966   Yarik   Validation try
51
52
53
54
55
          this.gridOptions.enableSorting = true;
          this.showGrid = true;
          this.gridOptions.rowModelType = 'virtual';
          this.booleanService.getModels().then((models) => this.boolean = models);
          this.dataService.getModels().then(models => {
c680f5b4   Administrator   first commit
56
57
58
59
60
61
62
63
64
          this.regions = models.regionSelectListDsM as RegionSelectList[];
          this.states = models.stateCommonSelectListDsM as StateCommonSelectList[];
          this.surfaceTypes = models.surfaceTypeSelectListDsM as SurfaceTypeSelectList[];
          this.settlements = models.settlementSelectListDsM as SettlementSelectList[];
          this.roads = models.roadSelectListDsM as RoadSelectList[];
        }).then(() => {
              this.createColumnDefs();
              this.isBootstrapping = false;
        });
7085f966   Yarik   Validation try
65
          this.service.getData().then((data) => {
c680f5b4   Administrator   first commit
66
67
68
            if (data.length){
              this.rowData = data;
            } else {
7085f966   Yarik   Validation try
69
              this.rowData = [new BusStop()];
c680f5b4   Administrator   first commit
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
            }
        }).then(() => {
            this.gridOptions.api.setDatasource(this.setRowData(this.rowData));
            this.gridOptions.api.refreshVirtualPageCache();
        });
      }
      setRowData(allOfTheData) {
        let dataSource = {
            rowCount: null, // behave as infinite scroll
            getRows: function (params) {
                console.log('asking for ' + params.startRow + ' to ' + params.endRow);
                // At this point in your code, you would call the server, using $http if in AngularJS.
                // To make the demo look real, wait for 500ms before returning
                // take a slice of the total rows
                let rowsThisPage = allOfTheData.slice(params.startRow, params.endRow);
                // if on or after the last page, work out the last row.
                let lastRow = -1;
                if (allOfTheData.length <= params.endRow) {
                    lastRow = allOfTheData.length;
                }
                // call the success callback
                params.successCallback(rowsThisPage, lastRow);
            }
        };
        return dataSource;
7085f966   Yarik   Validation try
95
96
97
98
99
100
101
102
103
104
105
106
    }
    enableLoader(): void {
        if (!this.isLoading) {
          this.isLoading = true;
          this.loadingService.register('loading');
        }
    }
    disableLoader(): void {
        if (this.isLoading) {
          this.isLoading = false;
          this.loadingService.resolve('loading');
        }
c680f5b4   Administrator   first commit
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
205
    }
    onDeleteConfirm(event): void {
      if (window.confirm('Вы уверены что хотите удалить??')) {
        event.confirm.resolve();
      } else {
        event.confirm.reject();
      }
    }
    public addNewRow() {
          this.rowData.unshift(new BusStop());
          this.gridOptions.api.setDatasource(this.setRowData(this.rowData));
          this.gridOptions.api.refreshVirtualPageCache();
    }
  
    private createColumnDefs() {
      this.columnDefs = [
        {
          headerName: '#',
          width: 30,
          checkboxSelection: true,
          suppressSorting: true,
          suppressMenu: true,
          pinned: true
        },
        {
          headerName: 'ID',
          field: 'busStopId',
        },
        {
          headerName: 'Назва дороги',
          field: 'roadId',
          editable: true,
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.roads,
            valueCol: 'roadId',
            labelCol: 'name'
          }
        },
        {
          headerName: 'Область',
          field: 'regionId',
          editable: true,
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.regions,
            valueCol: 'regionId',
            labelCol: 'name'
          }
        },
        {
          headerName: 'Місцезнаходження, км+ справа',
          field: 'locationRight',
          editable: true,
        },
        {
          headerName: 'Місцезнаходження, км+ зліва',
          field: 'locationLeft',
          editable: true,
        },
        {
          headerName: 'Технічний стан',
          field: 'stateCommonId',
          editable: true,
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.states,
            valueCol: 'stateCommonId',
            labelCol: 'value'
          }
        },
        {
          headerName: 'Наявність туалету',
          field: 'toiletAvailability',
          editable: true,
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.boolean,
            valueCol: 'value',
            labelCol: 'label'
          }
        }
      ];
    }
  
      private onCellClicked($event) {
          console.log('onCellClicked: ' + $event.rowIndex + ' ' + $event.colDef.field);
      }
  
      private onCellValueChanged($event) {
          if ($event.oldValue !== $event.newValue) {
              let data = JSON.stringify($event.data);
              let id = $event.data.busStopId;
              let result = null;
              if (id) {
7085f966   Yarik   Validation try
206
207
                  this.enableLoader();
                  result = this.service.update(id, data).then(() => this.disableLoader());
c680f5b4   Administrator   first commit
208
209
210
211
212
              } else {
                  // Protection of posting new row being already sent.
                  if (this.isLoading) {
                      return ;
                  }
7085f966   Yarik   Validation try
213
                  this.enableLoader();
c680f5b4   Administrator   first commit
214
215
216
217
                  result = this.service.create(data).then((busStop) => {
                      this.rowData[$event.node.rowIndex] = busStop;
                      this.gridOptions.api.setDatasource(this.setRowData(this.rowData));
                      this.gridOptions.api.refreshVirtualPageCache();
7085f966   Yarik   Validation try
218
                      this.disableLoader();
c680f5b4   Administrator   first commit
219
220
221
222
223
224
225
226
227
228
229
230
231
                  });
              }
          }
      }
  
      private deleteRows() {
          let rows = this.gridOptions.api.getSelectedNodes();
          if (!rows.length) {
              return ;
          }
          rows.forEach(element => {
              let id = element.data.busStopId;
              if (id) {
7085f966   Yarik   Validation try
232
233
                  this.enableLoader();
                  this.service.delete(id).then(() => this.disableLoader());
c680f5b4   Administrator   first commit
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
              }
          });
          // Sort in order to protect array from reindexing (remove rear elements first)
          let sorted = rows.sort((a, b) => {
                  if (a > b) {
                      return -1;
                  } else {
                      return 1;
                  }
              });
          sorted.forEach(item => {
              this.rowData.splice(item.rowIndex, 1);
          });
          this.gridOptions.api.setDatasource(this.setRowData(this.rowData));
          this.gridOptions.api.refreshVirtualPageCache();
      }
  
      private onCellDoubleClicked($event) {
          console.log('onCellDoubleClicked: ' + $event.rowIndex + ' ' + $event.colDef.field);
      }
  
      private onCellContextMenu($event) {
          console.log('onCellContextMenu: ' + $event.rowIndex + ' ' + $event.colDef.field);
      }
  
      private onCellFocused($event) {
          console.log('onCellFocused: (' + $event.rowIndex + ',' + $event.colIndex + ')');
      }
  
      private onRowSelected($event) {
          // taking out, as when we 'select all', it prints to much to the console!!
          // console.log('onRowSelected: ' + $event.node.data.name);
      }
  
      private onSelectionChanged() {
          console.log('selectionChanged');
      }
  
      private onBeforeFilterChanged() {
          console.log('beforeFilterChanged');
      }
  
      private onAfterFilterChanged() {
          console.log('afterFilterChanged');
      }
  
      private onFilterModified() {
          console.log('onFilterModified');
      }
  
      private onBeforeSortChanged() {
          console.log('onBeforeSortChanged');
      }
  
      private onAfterSortChanged() {
          console.log('onAfterSortChanged');
      }
  
      private onVirtualRowRemoved($event) {
          // because this event gets fired LOTS of times, we don't print it to the
          // console. if you want to see it, just uncomment out this line
          // console.log('onVirtualRowRemoved: ' + $event.rowIndex);
      }
  
      private onRowClicked($event) {
          console.log('onRowClicked: ' + $event.node.data.name);
      }
  
      public onQuickFilterChanged($event) {
          this.gridOptions.api.setQuickFilter($event.target.value);
      }
  
      // here we use one generic event to handle all the column type events.
      // the method just prints the event name
      private onColumnEvent($event) {
          console.log('onColumnEvent: ' + $event);
      }
  }