fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<template>
  <teleport to="#mars-main-view">
    <div class="pannel-box" :class="[customClass, animationClass]" ref="pannelBox" v-show="mergeProps.visible">
      <div class="pannel-content">
        <slot></slot>
      </div>
 
      <div v-if="mergeProps.closeable" class="pannel-close-icon" @click="closeModel">
        <mars-icon icon="close-one" width="20"></mars-icon>
      </div>
    </div>
  </teleport>
</template>
 
<script lang="ts" setup>
/**
 * pannel面板
 * @copyright 火星科技 mars3d.cn
 * @author 火星吴彦祖 2022-02-19
 */
import { onMounted, ref, computed } from "vue"
import { getConfig } from "../index"
 
const CONFIG = getConfig()
let globalConfig: Record<string, any> = {}
if (CONFIG.pannel) {
  globalConfig = CONFIG.pannel
}
 
interface Position {
  left?: number | string // 定位 left值
  right?: number | string // 定位right值
  top?: number | string // 定位top值
  bottom?: number | string // 定位bottom值
}
 
interface Props {
  warpper?: string // 容器id 默认是app,将作为定位的参照元素,一般不需要修改
  visible?: boolean // 是否显示
 
  width?: number | string // 初始宽度
  height?: number | string // 初始高度
  left?: number | string // 定位 left值
  right?: number | string // 定位right值
  position?: Position
 
  top?: number | string // 定位top值
  bottom?: number | string // 定位bottom值
  zIndex?: number // 层级
  customClass?: string // 自定义class
  closeable?: boolean
  beforeClose?: () => Promise<any> | boolean | void
}
const props = withDefaults(defineProps<Props>(), {
  warpper: "app",
  visible: false,
  closeable: false,
  zIndex: 900
})
 
const mergeProps = computed(() => {
  const newProps: Props = {}
 
  for (const k in props) {
    if (props[k] === undefined) {
      newProps[k] = globalConfig[k]
    } else {
      newProps[k] = props[k]
    }
  }
 
  if (newProps.position) {
    newProps.left = newProps.position.left
    newProps.right = newProps.position.right
    newProps.top = newProps.position.top
    newProps.bottom = newProps.position.bottom
  }
  return newProps
})
 
const animationClass = computed(() => {
  const right = getCssNumber(mergeProps.value.right)
  if (right && right >= 0 && right < 100) {
    return "fadein-right"
  }
  const left = getCssNumber(mergeProps.value.left)
  if (left || (left >= 0 && left < 100)) {
    return "fadein-left"
  }
  const top = getCssNumber(mergeProps.value.top)
  if (left || (top >= 0 && top < 100)) {
    return "fadein-down"
  }
  const bottom = getCssNumber(mergeProps.value.bottom)
  if (bottom || (bottom >= 0 && bottom < 100)) {
    return "fadein-up"
  }
  return "fadein-center"
})
 
function getCssNumber(value: number | string) {
  if (typeof value === "number") {
    return value
  }
  if (typeof value === "string") {
    if (/^[0-9]*$/.test(value)) {
      return Number(value)
    }
    if (value.endsWith("px")) {
      value = value.replace("px", "")
      return Number(value)
    }
  }
  return null
}
 
const emits = defineEmits(["update:visible"])
 
onMounted(() => {
  initPosition()
  initSize()
})
 
const pannelBox = ref()
 
// 点击按钮关闭
const closeModel = async () => {
  if (mergeProps.value.beforeClose && typeof mergeProps.value.beforeClose === "function") {
    const result = mergeProps.value.beforeClose()
 
    if (result instanceof Promise) {
      try {
        await result
        emits("update:visible", false)
      } catch (err) {
        console.log("取消关闭")
      }
    } else if (result !== false) {
      emits("update:visible", false)
    }
  } else {
    emits("update:visible", false)
  }
}
 
// 初始化位置
function initPosition() {
  const pannelStyle = pannelBox.value.style
  // 层级位置
  pannelStyle.zIndex = mergeProps.value.zIndex
  // 横向位置初始化
  if (mergeProps.value.left !== undefined) {
    pannelStyle.left = antoUnit(mergeProps.value.left)
  } else if (mergeProps.value.right !== undefined) {
    pannelStyle.right = antoUnit(mergeProps.value.right)
    pannelStyle.left = "initial"
  }
  // 纵向位置初始化
  if (mergeProps.value.top !== undefined) {
    pannelStyle.top = antoUnit(mergeProps.value.top)
  }
  if (mergeProps.value.bottom !== undefined) {
    pannelStyle.bottom = antoUnit(mergeProps.value.bottom)
  }
}
 
// 初始化大小
function initSize() {
  const pannelStyle = pannelBox.value.style
  if (mergeProps.value.width) {
    pannelStyle.width = antoUnit(mergeProps.value.width)
  }
  if (!mergeProps.value.top || !mergeProps.value.bottom) {
    if (mergeProps.value.height) {
      pannelStyle.height = antoUnit(mergeProps.value.height)
    }
  }
}
 
// 处理传入的单位问题
function antoUnit(value: number | string) {
  if (typeof value === "number" || (typeof value === "string" && /^[0-9]*$/.test(value))) {
    return `${value}px`
  } else {
    return value
  }
}
</script>
<script lang="ts">
export default {
  name: "mars-pannel"
}
</script>
 
<style lang="less" scoped>
.pannel-box {
  position: absolute;
  padding: 10px 10px;
  border-radius: 4px;
  z-index: 1000;
 
  background-image: url(../assets/images/pannel-bg.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
 
  .pannel-content {
    overflow-y: auto;
    width: 100%;
    height: 100%;
  }
 
  .pannel-close-icon {
    position: absolute;
    right: -8px;
    top: -8px;
    cursor: pointer;
  }
}
 
/**下面是 5个 动画 */
@keyframes fadeInRight {
  from {
    opacity: 0;
    transform: translate3d(100%, 0, 0);
  }
 
  to {
    opacity: 1;
    transform: none;
  }
}
.fadein-right {
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  -webkit-animation-name: fadeInRight;
  animation-name: fadeInRight;
}
 
@keyframes fadeInLeft {
  from {
    opacity: 0;
    transform: translate3d(-100%, 0, 0);
  }
 
  to {
    opacity: 1;
    transform: none;
  }
}
.fadein-left {
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  -webkit-animation-name: fadeInLeft;
  animation-name: fadeInLeft;
}
 
@keyframes fadeInUp {
  from {
    opacity: 0;
    -webkit-transform: translate3d(0, 100%, 0);
    transform: translate3d(0, 100%, 0);
  }
 
  to {
    opacity: 1;
    -webkit-transform: none;
    transform: none;
  }
}
 
.fadein-up {
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  -webkit-animation-name: fadeInUp;
  animation-name: fadeInUp;
}
 
@keyframes fadeInDown {
  from {
    opacity: 0;
    -webkit-transform: translate3d(0, -100%, 0);
    transform: translate3d(0, -100%, 0);
  }
 
  to {
    opacity: 1;
    -webkit-transform: none;
    transform: none;
  }
}
.fadein-down {
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  -webkit-animation-name: fadeInDown;
  animation-name: fadeInDown;
}
 
@keyframes fadeInCenter {
  from {
    opacity: 0;
    transform: scale(0.1);
  }
 
  to {
    opacity: 1;
    transform: none;
  }
}
.fadein-center {
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  -webkit-animation-name: fadeInCenter;
  animation-name: fadeInCenter;
}
</style>