/
directive.js
217 lines (183 loc) · 5.39 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
var config = require('./config'),
utils = require('./utils'),
directives = require('./directives'),
filters = require('./filters'),
// Regexes!
KEY_RE = /^[^\|]+/,
ARG_RE = /([^:]+):(.+)$/,
FILTERS_RE = /\|[^\|]+/g,
FILTER_TOKEN_RE = /[^\s']+|'[^']+'/g,
NESTING_RE = /^\^+/,
SINGLE_VAR_RE = /^[\w\.\$]+$/
/**
* Directive class
* represents a single directive instance in the DOM
*/
function Directive (definition, expression, rawKey, compiler, node) {
this.compiler = compiler
this.vm = compiler.vm
this.el = node
var isSimple = expression === ''
// mix in properties from the directive definition
if (typeof definition === 'function') {
this[isSimple ? 'bind' : '_update'] = definition
} else {
for (var prop in definition) {
if (prop === 'unbind' || prop === 'update') {
this['_' + prop] = definition[prop]
} else {
this[prop] = definition[prop]
}
}
}
// empty expression, we're done.
if (isSimple) {
this.isSimple = true
return
}
this.expression = expression.trim()
this.rawKey = rawKey
parseKey(this, rawKey)
this.isExp = !SINGLE_VAR_RE.test(this.key)
var filterExps = expression.match(FILTERS_RE)
if (filterExps) {
this.filters = []
var i = 0, l = filterExps.length, filter
for (; i < l; i++) {
filter = parseFilter(filterExps[i], this.compiler)
if (filter) this.filters.push(filter)
}
if (!this.filters.length) this.filters = null
} else {
this.filters = null
}
}
var DirProto = Directive.prototype
/**
* parse a key, extract argument and nesting/root info
*/
function parseKey (dir, rawKey) {
var argMatch = rawKey.match(ARG_RE)
var key = argMatch
? argMatch[2].trim()
: rawKey.trim()
dir.arg = argMatch
? argMatch[1].trim()
: null
var nesting = key.match(NESTING_RE)
dir.nesting = nesting
? nesting[0].length
: false
dir.root = key.charAt(0) === '*'
if (dir.nesting) {
key = key.replace(NESTING_RE, '')
} else if (dir.root) {
key = key.slice(1)
}
dir.key = key
}
/**
* parse a filter expression
*/
function parseFilter (filter, compiler) {
var tokens = filter.slice(1).match(FILTER_TOKEN_RE)
if (!tokens) return
tokens = tokens.map(function (token) {
return token.replace(/'/g, '').trim()
})
var name = tokens[0],
apply = compiler.getOption('filters', name) || filters[name]
if (!apply) {
utils.warn('Unknown filter: ' + name)
return
}
return {
name : name,
apply : apply,
args : tokens.length > 1
? tokens.slice(1)
: null
}
}
/**
* called when a new value is set
* for computed properties, this will only be called once
* during initialization.
*/
DirProto.update = function (value, init) {
if (!init && value === this.value) return
this.value = value
this.apply(value)
}
/**
* -- computed property only --
* called when a dependency has changed
*/
DirProto.refresh = function (value) {
// pass element and viewmodel info to the getter
// enables context-aware bindings
if (value) this.value = value
if (this.isFn) {
value = this.value
} else {
value = this.value.get({
el: this.el,
vm: this.vm
})
if (value !== undefined && value === this.computedValue) return
this.computedValue = value
}
this.apply(value)
}
/**
* Actually invoking the _update from the directive's definition
*/
DirProto.apply = function (value) {
this._update(
this.filters
? this.applyFilters(value)
: value
)
}
/**
* pipe the value through filters
*/
DirProto.applyFilters = function (value) {
var filtered = value, filter
for (var i = 0, l = this.filters.length; i < l; i++) {
filter = this.filters[i]
filtered = filter.apply(filtered, filter.args)
}
return filtered
}
/**
* Unbind diretive
* @ param {Boolean} update
* Sometimes we call unbind before an update (i.e. not destroy)
* just to teardown previous stuff, in that case we do not want
* to null everything.
*/
DirProto.unbind = function (update) {
// this can be called before the el is even assigned...
if (!this.el) return
if (this._unbind) this._unbind(update)
if (!update) this.vm = this.el = this.binding = this.compiler = null
}
/**
* make sure the directive and expression is valid
* before we create an instance
*/
Directive.parse = function (dirname, expression, compiler, node) {
var prefix = config.prefix
if (dirname.indexOf(prefix) === -1) return
dirname = dirname.slice(prefix.length + 1)
var dir = compiler.getOption('directives', dirname) || directives[dirname]
if (!dir) return utils.warn('unknown directive: ' + dirname)
var keyMatch = expression.match(KEY_RE),
rawKey = keyMatch && keyMatch[0].trim()
// have a valid raw key, or be an empty directive
return (rawKey || expression === '')
? new Directive(dir, expression, rawKey, compiler, node)
: utils.warn('invalid directive expression: ' + expression)
}
module.exports = Directive