fix people widget form component when no images are present (#2349)

This commit is contained in:
Eugenio Romano 2017-09-19 09:59:05 +01:00 committed by GitHub
parent c76e13de4c
commit c429aa8a10
4 changed files with 86 additions and 19 deletions

View File

@ -24,7 +24,7 @@ export class GroupUserModel {
firstName: string; firstName: string;
id: string; id: string;
lastName: string; lastName: string;
userImageUrl: string; userImage: string;
constructor(json?: any) { constructor(json?: any) {
if (json) { if (json) {
@ -33,6 +33,7 @@ export class GroupUserModel {
this.firstName = json.firstName; this.firstName = json.firstName;
this.id = json.id; this.id = json.id;
this.lastName = json.lastName; this.lastName = json.lastName;
this.userImage = json.userImage;
} }
} }
} }

View File

@ -21,15 +21,15 @@
<error-widget *ngIf="isInvalidFieldRequired()" required="{{ 'FORM.FIELD.REQUIRED' | translate }}"></error-widget> <error-widget *ngIf="isInvalidFieldRequired()" required="{{ 'FORM.FIELD.REQUIRED' | translate }}"></error-widget>
</div> </div>
<div class="adf-people-autocomplete mat-elevation-z2" [hidden]="!popupVisible || users.length === 0"> <div class="adf-people-autocomplete mat-elevation-z2" [hidden]="!popupVisible || users.length === 0">
<md-option *ngFor="let user of users" <md-option *ngFor="let user of users; let i = index"
[id]="field.id +'-'+user.id" [id]="field.id +'-'+user.id"
(click)="onItemClick(user, $event)"> (click)="onItemClick(user, $event)">
<div class="adf-people-widget-row"> <div class="adf-people-widget-row">
<div *ngIf="!user.userImage" class="adf-people-widget-pic"> <div id="adf-people-widget-initial-{{i}}" *ngIf="!user.userImage" class="adf-people-widget-pic">
{{user.firstName[0]}} {{user.lastName[0]}} {{getInitialUserName(user.firstName, user.lastName)}}
</div> </div>
<div *ngIf="user.userImage" class="adf-people-widget-image-row"> <div *ngIf="user.userImage" class="adf-people-widget-image-row">
<img class="adf-people-widget-image" <img id="adf-people-widget-pic-{{i}}" class="adf-people-widget-image"
[src]="user.userImage" [src]="user.userImage"
(error)="onErrorImageLoad(user)"/> (error)="onErrorImageLoad(user)"/>
</div> </div>

View File

@ -15,25 +15,52 @@
* limitations under the License. * limitations under the License.
*/ */
import { ElementRef } from '@angular/core'; import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CoreModule } from 'ng2-alfresco-core';
import { Observable } from 'rxjs/Rx'; import { Observable } from 'rxjs/Rx';
import { ActivitiAlfrescoContentService } from '../../../services/activiti-alfresco.service';
import { FormService } from '../../../services/form.service'; import { FormService } from '../../../services/form.service';
import { MaterialModule } from '../../material.module';
import { FormFieldModel } from '../core/form-field.model'; import { FormFieldModel } from '../core/form-field.model';
import { FormModel } from '../core/form.model'; import { FormModel } from '../core/form.model';
import { GroupUserModel } from '../core/group-user.model'; import { GroupUserModel } from '../core/group-user.model';
import { ErrorWidgetComponent } from '../error/error.component';
import { EcmModelService } from './../../../services/ecm-model.service';
import { PeopleWidgetComponent } from './people.widget'; import { PeopleWidgetComponent } from './people.widget';
describe('PeopleWidgetComponent', () => { describe('PeopleWidgetComponent', () => {
let elementRef: ElementRef;
let formService: FormService;
let widget: PeopleWidgetComponent; let widget: PeopleWidgetComponent;
let fixture: ComponentFixture<PeopleWidgetComponent>;
let element: HTMLElement;
let formService: FormService;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
CoreModule,
MaterialModule
],
declarations: [
PeopleWidgetComponent,
ErrorWidgetComponent
],
providers: [
FormService,
EcmModelService,
ActivitiAlfrescoContentService
]
}).compileComponents();
}));
beforeEach(() => { beforeEach(() => {
formService = new FormService(null, null, null); fixture = TestBed.createComponent(PeopleWidgetComponent);
elementRef = new ElementRef(null); formService = TestBed.get(FormService);
widget = new PeopleWidgetComponent(formService, elementRef);
element = fixture.nativeElement;
widget = fixture.componentInstance;
widget.field = new FormFieldModel(new FormModel()); widget.field = new FormFieldModel(new FormModel());
fixture.detectChanges();
}); });
it('should return empty display name for missing model', () => { it('should return empty display name for missing model', () => {
@ -220,4 +247,31 @@ describe('PeopleWidgetComponent', () => {
expect(widget.value).toBeNull(); expect(widget.value).toBeNull();
expect(widget.field.value).toBeNull(); expect(widget.field.value).toBeNull();
}); });
describe('Image', () => {
it('should show the initial in the image', () => {
widget.users = [
new GroupUserModel({firstName: 'Tony', lastName: 'Stark'}),
new GroupUserModel({firstName: 'John', lastName: 'Doe'})
];
fixture.detectChanges();
expect(element.querySelector('#adf-people-widget-initial-0').innerHTML.trim()).toBe('TS');
expect(element.querySelector('#adf-people-widget-initial-1').innerHTML.trim()).toBe('JD');
});
it('should show the the image if user has an image', () => {
widget.users = [
new GroupUserModel({firstName: 'Tony', lastName: 'Stark', userImage: 'testUrl0'}),
new GroupUserModel({firstName: 'John', lastName: 'Doe', userImage: 'testUrl1'})
];
fixture.detectChanges();
expect((element.querySelector('#adf-people-widget-pic-0') as any).src).toContain('testUrl0');
expect((element.querySelector('#adf-people-widget-pic-1') as any).src).toContain('testUrl1');
});
});
}); });

View File

@ -135,4 +135,16 @@ export class PeopleWidgetComponent extends WidgetComponent implements OnInit, Af
event.preventDefault(); event.preventDefault();
} }
} }
getInitialUserName(firstName: string, lastName: string) {
firstName = (firstName !== null && firstName !== '' ? firstName[0] : '');
lastName = (lastName !== null && lastName !== '' ? lastName[0] : '');
return this.getDisplayUser(firstName, lastName, '');
}
getDisplayUser(firstName: string, lastName: string, delimiter: string = '-'): string {
firstName = (firstName !== null ? firstName : '');
lastName = (lastName !== null ? lastName : '');
return firstName + delimiter + lastName;
}
} }