-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-directives.html
121 lines (83 loc) · 2.81 KB
/
custom-directives.html
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
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Diretivas personalizadas</title>
</head>
<body>
<div id="directives">
<h4 v-upper:parametro>{{ name }}</h4>
<!-- <h2 v-capitalize>hello world {{ name }}</h2> -->
<!-- <h2 v-custom:parametro.mod1.mod2="'Informação'">{{ name }}</h2> -->
<h2 v-custom="{ direction: argument, distance}">{{ name }}</h2>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue2-filters/dist/vue2-filters.min.js"></script>
<script>
Vue.directive('upper', {
inserted(el) {
el.innerText = el.innerText.toUpperCase();
}
})
function setDirection(element, binding) {
element.style.margin = 0;
let direction = binding.value.direction.toLowerCase();
let value = binding.value.distance;
switch(direction) {
case 'left':
case 'right':
case 'bottom':
case 'top':
let newDirection = direction.charAt(0).toUpperCase() + direction.slice(1)
eval(`element.style.margin${newDirection} = value + 'px'`);
break;
default:
console.error('Informe uma opção correta');
}
}
let app = new Vue({
el: '#directives',
data: {
name: 'Hcode',
argument: 'left',
distance: 150
},
directives: {
/*
capitalize: {
bind(element) {
console.log('BIND - Fui vinculado a um elemento');
},
inserted(element) {
console.log('INSERTED - O elemento foi inserido na página');
let words = element.innerText.split(' ');
words.forEach((text, index) => {
words[index] = text.charAt(0).toUpperCase() + text.slice(1)
});
element.innerText = words.join(' ');
},
update(element) {
console.log('UPDATE - O elemento foi atualizado');
},
unbind(element) {
console.log('UNBIND - O elemento foi desvinculado');
},
componentUpdated(element) {
console.log('COMPONENTUPDATED - O componente foi atualizado');
}
},
*/
custom: {
bind(element, bindind, vnode) {
setDirection(element, bindind);
},
update(element, bindind, vnode, oldvnode) {
setDirection(element, bindind);
}
}
}
})
</script>
</body>
</html>