ui/src/modal/notify.js

238 lines
4.6 KiB
JavaScript
Raw Normal View History

2023-04-10 19:05:00 +08:00
/**
* {通知组件}
* @author yutent<yutent.io@gmail.com>
* @date 2023/04/10 17:17:10
*/
2023-05-25 17:00:17 +08:00
import { css, html, Component, bind, styleMap } from 'wkit'
2023-11-24 15:50:48 +08:00
import '../base/icon.js'
2023-04-11 18:47:10 +08:00
const ANIMATION = [
{ transform: 'translateX(60%)', opacity: 0 },
{ transform: 'translateX(0)', opacity: 1 }
]
2023-04-12 14:22:37 +08:00
const DEFAULT_OPT = {
icon: '',
image: '',
body: '',
progress: 0,
duration: 4500
}
2023-04-11 18:47:10 +08:00
let notifyIns = null
function merge(opt = {}) {
2023-11-21 19:14:39 +08:00
let output = { ...DEFAULT_OPT }
2023-04-11 18:47:10 +08:00
for (let k in opt) {
if (opt[k] !== void 0) {
output[k] = opt[k]
}
}
return output
}
class NotifyGroup extends Component {
static styles = [
css`
:host {
position: fixed;
z-index: 65535;
top: 0;
right: 0;
}
`
]
2023-04-12 14:22:37 +08:00
mounted() {
this.$on('removed', ev => {
if (this.children.length < 1) {
notifyIns = null
this.remove()
}
})
}
2023-04-11 18:47:10 +08:00
}
2023-04-10 19:05:00 +08:00
class Notify extends Component {
static props = {
title: '',
2023-04-12 14:22:37 +08:00
opt: { ...DEFAULT_OPT }
2023-04-11 18:47:10 +08:00
}
static styles = [
css`
:host {
display: block;
margin-top: 16px !important;
}
.noselect {
2023-11-21 19:14:39 +08:00
-webkit-user-select: none;
2023-04-11 18:47:10 +08:00
user-select: none;
img,
a {
-webkit-user-drag: none;
}
}
cite {
font-style: normal;
}
.notification {
width: 300px;
padding: 12px;
border-radius: 3px;
font-size: 14px;
background: rgba(255, 255, 255, 0.9);
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
}
.icon {
flex-shrink: 0;
width: 48px;
height: 48px;
margin-right: 8px;
}
.main,
.content {
display: flex;
}
.content {
flex: 1;
flex-direction: column;
}
.title {
display: flex;
align-items: center;
justify-content: space-between;
line-height: 2;
wc-icon {
2023-08-03 15:47:02 +08:00
--wc-icon-size: 14px;
2023-04-11 18:47:10 +08:00
cursor: pointer;
}
}
`,
// 额外功能
css`
.image {
width: 284px;
height: 86px;
margin-top: 8px;
object-fit: cover;
}
.progress {
width: 100%;
margin-top: 8px;
.bar {
display: flex;
width: 100%;
height: 8px;
border-radius: 4px;
background: var(--color-plain-1);
}
.thumb {
width: 10%;
height: 8px;
border-radius: 4px;
background: var(--color-teal-1);
}
}
`
]
2023-04-12 14:22:37 +08:00
#up = null
2023-04-11 18:47:10 +08:00
close() {
clearTimeout(this.timer)
this.$refs.box.$animate(true).then(_ => {
this.remove()
2023-04-12 14:22:37 +08:00
this.#up.$emit('removed')
2023-04-11 18:47:10 +08:00
})
}
setProgress(val) {
let n = +val || 0
if (n < 0) {
n = 0
} else if (n > 100) {
n = 100
}
this.opt.progress = n
2023-04-12 14:22:37 +08:00
this.$requestUpdate()
2023-04-11 18:47:10 +08:00
}
mounted() {
this.$refs.box.$animate()
2023-04-12 14:22:37 +08:00
this.#up = this.parentNode
if (this.opt.duration > 0) {
this.timer = setTimeout(() => {
this.close()
}, this.opt.duration)
}
2023-04-10 19:05:00 +08:00
}
render() {
2023-04-11 18:47:10 +08:00
let progress = styleMap({ width: this.opt.progress + '%' })
let iconShow = styleMap({ display: this.opt.icon ? '' : 'none' })
let imageShow = styleMap({ display: this.opt.image ? '' : 'none' })
let progressShow = styleMap({
display: this.opt.progress > 0 ? '' : 'none'
})
return html`<div
class="notification noselect"
ref="box"
#animation=${{ custom: ANIMATION }}
>
<main class="main">
<img class="icon" src=${this.opt.icon} style=${iconShow} />
<section class="content">
<strong class="title">
${this.title}
<wc-icon name="close" @click=${this.close}></wc-icon>
</strong>
<cite class="body">${this.opt.body}</cite>
</section>
</main>
<img src=${this.opt.image} class="image" style=${imageShow} />
<div class="progress" style=${progressShow}>
<div class="bar"><span class="thumb" style=${progress}></span></div>
</div>
</div>`
2023-04-10 19:05:00 +08:00
}
}
2023-04-11 18:47:10 +08:00
NotifyGroup.reg('notify-group')
2023-04-10 19:05:00 +08:00
Notify.reg('notify')
2023-04-11 18:47:10 +08:00
export default function notify(
title = '通知',
2023-04-12 14:22:37 +08:00
{ icon, body, image, progress, duration }
2023-04-11 18:47:10 +08:00
) {
2023-04-10 19:05:00 +08:00
//
2023-04-11 18:47:10 +08:00
let container = notifyIns || document.createElement('wc-notify-group')
2023-04-10 19:05:00 +08:00
let elem = document.createElement('wc-notify')
elem.title = title
2023-04-12 14:22:37 +08:00
elem.opt = merge({ icon, body, image, progress, duration })
2023-04-11 18:47:10 +08:00
container.append(elem)
if (!notifyIns) {
notifyIns = container
document.body.append(notifyIns)
}
return elem
2023-04-10 19:05:00 +08:00
}
window.notify = notify