Skip to content

Commit

Permalink
allow customize responsive image src generation. add round size gener…
Browse files Browse the repository at this point in the history
…ator and default generator
  • Loading branch information
EverettSummer committed Oct 6, 2024
1 parent 6c24864 commit ab3cc28
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deneb-ui",
"version": "4.3.1",
"version": "4.4.0",
"scripts": {
"ng": "ng",
"start": "ng serve --project Deneb-UI-Demo",
Expand Down
1 change: 1 addition & 0 deletions projects/deneb-ui-demo/src/app/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<a class="item" routerLink="/dropdown">Dropdown</a>
<a class="item" routerLink="/toggle">Toggle</a>
<a class="item" routerLink="/popover">Popover</a>
<a class="item" routerLink="/responsive-image">ResponsiveImage</a>
<div class="item">
<ui-toggle text="Dark Theme" [(ngModel)]="darkTheme" name="darkThemeToggle"></ui-toggle>
</div>
Expand Down
8 changes: 7 additions & 1 deletion projects/deneb-ui-demo/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { PopoverComponent } from './popover/popover.component';
import { TimelineMeterExample } from './timeline-meter/timeline-meter.component';
import { ToastDemo } from './toast/toast.component';
import { ToggleDemo } from './toggle/toggle.component';
import { ResponsiveImageComponent } from './responsive-image/responsive-image.component';

@NgModule({
declarations: [
Expand All @@ -30,7 +31,8 @@ import { ToggleDemo } from './toggle/toggle.component';
DropdownDemo,
ToggleDemo,
PopoverComponent,
PopoverContentComponent
PopoverContentComponent,
ResponsiveImageComponent
],
imports: [
UIModule,
Expand Down Expand Up @@ -70,6 +72,10 @@ import { ToggleDemo } from './toggle/toggle.component';
path: 'popover',
component: PopoverComponent
},
{
path: 'responsive-image',
component: ResponsiveImageComponent
}
], {enableTracing: false})
],
bootstrap: [App]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<div class="demo-container">
<div class="ui six doubling cards">
<div class="card">
<div class="image">
<ui-responsive-image src="https://semantic-ui.com/images/avatar/large/elliot.jpg" [size]="{
width: '100%',
height: 'auto',
ratio: 1,
originalWidth: 650,
originalHeight: 650
}" ></ui-responsive-image>
</div>
</div>
<div class="card">
<div class="image">
<ui-responsive-image src="https://semantic-ui.com/images/avatar/large/helen.jpg" [size]="{
width: '100%',
height: 'auto',
ratio: 1,
originalWidth: 650,
originalHeight: 650
}" ></ui-responsive-image>
</div>
</div>
<div class="card">
<div class="image">
<ui-responsive-image src="https://semantic-ui.com/images/avatar/large/jenny.jpg" [size]="{
width: '100%',
height: 'auto',
ratio: 1,
originalWidth: 650,
originalHeight: 650
}" ></ui-responsive-image>
</div>
</div>
<div class="card">
<div class="image">
<ui-responsive-image src="https://semantic-ui.com/images/avatar/large/veronika.jpg" [size]="{
width: '100%',
height: 'auto',
ratio: 1,
originalWidth: 650,
originalHeight: 650
}" ></ui-responsive-image>
</div>
</div>
<div class="card">
<div class="image">
<ui-responsive-image src="https://semantic-ui.com/images/avatar/large/stevie.jpg" [size]="{
width: '100%',
height: 'auto',
ratio: 1,
originalWidth: 650,
originalHeight: 650
}" ></ui-responsive-image>
</div>
</div>
<div class="card">
<div class="image">
<ui-responsive-image src="https://semantic-ui.com/images/avatar/large/steve.jpg" [size]="{
width: '100%',
height: 'auto',
ratio: 1,
originalWidth: 650,
originalHeight: 650
}" ></ui-responsive-image>
</div>
</div>
</div>
</div>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { ResponsiveImageComponent } from './responsive-image.component';

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

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

fixture = TestBed.createComponent(ResponsiveImageComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Component } from '@angular/core';
import {
ResponsiveGenerateSrcService,
RoundGenerateSrcService,
SRC_GENERATOR_SERVICE
} from '../../../../irohalab/deneb-ui/src';

@Component({
selector: 'app-responsive-image',
templateUrl: './responsive-image.component.html',
styleUrl: './responsive-image.component.less',
providers: [
// {provide: SRC_GENERATOR_SERVICE, useClass: ResponsiveGenerateSrcService},
{provide: SRC_GENERATOR_SERVICE, useClass: RoundGenerateSrcService}
]
})
export class ResponsiveImageComponent {

}
2 changes: 1 addition & 1 deletion projects/irohalab/deneb-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@irohalab/deneb-ui",
"version": "4.3.1",
"version": "4.4.0",
"description": "Building blocks for mira-ui",
"author": "Everett Summer",
"license": "Apache-2.0",
Expand Down
3 changes: 2 additions & 1 deletion projects/irohalab/deneb-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { UIDropdownModule } from './dropdown';
import { UIToggleModule } from './toggle';
import { UIPopoverModule } from './popover';
import { DarkThemeService } from './dark-theme.service';
import { UIResponsiveImageModule } from './responsive-image';

