-
Notifications
You must be signed in to change notification settings - Fork 4
/
toast.wpy
214 lines (197 loc) · 9.57 KB
/
toast.wpy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<style lang="css">
@keyframes simple-toast-fall-down {
0% {
transform: translateY(-40px);
opacity: 0;
}
100% {
transform: translateY(0px);
opacity: 1;
}
}
.simple-toast {
display: block;
}
.simple-toast-box {
position: fixed;
top: 10px;
left: 0;
font-size: 12px;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 3;
}
.simple-toast-content {
animation: simple-toast-fall-down 0.5s ease 1;
background: white;
padding: 8px 16px;
border-radius: 4px;
border: 1px #ddd solid;
box-shadow: 0 1px 6px rgba(0, 0, 0, 0.2);
display: flex;
opacity: 1;
position: relative;
top: 0;
align-items: center;
word-break: keep-all;
white-space: nowrap;
}
.simple-toast-icon {
width: 14px;
height: 14px;
margin-right: 10px;
}
.simple-toast-transition {
transition: top 500ms, opacity 500ms;
opacity: 0;
top: -40px;
position: absolute;
}
.simple-toast-item {
position: relative;
}
.simple-toast-item + .simple-toast-item {
margin-top: 5px;
}
@keyframes simple-toast-temp-remove {
0% {
height: 35px;
padding-top: 8px;
padding-bottom: 8px;
border: none;
}
100% {
height: 0px;
padding-top: 0px;
padding-bottom: 0px;
border: none;
}
}
.simple-toast-remove-temp {
height: 0px;
animation: simple-toast-temp-remove 0.5s ease 1;
padding: 8px 16px;
border-radius: 4px;
border: 1px #ddd solid;
box-shadow: 0 1px 6px rgba(0, 0, 0, 0.2);
display: flex;
position: relative;
align-items: center;
word-break: keep-all;
white-space: nowrap;
opacity: 0;
}
</style>
<template>
<view class="simple-toast">
<view class="simple-toast-box">
<view class="simple-toast-item" wx:for="{{tipList}}" wx:for-item="item" wx:key="index">
<view class="simple-toast-content {{item.isRemove ?'simple-toast-transition':''}}">
<image src="{{icons[item.type]}}" class="simple-toast-icon" />
<text>{{item.title}}</text>
</view>
<view wx:if="{{item.isRemove}}" class="simple-toast-remove-temp">
<image src="{{icons[item.type]}}" class="simple-toast-icon" />
<text>{{item.title}}</text>
</view>
</view>
</view>
</view>
</template>
<script>
import wepy from 'wepy'
const INFO_ICON_DATA = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACoAAAAqCAYAAADFw8lbAAADDklEQVRYhb3ZTahVVRjG8d/dXjNT067UpRIaSET4AVZKVEQfgohFNDGjUeFEEzFQIZrU5A7CgdFA6GPSwFR0FtdBZDeoiWjUoNTID8hKEK1Mo4uUDdbZ3HXOPefcs+9+Tw9sePdm7Wf92eustd71noFlu35XQ8N4Ag9jGRZhPgbwF37G9ziGz/HjdDsamAbofGzCY3gaN1d496vG9SF+qNJpFdBhvILtGKrSSQftxvvSF59SRY+m63ESI2IgYRu+w+u9NO4FdB/2Y0ENqG4awbe4o1ujbqBzcBwvBEJ10nJpxB7s1KAT6EKM4YF4po66DUck6EnqBPoFHuoXURfdii+1+Rm0A92PJf0m6qJ5+Lr1YSvos9IMn65u4C1sxdUaPnfj3fxBDroQH9cwh3fwZqOTl2t6bcGK8iYH3SzN9Do6mcXHa3rB22VQgs7BzgDj4Sy+M8BvNZ5iAvRVzA0wnpXFtwT4wUYmQNcEmc7I4ijQNZhZSNnQo0Gm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yyBO6QcG36hKniDXbxEyhN+H2rshpzRCeyd7I5tE+/ID5XRbViguxl+Ynusb/SyQ8mV6kQhfgkf9GS0uuE6X6qNBRnFhhgKdwtmrrebQRc1+FGLiff9bodlxVIbgncz2MHW36r8HzmfYEzig51684LRHp4IqSQSkjmetxrMAfBX3XzhB5lnBgZRnA9QmuxbEVAmGjxtppsBUrc/o9jOcy7VPxkXgnqjCSYGnFoJRxXJNpb9Msdh2ezrQXCe9Qx6kvnofFNQKJF3EMN+Pdxr0tuAtnCr+ZMoTd6K85V2/PweGBfTin5gApK4XIPAbxmTY2rg0HEtUWdhFjuDvn/lLs15lIOCFR3uW3YyjnXj+On4WxexJMzcJA6/BYzv235GeDqkwnYkvbCes1vziX4pZMewtu7HCO3xKxwarLWlFOUwbFHv5NUQRS3hEW8s+a80wlIgHXYbXmZD4o8mS65sdEMUl5W5TbOuxPxLetyiheyLRTkTPz5GbckGnvEp6iqNwWsSk1JbvF2ipL1pSc3ohvZTKWiUqWcqARV4YjGqYE3i8ZlHIvjhFV7QvtndAOXI15wiNUqVDbcDh9okP4rppWP4ljnir8qPyTTFmFDekT/R4vVRygqkiqi/xc7IqbqtKjNSaeax5ML7JCJ5Q8sOoSm8XOA/nHjl/ivG4qKmA+fk8beYZkCaa7JiefJTIiyRd6WOyhDnVDUQ4jogw3UWTx9ohyNzGXinK4TfiEf9HKi+4VRyqljq475Ij4weKNog7tTPMkztXskGabjWIL/XGrTmXc/V8ibQ3j1c51HWVcrMdb8XO7znV+vlmOO8VWeVHF2Cm8h014pUpgHaEpiTDDK8TZe784v0pPBKdFBpkU5nyrSOKTdSb7G6RIoAsNOfeZAAAAAElFTkSuQmCC'
class toastMixin extends wepy.mixin {
$successTip(title, options) {
return this.$invoke(this.toastTagName || 'toast', 'success', title, options)
}
$errorTip(title, options) {
return this.$invoke(this.toastTagName || 'toast', 'error', title, options)
}
$warningTip(title, options) {
return this.$invoke(this.toastTagName || 'toast', 'warning', title, options)
}
$infoTip(title, options) {
return this.$invoke(this.toastTagName || 'toast', 'info', title, options)
}
}
export default class toast extends wepy.component {
static mixin = toastMixin
data = {
icons: {
info: INFO_ICON_DATA,
success: SUCCESS_ICON_DATA,
warning: WARNING_ICON_DATA,
error: ERROR_ICON_DATA
},
id: 0,
tipList: []
}
events = {
}
methods = {
show(arg, options = { type: 'success', duration: 3000 }) {
return new Promise((resolve, reject) => {
(typeof arg === 'string') ? options.title = arg : options = { ...options, ...arg }
options.id = this.id++
// if (this.id > 999999) { this.id = 0 }
this.tipList.push(options)
this.$apply()
setTimeout(() => {
this.methods.removeItem.call(this, options.id)
let callBackData = {
status: `removed`,
options
}
resolve(callBackData)
}, options.duration)
})
},
getOptions(arg1, arg2 = {}) {
let options = (arg2 !== undefined && arg2.type && arg2.type === 'invoke') ? {} : arg2
if (typeof arg1 === 'string') {
options.title = arg1
} else {
options = { ...options, ...arg1 }
}
return options
},
removeItem(id) {
this.tipList.map(item => {
if (id === item.id) {
item.isRemove = true
}
})
this.$apply()
setTimeout(() => {
this.tipList = this.tipList.filter(item => id !== item.id)
this.$apply()
}, 480)
},
info(arg1, arg2) {
let options = this.methods.getOptions(arg1, arg2)
return this.methods.show.call(this, { ...options, type: 'info' })
},
success(arg1, arg2) {
let options = this.methods.getOptions(arg1, arg2)
return this.methods.show.call(this, { ...options, type: 'success' })
},
warning(arg1, arg2) {
let options = this.methods.getOptions(arg1, arg2)
return this.methods.show.call(this, { ...options, type: 'warning' })
},
error(arg1, arg2) {
let options = this.methods.getOptions(arg1, arg2)
return this.methods.show.call(this, { ...options, type: 'error' })
}
}
onLoad() {
}
}
</script>