src/app/contacts/groups/groups.component.ts
selector | crm-contact-groups |
styleUrls | groups.component.scss |
templateUrl | ./groups.component.html |
Properties |
|
Methods |
Inputs |
Outputs |
constructor(contactsService: ContactsService, notificationsService: NotificationsService, confirmDialogService: ConfirmDialogService)
|
||||||||||||||||
Parameters :
|
contactsBookId
|
Type: |
groups
|
Type: |
isOwner
|
Type: |
deleteGroupEvent
|
$event type: EventEmitter
|
editGroupEvent
|
$event type: EventEmitter
|
filterEvent
|
$event type: EventEmitter
|
newGroupEvent
|
$event type: EventEmitter
|
addGroup |
addGroup()
|
Returns :
void
|
deleteGroup | ||||||||
deleteGroup(id: number)
|
||||||||
Parameters :
Returns :
void
|
editGroup | ||||||||
editGroup(id: number)
|
||||||||
Parameters :
Returns :
void
|
isGroupNameToLong |
isGroupNameToLong()
|
Returns :
boolean
|
isNewGroupNameToLong |
isNewGroupNameToLong()
|
Returns :
boolean
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
onSelectGroup | ||||||||
onSelectGroup(group: Group)
|
||||||||
Parameters :
Returns :
void
|
switchEditField | ||||||||
switchEditField(index: )
|
||||||||
Parameters :
Returns :
void
|
Private activeGroup |
activeGroup:
|
Type : string
|
Default value : ''
|
Private currentGroupToEdit |
currentGroupToEdit:
|
Type : string
|
Default value : ''
|
Private enableEditGroup |
enableEditGroup:
|
Type : boolean
|
Default value : false
|
Private newGroup |
newGroup:
|
Type : string
|
Default value : ''
|
Private newGroupName |
newGroupName:
|
Type : string
|
Default value : ''
|
import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
import {ContactsService} from "../contacts.service";
import {NotificationsService} from "../../core/notifications/notifications.service";
import {CreateGroupData, UpdateGroupData} from "../contacts.data";
import {Group} from "../shared/group.model";
import {ConfirmDialogService} from "../../core/confirm-dialog/confirm-dialog.service";
@Component({
selector: 'crm-contact-groups',
templateUrl: './groups.component.html',
styleUrls: ['./groups.component.scss']
})
export class GroupsComponent implements OnInit {
@Input() groups: Group[];
@Input() contactsBookId: number;
@Input() isOwner: boolean;
@Output() newGroupEvent = new EventEmitter<Group>();
@Output() editGroupEvent = new EventEmitter();
@Output() deleteGroupEvent = new EventEmitter();
@Output() filterEvent = new EventEmitter<Group>();
private newGroup: string = '';
private newGroupName: string = '';
private enableEditGroup: boolean = false;
private activeGroup: string = '';
private currentGroupToEdit: string = '';
constructor(private contactsService: ContactsService,
private notificationsService: NotificationsService,
private confirmDialogService: ConfirmDialogService) {
}
switchEditField(index) {
this.enableEditGroup = !this.enableEditGroup;
this.activeGroup = index;
this.newGroupName = this.groups[index].name;
this.currentGroupToEdit = this.newGroupName;
}
addGroup() {
let exists = this.groups.some((item) => item.name === this.newGroup);
if (this.newGroup.trim() && !exists) {
this.contactsService.createGroup(new CreateGroupData(this.newGroup, this.contactsBookId)).subscribe(
(group: Group) => {
this.notificationsService.success('Group created');
this.newGroupEvent.emit(group);
this.newGroup = '';
},
error => {
console.log(error)
});
} else {
this.notificationsService.info('Group already exists');
}
}
editGroup(id: number) {
if (this.currentGroupToEdit === this.newGroupName) {// if no changes
this.enableEditGroup = false;
this.activeGroup = '';
} else { // edited group name
let exists = this.groups.some((item) => item.name === this.newGroupName);
if (this.newGroupName.trim() && !exists) {
this.contactsService.updateGroup(new UpdateGroupData(this.newGroupName, id)).subscribe(
ok => {
this.enableEditGroup = false;
this.activeGroup = '';
this.editGroupEvent.emit(new Group(this.newGroupName, id));
this.notificationsService.success('Group updated')
},
error => {
this.notificationsService.error(error.json())
}
);
} else {
this.notificationsService.info('Group with this name already exist')
}
}
}
deleteGroup(id: number) {
this.confirmDialogService.ask('You really want delete group?', 'Delete', 'Delete group confirmation').then(
confirm => {
this.contactsService.deleteGroup(id).subscribe(
ok => {
this.deleteGroupEvent.emit(id);
this.notificationsService.success('Group deleted')
},
error => this.notificationsService.error(error.json())
);
},
cancel => console.debug('Canceled')
);
}
ngOnInit() {
}
onSelectGroup(group: Group) {
this.filterEvent.emit(group);
}
isGroupNameToLong() {
return this.newGroup.length > 25;
}
isNewGroupNameToLong() {
return this.newGroupName.length > 25;
}
}
<div class="card">
<div class="card-block">
<div class="row margin-row-bottom">
<div class="col-sm-12">
<div class="h5 text-primary m-b-0 m-t-h">GROUPS</div>
<div class="text-muted text-uppercase font-weight-bold font-xs">total: {{groups.length}}</div>
</div>
<div class="col-sm-12" *ngIf="isOwner">
<div class="form-group">
<div class="input-group">
<input id="addGroup" class="form-control" type="text" placeholder="Enter new group name..."
[(ngModel)]="newGroup"
(keyup.enter)="addGroup()">
<span class="input-group-btn">
<button class="btn btn-primary" type="button" (click)="addGroup()" [disabled]="newGroup.trim()===''">Add</button>
</span>
</div>
<span *ngIf="isGroupNameToLong()" class="error">Group name is to long</span>
</div>
</div>
<table class="table table-hover table-outline m-b-0 hidden-sm-down">
<tbody>
<tr *ngFor="let group of groups; let i=index">
<td *ngIf="activeGroup!==i" (click)="onSelectGroup(group)">{{group.name}}</td>
<td *ngIf="activeGroup===i" class="low-padding"><input class="form-control" type="text"
[(ngModel)]="newGroupName"
(keyup.enter)="editGroup(group.id)">
<div *ngIf="isNewGroupNameToLong()" class="error">Group name is to long</div>
</td>
<td width="90">
<button *ngIf="!enableEditGroup && isOwner"
class="btn btn-link text-muted action-padding" title="Edit group"
(click)="switchEditField(i)"><span class="fa fa-pencil fa-lg"></span>
</button>
<button *ngIf="activeGroup===i && isOwner"
class="btn btn-link text-muted action-padding" title="Save changes"
(click)="editGroup(group.id)" [disabled]="isNewGroupNameToLong()"><span class="fa fa-check fa-lg"></span>
</button>
<button *ngIf="!enableEditGroup && isOwner"
class="btn btn-link text-muted action-padding" title="Delete group"
(click)="deleteGroup(group.id)"><span
class="fa fa-trash fa-lg"></span>
</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>