init commit
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
<div class="toast-container" [ngClass]="toastPosition">
|
||||
<div *ngFor="let toastCfg of getToastConfigs()" [@animation]="toastAnimation">
|
||||
<toast [config]="toastCfg" (dismissed)="remove(toastCfg)"></toast>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,36 @@
|
||||
.toast-container {
|
||||
position: absolute;
|
||||
z-index: 22222;
|
||||
margin-top:10px;
|
||||
width:450px;
|
||||
}
|
||||
|
||||
.toast-top-left{
|
||||
left: 10px;
|
||||
top:75px;
|
||||
}
|
||||
|
||||
.toast-top-center{
|
||||
top:75px;
|
||||
left: -moz-calc( 50% - 225px);
|
||||
left: -webkit-calc( 50% - 225px);
|
||||
left: calc( 50% - 225px);
|
||||
}
|
||||
|
||||
.toast-top-right{
|
||||
top:75px;
|
||||
right: 10px
|
||||
}
|
||||
|
||||
|
||||
.toast-bottom-right{
|
||||
right: 75px;
|
||||
bottom:0;
|
||||
}
|
||||
|
||||
.toast-bottom-left{
|
||||
left: 10px;
|
||||
bottom:0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { ToastBoxComponent } from './toast-box.component';
|
||||
|
||||
describe('ToastBoxComponent', () => {
|
||||
let component: ToastBoxComponent;
|
||||
let fixture: ComponentFixture<ToastBoxComponent>;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [ ToastBoxComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(ToastBoxComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,68 @@
|
||||
import {Component, Input, OnInit} from '@angular/core';
|
||||
import {trigger, transition, style, animate, state} from '@angular/animations'
|
||||
import {ToastService} from './toast.service';
|
||||
import {ToastConfig} from './toast-model';
|
||||
// import { from } from 'rxjs';
|
||||
|
||||
/**
|
||||
* toast外层组件
|
||||
*/
|
||||
@Component({
|
||||
selector: 'toast-box',
|
||||
templateUrl : './toast-box.component.html',
|
||||
styleUrls:['./toast-box.component.scss'],
|
||||
animations: [
|
||||
trigger('animation', [
|
||||
state('none', style({})),
|
||||
state('decent', style([{opacity: 1}, {maxWidth: 450}])),
|
||||
state('fancy', style([{transform: 'translateX(0)'},{transform: 'translateY(0)'}, {opacity: 1}, {maxWidth: 450}])),
|
||||
transition('void => fancy', [
|
||||
style({opacity: 0, maxWidth: 450, transform: 'translateX(100%)'}),
|
||||
animate('300ms ease-in-out')
|
||||
]),
|
||||
transition('fancy => void', [
|
||||
animate('300ms ease-in-out', style({transform: 'translateX(100%)', height: 0, opacity: 0}))
|
||||
]),
|
||||
transition('void => decent', [
|
||||
style({opacity: 0, maxWidth: 0}),
|
||||
animate('300ms ease-in-out')
|
||||
]),
|
||||
transition('decent => void', [
|
||||
animate('300ms ease-in-out', style({width: 0, opacity: 0}))
|
||||
])
|
||||
])
|
||||
]
|
||||
})
|
||||
export class ToastBoxComponent implements OnInit {
|
||||
@Input() toastAnimation: string = 'none';
|
||||
@Input() toastPosition: string = 'toast-top-center';
|
||||
|
||||
private toastConfigs: Array<ToastConfig> = [];
|
||||
|
||||
constructor(private toastService: ToastService) {
|
||||
this.toastService.getToasts().forEach((config: ToastConfig) => {
|
||||
this.toastConfigs.unshift(config);
|
||||
});
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得所有toast配置
|
||||
*/
|
||||
getToastConfigs(): Array<ToastConfig> {
|
||||
return this.toastConfigs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除
|
||||
* @param toastCfg
|
||||
*/
|
||||
remove(toastCfg: ToastConfig) {
|
||||
if(this.toastConfigs.indexOf(toastCfg) >= 0) {
|
||||
this.toastConfigs.splice(this.toastConfigs.indexOf(toastCfg), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
export enum ToastType {
|
||||
SUCCESS, INFO, WARNING, ERROR
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 配置
|
||||
*/
|
||||
export class ToastConfig {
|
||||
toastType: ToastType;
|
||||
text: string;
|
||||
textStrong: string;
|
||||
autoDismissTime: number;
|
||||
dismissable: boolean;
|
||||
|
||||
|
||||
constructor(toastType: ToastType, textStrong: string = '', text: string = '', autoDismissTime = 0, dismissable = true) {
|
||||
this.toastType = toastType;
|
||||
this.text = text;
|
||||
this.textStrong = textStrong;
|
||||
this.autoDismissTime = autoDismissTime;
|
||||
this.dismissable = dismissable;
|
||||
}
|
||||
|
||||
getToastType(): ToastType {
|
||||
return this.toastType;
|
||||
}
|
||||
|
||||
getText(): string {
|
||||
return this.text;
|
||||
}
|
||||
|
||||
getTextStrong(): string {
|
||||
return this.textStrong;
|
||||
}
|
||||
|
||||
getAutoDismissTime(): number {
|
||||
return this.autoDismissTime;
|
||||
}
|
||||
|
||||
getDismissable(): boolean {
|
||||
return this.dismissable;
|
||||
}
|
||||
|
||||
|
||||
isDismissable() {
|
||||
return this.autoDismissTime === 0 || this.dismissable;
|
||||
}
|
||||
|
||||
isAutoDismissing() {
|
||||
return this.autoDismissTime > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<div class="alert text-white c-toast" [class.bg-success]="isSuccess()" [class.bg-info]="isInfo()" [class.bg-warning]="isWarning()" [class.bg-danger]="isError()">
|
||||
<button *ngIf="isDismissEnabled()" type="button" class="close" data-dismiss="config" aria-label="Close" (click)="dismiss()">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
<i class="fa mr-3" [class.fa-check-circle]="isSuccess()" [class.fa-info-circle]="isInfo()" [class.fa-warning]="isWarning()" [class.fa-times-circle]="isError()"></i>
|
||||
<strong>{{config.textStrong}}</strong><span>{{config.text}}</span>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
.toast{
|
||||
border:0px;
|
||||
-webkit-box-shadow: 1px 1px 3px rgba(100, 100, 100, 0.3);
|
||||
box-shadow: 1px 1px 3px rgba(100, 100, 100, 0.3);
|
||||
-moz-box-shadow: 1px 1px 3px rgba(100, 100, 100, 0.3);
|
||||
border-radius: 20px;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { ToastComponent } from './toast.component';
|
||||
|
||||
describe('ToastComponent', () => {
|
||||
let component: ToastComponent;
|
||||
let fixture: ComponentFixture<ToastComponent>;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [ ToastComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(ToastComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,72 @@
|
||||
import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
|
||||
import {ToastConfig,ToastType} from './toast-model';
|
||||
|
||||
/**
|
||||
* toast组件
|
||||
*/
|
||||
@Component({
|
||||
selector: 'toast',
|
||||
templateUrl : './toast.component.html',
|
||||
styleUrls:['./toast.component.scss']
|
||||
})
|
||||
export class ToastComponent implements OnInit {
|
||||
|
||||
@Input() config = new ToastConfig(ToastType.INFO, '', '');
|
||||
|
||||
@Output() dismissed = new EventEmitter();
|
||||
|
||||
constructor() {
|
||||
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
//自动关闭
|
||||
if (this.config.isAutoDismissing()) {
|
||||
setTimeout(() => this.dismiss(), this.config.getAutoDismissTime());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是成功
|
||||
*/
|
||||
isSuccess() {
|
||||
return this.config.getToastType() === ToastType.SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是信息
|
||||
*/
|
||||
isInfo() {
|
||||
return this.config.getToastType() === ToastType.INFO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是警告
|
||||
*/
|
||||
isWarning() {
|
||||
return this.config.getToastType() === ToastType.WARNING;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是错误
|
||||
*/
|
||||
isError() {
|
||||
return this.config.getToastType() === ToastType.ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解除
|
||||
*/
|
||||
dismiss() {
|
||||
this.dismissed.emit();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否启用
|
||||
*/
|
||||
isDismissEnabled() {
|
||||
return this.config.isDismissable();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
|
||||
import { ToastBoxComponent } from './toast-box.component';
|
||||
import { ToastComponent } from './toast.component';
|
||||
import { ToastService } from './toast.service'
|
||||
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
CommonModule,
|
||||
],
|
||||
declarations: [
|
||||
ToastBoxComponent,
|
||||
ToastComponent
|
||||
],
|
||||
providers: [
|
||||
ToastService
|
||||
],
|
||||
exports: [
|
||||
ToastBoxComponent,
|
||||
ToastComponent
|
||||
]
|
||||
})
|
||||
export class ToastModule { }
|
||||
@@ -0,0 +1,15 @@
|
||||
import { TestBed, inject } from '@angular/core/testing';
|
||||
|
||||
import { ToastService } from './toast.service';
|
||||
|
||||
describe('ToastService', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [ToastService]
|
||||
});
|
||||
});
|
||||
|
||||
it('should be created', inject([ToastService], (service: ToastService) => {
|
||||
expect(service).toBeTruthy();
|
||||
}));
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Observable,Subject} from 'rxjs';
|
||||
import {ToastConfig} from './toast-model';
|
||||
|
||||
/**
|
||||
* toast服务
|
||||
*/
|
||||
@Injectable()
|
||||
export class ToastService {
|
||||
|
||||
private toastSubject = new Subject<ToastConfig>();
|
||||
|
||||
constructor() {}
|
||||
|
||||
getToasts(): Observable<ToastConfig> {
|
||||
return this.toastSubject;
|
||||
}
|
||||
|
||||
|
||||
toast(toastConfig: ToastConfig) {
|
||||
this.toastSubject.next(toastConfig);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user