Skip to content

Commit

Permalink
refactor(control-flow): includes alert, anchor, avatar, back-top, aff…
Browse files Browse the repository at this point in the history
…ix and badge
  • Loading branch information
ng-nest-moon committed Dec 12, 2023
1 parent 8f1e8d0 commit 68b0124
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 176 deletions.
118 changes: 66 additions & 52 deletions lib/ng-nest/ui/alert/alert.component.html
Original file line number Diff line number Diff line change
@@ -1,54 +1,68 @@
<div
#alert
class="x-alert"
cdkDrag
(cdkDragEnded)="dragEnded.emit($event)"
[cdkDragFreeDragPosition]="dragFreeDragPosition"
[cdkDragDisabled]="!draggable"
[cdkDragBoundary]="dragBoundary"
[ngClass]="classMap"
*ngIf="!hide"
@x-fade-animation
[@.disabled]="disabledAnimation"
(@x-fade-animation.done)="onCloseAnimationDone()"
[xResizable]="resizable!"
(resizing)="resizing.emit($event)"
[offsetLeft]="offsetLeft"
[offsetTop]="offsetTop"
[style.minWidth]="minWidth"
[style.minHeight]="minHeight"
>
<ng-container *ngIf="showIcon">
<ng-container *ngTemplateOutlet="iconTpl"></ng-container>
</ng-container>
<ng-template #iconTpl>
<ng-container [ngSwitch]="type">
<x-icon class="x-alert-icon" *ngSwitchCase="'success'" type="adf-check-circle"></x-icon>
<x-icon class="x-alert-icon" *ngSwitchCase="'info'" type="adf-info-circle"></x-icon>
<x-icon class="x-alert-icon" *ngSwitchCase="'warning'" type="adf-exclamation-circle"></x-icon>
<x-icon class="x-alert-icon" *ngSwitchCase="'error'" type="adf-close-circle"></x-icon>
<x-icon class="x-alert-icon" *ngSwitchCase="'loading'" type="fto-loader" [spin]="true"></x-icon>
</ng-container>
</ng-template>
<div class="x-alert-inner">
<span class="x-alert-title" cdkDragHandle [class.x-bold]="title && content">
<ng-container *xOutlet="title; context: { $iconTpl: iconTpl }">{{ title }}</ng-container>
</span>
<div class="x-alert-content" *ngIf="content">
<ng-container *xOutlet="content; context: { $iconTpl: iconTpl }">{{ content }}</ng-container>
</div>
<div class="x-alert-close">
<div class="x-alert-operation" *xOutlet="operationTpl">{{ operationTpl }}</div>
<x-button
*ngIf="!hideClose"
size="small"
[icon]="!closeText ? 'fto-x' : ''"
[onlyIcon]="!closeText"
[type]="closeText ? 'text' : 'initial'"
(click)="onClose()"
closable
>{{ closeText }}</x-button
>
@if (!hide) {
<div
#alert
class="x-alert"
cdkDrag
(cdkDragEnded)="dragEnded.emit($event)"
[cdkDragFreeDragPosition]="dragFreeDragPosition"
[cdkDragDisabled]="!draggable"
[cdkDragBoundary]="dragBoundary"
[ngClass]="classMap"
@x-fade-animation
[@.disabled]="disabledAnimation"
(@x-fade-animation.done)="onCloseAnimationDone()"
[xResizable]="resizable!"
(resizing)="resizing.emit($event)"
[offsetLeft]="offsetLeft"
[offsetTop]="offsetTop"
[style.minWidth]="minWidth"
[style.minHeight]="minHeight"
>
@if (showIcon) {
<ng-container *ngTemplateOutlet="iconTpl"></ng-container>
}
<ng-template #iconTpl>
@switch (type) {
@case ('success') {
<x-icon class="x-alert-icon" type="adf-check-circle"></x-icon>
}
@case ('info') {
<x-icon class="x-alert-icon" type="adf-info-circle"></x-icon>
}
@case ('warning') {
<x-icon class="x-alert-icon" type="adf-exclamation-circle"></x-icon>
}
@case ('error') {
<x-icon class="x-alert-icon" type="adf-close-circle"></x-icon>
}
@case ('loading') {
<x-icon class="x-alert-icon" type="fto-loader" [spin]="true"></x-icon>
}
}
</ng-template>
<div class="x-alert-inner">
<span class="x-alert-title" cdkDragHandle [class.x-bold]="title && content">
<ng-container *xOutlet="title; context: { $iconTpl: iconTpl }">{{ title }}</ng-container>
</span>
@if (content) {
<div class="x-alert-content">
<ng-container *xOutlet="content; context: { $iconTpl: iconTpl }">{{ content }}</ng-container>
</div>
}
<div class="x-alert-close">
<div class="x-alert-operation" *xOutlet="operationTpl">{{ operationTpl }}</div>
@if (!hideClose) {
<x-button
size="small"
[icon]="!closeText ? 'fto-x' : ''"
[onlyIcon]="!closeText"
[type]="closeText ? 'text' : 'initial'"
(click)="onClose()"
closable
>{{ closeText }}</x-button
>
}
</div>
</div>
</div>
</div>
}
14 changes: 3 additions & 11 deletions lib/ng-nest/ui/alert/alert.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,17 @@ import { XFadeAnimation, XIsEmpty, XConfigService, XIsChange, XClearClass } from
import { of, Subject } from 'rxjs';
import { delay, takeUntil } from 'rxjs/operators';
import { CdkDrag } from '@angular/cdk/drag-drop';
import { CommonModule } from '@angular/common';
import { XIconComponent } from '@ng-nest/ui/icon';
import { XOutletDirective } from '@ng-nest/ui/outlet';
import { XButtonComponent } from '@ng-nest/ui/button';
import { DragDropModule } from '@angular/cdk/drag-drop';
import { XResizableDirective } from '@ng-nest/ui/resizable';
import { NgClass, NgTemplateOutlet } from '@angular/common';

