Blame view

src/app/data/bus-stop/bus-stop.component.ts 12.2 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
  
  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';
  
c36e5ea5   Administrator   add service-object
18
  import { routerTransition } from '../../../animations/router.animation';
c680f5b4   Administrator   first commit
19
20
21
22
  
  // only import this if you are using the ag-Grid-Enterprise
  
  @Component({
8ae52ec0   Administrator   add animation fil
23
      // tslint:disable-next-line:component-selector
74a2441c   Administrator   Add ServiceObject
24
      selector: 'bus-grid',
c680f5b4   Administrator   first commit
25
26
      templateUrl: 'bus-stop.component.html',
      styleUrls: ['bus-stop.scss'],
7085f966   Yarik   Validation try
27
      encapsulation: ViewEncapsulation.None,
c680f5b4   Administrator   first commit
28
29
30
  })
  export class BusStopComponent {
  
7085f966   Yarik   Validation try
31
32
      private columnDefs: any[];
      private gridOptions: GridOptions;
c680f5b4   Administrator   first commit
33
34
35
36
37
38
39
40
41
42
43
44
      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
45
46
47
48
  
      constructor(
          protected service: BusStopService,
          private dataService: BusStopCreateService,
7085f966   Yarik   Validation try
49
50
          private booleanService: BooleanSelectListService,
          private loadingService: TdLoadingService,
c680f5b4   Administrator   first commit
51
52
      ) {
          this.gridOptions = <GridOptions>{};
7085f966   Yarik   Validation try
53
54
55
56
          this.gridOptions.enableSorting = true;
          this.showGrid = true;
          this.gridOptions.rowModelType = 'virtual';
          this.booleanService.getModels().then((models) => this.boolean = models);
8ae52ec0   Administrator   add animation fil
57
          this.dataService.getModels().then((models) => {
c680f5b4   Administrator   first commit
58
59
60
61
62
63
64
65
66
          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
67
          this.service.getData().then((data) => {
c680f5b4   Administrator   first commit
68
69
70
            if (data.length){
              this.rowData = data;
            } else {
7085f966   Yarik   Validation try
71
              this.rowData = [new BusStop()];
c680f5b4   Administrator   first commit
72
73
74
75
76
77
78
            }
        }).then(() => {
            this.gridOptions.api.setDatasource(this.setRowData(this.rowData));
            this.gridOptions.api.refreshVirtualPageCache();
        });
      }
      setRowData(allOfTheData) {
8ae52ec0   Administrator   add animation fil
79
        // tslint:disable-next-line:typedef
c680f5b4   Administrator   first commit
80
81
82
        let dataSource = {
            rowCount: null, // behave as infinite scroll
            getRows: function (params) {
c680f5b4   Administrator   first commit
83
84
85
86
87
88
89
90
91
92
93
                // 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);
8ae52ec0   Administrator   add animation fil
94
            },
c680f5b4   Administrator   first commit
95
96
        };
        return dataSource;
7085f966   Yarik   Validation try
97
98
99
100
101
102
103
104
105
106
107
108
    }
    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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    }
    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,
8ae52ec0   Administrator   add animation fil
131
          pinned: true,
c680f5b4   Administrator   first commit
132
133
134
135
136
137
138
139
140
141
142
143
144
145
        },
        {
          headerName: 'ID',
          field: 'busStopId',
        },
        {
          headerName: 'Назва дороги',
          field: 'roadId',
          editable: true,
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.roads,
            valueCol: 'roadId',
8ae52ec0   Administrator   add animation fil
146
147
            labelCol: 'name',
          },
c680f5b4   Administrator   first commit
148
149
150
151
152
153
154
155
156
157
        },
        {
          headerName: 'Область',
          field: 'regionId',
          editable: true,
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.regions,
            valueCol: 'regionId',
8ae52ec0   Administrator   add animation fil
158
159
            labelCol: 'name',
          },
