vue实现自定义窗口

窗口调用方法

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
import Vue from 'vue'
let iNum = 0
let _components = {}
const _util = {
// 查找对应的模板组件
hasTemplate (__file) {
return new Promise((resolve, reject) => {
let _find = false
_components[__file].model.$children.map(child => {
if ('model__' in child && child.model__ === 'iv-custom-model' &&
child.$options.componentName === 'CustModelTemplate') {
_find = true
resolve(child)
}
})
if (!_find) {
reject(_find)
}
})
},
// 查找组件
findComponent (config) {
let __file = null
Object.keys(_components).map(key => {
if (_components[key].component === config.component) {
__file = key
}
})
return __file
},
// 创建示例
created (config) {
iNum++
const Model = Vue.extend(config.component)
let options = {
provide () {
return {
app: Object.assign({}, config.app || {}),
model: this
}
}
}
if (config.router) {
options.router = config.router
}
options.data = Object.assign({ app: config.app || {} }, config.options)
let _model = new Model(options)
_model.$mount()
document.body.appendChild(_model.$el)
const __file = 'model_' + iNum
_components[__file] = {
component: config.component,
model: _model, // model
listen: false // 是否加入了监听
}
_CustModel.show(__file)
this.listeners(config, __file)
},
// 监听
listeners (config, __file) {
Object.keys(_components).map(key => {
let cp = _components[key]
if (!cp.listen) {
cp.listen = true
let _listen = Object.assign({
close: () => {},
update: null,
router: null
}, ('on' in config && typeof config.on === 'object') ? config.on : {})
Object.keys(_listen).map(key => {
switch (key) {
case 'close':
case 'router':
case 'update':
if (_listen[key] && typeof _listen[key] === 'function') {
cp.model.$on(`on-${key}`, (...args) => {
_listen[key].apply(cp.model, args)
if (key === 'close' || key === 'router') {
if ('destroy' in config && config.destroy) {
_CustModel.destroy(config)
} else {
_CustModel.hide(config)
}
}
})
}
break
default:
// 用以匹配任意方法
cp.model.$on(`on-${key}`, (...args) => {
_listen[key].apply(cp.model, args)
})
}
})
}
return cp
})
}
}
const _CustModel = {
// 创建入口
install (config) {
if (!config.component) {
return
}
let __file = _util.findComponent(config)
if (__file) {
if ('options' in config && typeof config.options === 'object') {
Object.keys(config.options).map(key => {
_components[__file].model._data[key] = config.options[key]
})
}
this.show(__file)
} else {
_util.created(config)
}
},
// 示例显示
show: function (__file) {
_util.hasTemplate(__file).then(child => {
child.visible = true
})
},
// 示例隐藏
hide: function (config) {
if (config && config.component) {
let __file = _util.findComponent(config)
if (__file) {
_util.hasTemplate(__file).then(child => {
child.visible = false
})
}
}
},
// 销毁
destroy: function (config) {
if (config && config.component) {
let __file = _util.findComponent(config)
if (__file) {
_components[__file].model.$destroy(true)
_components[__file].model.$el.parentNode.removeChild(_components[__file].model.$el)
_components[__file] = null
delete _components[__file]
}
}
}
}

export default _CustModel