const UI_MODULES = [
UIDialogModule,
Expand All @@ -20,7 +21,7 @@ const UI_MODULES = [
UIDropdownModule,
UIToggleModule,
UIPopoverModule,

UIResponsiveImageModule
];

@NgModule({
Expand Down
2 changes: 2 additions & 0 deletions projects/irohalab/deneb-ui/src/responsive-image/DI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const SRC_GENERATOR_SERVICE = 'SRC_GENERATOR_SERVICE';

2 changes: 2 additions & 0 deletions projects/irohalab/deneb-ui/src/responsive-image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ export class UIResponsiveImageModule {
export * from './responsive.service';
export * from './responsive-image.directive'
export * from './responsive-image-wrapper';
export * from './responsive.generate-src.service';
export * from './DI';
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import {
ChangeDetectorRef, Directive, ElementRef, EventEmitter, HostBinding, HostListener, Input, OnChanges, OnDestroy,
OnInit, Output, SimpleChanges
ChangeDetectorRef,
Directive,
ElementRef,
EventEmitter,
HostBinding,
HostListener,
Inject,
Input,
OnChanges,
OnDestroy,
OnInit,
Output,
SimpleChanges
} from '@angular/core';
import { ObservableStub, UIResponsiveService } from './responsive.service';
import { getRemPixel, getVhInPixel, getVwInPixel } from '../core/dom';
import { SRC_GENERATOR_SERVICE } from './DI';
import { IResponsiveGenerateSrc } from './responsive.generate-src.service';

export interface ResponsiveDimension {
width: string; // px, rem, vw, auto, 100%
Expand Down Expand Up @@ -59,7 +72,8 @@ export class UIResponsiveImage implements OnInit, OnChanges, OnDestroy {

constructor(private _element: ElementRef,
private _responsiveService: UIResponsiveService,
private _changeDetector: ChangeDetectorRef) {
private _changeDetector: ChangeDetectorRef,
@Inject(SRC_GENERATOR_SERVICE) private _srcGeneratorService: IResponsiveGenerateSrc) {
}

@HostListener('load', ['$event'])
Expand Down Expand Up @@ -165,10 +179,11 @@ export class UIResponsiveImage implements OnInit, OnChanges, OnDestroy {
} else {
let width = this._width;
let height = this._height;
let ratio = 0;
if (this._width !== 0 && this._height !== 0
&& Number.isFinite(this.dimension.originalWidth)
&& Number.isFinite(this.dimension.originalHeight)) {
let ratio = this._height / this._width;
ratio = this._height / this._width;
let originalRatio = this.dimension.originalHeight / this.dimension.originalWidth;
if (originalRatio > ratio) {
width = this._width;
Expand All @@ -181,7 +196,8 @@ export class UIResponsiveImage implements OnInit, OnChanges, OnDestroy {
height = this._height;
}
}
this._respSrc = `${this._src}?size=${width}x${height}`;
this._respSrc = this._srcGeneratorService.makeRespSrc(
this._src, width, height, this.dimension.originalWidth, this.dimension.originalHeight, ratio);
}
if (manualChangeDetection) {
this._changeDetector.detectChanges();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Injectable } from '@angular/core';

export interface IResponsiveGenerateSrc {
/**
* return an url for the real src, the width or height may be 0 that means it will be automatic to keep ratio.
* @param src
* @param width
* @param height
* @param originalWidth
* @param originalHeight
* @param ratio the client height/width ratio, can be 0 means the width or height is 0
*/
makeRespSrc(src: string, width: number, height: number, originalWidth: number, originalHeight: number, ratio: number): string;
}

@Injectable()
export class ResponsiveGenerateSrcService implements IResponsiveGenerateSrc {
makeRespSrc(src: string, width: number, height: number, originalWidth: number, originalHeight: number, ratio: number): string {
return `${src}?size=${width}x${height}`;
}
}

@Injectable()
export class RoundGenerateSrcService implements IResponsiveGenerateSrc {
makeRespSrc(src: string, width: number, height: number, originalWidth: number, originalHeight: number, ratio: number): string {
if (width !== 0) {
width = Math.round(width / 20) * 20;
if (height !== 0) {
height = width * ratio;
}
} else if (height !== 0) {
height = Math.round(height / 20) * 20;
if (width !== 0) {
width = height / ratio;
}
}
return `${src}?size=${width}x${height}`;
}
}

0 comments on commit ab3cc28

Please sign in to comment.