organizations.component.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 { I18NService } from '@core';
  19. import { ALAIN_I18N_TOKEN, SettingsService } from '@delon/theme';
  20. import { format, addDays } from 'date-fns';
  21. import { NzSafeAny } from 'ng-zorro-antd/core/types';
  22. import { NzContextMenuService, NzDropdownMenuComponent } from 'ng-zorro-antd/dropdown';
  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 { NzFormatEmitEvent, NzTreeNode, NzTreeNodeOptions } from 'ng-zorro-antd/tree';
  27. import { PageResults } from '../../../entity/PageResults';
  28. import { TreeNodes } from '../../../entity/TreeNodes';
  29. import { OrganizationsService } from '../../../service/organizations.service';
  30. import { set2String } from '../../../shared/index';
  31. import { OrganizationEditerComponent } from './organization-editer/organization-editer.component';
  32. @Component({
  33. selector: 'app-organizations',
  34. templateUrl: './organizations.component.html',
  35. styles: [
  36. `
  37. nz-tree {
  38. overflow: hidden;
  39. margin: 0 -24px;
  40. padding: 0 24px;
  41. }
  42. .custom-node {
  43. cursor: pointer;
  44. line-height: 24px;
  45. margin-left: 4px;
  46. display: inline-block;
  47. }
  48. .file-name,
  49. .folder-name {
  50. margin-left: 4px;
  51. }
  52. .file-desc,
  53. .folder-desc {
  54. padding: 0 8px;
  55. display: inline-block;
  56. background: #87ceff;
  57. color: #ffffff;
  58. position: relative;
  59. left: 12px;
  60. }
  61. `
  62. ],
  63. styleUrls: ['./organizations.component.less']
  64. })
  65. export class OrganizationsComponent implements OnInit {
  66. query: {
  67. params: {
  68. orgName: String;
  69. displayName: String;
  70. parentId: String;
  71. startDate: String;
  72. endDate: String;
  73. startDatePicker: Date;
  74. endDatePicker: Date;
  75. pageSize: number;
  76. pageNumber: number;
  77. pageSizeOptions: number[];
  78. };
  79. results: PageResults;
  80. expandForm: Boolean;
  81. submitLoading: boolean;
  82. tableLoading: boolean;
  83. tableCheckedId: Set<String>;
  84. indeterminate: boolean;
  85. checked: boolean;
  86. } = {
  87. params: {
  88. orgName: '',
  89. displayName: '',
  90. parentId: '',
  91. startDate: '',
  92. endDate: '',
  93. startDatePicker: addDays(new Date(), -30),
  94. endDatePicker: new Date(),
  95. pageSize: 10,
  96. pageNumber: 1,
  97. pageSizeOptions: [10, 20, 50]
  98. },
  99. results: new PageResults(),
  100. expandForm: false,
  101. submitLoading: false,
  102. tableLoading: false,
  103. tableCheckedId: new Set<String>(),
  104. indeterminate: false,
  105. checked: false
  106. };
  107. // TreeNodes
  108. treeNodes = new TreeNodes(false);
  109. constructor(
  110. private modal: NzModalService,
  111. private viewContainerRef: ViewContainerRef,
  112. private orgsService: OrganizationsService,
  113. private fb: FormBuilder,
  114. private msg: NzMessageService,
  115. @Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,
  116. private cdr: ChangeDetectorRef
  117. ) {}
  118. ngOnInit(): void {
  119. this.fetch();
  120. this.tree();
  121. }
  122. onQueryParamsChange(tableQueryParams: NzTableQueryParams): void {
  123. this.query.params.pageNumber = tableQueryParams.pageIndex;
  124. this.query.params.pageSize = tableQueryParams.pageSize;
  125. this.fetch();
  126. }
  127. onSearch(): void {
  128. this.fetch();
  129. }
  130. onReset(): void {}
  131. onBatchDelete(): void {
  132. this.orgsService.delete(set2String(this.query.tableCheckedId)).subscribe(res => {
  133. if (res.code == 0) {
  134. this.msg.success(this.i18n.fanyi('mxk.alert.delete.success'));
  135. this.fetch();
  136. } else {
  137. this.msg.error(this.i18n.fanyi('mxk.alert.delete.error'));
  138. }
  139. this.cdr.detectChanges();
  140. });
  141. }
  142. onAdd(e: MouseEvent): void {
  143. e.preventDefault();
  144. const modal = this.modal.create({
  145. nzContent: OrganizationEditerComponent,
  146. nzViewContainerRef: this.viewContainerRef,
  147. nzComponentParams: {
  148. isEdit: false,
  149. parentNode: this.treeNodes.activated,
  150. orgNodes: this.treeNodes.nodes,
  151. id: ''
  152. },
  153. nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
  154. });
  155. // Return a result when closed
  156. modal.afterClose.subscribe(result => {
  157. if (result.refresh) {
  158. this.fetch();
  159. this.tree();
  160. }
  161. });
  162. }
  163. onEdit(e: MouseEvent, editId: String): void {
  164. e.preventDefault();
  165. const modal = this.modal.create({
  166. nzContent: OrganizationEditerComponent,
  167. nzViewContainerRef: this.viewContainerRef,
  168. nzComponentParams: {
  169. isEdit: true,
  170. orgNodes: this.treeNodes.nodes,
  171. id: editId
  172. },
  173. nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
  174. });
  175. // Return a result when closed
  176. modal.afterClose.subscribe(result => {
  177. if (result.refresh) {
  178. this.fetch();
  179. this.tree();
  180. }
  181. });
  182. }
  183. onDelete(deleteId: String): void {
  184. this.orgsService.delete(deleteId).subscribe(res => {
  185. if (res.code == 0) {
  186. this.msg.success(this.i18n.fanyi('mxk.alert.delete.success'));
  187. this.fetch();
  188. this.tree();
  189. } else {
  190. this.msg.error(this.i18n.fanyi('mxk.alert.delete.error'));
  191. }
  192. this.cdr.detectChanges();
  193. });
  194. }
  195. tree(): void {
  196. this.orgsService.tree({}).subscribe(res => {
  197. this.treeNodes.init(res.data);
  198. this.treeNodes.nodes = this.treeNodes.build();
  199. this.cdr.detectChanges();
  200. });
  201. }
  202. fetch(): void {
  203. this.query.submitLoading = true;
  204. this.query.tableLoading = true;
  205. this.query.indeterminate = false;
  206. this.query.checked = false;
  207. this.query.tableCheckedId.clear();
  208. if (this.query.expandForm) {
  209. this.query.params.endDate = format(this.query.params.endDatePicker, 'yyyy-MM-dd HH:mm:ss');
  210. this.query.params.startDate = format(this.query.params.startDatePicker, 'yyyy-MM-dd HH:mm:ss');
  211. } else {
  212. this.query.params.endDate = '';
  213. this.query.params.startDate = '';
  214. }
  215. this.orgsService.fetch(this.query.params).subscribe(res => {
  216. this.query.results = res.data;
  217. this.query.submitLoading = false;
  218. this.query.tableLoading = false;
  219. this.cdr.detectChanges();
  220. });
  221. }
  222. updateTableCheckedSet(id: String, checked: boolean): void {
  223. if (checked) {
  224. this.query.tableCheckedId.add(id);
  225. } else {
  226. this.query.tableCheckedId.delete(id);
  227. }
  228. }
  229. refreshTableCheckedStatus(): void {
  230. const listOfEnabledData = this.query.results.rows.filter(({ disabled }) => !disabled);
  231. this.query.checked = listOfEnabledData.every(({ id }) => this.query.tableCheckedId.has(id));
  232. this.query.indeterminate = listOfEnabledData.some(({ id }) => this.query.tableCheckedId.has(id)) && !this.query.checked;
  233. }
  234. onTableItemChecked(id: String, checked: boolean): void {
  235. this.updateTableCheckedSet(id, checked);
  236. this.refreshTableCheckedStatus();
  237. }
  238. onTableAllChecked(checked: boolean): void {
  239. this.query.results.rows.filter(({ disabled }) => !disabled).forEach(({ id }) => this.updateTableCheckedSet(id, checked));
  240. this.refreshTableCheckedStatus();
  241. }
  242. openFolder(data: NzTreeNode | NzFormatEmitEvent): void {
  243. // do something if u want
  244. if (data instanceof NzTreeNode) {
  245. data.isExpanded = !data.isExpanded;
  246. } else {
  247. const node = data.node;
  248. if (node) {
  249. node.isExpanded = !node.isExpanded;
  250. }
  251. }
  252. }
  253. activeNode(data: NzFormatEmitEvent): void {
  254. this.treeNodes.activated = data.node!;
  255. this.query.params.parentId = data.node!.key;
  256. this.fetch();
  257. }
  258. contextMenu($event: MouseEvent, menu: NzDropdownMenuComponent): void {
  259. //this.nzContextMenuService.create($event, menu);
  260. }
  261. selectDropdown(): void {
  262. // do something
  263. }
  264. }