Skip to content

Commit

Permalink
feat(user): allow use base64 images for user picture (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
elupanov authored and nnixaa committed Feb 19, 2018
1 parent f12e203 commit 28338a7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
8 changes: 7 additions & 1 deletion e2e/user.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

import { browser } from 'protractor';
import { browser, element, by } from 'protractor';
import { NbBadgeComponent } from '../src/framework/theme/components/badge/badge.component';
import badgeTests from './badge.e2e-spec';

Expand Down Expand Up @@ -36,4 +36,10 @@ describe('nb-user', () => {
badgeTests(badgesConf);
});

it('background image should have base64 image', () => {
element(by.css('#base64-image .user-picture.image')).getCssValue('background-image').then(value => {
expect(value).toEqual('url("data:image/png;base64,aaa")');
});
});

});
4 changes: 4 additions & 0 deletions src/app/user-test/user-test.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ import { NbBadgeComponent } from 'framework/theme/components/badge/badge.compone
[badgePosition]="badge.TOP_LEFT">
</nb-user>
</div>
<div class="test-row" id="base64-image">
<nb-user
[picture]="'data:image/png;base64,aaa'"></nb-user>
</div>
</nb-layout-column>
</nb-layout>
`,
Expand Down
4 changes: 2 additions & 2 deletions src/framework/theme/components/user/user.component.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div class="user-container" (click)="toggleMenu()" [ngClass]="{'with-menu' : hasMenu()}">
<div *ngIf="picture" class="user-picture image" style.background-image="url({{picture}})">
<div *ngIf="imageBackgroundStyle" class="user-picture image" [style.background-image]="imageBackgroundStyle">
<nb-badge *ngIf="badgeText" [text]="badgeText" [status]="badgeStatus" [position]="badgePosition"></nb-badge>
</div>
<div *ngIf="!picture" class="user-picture background" [style.background-color]="color">
<div *ngIf="!imageBackgroundStyle" class="user-picture background" [style.background-color]="color">
<ng-container *ngIf="showInitialsValue">
{{ getInitials() }}
</ng-container>
Expand Down
12 changes: 9 additions & 3 deletions src/framework/theme/components/user/user.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { Component, Input, HostBinding, Output, EventEmitter, HostListener, ElementRef } from '@angular/core';
import { DomSanitizer, SafeStyle } from '@angular/platform-browser';
import { convertToBoolProperty } from '../helpers';

/**
Expand Down Expand Up @@ -118,11 +119,13 @@ export class NbUserComponent {
@Input() title: string;

/**
* Absolute path to a user picture
* Absolute path to a user picture. Or base64 image
* User name initials (JD for John Doe) will be shown if no picture specified
* @type string
*/
@Input() picture: string;
@Input() set picture(value: string) {
this.imageBackgroundStyle = value ? this.domSanitizer.bypassSecurityTrustStyle(`url(${value})`) : null;
}

/**
* Color of the area shown when no picture specified
Expand Down Expand Up @@ -217,12 +220,15 @@ export class NbUserComponent {
*/
@Output() menuClick = new EventEmitter<NbUserMenuItem>();

imageBackgroundStyle: SafeStyle;
showNameValue: boolean = true;
showTitleValue: boolean = true;
showInitialsValue: boolean = true;
isMenuShown: boolean = false;

constructor(private el: ElementRef) { }
constructor(
private el: ElementRef,
private domSanitizer: DomSanitizer) { }

itemClick(event: any, item: NbUserMenuItem): boolean {
this.menuClick.emit(item);
Expand Down

0 comments on commit 28338a7

Please sign in to comment.