c680f5b4   Administrator   first commit
160
161
162
163
164
165
166
167
168
169
170
171
        },
        {
          headerName: 'Місцезнаходження, км+ справа',
          field: 'locationRight',
          editable: true,
        },
        {
          headerName: 'Місцезнаходження, км+ зліва',
          field: 'locationLeft',
          editable: true,
        },
        {
8f0bd441   Yarik   Fields
172
173
174
175
176
177
178
179
          headerName: 'Тип покриття',
          field: 'surfaceTypeId',
          editable: true,
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.surfaceTypes,
            valueCol: 'surfaceTypeId',
8ae52ec0   Administrator   add animation fil
180
181
            labelCol: 'name',
          },
8f0bd441   Yarik   Fields
182
183
        },
        {
c680f5b4   Administrator   first commit
184
185
186
187
188
189
190
191
          headerName: 'Технічний стан',
          field: 'stateCommonId',
          editable: true,
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.states,
            valueCol: 'stateCommonId',
8ae52ec0   Administrator   add animation fil
192
193
            labelCol: 'value',
          },
c680f5b4   Administrator   first commit
194
        },
8f0bd441   Yarik   Fields
195
196
197
198
199
200
201
202
203
204
        ,
        {
          headerName: 'Наявність елементів зупин. майдан',
          field: 'areaStopAvailability',
          editable: true,
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.boolean,
            valueCol: 'value',
8ae52ec0   Administrator   add animation fil
205
206
            labelCol: 'label',
          },
8f0bd441   Yarik   Fields
207
208
209
210
211
212
213
214
215
216
        },
        {
          headerName: 'Наявність елементів посад. майдан',
          field: 'areaLandAvailability',
          editable: true,
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.boolean,
            valueCol: 'value',
8ae52ec0   Administrator   add animation fil
217
218
            labelCol: 'label',
          },
8f0bd441   Yarik   Fields
219
220
221
222
223
224
225
226
227
228
        },
        {
          headerName: 'Наявність елементів заїзна кишеня',
          field: 'pocketAvailability',
          editable: true,
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.boolean,
            valueCol: 'value',
8ae52ec0   Administrator   add animation fil
229
230
            labelCol: 'label',
          },
8f0bd441   Yarik   Fields
231
        },
c680f5b4   Administrator   first commit
232
233
234
235
236
237
238
239
240
        {
          headerName: 'Наявність туалету',
          field: 'toiletAvailability',
          editable: true,
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.boolean,
            valueCol: 'value',
8ae52ec0   Administrator   add animation fil
241
242
            labelCol: 'label',
          },
8f0bd441   Yarik   Fields
243
244
245
246
247
248
249
250
251
252
        },
        {
          headerName: 'Рік будівництва',
          field: 'yearBuild',
          editable: true,
        },
        {
          headerName: 'Рік ремонту',
          field: 'yearRepair',
          editable: true,
8ae52ec0   Administrator   add animation fil
253
        },
c680f5b4   Administrator   first commit
254
255
256
257
258
259
260
261
262
263
264
265
266
      ];
    }
  
      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
267
268
                  this.enableLoader();
                  result = this.service.update(id, data).then(() => this.disableLoader());
c680f5b4   Administrator   first commit
269
270
271
272
273
              } else {
                  // Protection of posting new row being already sent.
                  if (this.isLoading) {
                      return ;
                  }
7085f966   Yarik   Validation try
274
                  this.enableLoader();
c680f5b4   Administrator   first commit
275
276
277
278
                  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
279
                      this.disableLoader();
c680f5b4   Administrator   first commit
280
281
282
283
284
285
286
287
288
289
                  });
              }
          }
      }
  
      private deleteRows() {
          let rows = this.gridOptions.api.getSelectedNodes();
          if (!rows.length) {
              return ;
          }
8ae52ec0   Administrator   add animation fil
290
          rows.forEach((element) => {
c680f5b4   Administrator   first commit
291
292
              let id = element.data.busStopId;
              if (id) {
7085f966   Yarik   Validation try
293
294
                  this.enableLoader();
                  this.service.delete(id).then(() => this.disableLoader());
c680f5b4   Administrator   first commit
295
296
297
298
299
300
301
302
303
304
              }
          });
          // 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;
                  }
              });
8ae52ec0   Administrator   add animation fil
305
          sorted.forEach((item) => {
c680f5b4   Administrator   first commit
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
              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);
      }
  }