|
1 | | -import Vue from 'vue'; |
2 | | -import { util } from 'vue'; |
3 | | - |
4 | | -// @NOTE: We have to use Vue.nextTick because the element might not be |
5 | | -// present at the time model changes, but will be in the next batch. |
6 | | -// But because we use Vue.nextTick, the directive may already be unbound |
7 | | -// by the time the callback executes, so we have to make sure it was not. |
8 | | - |
9 | 1 | export var focus = { |
10 | | - priority: 1000, |
11 | | - |
12 | | - bind: function() { |
13 | | - var self = this; |
14 | | - this.bound = true; |
15 | | - |
16 | | - this.focus = function() { |
17 | | - if (self.bound === true) { |
18 | | - self.el.focus(); |
19 | | - } |
20 | | - }; |
21 | | - |
22 | | - this.blur = function() { |
23 | | - if (self.bound === true) { |
24 | | - self.el.blur(); |
25 | | - } |
26 | | - }; |
27 | | - }, |
28 | | - |
29 | | - update: function(value) { |
30 | | - if (value) { |
31 | | - Vue.nextTick(this.focus); |
32 | | - } else { |
33 | | - Vue.nextTick(this.blur); |
34 | | - } |
| 2 | + inserted: function(el, binding) { |
| 3 | + if (binding.value) el.focus(); |
| 4 | + else el.blur(); |
35 | 5 | }, |
36 | 6 |
|
37 | | - unbind: function() { |
38 | | - this.bound = false; |
39 | | - }, |
40 | | -}; |
41 | | - |
42 | | -export var focusModel = { |
43 | | - twoWay: true, |
44 | | - priority: 1000, |
45 | | - |
46 | | - bind: function() { |
47 | | - util.warn( |
48 | | - this.name + '="' + |
49 | | - this.expression + '" is deprecated, ' + |
50 | | - 'use v-focus="' + this.expression + '" @focus="' + this.expression + ' = true" @blur="' + this.expression + ' = false" instead' |
51 | | - ); |
52 | | - |
53 | | - var self = this; |
54 | | - this.bound = true; |
55 | | - |
56 | | - this.focus = function() { |
57 | | - if (self.bound === true) { |
58 | | - self.el.focus(); |
59 | | - } |
60 | | - }; |
61 | | - |
62 | | - this.blur = function() { |
63 | | - if (self.bound === true) { |
64 | | - self.el.blur(); |
65 | | - } |
66 | | - }; |
67 | | - |
68 | | - this.focusHandler = function() { |
69 | | - self.set(true); |
70 | | - }; |
71 | | - |
72 | | - this.blurHandler = function() { |
73 | | - self.set(false); |
74 | | - }; |
75 | | - |
76 | | - util.on(this.el, 'focus', this.focusHandler); |
77 | | - util.on(this.el, 'blur', this.blurHandler); |
78 | | - }, |
79 | | - |
80 | | - update: function(value) { |
81 | | - if (value === true) { |
82 | | - Vue.nextTick(this.focus); |
83 | | - } else if (value === false) { |
84 | | - Vue.nextTick(this.blur); |
85 | | - } else { |
86 | | - if (process.env.NODE_ENV !== 'production') { |
87 | | - util.warn( |
88 | | - this.name + '="' + |
89 | | - this.expression + '" expects a boolean value, ' + |
90 | | - 'got ' + JSON.stringify(value) |
91 | | - ); |
92 | | - } |
93 | | - } |
94 | | - }, |
95 | | - |
96 | | - unbind: function() { |
97 | | - util.off(this.el, 'focus', this.focusHandler); |
98 | | - util.off(this.el, 'blur', this.blurHandler); |
99 | | - this.bound = false; |
100 | | - }, |
101 | | -}; |
102 | | - |
103 | | -export var focusAuto = { |
104 | | - priority: 100, |
105 | | - bind: function() { |
106 | | - util.warn( |
107 | | - this.name + ' is deprecated, ' + |
108 | | - 'use v-focus="true" instead' |
109 | | - ); |
110 | | - |
111 | | - var self = this; |
112 | | - this.bound = true; |
113 | | - |
114 | | - Vue.nextTick(function() { |
115 | | - if (self.bound === true) { |
116 | | - self.el.focus(); |
117 | | - } |
118 | | - }); |
119 | | - }, |
120 | | - unbind: function(){ |
121 | | - this.bound = false; |
| 7 | + componentUpdated: function(el, binding) { |
| 8 | + if (binding.value) el.focus(); |
| 9 | + else el.blur(); |
122 | 10 | }, |
123 | 11 | }; |
124 | 12 |
|
125 | 13 | export var mixin = { |
126 | 14 | directives: { |
127 | 15 | focus: focus, |
128 | | - focusModel: focusModel, |
129 | | - focusAuto: focusAuto, |
130 | 16 | }, |
131 | 17 | }; |
0 commit comments