Skip to content

Commit 28338a7

Browse files
elupanovnnixaa
authored andcommitted
feat(user): allow use base64 images for user picture (#238)
1 parent f12e203 commit 28338a7

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

e2e/user.e2e-spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*/
66

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

@@ -36,4 +36,10 @@ describe('nb-user', () => {
3636
badgeTests(badgesConf);
3737
});
3838

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

src/app/user-test/user-test.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ import { NbBadgeComponent } from 'framework/theme/components/badge/badge.compone
108108
[badgePosition]="badge.TOP_LEFT">
109109
</nb-user>
110110
</div>
111+
<div class="test-row" id="base64-image">
112+
<nb-user
113+
[picture]="'data:image/png;base64,aaa'"></nb-user>
114+
</div>
111115
</nb-layout-column>
112116
</nb-layout>
113117
`,

src/framework/theme/components/user/user.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<div class="user-container" (click)="toggleMenu()" [ngClass]="{'with-menu' : hasMenu()}">
2-
<div *ngIf="picture" class="user-picture image" style.background-image="url({{picture}})">
2+
<div *ngIf="imageBackgroundStyle" class="user-picture image" [style.background-image]="imageBackgroundStyle">
33
<nb-badge *ngIf="badgeText" [text]="badgeText" [status]="badgeStatus" [position]="badgePosition"></nb-badge>
44
</div>
5-
<div *ngIf="!picture" class="user-picture background" [style.background-color]="color">
5+
<div *ngIf="!imageBackgroundStyle" class="user-picture background" [style.background-color]="color">
66
<ng-container *ngIf="showInitialsValue">
77
{{ getInitials() }}
88
</ng-container>

src/framework/theme/components/user/user.component.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

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

1011
/**
@@ -118,11 +119,13 @@ export class NbUserComponent {
118119
@Input() title: string;
119120

120121
/**
121-
* Absolute path to a user picture
122+
* Absolute path to a user picture. Or base64 image
122123
* User name initials (JD for John Doe) will be shown if no picture specified
123124
* @type string
124125
*/
125-
@Input() picture: string;
126+
@Input() set picture(value: string) {
127+
this.imageBackgroundStyle = value ? this.domSanitizer.bypassSecurityTrustStyle(`url(${value})`) : null;
128+
}
126129

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

223+
imageBackgroundStyle: SafeStyle;
220224
showNameValue: boolean = true;
221225
showTitleValue: boolean = true;
222226
showInitialsValue: boolean = true;
223227
isMenuShown: boolean = false;
224228

225-
constructor(private el: ElementRef) { }
229+
constructor(
230+
private el: ElementRef,
231+
private domSanitizer: DomSanitizer) { }
226232

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

0 commit comments

Comments
 (0)