-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirective.js
196 lines (167 loc) · 5.18 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
var utils = require('./utils'),
directives = require('./directives'),
filters = require('./filters'),
// Regexes!
// regex to split multiple directive expressions
// split by commas, but ignore commas within quotes, parens and escapes.
SPLIT_RE = /(?:['"](?:\\.|[^'"])*['"]|\((?:\\.|[^\)])*\)|\\.|[^,])+/g,
// match up to the first single pipe, ignore those within quotes.
KEY_RE = /^(?:['"](?:\\.|[^'"])*['"]|\\.|[^\|]|\|\|)+/,
ARG_RE = /^([\w-$ ]+):(.+)$/,
FILTERS_RE = /\|[^\|]+/g,
FILTER_TOKEN_RE = /[^\s']+|'[^']+'/g,
NESTING_RE = /^\$(parent|root)\./,
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 isEmpty = expression === ''
// mix in properties from the directive definition
if (typeof definition === 'function') {
this[isEmpty ? '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 (isEmpty || this.isEmpty) {
this.isEmpty = true
return
}
this.expression = expression.trim()
this.rawKey = rawKey
parseKey(this, rawKey)
this.isExp = !SINGLE_VAR_RE.test(this.key) || NESTING_RE.test(this.key)
var filterExps = this.expression.slice(rawKey.length).match(FILTERS_RE)
if (filterExps) {
this.filters = []
for (var i = 0, l = filterExps.length, filter; 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 key = rawKey
if (rawKey.indexOf(':') > -1) {
var argMatch = rawKey.match(ARG_RE)
key = argMatch
? argMatch[2].trim()
: key
dir.arg = argMatch
? argMatch[1].trim()
: null
}
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) {
var type = utils.typeOf(value)
if (init || value !== this.value || type === 'Object' || type === 'Array') {
this.value = value
if (this._update) {
this._update(
this.filters
? this.applyFilters(value)
: value,
init
)
}
}
}
/**
* 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.call(this.vm, filtered, filter.args)
}
return filtered
}
/**
* Unbind diretive
*/
DirProto.unbind = function () {
// this can be called before the el is even assigned...
if (!this.el || !this.vm) return
if (this._unbind) this._unbind()
this.vm = this.el = this.binding = this.compiler = null
}
// exposed methods ------------------------------------------------------------
/**
* split a unquoted-comma separated expression into
* multiple clauses
*/
Directive.split = function (exp) {
return exp.indexOf(',') > -1
? exp.match(SPLIT_RE) || ['']
: [exp]
}
/**
* make sure the directive and expression is valid
* before we create an instance
*/
Directive.parse = function (dirname, expression, compiler, node) {
var dir = compiler.getOption('directives', dirname) || directives[dirname]
if (!dir) return utils.warn('unknown directive: ' + dirname)
var rawKey
if (expression.indexOf('|') > -1) {
var keyMatch = expression.match(KEY_RE)
if (keyMatch) {
rawKey = keyMatch[0].trim()
}
} else {
rawKey = expression.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