配套窗口模板

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
326
327
328
329
330
331
332
333
334
335
336
337
<template>
<div class="iv-custom-mask" v-show="visible" :style="maskStyle" @click.stop="onCloseHandle">
<div class="iv-custom-model" ref="model" :style="styles">
<section class="iv-custom-model__header"
@mousedown.stop="onMouseDownHandle"
:style="headStyle" v-if="header">
<i v-if="icon" :style="`font-size: ${iconSize}px`" :class="['title-icon', icon]" />
<slot class="iv-custom-model__header-title" name="header">
<h2 class="iv-custom-model__header-title" :style="titleStyle">{{title}}</h2>
</slot>
<i v-if="full" :class="['full', 'ivu-icon', `ivu-icon-${fullFlag ? 'ios-contract' : 'ios-expand'}`]" @click="onFullHandle" />
<i class="close omd omd-close" @click.stop="onCloseHandle" />
</section>
<section class="iv-custom-model__main" :style="mainStyles">
<Spin fix v-if="pageLoading"></Spin>
<slot></slot>
</section>
<section class="iv-custom-model__footer" :style="footStyle" v-if="footer">
<div class="iv-custom-model__footer-prefix">
<slot name="footPrefix" />
</div>
<div class="iv-custom-model__footer-btns">
<slot name="footer">
<Button type="text" :size="footButtonSize" @click="onCancelHandle">{{cancelText}}</Button>
<Button type="primary" :size="footButtonSize" :disabled="saveDisabled" :loading="loading" @click="onSaveHandle">{{okText}}</Button>
</slot>
</div>
</section>
</div>
</div>
</template>
<script>
export default {
name: 'iv-custom-model',
componentName: 'CustModelTemplate',
props: {
loading: {
type: Boolean,
default: false
},
pageLoading: {
type: Boolean,
default: false
},
icon: {
type: String,
default: ''
},
iconSize: {
type: [String, Number],
default: 22
},
mask: {
type: Boolean,
default: false
},
full: {
type: Boolean,
default: false
},
header: {
type: Boolean,
default: true
},
headHeight: {
type: [ Number, String ],
default: 50
},
footer: {
type: Boolean,
default: true
},
footHeight: {
type: [ Number, String ],
default: 50
},
footButtonSize: {
type: String,
default: 'default'
},
cancelText: {
type: String,
default: '取消'
},
okText: {
type: String,
default: '保存'
},
title: {
type: String,
default: ''
},
zIndex: {
type: Number,
default: 2
},
titleStyle: {
type: Object,
default: () => {
return {}
}
},
saveDisabled: {
type: Boolean,
default: false
},
// 是否自定义位置 默认居中
position: {
type: Boolean,
default: false
},
bgColor: {
type: String,
default: '#f5f5f5'
},
top: {
type: [Number, String],
default: 0
},
left: {
type: [Number, String],
default: 0
},
width: {
type: [Number, String],
default: 600
},
height: {
type: [Number, String],
default: 420
}
},
data () {
return {
fullFlag: false,
mouseMoveHandle: null,
mouseUpHandle: null,
visible: false,
model__: 'iv-custom-model',
dist: {
isMove: false,
disX: 0,
disY: 0,
left: 0,
top: 0
}
}
},
watch: {
visible: {
handler (v) {
this.$emit('on-visible-change', v)
}
},
immediate: true,
deep: true
},
computed: {
maskStyle () {
if (this.mask) {
return {
zIndex: this.zIndex,
background: `rgba(0, 0, 0, 0.2)`
}
} else {
return {
zIndex: this.zIndex,
background: `rgba(0, 0, 0, 0)`
}
}
},
styles () {
if (this.position) {
return {
width: `${this.width}px`,
height: `${this.height > window.innerHeight ? (window.innerHeight - 40) : this.height}px`,
top: `${this.top}px`,
left: `${this.left}px`,
margin: 'unset'
}
}
return {
width: `${this.width}px`,
height: `${this.height}px`
}
},
headStyle () {
return {
cursor: `${this.position ? 'move' : 'default'}`,
height: `${this.headHeight}px`,
lineHeight: `${this.headHeight}px`
}
},
footStyle () {
return {
height: `${this.headHeight}px`,
lineHeight: `${this.headHeight}px`
}
},
mainStyles () {
let _header = this.header ? parseInt(this.headHeight) : 0
let _footer = this.footer ? parseInt(this.footHeight) : 0
return {
height: `calc(100% - ${_header + _footer}px)`,
background: this.bgColor
}
}
},
mounted () {
let self = this
this.mouseMoveHandle = function (e) {
if (self.position && self.dist.isMove) {
let x = e.pageX - self.dist.disX
let y = e.pageY - self.dist.disY
self.$refs.model.style.left = self.dist.left + x + 'px'
self.$refs.model.style.top = self.dist.top + y + 'px'
}
}
this.mouseUpHandle = function () {
if (self.position && self.dist.isMove) {
self.dist.isMove = false
}
}
window.document.addEventListener('mousemove', this.mouseMoveHandle, false)
window.document.addEventListener('mouseup', this.mouseUpHandle, false)
},
methods: {
_removeEvents () {
window.document.removeEventListener('mousemove', this.mouseMoveHandle, false)
window.document.removeEventListener('mouseup', this.mouseUpHandle, false)
},
onMouseDownHandle (e) {
if (!this.position) {
return
}
this.dist.isMove = true
this.dist.disX = e.pageX
this.dist.disY = e.pageY
this.dist.top = parseInt(this.$refs.model.style.top)
this.dist.left = parseInt(this.$refs.model.style.left)
},
onCancelHandle () {
this.$emit('on-close')
},
onSaveHandle () {
if (!this.loading) {
this.$emit('on-save')
}
},
onFullHandle () {
this.fullFlag = !this.fullFlag
if (this.fullFlag) {
this.$refs.model.style.cssText = `width: ${window.innerWidth}px; height: ${window.innerHeight}px; top: 0; left: 0; z-index: 9999; border-radius: 0; border: none;`
} else {
this.$refs.model.style.cssText = `width: ${this.width}px; height: ${this.height}px;`
}
this.$emit('on-full', this.fullFlag)
},
onCloseHandle ($event) {
if (['iv-custom-mask', 'close omd omd-close'].includes($event.target.className)) {
this.$emit('on-close')
}
}
}
}
</script>
<style lang="less" scoped>
@borderColor: #e1e1e1;
.iv-custom-mask{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: hidden;
z-index: 2;
}
.iv-custom-model{
position: absolute;
border: none;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: hidden;
border-radius: 6px;
box-shadow: 0 4px 12px rgba(0,0,0,.15);
&__header{
background: #fff;
border-bottom: 1px solid @borderColor;
position: relative;
.title-icon{
margin: -7px 0 0 15px;
}
&-title{
width: auto;
font-weight: normal;
padding: 0 15px;
display: inline-block;
}
.close{
position: absolute;
font-size: 22px;
top: 12px;
right: 12px;
cursor: pointer;
}
.full{
position: absolute;
font-size: 20px;
top: 13px;
right: 38px;
cursor: pointer;
}
}
&__main{
box-sizing: border-box;
padding: 15px;
overflow: auto;
}
&__footer{
display: flex;
background: #fff;
border-top: 1px solid @borderColor;
text-align: right;
padding: 0 15px;
&-prefix{
flex: 1;
text-align: left;
}
&-btns{
width: auto;
}
}
}
</style>
-------------本文结束感谢您的阅读-------------