Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple tag selection into an independent basic component. #2257

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,7 @@
>{{ item.name }}
</nz-form-label>
<nz-form-control nzSpan="8">
<nz-tag
*ngFor="let tag of value; let i = index"
[nzMode]="extra.tag_mode || (tag.type === 1 ? 'closeable' : 'default')"
(nzOnClose)="onRemoveTag(tag)"
[nzColor]="tag.color"
style="margin-top: 4px"
>
{{ sliceTagName(tag) }}
</nz-tag>
<a (click)="onShowTagsModal()">
<nz-tag style="margin-top: 4px">
<i nz-icon nzType="plus"></i>
{{ 'tag.new' | i18n }}
</nz-tag>
</a>
<app-tags-select [mode]="extra.tag_mode" [(value)]="value" (valueChange)="onChange($event)" />
</nz-form-control>
</nz-form-item>

Expand All @@ -239,48 +225,3 @@
</nz-textarea-count>
</nz-form-control>
</nz-form-item>

<!-- 选择TAG弹出框 -->
<nz-modal
*ngIf="item.type === 'tags-selection'"
[(nzVisible)]="isManageModalVisible"
[nzTitle]="'tag.bind' | i18n"
(nzOnCancel)="onManageModalCancel()"
(nzOnOk)="onManageModalOk()"
nzMaskClosable="false"
nzWidth="30%"
[nzOkLoading]="isManageModalOkLoading"
>
<div *nzModalContent class="-inner-content">
<app-toolbar>
<ng-template #left>
<app-multi-func-input type="search" [placeholder]="'tag.search' | i18n" [(value)]="tagSearch" (valueChange)="loadTagsTable()" />
</ng-template>
<ng-template #right>
<button nz-button nzType="primary" routerLink="/setting/tags" style="margin-left: 5px">
<i nz-icon nzType="setting" nzTheme="outline"></i>
{{ 'tag.setting' | i18n }}
</button>
</ng-template>
</app-toolbar>
<nz-table #smallTable nzSize="small" [nzData]="tags" [nzPageSize]="8" [nzLoading]="tagTableLoading">
<thead>
<tr>
<th nzAlign="center" nzLeft nzWidth="4%" [(nzChecked)]="tagCheckedAll" (nzCheckedChange)="onAllChecked($event)"></th>
<th nzAlign="left">{{ 'tag' | i18n }}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let data of smallTable.data">
<td nzAlign="center" nzLeft [nzChecked]="checkedTags.has(data)" (nzCheckedChange)="onItemChecked(data, $event)"></td>
<td nzAlign="left">
<nz-tag *ngIf="data.tagValue == undefined || data.tagValue.trim() == ''" [nzColor]="data.color">{{ data.name }}</nz-tag>
<nz-tag *ngIf="data.tagValue != undefined && data.tagValue.trim() != ''" [nzColor]="data.color">
{{ data.name + ':' + data.tagValue }}
</nz-tag>
</td>
</tr>
</tbody>
</nz-table>
</div>
</nz-modal>
Original file line number Diff line number Diff line change
Expand Up @@ -19,105 +19,19 @@

import { Component, EventEmitter, Input, Output } from '@angular/core';

import { TagItem } from '../../../pojo/NoticeRule';
import { Tag } from '../../../pojo/Tag';
import { TagService } from '../../../service/tag.service';

