11import { Component , OnInit } from '@angular/core' ;
2- import { TableField , TableSettings } from 'src/app/models/table' ;
2+ import { TableField , TableOrdering , TableSettings } from 'src/app/models/table' ;
33
44import { AlertComponent } from '../../../ui-components/alert/alert.component' ;
55import { Angulartics2 } from 'angulartics2' ;
66import { BreadcrumbsComponent } from '../../../ui-components/breadcrumbs/breadcrumbs.component' ;
77import { CommonModule } from '@angular/common' ;
88import { CompanyService } from 'src/app/services/company.service' ;
99import { ConnectionsService } from 'src/app/services/connections.service' ;
10- import { DragDropModule } from '@angular/cdk/drag-drop' ;
10+ import { CdkDragDrop , DragDropModule , moveItemInArray } from '@angular/cdk/drag-drop' ;
1111import { FormsModule } from '@angular/forms' ;
1212import { IconPickerComponent } from '../../../ui-components/icon-picker/icon-picker.component' ;
1313import { Location } from '@angular/common' ;
@@ -60,14 +60,20 @@ export class DbTableSettingsComponent implements OnInit {
6060 public loading : boolean = true ;
6161 public fields : string [ ] ;
6262 public fields_to_exclude : string [ ] ;
63+ public orderChanged : boolean = false ;
6364 public iconChanged : boolean = false ;
65+ public listFieldsOrder : string [ ] ;
6466 public tableSettingsInitial : TableSettings = {
6567 connection_id : '' ,
6668 table_name : '' ,
6769 icon : '' ,
6870 display_name : '' ,
6971 autocomplete_columns : [ ] ,
7072 identity_column : '' ,
73+ ordering : TableOrdering . Ascending ,
74+ ordering_field : '' ,
75+ list_fields : [ ] ,
76+ columns_view : [ ] ,
7177 search_fields : [ ] ,
7278 excluded_fields : [ ] ,
7379 readonly_fields : [ ] ,
@@ -137,9 +143,13 @@ export class DbTableSettingsComponent implements OnInit {
137143 if ( Object . keys ( res ) . length !== 0 ) {
138144 this . isSettingsExist = true
139145 this . tableSettings = res ;
146+ this . listFieldsOrder = [ ...res . list_fields ] ;
140147 } else {
141148 this . tableSettings = this . tableSettingsInitial ;
142149 } ;
150+ if ( Object . keys ( res ) . length === 0 || ( res && res . list_fields && ! res . list_fields . length ) ) {
151+ this . listFieldsOrder = [ ...this . fields ] ;
152+ } ;
143153 this . title . setTitle ( `${ res . display_name || this . displayTableName } - Table settings | ${ this . _company . companyTabTitle || 'Rocketadmin' } ` ) ;
144154 }
145155 ) ;
@@ -150,6 +160,18 @@ export class DbTableSettingsComponent implements OnInit {
150160 this . iconChanged = true ;
151161 }
152162
163+ drop ( event : CdkDragDrop < string [ ] > ) {
164+ moveItemInArray ( this . listFieldsOrder , event . previousIndex , event . currentIndex ) ;
165+ this . tableSettings . list_fields = [ ...this . listFieldsOrder ] ;
166+ this . orderChanged = true ;
167+ }
168+
169+ resetColumnsOrder ( ) {
170+ this . tableSettings . list_fields = [ ] ;
171+ this . listFieldsOrder = [ ...this . fields ] ;
172+ this . orderChanged = true ;
173+ }
174+
153175 updateSettings ( ) {
154176 this . submitting = true ;
155177 this . tableSettings . connection_id = this . connectionID ;
@@ -160,7 +182,11 @@ export class DbTableSettingsComponent implements OnInit {
160182 for ( const [ key , value ] of Object . entries ( this . tableSettings ) ) {
161183 if ( key !== 'connection_id' && key !== 'table_name' && key !== 'ordering' ) {
162184 if ( Array . isArray ( value ) ) {
163- updatedSettings [ key ] = value . length > 0
185+ if ( key === 'list_fields' ) {
186+ updatedSettings [ key ] = this . orderChanged ;
187+ } else {
188+ updatedSettings [ key ] = value . length > 0 ;
189+ }
164190 } else {
165191 updatedSettings [ key ] = Boolean ( value ) ;
166192 }
0 commit comments