groups.component.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { ChangeDetectionStrategy, ViewContainerRef, ChangeDetectorRef, Component, OnInit, Inject } from '@angular/core';
  17. import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  18. import { Router } from '@angular/router';
  19. import { I18NService } from '@core';
  20. import { _HttpClient, ALAIN_I18N_TOKEN, SettingsService } from '@delon/theme';
  21. import { format, addDays } from 'date-fns';
  22. import { NzSafeAny } from 'ng-zorro-antd/core/types';
  23. import { NzMessageService } from 'ng-zorro-antd/message';
  24. import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
  25. import { NzTableQueryParams } from 'ng-zorro-antd/table';
  26. import { GroupsService } from '../../../service/Groups.service';
  27. import { set2String } from '../../../shared/index';
  28. import { GroupEditerComponent } from './group-editer/group-editer.component';
  29. @Component({
  30. selector: 'app-groups',
  31. templateUrl: './groups.component.html',
  32. styleUrls: ['./groups.component.less']
  33. })
  34. export class GroupsComponent implements OnInit {
  35. query: {
  36. params: {
  37. groupName: String;
  38. displayName: String;
  39. employeeNumber: String;
  40. startDate: String;
  41. endDate: String;
  42. startDatePicker: Date;
  43. endDatePicker: Date;
  44. pageSize: number;
  45. pageNumber: number;
  46. pageSizeOptions: number[];
  47. };
  48. results: {
  49. records: number;
  50. rows: NzSafeAny[];
  51. };
  52. expandForm: Boolean;
  53. submitLoading: boolean;
  54. tableLoading: boolean;
  55. tableCheckedId: Set<String>;
  56. indeterminate: boolean;
  57. checked: boolean;
  58. } = {
  59. params: {
  60. groupName: '',
  61. displayName: '',
  62. employeeNumber: '',
  63. startDate: '',
  64. endDate: '',
  65. startDatePicker: addDays(new Date(), -30),
  66. endDatePicker: new Date(),
  67. pageSize: 10,
  68. pageNumber: 1,
  69. pageSizeOptions: [10, 20, 50]
  70. },
  71. results: {
  72. records: 0,
  73. rows: []
  74. },
  75. expandForm: false,
  76. submitLoading: false,
  77. tableLoading: false,
  78. tableCheckedId: new Set<String>(),
  79. indeterminate: false,
  80. checked: false
  81. };
  82. constructor(
  83. private modalService: NzModalService,
  84. private viewContainerRef: ViewContainerRef,
  85. private groupsService: GroupsService,
  86. private fb: FormBuilder,
  87. private msg: NzMessageService,
  88. private router: Router,
  89. @Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,
  90. private cdr: ChangeDetectorRef
  91. ) {}
  92. ngOnInit(): void {
  93. this.fetch();
  94. }
  95. onQueryParamsChange(tableQueryParams: NzTableQueryParams): void {
  96. this.query.params.pageNumber = tableQueryParams.pageIndex;
  97. this.query.params.pageSize = tableQueryParams.pageSize;
  98. this.fetch();
  99. }
  100. onSearch(): void {
  101. this.fetch();
  102. }
  103. onReset(): void {}
  104. onBatchDelete(e: MouseEvent): void {
  105. e.preventDefault();
  106. this.groupsService.delete(set2String(this.query.tableCheckedId)).subscribe(res => {
  107. if (res.code == 0) {
  108. this.msg.success(this.i18n.fanyi('mxk.alert.delete.success'));
  109. this.fetch();
  110. } else {
  111. this.msg.error(this.i18n.fanyi('mxk.alert.delete.error'));
  112. }
  113. this.cdr.detectChanges();
  114. });
  115. }
  116. onAdd(e: MouseEvent): void {
  117. e.preventDefault();
  118. const modal = this.modalService.create({
  119. nzContent: GroupEditerComponent,
  120. nzViewContainerRef: this.viewContainerRef,
  121. nzComponentParams: {
  122. isEdit: false,
  123. id: ''
  124. },
  125. nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
  126. });
  127. // Return a result when closed
  128. modal.afterClose.subscribe(result => {
  129. if (result.refresh) {
  130. this.fetch();
  131. }
  132. });
  133. }
  134. onEdit(e: MouseEvent, editiId: String): void {
  135. e.preventDefault();
  136. const modal = this.modalService.create({
  137. nzContent: GroupEditerComponent,
  138. nzViewContainerRef: this.viewContainerRef,
  139. nzComponentParams: {
  140. isEdit: true,
  141. id: editiId
  142. },
  143. nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
  144. });
  145. // Return a result when closed
  146. modal.afterClose.subscribe(result => {
  147. if (result.refresh) {
  148. this.fetch();
  149. }
  150. });
  151. }
  152. onDelete(e: MouseEvent, deleteId: String): void {
  153. e.preventDefault();
  154. this.groupsService.delete(deleteId).subscribe(res => {
  155. if (res.code == 0) {
  156. this.msg.success(this.i18n.fanyi('mxk.alert.delete.success'));
  157. this.fetch();
  158. } else {
  159. this.msg.error(this.i18n.fanyi('mxk.alert.delete.error'));
  160. }
  161. this.cdr.detectChanges();
  162. });
  163. }
  164. onMembers(e: MouseEvent, groupId: String, groupName: String): void {
  165. this.router.navigateByUrl(`/idm/groupmembers?groupId=${groupId}&groupName=${groupName}`);
  166. }
  167. onPermissions(e: MouseEvent, groupId: String, groupName: String): void {
  168. this.router.navigateByUrl(`/access/permissions?groupId=${groupId}&groupName=${groupName}`);
  169. }
  170. fetch(): void {
  171. this.query.submitLoading = true;
  172. this.query.tableLoading = true;
  173. this.query.indeterminate = false;
  174. this.query.checked = false;
  175. this.query.tableCheckedId.clear();
  176. if (this.query.expandForm) {
  177. this.query.params.endDate = format(this.query.params.endDatePicker, 'yyyy-MM-dd HH:mm:ss');
  178. this.query.params.startDate = format(this.query.params.startDatePicker, 'yyyy-MM-dd HH:mm:ss');
  179. } else {
  180. this.query.params.endDate = '';
  181. this.query.params.startDate = '';
  182. }
  183. this.groupsService.fetch(this.query.params).subscribe(res => {
  184. this.query.results = res.data;
  185. this.query.submitLoading = false;
  186. this.query.tableLoading = false;
  187. this.cdr.detectChanges();
  188. });
  189. }
  190. updateTableCheckedSet(id: String, checked: boolean): void {
  191. if (checked) {
  192. this.query.tableCheckedId.add(id);
  193. } else {
  194. this.query.tableCheckedId.delete(id);
  195. }
  196. }
  197. refreshTableCheckedStatus(): void {
  198. const listOfEnabledData = this.query.results.rows.filter(({ disabled }) => !disabled);
  199. this.query.checked = listOfEnabledData.every(({ id }) => this.query.tableCheckedId.has(id));
  200. this.query.indeterminate = listOfEnabledData.some(({ id }) => this.query.tableCheckedId.has(id)) && !this.query.checked;
  201. }
  202. onTableItemChecked(id: String, checked: boolean): void {
  203. this.updateTableCheckedSet(id, checked);
  204. this.refreshTableCheckedStatus();
  205. }
  206. onTableAllChecked(checked: boolean): void {
  207. this.query.results.rows.filter(({ disabled }) => !disabled).forEach(({ id }) => this.updateTableCheckedSet(id, checked));
  208. this.refreshTableCheckedStatus();
  209. }
  210. }