@Component({
selector: 'app-form-item',
templateUrl: './form-item.component.html',
styleUrls: ['./form-item.component.less']
})
export class FormItemComponent {
constructor(private tagSvc: TagService) {}
constructor() {}
@Input() item!: any;
@Input() value!: any;
@Input() extra: any = {};
@Output() readonly valueChange = new EventEmitter<any>();

isManageModalVisible = false;
isManageModalOkLoading = false;
checkedTags = new Set<Tag>();
tagTableLoading = false;
tagCheckedAll: boolean = false;
tagSearch!: string;
tags!: Tag[];

loadTagsTable() {
this.tagTableLoading = true;
let tagsReq$ = this.tagSvc.loadTags(this.tagSearch, 1, 0, 1000).subscribe(
message => {
this.tagTableLoading = false;
this.tagCheckedAll = false;
this.checkedTags.clear();
if (message.code === 0) {
let page = message.data;
this.tags = page.content;
} else {
console.warn(message.msg);
}
tagsReq$.unsubscribe();
},
error => {
this.tagTableLoading = false;
tagsReq$.unsubscribe();
}
);
}

onChange(value: any) {
this.valueChange.emit(value);
}

onRemoveTag(tag: TagItem) {
if (this.value != undefined) {
this.onChange(this.value.filter((item: TagItem) => item !== tag));
}
}

sliceTagName(tag: any): string {
if (tag.value != undefined && tag.value.trim() != '') {
return `${tag.name}:${tag.value}`;
} else {
return tag.name;
}
}

onShowTagsModal() {
this.isManageModalVisible = true;
this.loadTagsTable();
}

onManageModalCancel() {
this.isManageModalVisible = false;
}

onManageModalOk() {
this.isManageModalOkLoading = true;
let value = this.value == undefined ? [] : this.value;
this.checkedTags.forEach(item => {
if (this.value.find((tag: { id: number }) => tag.id == item.id) == undefined) {
value.push(item);
}
});
this.onChange(value);
this.isManageModalOkLoading = false;
this.isManageModalVisible = false;
}

onAllChecked(checked: boolean) {
if (checked) {
this.tags.forEach(tag => this.checkedTags.add(tag));
} else {
this.checkedTags.clear();
}
}

onItemChecked(tag: Tag, checked: boolean) {
if (checked) {
this.checkedTags.add(tag);
} else {
this.checkedTags.delete(tag);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<ng-container>
<nz-tag
*ngFor="let tag of value; let i = index"
[nzMode]="mode || (tag.type === 1 ? 'closeable' : 'default')"
(nzOnClose)="onRemoveTag(tag)"
[nzColor]="tag.color"
>
{{ sliceTagName(tag) }}
</nz-tag>
<a (click)="onShowTagsModal()">
<nz-tag>
<i nz-icon nzType="plus"></i>
{{ 'tag.new' | i18n }}
</nz-tag>
</a>
</ng-container>

<nz-modal
[(nzVisible)]="isManageModalVisible"
[nzTitle]="'tag.bind' | i18n"
(nzOnCancel)="onManageModalCancel()"
(nzOnOk)="onManageModalOk()"
nzMaskClosable="false"
nzWidth="30%"
[nzOkLoading]="isManageModalOkLoading"
>
<div *nzModalContent class="-inner-content">
<app-toolbar>
<ng-template #left>
<app-multi-func-input type="search" [placeholder]="'tag.search' | i18n" [(value)]="tagSearch" (valueChange)="loadTagsTable()" />
</ng-template>
<ng-template #right>
<button nz-button nzType="primary" routerLink="/setting/tags" style="margin-left: 5px">
<i nz-icon nzType="setting" nzTheme="outline"></i>
{{ 'tag.setting' | i18n }}
</button>
</ng-template>
</app-toolbar>
<nz-table #smallTable nzSize="small" [nzData]="tags" [nzPageSize]="8" [nzLoading]="tagTableLoading">
<thead>
<tr>
<th nzAlign="center" nzLeft nzWidth="4%" [(nzChecked)]="tagCheckedAll" (nzCheckedChange)="onAllChecked($event)"></th>
<th nzAlign="left">{{ 'tag' | i18n }}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let data of smallTable.data">
<td nzAlign="center" nzLeft [nzChecked]="checkedTags.has(data)" (nzCheckedChange)="onItemChecked(data, $event)"></td>
<td nzAlign="left">
<nz-tag *ngIf="data.tagValue == undefined || data.tagValue.trim() == ''" [nzColor]="data.color">{{ data.name }}</nz-tag>
<nz-tag *ngIf="data.tagValue != undefined && data.tagValue.trim() != ''" [nzColor]="data.color">
{{ data.name + ':' + data.tagValue }}
</nz-tag>
</td>
</tr>
</tbody>
</nz-table>
</div>
</nz-modal>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:host {
::ng-deep {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { ComponentFixture, TestBed } from '@angular/core/testing';

import { TagsSelectComponent } from './tags-select.component';

describe('TagsSelectComponent', () => {
let component: TagsSelectComponent;
let fixture: ComponentFixture<TagsSelectComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [TagsSelectComponent]
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(TagsSelectComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Loading
Loading