-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
354 lines (323 loc) · 7.89 KB
/
options.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
var _ = require('./index')
var config = require('../config')
var extend = _.extend
/**
* Option overwriting strategies are functions that handle
* how to merge a parent option value and a child option
* value into the final value.
*
* All strategy functions follow the same signature:
*
* @param {*} parentVal
* @param {*} childVal
* @param {Vue} [vm]
*/
var strats = config.optionMergeStrategies = Object.create(null)
/**
* Helper that recursively merges two data objects together.
*/
function mergeData (to, from) {
var key, toVal, fromVal
for (key in from) {
toVal = to[key]
fromVal = from[key]
if (!to.hasOwnProperty(key)) {
_.set(to, key, fromVal)
} else if (_.isObject(toVal) && _.isObject(fromVal)) {
mergeData(toVal, fromVal)
}
}
return to
}
/**
* Data
*/
strats.data = function (parentVal, childVal, vm) {
if (!vm) {
// in a Vue.extend merge, both should be functions
if (!childVal) {
return parentVal
}
if (typeof childVal !== 'function') {
process.env.NODE_ENV !== 'production' && _.warn(
'The "data" option should be a function ' +
'that returns a per-instance value in component ' +
'definitions.'
)
return parentVal
}
if (!parentVal) {
return childVal
}
// when parentVal & childVal are both present,
// we need to return a function that returns the
// merged result of both functions... no need to
// check if parentVal is a function here because
// it has to be a function to pass previous merges.
return function mergedDataFn () {
return mergeData(
childVal.call(this),
parentVal.call(this)
)
}
} else if (parentVal || childVal) {
return function mergedInstanceDataFn () {
// instance merge
var instanceData = typeof childVal === 'function'
? childVal.call(vm)
: childVal
var defaultData = typeof parentVal === 'function'
? parentVal.call(vm)
: undefined
if (instanceData) {
return mergeData(instanceData, defaultData)
} else {
return defaultData
}
}
}
}
/**
* El
*/
strats.el = function (parentVal, childVal, vm) {
if (!vm && childVal && typeof childVal !== 'function') {
process.env.NODE_ENV !== 'production' && _.warn(
'The "el" option should be a function ' +
'that returns a per-instance value in component ' +
'definitions.'
)
return
}
var ret = childVal || parentVal
// invoke the element factory if this is instance merge
return vm && typeof ret === 'function'
? ret.call(vm)
: ret
}
/**
* Hooks and param attributes are merged as arrays.
*/
strats.init =
strats.created =
strats.ready =
strats.attached =
strats.detached =
strats.beforeCompile =
strats.compiled =
strats.beforeDestroy =
strats.destroyed = function (parentVal, childVal) {
return childVal
? parentVal
? parentVal.concat(childVal)
: _.isArray(childVal)
? childVal
: [childVal]
: parentVal
}
/**
* 0.11 deprecation warning
*/
strats.paramAttributes = function () {
/* istanbul ignore next */
process.env.NODE_ENV !== 'production' && _.warn(
'"paramAttributes" option has been deprecated in 0.12. ' +
'Use "props" instead.'
)
}
/**
* Assets
*
* When a vm is present (instance creation), we need to do
* a three-way merge between constructor options, instance
* options and parent options.
*/
function mergeAssets (parentVal, childVal) {
var res = Object.create(parentVal)
return childVal
? extend(res, guardArrayAssets(childVal))
: res
}
config._assetTypes.forEach(function (type) {
strats[type + 's'] = mergeAssets
})
/**
* Events & Watchers.
*
* Events & watchers hashes should not overwrite one
* another, so we merge them as arrays.
*/
strats.watch =
strats.events = function (parentVal, childVal) {
if (!childVal) return parentVal
if (!parentVal) return childVal
var ret = {}
extend(ret, parentVal)
for (var key in childVal) {
var parent = ret[key]
var child = childVal[key]
if (parent && !_.isArray(parent)) {
parent = [parent]
}
ret[key] = parent
? parent.concat(child)
: [child]
}
return ret
}
/**
* Other object hashes.
*/
strats.props =
strats.methods =
strats.computed = function (parentVal, childVal) {
if (!childVal) return parentVal
if (!parentVal) return childVal
var ret = Object.create(null)
extend(ret, parentVal)
extend(ret, childVal)
return ret
}
/**
* Default strategy.
*/
var defaultStrat = function (parentVal, childVal) {
return childVal === undefined
? parentVal
: childVal
}
/**
* Make sure component options get converted to actual
* constructors.
*
* @param {Object} options
*/
function guardComponents (options) {
if (options.components) {
var components = options.components =
guardArrayAssets(options.components)
var def
var ids = Object.keys(components)
for (var i = 0, l = ids.length; i < l; i++) {
var key = ids[i]
if (_.commonTagRE.test(key)) {
process.env.NODE_ENV !== 'production' && _.warn(
'Do not use built-in HTML elements as component ' +
'id: ' + key
)
continue
}
def = components[key]
if (_.isPlainObject(def)) {
components[key] = _.Vue.extend(def)
}
}
}
}
/**
* Ensure all props option syntax are normalized into the
* Object-based format.
*
* @param {Object} options
*/
function guardProps (options) {
var props = options.props
var i
if (_.isArray(props)) {
options.props = {}
i = props.length
while (i--) {
options.props[props[i]] = null
}
} else if (_.isPlainObject(props)) {
var keys = Object.keys(props)
i = keys.length
while (i--) {
var val = props[keys[i]]
if (typeof val === 'function') {
props[keys[i]] = { type: val }
}
}
}
}
/**
* Guard an Array-format assets option and converted it
* into the key-value Object format.
*
* @param {Object|Array} assets
* @return {Object}
*/
function guardArrayAssets (assets) {
if (_.isArray(assets)) {
var res = {}
var i = assets.length
var asset
while (i--) {
asset = assets[i]
var id = typeof asset === 'function'
? ((asset.options && asset.options.name) || asset.id)
: (asset.name || asset.id)
if (!id) {
process.env.NODE_ENV !== 'production' && _.warn(
'Array-syntax assets must provide a "name" or "id" field.'
)
} else {
res[id] = asset
}
}
return res
}
return assets
}
/**
* Merge two option objects into a new one.
* Core utility used in both instantiation and inheritance.
*
* @param {Object} parent
* @param {Object} child
* @param {Vue} [vm] - if vm is present, indicates this is
* an instantiation merge.
*/
exports.mergeOptions = function merge (parent, child, vm) {
guardComponents(child)
guardProps(child)
var options = {}
var key
if (child.mixins) {
for (var i = 0, l = child.mixins.length; i < l; i++) {
parent = merge(parent, child.mixins[i], vm)
}
}
for (key in parent) {
mergeField(key)
}
for (key in child) {
if (!(parent.hasOwnProperty(key))) {
mergeField(key)
}
}
function mergeField (key) {
var strat = strats[key] || defaultStrat
options[key] = strat(parent[key], child[key], vm, key)
}
return options
}
/**
* Resolve an asset.
* This function is used because child instances need access
* to assets defined in its ancestor chain.
*
* @param {Object} options
* @param {String} type
* @param {String} id
* @return {Object|Function}
*/
exports.resolveAsset = function resolve (options, type, id) {
var assets = options[type]
var camelizedId
return assets[id] ||
// camelCase ID
assets[camelizedId = _.camelize(id)] ||
// Pascal Case ID
assets[camelizedId.charAt(0).toUpperCase() + camelizedId.slice(1)]
}