This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 927
/
Copy pathDropdown.vue
119 lines (117 loc) · 2.75 KB
/
Dropdown.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
<template>
<li v-if="isLi" v-el:dropdown :class="classes">
<slot name="button">
<a class="dropdown-toggle" role="button" :class="{disabled: disabled}" @keyup.esc="show = false">
{{ text }}
<span class="caret"></span>
</a>
</slot>
<slot name="dropdown-menu">
<ul v-else class="dropdown-menu">
<slot></slot>
</ul>
</slot>
</li>
<div v-else v-el:dropdown :class="classes">
<slot name="before"></slot>
<slot name="button">
<button type="button" class="btn btn-{{type}} dropdown-toggle" @keyup.esc="show = false" :disabled="disabled">
{{ text }}
<span class="caret"></span>
</button>
</slot>
<slot name="dropdown-menu">
<ul class="dropdown-menu">
<slot></slot>
</ul>
</slot>
</div>
</template>
<script>
import {coerce} from './utils/utils.js'
import $ from './utils/NodeList.js'
export default {
props: {
show: {
twoWay: true,
type: Boolean,
coerce: coerce.boolean,
default: false
},
'class': null,
disabled: {
type: Boolean,
coerce: coerce.boolean,
default: false
},
text: {
type: String,
default: null
},
type: {
type: String,
default: 'default'
}
},
computed: {
classes () {
return [{open: this.show, disabled: this.disabled}, this.class, this.isLi ? 'dropdown' : this.inInput ? 'input-group-btn': 'btn-group']
},
inInput () { return this.$parent._input },
isLi () { return this.$parent._navbar || this.$parent.menu || this.$parent._tabset },
menu () {
return !this.$parent || this.$parent.navbar
},
submenu () {
return this.$parent && (this.$parent.menu || this.$parent.submenu)
},
slots () {
return this._slotContents
}
},
methods: {
blur () {
this.unblur()
this._hide = setTimeout(() => {
this._hide = null
this.show = false
}, 100)
},
unblur () {
if (this._hide) {
clearTimeout(this._hide)
this._hide = null
}
}
},
ready () {
const $el = $(this.$els.dropdown)
$el.onBlur((e) => { this.show = false })
$el.findChildren('a,button.dropdown-toggle').on('click', e => {
e.preventDefault()
if (this.disabled) { return false }
this.show = !this.show
return false
})
$el.findChildren('ul').on('click', 'li>a', e => { this.show = false })
},
beforeDestroy () {
const $el = $(this.$els.dropdown)
$el.offBlur()
$el.findChildren('a,button').off()
$el.findChildren('ul').off()
}
}
</script>
<style scoped>
.secret {
position: absolute;
clip: rect(0 0 0 0);
overflow: hidden;
margin: -1px;
height: 1px;
width: 1px;
padding: 0;
border: 0;
}
</style>