-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirective.js
251 lines (232 loc) · 5.83 KB
/
directive.js
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
var _ = require('./util')
var config = require('./config')
var Watcher = require('./watcher')
var textParser = require('./parsers/text')
var expParser = require('./parsers/expression')
/**
* A directive links a DOM element with a piece of data,
* which is the result of evaluating an expression.
* It registers a watcher with the expression and calls
* the DOM update function when a change is triggered.
*
* @param {String} name
* @param {Node} el
* @param {Vue} vm
* @param {Object} descriptor
* - {String} expression
* - {String} [arg]
* - {Array<Object>} [filters]
* @param {Object} def - directive definition object
* @param {Vue|undefined} host - transclusion host target
* @constructor
*/
function Directive (name, el, vm, descriptor, def, host) {
// public
this.name = name
this.el = el
this.vm = vm
// copy descriptor props
this.raw = descriptor.raw
this.expression = descriptor.expression
this.arg = descriptor.arg
this.filters = descriptor.filters
// private
this._descriptor = descriptor
this._host = host
this._locked = false
this._bound = false
this._listeners = null
// init
this._bind(def)
}
/**
* Initialize the directive, mixin definition properties,
* setup the watcher, call definition bind() and update()
* if present.
*
* @param {Object} def
*/
Directive.prototype._bind = function (def) {
if (
(this.name !== 'cloak' || this.vm._isCompiled) &&
this.el && this.el.removeAttribute
) {
this.el.removeAttribute(config.prefix + this.name)
}
if (typeof def === 'function') {
this.update = def
} else {
_.extend(this, def)
}
this._watcherExp = this.expression
this._checkDynamicLiteral()
if (this.bind) {
this.bind()
}
if (this._watcherExp &&
(this.update || this.twoWay) &&
(!this.isLiteral || this._isDynamicLiteral) &&
!this._checkStatement()) {
// wrapped updater for context
var dir = this
var update = this._update = this.update
? function (val, oldVal) {
if (!dir._locked) {
dir.update(val, oldVal)
}
}
: function () {} // noop if no update is provided
// pre-process hook called before the value is piped
// through the filters. used in v-repeat.
var preProcess = this._preProcess
? _.bind(this._preProcess, this)
: null
var watcher = this._watcher = new Watcher(
this.vm,
this._watcherExp,
update, // callback
{
filters: this.filters,
twoWay: this.twoWay,
deep: this.deep,
preProcess: preProcess
}
)
if (this._initValue != null) {
watcher.set(this._initValue)
} else if (this.update) {
this.update(watcher.value)
}
}
this._bound = true
}
/**
* check if this is a dynamic literal binding.
*
* e.g. v-component="{{currentView}}"
*/
Directive.prototype._checkDynamicLiteral = function () {
var expression = this.expression
if (expression && this.isLiteral) {
var tokens = textParser.parse(expression)
if (tokens) {
var exp = textParser.tokensToExp(tokens)
this.expression = this.vm.$get(exp)
this._watcherExp = exp
this._isDynamicLiteral = true
}
}
}
/**
* Check if the directive is a function caller
* and if the expression is a callable one. If both true,
* we wrap up the expression and use it as the event
* handler.
*
* e.g. v-on="click: a++"
*
* @return {Boolean}
*/
Directive.prototype._checkStatement = function () {
var expression = this.expression
if (
expression && this.acceptStatement &&
!expParser.isSimplePath(expression)
) {
var fn = expParser.parse(expression).get
var vm = this.vm
var handler = function () {
fn.call(vm, vm)
}
if (this.filters) {
handler = vm._applyFilters(handler, null, this.filters)
}
this.update(handler)
return true
}
}
/**
* Check for an attribute directive param, e.g. lazy
*
* @param {String} name
* @return {String}
*/
Directive.prototype._checkParam = function (name) {
var param = this.el.getAttribute(name)
if (param !== null) {
this.el.removeAttribute(name)
param = this.vm.$interpolate(param)
}
return param
}
/**
* Set the corresponding value with the setter.
* This should only be used in two-way directives
* e.g. v-model.
*
* @param {*} value
* @public
*/
Directive.prototype.set = function (value) {
/* istanbul ignore else */
if (this.twoWay) {
this._withLock(function () {
this._watcher.set(value)
})
} else if (process.env.NODE_ENV !== 'production') {
_.warn(
'Directive.set() can only be used inside twoWay' +
'directives.'
)
}
}
/**
* Execute a function while preventing that function from
* triggering updates on this directive instance.
*
* @param {Function} fn
*/
Directive.prototype._withLock = function (fn) {
var self = this
self._locked = true
fn.call(self)
_.nextTick(function () {
self._locked = false
})
}
/**
* Convenience method that attaches a DOM event listener
* to the directive element and autometically tears it down
* during unbind.
*
* @param {String} event
* @param {Function} handler
*/
Directive.prototype.on = function (event, handler) {
_.on(this.el, event, handler)
;(this._listeners || (this._listeners = []))
.push([event, handler])
}
/**
* Teardown the watcher and call unbind.
*/
Directive.prototype._teardown = function () {
if (this._bound) {
this._bound = false
if (this.unbind) {
this.unbind()
}
if (this._watcher) {
this._watcher.teardown()
}
var listeners = this._listeners
if (listeners) {
for (var i = 0; i < listeners.length; i++) {
_.off(this.el, listeners[i][0], listeners[i][1])
}
}
this.vm = this.el =
this._watcher = this._listeners = null
}
}
module.exports = Directive