-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirective.js
256 lines (231 loc) · 6.75 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
252
253
254
255
256
var dirId = 1,
ARG_RE = /^[\w\$-]+$/,
FILTER_TOKEN_RE = /[^\s'"]+|'[^']+'|"[^"]+"/g,
NESTING_RE = /^\$(parent|root)\./,
SINGLE_VAR_RE = /^[\w\.$]+$/,
QUOTE_RE = /"/g
/**
* Directive class
* represents a single directive instance in the DOM
*/
function Directive (name, ast, definition, compiler, el) {
this.id = dirId++
this.name = name
this.compiler = compiler
this.vm = compiler.vm
this.el = el
this.computeFilters = false
this.key = ast.key
this.arg = ast.arg
this.expression = ast.expression
var isEmpty = this.expression === ''
// mix in properties from the directive definition
if (typeof definition === 'function') {
this[isEmpty ? 'bind' : 'update'] = definition
} else {
for (var prop in definition) {
this[prop] = definition[prop]
}
}
// empty expression, we're done.
if (isEmpty || this.isEmpty) {
this.isEmpty = true
return
}
this.expression = (
this.isLiteral
? compiler.eval(this.expression)
: this.expression
).trim()
var filters = ast.filters,
filter, fn, i, l, computed
if (filters) {
this.filters = []
for (i = 0, l = filters.length; i < l; i++) {
filter = filters[i]
fn = this.compiler.getOption('filters', filter.name)
if (fn) {
filter.apply = fn
this.filters.push(filter)
if (fn.computed) {
computed = true
}
}
}
}
if (!this.filters || !this.filters.length) {
this.filters = null
}
if (computed) {
this.computedKey = Directive.inlineFilters(this.key, this.filters)
this.filters = null
}
this.isExp =
computed ||
!SINGLE_VAR_RE.test(this.key) ||
NESTING_RE.test(this.key)
}
var DirProto = Directive.prototype
/**
* called when a new value is set
* for computed properties, this will only be called once
* during initialization.
*/
DirProto.$update = function (value, init) {
if (this.$lock) return
if (init || value !== this.value || (value && typeof value === 'object')) {
this.value = value
if (this.update) {
this.update(
this.filters && !this.computeFilters
? 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.apply(this.vm, [filtered].concat(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 static methods -----------------------------------------------------
/**
* Parse a directive string into an Array of
* AST-like objects representing directives
*/
Directive.parse = function (str) {
var inSingle = false,
inDouble = false,
curly = 0,
square = 0,
paren = 0,
begin = 0,
argIndex = 0,
dirs = [],
dir = {},
lastFilterIndex = 0,
arg
for (var c, i = 0, l = str.length; i < l; i++) {
c = str.charAt(i)
if (inSingle) {
// check single quote
if (c === "'") inSingle = !inSingle
} else if (inDouble) {
// check double quote
if (c === '"') inDouble = !inDouble
} else if (c === ',' && !paren && !curly && !square) {
// reached the end of a directive
pushDir()
// reset & skip the comma
dir = {}
begin = argIndex = lastFilterIndex = i + 1
} else if (c === ':' && !dir.key && !dir.arg) {
// argument
arg = str.slice(begin, i).trim()
if (ARG_RE.test(arg)) {
argIndex = i + 1
dir.arg = str.slice(begin, i).trim()
}
} else if (c === '|' && str.charAt(i + 1) !== '|' && str.charAt(i - 1) !== '|') {
if (dir.key === undefined) {
// first filter, end of key
lastFilterIndex = i + 1
dir.key = str.slice(argIndex, i).trim()
} else {
// already has filter
pushFilter()
}
} else if (c === '"') {
inDouble = true
} else if (c === "'") {
inSingle = true
} else if (c === '(') {
paren++
} else if (c === ')') {
paren--
} else if (c === '[') {
square++
} else if (c === ']') {
square--
} else if (c === '{') {
curly++
} else if (c === '}') {
curly--
}
}
if (i === 0 || begin !== i) {
pushDir()
}
function pushDir () {
dir.expression = str.slice(begin, i).trim()
if (dir.key === undefined) {
dir.key = str.slice(argIndex, i).trim()
} else if (lastFilterIndex !== begin) {
pushFilter()
}
if (i === 0 || dir.key) {
dirs.push(dir)
}
}
function pushFilter () {
var exp = str.slice(lastFilterIndex, i).trim(),
filter
if (exp) {
filter = {}
var tokens = exp.match(FILTER_TOKEN_RE)
filter.name = tokens[0]
filter.args = tokens.length > 1 ? tokens.slice(1) : null
}
if (filter) {
(dir.filters = dir.filters || []).push(filter)
}
lastFilterIndex = i + 1
}
return dirs
}
/**
* Inline computed filters so they become part
* of the expression
*/
Directive.inlineFilters = function (key, filters) {
var args, filter
for (var i = 0, l = filters.length; i < l; i++) {
filter = filters[i]
args = filter.args
? ',"' + filter.args.map(escapeQuote).join('","') + '"'
: ''
key = 'this.$compiler.getOption("filters", "' +
filter.name +
'").call(this,' +
key + args +
')'
}
return key
}
/**
* Convert double quotes to single quotes
* so they don't mess up the generated function body
*/
function escapeQuote (v) {
return v.indexOf('"') > -1
? v.replace(QUOTE_RE, '\'')
: v
}
module.exports = Directive