@Component({
selector: `${XAlertPrefix}`,
standalone: true,
imports: [
CommonModule,
DragDropModule,
XIconComponent,
XButtonComponent,
XOutletDirective,
XResizableDirective
],
imports: [NgClass, NgTemplateOutlet, DragDropModule, XIconComponent, XButtonComponent, XOutletDirective, XResizableDirective],
templateUrl: './alert.component.html',
styleUrls: ['./alert.component.scss'],
encapsulation: ViewEncapsulation.None,
Expand Down Expand Up @@ -64,8 +57,7 @@ export class XAlertComponent extends XAlertProperty implements OnInit, OnDestroy
this.classMap = {
[`${XAlertPrefix}-${this.type}`]: !XIsEmpty(this.type),
[`x-${this.effect}`]: !XIsEmpty(this.effect),
[`${XAlertPrefix}-icon-medium`]:
!XIsEmpty(this.title) && !XIsEmpty(this.content) && !XIsEmpty(this.showIcon),
[`${XAlertPrefix}-icon-medium`]: !XIsEmpty(this.title) && !XIsEmpty(this.content) && !XIsEmpty(this.showIcon),
[`${XAlertPrefix}-draggable`]: Boolean(this.draggable)
};
}
Expand Down
31 changes: 8 additions & 23 deletions lib/ng-nest/ui/anchor/anchor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,23 @@ import {
inject
} from '@angular/core';
import { XAnchorPrefix, XAnchorNode, XAnchorProperty } from './anchor.property';
import {
XComputedStyle,
XIsEmpty,
reqAnimFrame,
XIsNumber,
XIsUndefined,
XConfigService
} from '@ng-nest/ui/core';
import { XComputedStyle, XIsEmpty, reqAnimFrame, XIsNumber, XIsUndefined, XConfigService } from '@ng-nest/ui/core';
import { XSliderNode, XSliderComponent } from '@ng-nest/ui/slider';
import { XAffixComponent } from '@ng-nest/ui/affix';
import { CommonModule, DOCUMENT } from '@angular/common';
import { DOCUMENT, NgClass } from '@angular/common';
import { fromEvent, Subject } from 'rxjs';
import { throttleTime, takeUntil, distinctUntilChanged } from 'rxjs/operators';

@Component({
selector: `${XAnchorPrefix}`,
standalone: true,
imports: [CommonModule, XAffixComponent, XSliderComponent],
imports: [NgClass, XAffixComponent, XSliderComponent],
templateUrl: './anchor.component.html',
styleUrls: ['./anchor.component.scss'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class XAnchorComponent
extends XAnchorProperty
implements OnInit, AfterViewInit, OnDestroy, AfterContentChecked
{
export class XAnchorComponent extends XAnchorProperty implements OnInit, AfterViewInit, OnDestroy, AfterContentChecked {
@ViewChild('anchor', { static: true }) anchor!: ElementRef<HTMLElement>;
@ViewChild('content', { static: true }) content!: ElementRef<HTMLElement>;
hElements!: NodeListOf<HTMLElement>;
Expand Down Expand Up @@ -87,9 +77,7 @@ export class XAnchorComponent
this._scrolling = true;
const hElement = this.hElements[index];
let scrollTop =
hElement.offsetTop -
this.anchor.nativeElement.offsetTop -
parseFloat(XComputedStyle(hElement, 'margin-top'));
hElement.offsetTop - this.anchor.nativeElement.offsetTop - parseFloat(XComputedStyle(hElement, 'margin-top'));
let maxScrollTop = this.scroll.scrollHeight - this.scroll.clientHeight;
if (scrollTop > maxScrollTop) scrollTop = maxScrollTop;
this.scrollTo(this.scroll, parseInt(`${scrollTop}`), 150);
Expand Down Expand Up @@ -175,18 +163,15 @@ export class XAnchorComponent
if (!this.affixWidth) return null;
if (this.affixWidth === '0') return 0;
if (XIsNumber(this.affixWidth)) return this.affixWidth;
else if (this.affixWidth.indexOf('rem') !== -1)
return Number(this.affixWidth.replace(/rem/g, '')) * this._fontSize;
else if (this.affixWidth.indexOf('px') !== -1)
return Number(this.affixWidth.replace(/px/g, ''));
else if (this.affixWidth.indexOf('rem') !== -1) return Number(this.affixWidth.replace(/rem/g, '')) * this._fontSize;
else if (this.affixWidth.indexOf('px') !== -1) return Number(this.affixWidth.replace(/px/g, ''));
else return Number(this.affixWidth);
}

private getTop() {
if (this.affixTop === '0') return 0;
if (XIsNumber(this.affixTop)) return Number(this.affixTop);
else if (this.affixTop.indexOf('rem') !== -1)
return Number(this.affixTop.replace(/rem/g, '')) * this._fontSize;
else if (this.affixTop.indexOf('rem') !== -1) return Number(this.affixTop.replace(/rem/g, '')) * this._fontSize;
else if (this.affixTop.indexOf('px') !== -1) return Number(this.affixTop.replace(/px/g, ''));
return 0;
}
Expand Down
12 changes: 9 additions & 3 deletions lib/ng-nest/ui/avatar/avatar.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
[ngStyle]="styleMap"
[style.backgroundColor]="backgroundColor"
>
<img *ngIf="src && !isImgError" [src]="src" [style.object-fit]="fit" (error)="imgError()" />
<x-icon *ngIf="icon" [type]="icon"></x-icon>
<x-icon *ngIf="isImgError" type="fto-image"></x-icon>
@if (src && !isImgError) {
<img [src]="src" [style.object-fit]="fit" (error)="imgError()" />
}
@if (icon) {
<x-icon [type]="icon"></x-icon>
}
@if (isImgError) {
<x-icon type="fto-image"></x-icon>
}
<span #labelRef *xOutlet="label; context: { $label: label }">
{{ label }}
</span>
Expand Down
9 changes: 3 additions & 6 deletions lib/ng-nest/ui/avatar/avatar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,21 @@ import {
XIsChange,
XResizeObserver
} from '@ng-nest/ui/core';
import { DOCUMENT, CommonModule } from '@angular/common';
import { DOCUMENT, NgClass, NgStyle } from '@angular/common';
import { Subject, takeUntil } from 'rxjs';
import { XIconComponent } from '@ng-nest/ui/icon';
import { XOutletDirective } from '@ng-nest/ui/outlet';

@Component({
selector: `${XAvatarPrefix}`,
standalone: true,
imports: [CommonModule, XOutletDirective, XIconComponent],
imports: [NgClass, NgStyle, XOutletDirective, XIconComponent],
templateUrl: './avatar.component.html',
styleUrls: ['./avatar.component.scss'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class XAvatarComponent
extends XAvatarProperty
implements OnDestroy, OnChanges, AfterViewInit
{
export class XAvatarComponent extends XAvatarProperty implements OnDestroy, OnChanges, AfterViewInit {
isImgError: boolean = false;

styleMap: { [key: string]: any } = {};
Expand Down
4 changes: 3 additions & 1 deletion lib/ng-nest/ui/back-top/back-top.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<ng-template #backTopTpl>
<div class="x-back-top" [ngClass]="classMap" (click)="onBackTop()">
<x-link *ngIf="!template" icon="fto-arrow-up"> </x-link>
@if (!template) {
<x-link icon="fto-arrow-up"> </x-link>
}
<ng-container *ngTemplateOutlet="template"></ng-container>
</div>
</ng-template>
7 changes: 3 additions & 4 deletions lib/ng-nest/ui/back-top/back-top.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '@angular/core';
import { XBackTopPrefix, XBackTopProperty } from './back-top.property';
import { reqAnimFrame, XConfigService, XIsChange, XIsNumber } from '@ng-nest/ui/core';
import { CommonModule, DOCUMENT } from '@angular/common';
import { DOCUMENT, NgClass, NgTemplateOutlet } from '@angular/common';
import { fromEvent, Subject } from 'rxjs';
import { throttleTime, takeUntil } from 'rxjs/operators';
import { XPortalService, XPortalOverlayRef } from '@ng-nest/ui/portal';
Expand All @@ -23,7 +23,7 @@ import { XLinkComponent } from '@ng-nest/ui/link';
@Component({
selector: `${XBackTopPrefix}`,
standalone: true,
imports: [CommonModule, XLinkComponent],
imports: [NgClass, NgTemplateOutlet, XLinkComponent],
templateUrl: './back-top.component.html',
styleUrls: ['./back-top.component.scss'],
encapsulation: ViewEncapsulation.None,
Expand Down Expand Up @@ -66,8 +66,7 @@ export class XBackTopComponent extends XBackTopProperty implements OnInit, OnCha
ngOnChanges(changes: SimpleChanges): void {
const { target } = changes;
if (XIsChange(target)) {
this._target =
typeof this.target === 'string' ? this.doc.querySelector(this.target) : this.target!;
this._target = typeof this.target === 'string' ? this.doc.querySelector(this.target) : this.target!;
this.setScrollEvent();
}
}
Expand Down
44 changes: 24 additions & 20 deletions lib/ng-nest/ui/badge/badge.component.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
<div #badge class="x-badge" [ngClass]="classMap" [class.x-badge-dot]="dot" [class.x-badge-standalone]="standalone">
<ng-content></ng-content>
<ng-container *ngIf="valueNumber > 0 || dot">
<sup *ngIf="standalone" [@x-badge-standalone-animation] [style.marginRight]="getOffsetRight" [style.marginTop]="getOffsetTop">
<ng-container *ngIf="!dot">
<ng-container *ngTemplateOutlet="diaplayValueTpl"></ng-container>
</ng-container>
</sup>
<sup *ngIf="!standalone" [@x-badge-animation] [style.marginRight]="getOffsetRight" [style.marginTop]="getOffsetTop">
<ng-container *ngIf="!dot">
<ng-container *ngTemplateOutlet="diaplayValueTpl"></ng-container>
</ng-container>
</sup>
</ng-container>
@if (valueNumber > 0 || dot) {
@if (standalone) {
<sup [@x-badge-standalone-animation] [style.marginRight]="getOffsetRight" [style.marginTop]="getOffsetTop">
<ng-container *ngTemplateOutlet="dotTpl"></ng-container>
</sup>
} @else {
<sup [@x-badge-animation] [style.marginRight]="getOffsetRight" [style.marginTop]="getOffsetTop">
<ng-container *ngTemplateOutlet="dotTpl"></ng-container>
</sup>
}
}
</div>

<ng-template #diaplayValueTpl>
<span
class="x-badge-scroll"
*ngFor="let item of maxNums; let i = index"
[style.transform]="'translateY(-' + (displayNums[i] == '+' ? 10 : displayNums[i]) * 100 + '%)'"
>
<p *ngFor="let j of range">{{ j }}</p>
</span>
<ng-template #dotTpl>
@if (!dot) {
@for (item of maxNums; track item; let i = $index) {
<span
class="x-badge-scroll"
[style.transform]="'translateY(-' + (displayNums[i] == '+' ? 10 : displayNums[i]) * 100 + '%)'"
>
@for (j of range; track j) {
<p>{{ j }}</p>
}
</span>
}
}
</ng-template>
4 changes: 2 additions & 2 deletions lib/ng-nest/ui/badge/badge.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
OnChanges,
inject
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { NgClass, NgTemplateOutlet } from '@angular/common';
import { XBadgePrefix, XBadgeProperty } from './badge.property';
import {
XIsNumber,
Expand All @@ -22,7 +22,7 @@ import {
@Component({
selector: `${XBadgePrefix}`,
standalone: true,
imports: [CommonModule],
imports: [NgClass, NgTemplateOutlet],
templateUrl: './badge.component.html',
styleUrls: ['./badge.component.scss'],
encapsulation: ViewEncapsulation.None,
Expand Down
4 changes: 1 addition & 3 deletions lib/ng-nest/ui/carousel/carousel.property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ export class XCarouselProperty extends XProperty {
* @zh_CN 幻灯片轮播方向
* @en_US Slide rotation direction
*/
@Input()
@XWithConfig<XCarouselDirection>(X_CONFIG_NAME, 'horizontal')
direction?: XCarouselDirection;
@Input() @XWithConfig<XCarouselDirection>(X_CONFIG_NAME, 'horizontal') direction?: XCarouselDirection;
/**
* @zh_CN 自动切换
* @en_US Automatic switching
Expand Down
Loading

0 comments on commit 68b0124

Please sign in to comment.