You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3768 lines
94 KiB
3768 lines
94 KiB
'use strict';
|
|
|
|
var entities = require('entities');
|
|
|
|
/**
|
|
* Convert a value to a string that is actually rendered.
|
|
*
|
|
* @param {*} val
|
|
* @return {String}
|
|
*/
|
|
|
|
function renderString(val) {
|
|
return val == null ? '' : typeof val === 'object' ? JSON.stringify(val, null, 2) : String(val);
|
|
}
|
|
|
|
/**
|
|
* Make a map and return a function for checking if a key
|
|
* is in that map.
|
|
*
|
|
* @param {String} str
|
|
* @param {Boolean} expectsLowerCase
|
|
* @return {Function}
|
|
*/
|
|
|
|
function makeMap(str, expectsLowerCase) {
|
|
var map = Object.create(null);
|
|
var list = str.split(',');
|
|
for (var i = 0; i < list.length; i++) {
|
|
map[list[i]] = true;
|
|
}
|
|
return expectsLowerCase ? function (val) {
|
|
return map[val.toLowerCase()];
|
|
} : function (val) {
|
|
return map[val];
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Check if a tag is a built-in tag.
|
|
*/
|
|
|
|
var isBuiltInTag = makeMap('slot,component,render,transition', true);
|
|
|
|
/**
|
|
* Remove an item from an array
|
|
*
|
|
* @param {Array} arr
|
|
* @param {*} item
|
|
*/
|
|
|
|
function remove(arr, item) {
|
|
if (arr.length) {
|
|
var index = arr.indexOf(item);
|
|
if (index > -1) {
|
|
return arr.splice(index, 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check whether the object has the property.
|
|
*
|
|
* @param {Object} obj
|
|
* @param {String} key
|
|
* @return {Boolean}
|
|
*/
|
|
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
function hasOwn(obj, key) {
|
|
return hasOwnProperty.call(obj, key);
|
|
}
|
|
|
|
/**
|
|
* Check if value is primitive
|
|
*
|
|
* @param {*} value
|
|
* @return {Boolean}
|
|
*/
|
|
|
|
function isPrimitive(value) {
|
|
return typeof value === 'string' || typeof value === 'number';
|
|
}
|
|
|
|
/**
|
|
* Create a cached version of a pure function.
|
|
*
|
|
* @param {Function} fn
|
|
* @return {Function}
|
|
*/
|
|
|
|
function cached(fn) {
|
|
var cache = Object.create(null);
|
|
return function cachedFn(str) {
|
|
var hit = cache[str];
|
|
return hit || (cache[str] = fn(str));
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Camelize a hyphen-delmited string.
|
|
*
|
|
* @param {String} str
|
|
* @return {String}
|
|
*/
|
|
|
|
var camelizeRE = /-(\w)/g;
|
|
var camelize = cached(function (str) {
|
|
return str.replace(camelizeRE, toUpper);
|
|
});
|
|
|
|
function toUpper(_, c) {
|
|
return c ? c.toUpperCase() : '';
|
|
}
|
|
|
|
/**
|
|
* Hyphenate a camelCase string.
|
|
*
|
|
* @param {String} str
|
|
* @return {String}
|
|
*/
|
|
|
|
var hyphenateRE = /([a-z\d])([A-Z])/g;
|
|
var hyphenate = cached(function (str) {
|
|
return str.replace(hyphenateRE, '$1-$2').toLowerCase();
|
|
});
|
|
|
|
/**
|
|
* Simple bind, faster than native
|
|
*
|
|
* @param {Function} fn
|
|
* @param {Object} ctx
|
|
* @return {Function}
|
|
*/
|
|
|
|
function bind(fn, ctx) {
|
|
return function (a) {
|
|
var l = arguments.length;
|
|
return l ? l > 1 ? fn.apply(ctx, arguments) : fn.call(ctx, a) : fn.call(ctx);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Convert an Array-like object to a real Array.
|
|
*
|
|
* @param {Array-like} list
|
|
* @param {Number} [start] - start index
|
|
* @return {Array}
|
|
*/
|
|
|
|
function toArray(list, start) {
|
|
start = start || 0;
|
|
var i = list.length - start;
|
|
var ret = new Array(i);
|
|
while (i--) {
|
|
ret[i] = list[i + start];
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* Mix properties into target object.
|
|
*
|
|
* @param {Object} to
|
|
* @param {Object} from
|
|
*/
|
|
|
|
function extend(to, from) {
|
|
for (var key in from) {
|
|
to[key] = from[key];
|
|
}
|
|
return to;
|
|
}
|
|
|
|
/**
|
|
* Quick object check - this is primarily used to tell
|
|
* Objects from primitive values when we know the value
|
|
* is a JSON-compliant type.
|
|
*
|
|
* @param {*} obj
|
|
* @return {Boolean}
|
|
*/
|
|
|
|
function isObject(obj) {
|
|
return obj !== null && typeof obj === 'object';
|
|
}
|
|
|
|
/**
|
|
* Strict object type check. Only returns true
|
|
* for plain JavaScript objects.
|
|
*
|
|
* @param {*} obj
|
|
* @return {Boolean}
|
|
*/
|
|
|
|
var toString = Object.prototype.toString;
|
|
var OBJECT_STRING = '[object Object]';
|
|
function isPlainObject(obj) {
|
|
return toString.call(obj) === OBJECT_STRING;
|
|
}
|
|
|
|
/**
|
|
* Array type check.
|
|
*
|
|
* @param {*} obj
|
|
* @return {Boolean}
|
|
*/
|
|
|
|
var isArray = Array.isArray;
|
|
|
|
/**
|
|
* Check if a string starts with $ or _
|
|
*
|
|
* @param {String} str
|
|
* @return {Boolean}
|
|
*/
|
|
|
|
function isReserved(str) {
|
|
var c = (str + '').charCodeAt(0);
|
|
return c === 0x24 || c === 0x5F;
|
|
}
|
|
|
|
/**
|
|
* Define a property.
|
|
*
|
|
* @param {Object} obj
|
|
* @param {String} key
|
|
* @param {*} val
|
|
* @param {Boolean} [enumerable]
|
|
*/
|
|
|
|
function def(obj, key, val, enumerable) {
|
|
Object.defineProperty(obj, key, {
|
|
value: val,
|
|
enumerable: !!enumerable,
|
|
writable: true,
|
|
configurable: true
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Parse simple path.
|
|
*/
|
|
|
|
var bailRE = /[^\w\.]/;
|
|
function parsePath(path) {
|
|
if (bailRE.test(path)) {
|
|
return;
|
|
} else {
|
|
path = path.split('.');
|
|
return function (obj) {
|
|
for (var i = 0; i < path.length; i++) {
|
|
if (!obj) return;
|
|
obj = obj[path[i]];
|
|
}
|
|
return obj;
|
|
};
|
|
}
|
|
}
|
|
|
|
/* global MutationObserver */
|
|
|
|
// can we use __proto__?
|
|
var hasProto = '__proto__' in {};
|
|
|
|
// Browser environment sniffing
|
|
var inBrowser = typeof window !== 'undefined' && Object.prototype.toString.call(window) !== '[object Object]';
|
|
|
|
// UA sniffing for working around browser-specific quirks
|
|
var UA$1 = inBrowser && window.navigator.userAgent.toLowerCase();
|
|
var isIos = UA$1 && /(iphone|ipad|ipod|ios)/i.test(UA$1);
|
|
var isWechat = UA$1 && UA$1.indexOf('micromessenger') > 0;
|
|
|
|
/**
|
|
* Defer a task to execute it asynchronously. Ideally this
|
|
* should be executed as a microtask, so we leverage
|
|
* MutationObserver if it's available, and fallback to
|
|
* setTimeout(0).
|
|
*
|
|
* @param {Function} cb
|
|
* @param {Object} ctx
|
|
*/
|
|
|
|
var nextTick = function () {
|
|
var callbacks = [];
|
|
var pending = false;
|
|
var timerFunc;
|
|
function nextTickHandler() {
|
|
pending = false;
|
|
var copies = callbacks.slice(0);
|
|
callbacks = [];
|
|
for (var i = 0; i < copies.length; i++) {
|
|
copies[i]();
|
|
}
|
|
}
|
|
|
|
/* istanbul ignore if */
|
|
if (typeof MutationObserver !== 'undefined' && !(isWechat && isIos)) {
|
|
var counter = 1;
|
|
var observer = new MutationObserver(nextTickHandler);
|
|
var textNode = document.createTextNode(counter);
|
|
observer.observe(textNode, {
|
|
characterData: true
|
|
});
|
|
timerFunc = function timerFunc() {
|
|
counter = (counter + 1) % 2;
|
|
textNode.data = counter;
|
|
};
|
|
} else {
|
|
// webpack attempts to inject a shim for setImmediate
|
|
// if it is used as a global, so we have to work around that to
|
|
// avoid bundling unnecessary code.
|
|
var context = inBrowser ? window : typeof global !== 'undefined' ? global : {};
|
|
timerFunc = context.setImmediate || setTimeout;
|
|
}
|
|
return function (cb, ctx) {
|
|
var func = ctx ? function () {
|
|
cb.call(ctx);
|
|
} : cb;
|
|
callbacks.push(func);
|
|
if (pending) return;
|
|
pending = true;
|
|
timerFunc(nextTickHandler, 0);
|
|
};
|
|
}();
|
|
|
|
var Set$1 = void 0;
|
|
/* istanbul ignore if */
|
|
if (typeof Set !== 'undefined' && Set.toString().match(/native code/)) {
|
|
// use native Set when available.
|
|
Set$1 = Set;
|
|
} else {
|
|
// a non-standard Set polyfill that only works with primitive keys.
|
|
Set$1 = function _Set() {
|
|
this.set = Object.create(null);
|
|
};
|
|
Set$1.prototype.has = function (key) {
|
|
return this.set[key] !== undefined;
|
|
};
|
|
Set$1.prototype.add = function (key) {
|
|
this.set[key] = 1;
|
|
};
|
|
Set$1.prototype.clear = function () {
|
|
this.set = Object.create(null);
|
|
};
|
|
}
|
|
|
|
var hasProxy = void 0;
|
|
var proxyHandlers = void 0;
|
|
var initProxy = void 0;
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
(function () {
|
|
var allowedGlobals = makeMap('Infinity,undefined,NaN,isFinite,isNaN,' + 'parseFloat,parseInt,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' + 'Math,Number,Date,Array,Object,Boolean,String,RegExp,Map,Set,JSON,Intl');
|
|
|
|
hasProxy = typeof Proxy !== 'undefined' && Proxy.toString().match(/native code/);
|
|
|
|
proxyHandlers = {
|
|
has: function has(target, key) {
|
|
var has = key in target;
|
|
var isAllowedGlobal = allowedGlobals(key);
|
|
if (!has && !isAllowedGlobal) {
|
|
warn$1('Trying to access non-existent property "' + key + '" while rendering.', target);
|
|
}
|
|
return !isAllowedGlobal;
|
|
}
|
|
};
|
|
|
|
initProxy = function initProxy(vm) {
|
|
if (hasProxy) {
|
|
vm._renderProxy = new Proxy(vm, proxyHandlers);
|
|
} else {
|
|
vm._renderProxy = vm;
|
|
}
|
|
};
|
|
})();
|
|
}
|
|
|
|
var uid$2 = 0;
|
|
|
|
/**
|
|
* A dep is an observable that can have multiple
|
|
* directives subscribing to it.
|
|
*
|
|
* @constructor
|
|
*/
|
|
|
|
function Dep() {
|
|
this.id = uid$2++;
|
|
this.subs = [];
|
|
}
|
|
|
|
// the current target watcher being evaluated.
|
|
// this is globally unique because there could be only one
|
|
// watcher being evaluated at any time.
|
|
Dep.target = null;
|
|
|
|
/**
|
|
* Add a directive subscriber.
|
|
*
|
|
* @param {Directive} sub
|
|
*/
|
|
|
|
Dep.prototype.addSub = function (sub) {
|
|
this.subs.push(sub);
|
|
};
|
|
|
|
/**
|
|
* Remove a directive subscriber.
|
|
*
|
|
* @param {Directive} sub
|
|
*/
|
|
|
|
Dep.prototype.removeSub = function (sub) {
|
|
remove(this.subs, sub);
|
|
};
|
|
|
|
/**
|
|
* Add self as a dependency to the target watcher.
|
|
*/
|
|
|
|
Dep.prototype.depend = function () {
|
|
Dep.target.addDep(this);
|
|
};
|
|
|
|
/**
|
|
* Notify all subscribers of a new value.
|
|
*/
|
|
|
|
Dep.prototype.notify = function () {
|
|
// stablize the subscriber list first
|
|
var subs = this.subs.slice();
|
|
for (var i = 0, l = subs.length; i < l; i++) {
|
|
subs[i].update();
|
|
}
|
|
};
|
|
|
|
var config = {
|
|
|
|
/**
|
|
* Preserve whitespaces between elements.
|
|
*/
|
|
|
|
preserveWhitespace: true,
|
|
|
|
/**
|
|
* Whether to suppress warnings.
|
|
*
|
|
* @type {Boolean}
|
|
*/
|
|
|
|
silent: false,
|
|
|
|
/**
|
|
* Check if a tag is reserved so that it cannot be registered as a
|
|
* component. This is platform-dependent and may be overwritten.
|
|
*/
|
|
|
|
isReservedTag: function isReservedTag() {
|
|
return false;
|
|
},
|
|
|
|
/**
|
|
* Check if a tag is an unknown element.
|
|
* Platform-dependent.
|
|
*/
|
|
|
|
isUnknownElement: function isUnknownElement() {
|
|
return false;
|
|
},
|
|
|
|
/**
|
|
* List of asset types that a component can own.
|
|
*
|
|
* @type {Array}
|
|
*/
|
|
|
|
_assetTypes: ['component', 'directive', 'transition'],
|
|
|
|
/**
|
|
* List of lifecycle hooks.
|
|
*
|
|
* @type {Array}
|
|
*/
|
|
|
|
_lifecycleHooks: ['init', 'created', 'beforeMount', 'mounted', 'ready', 'beforeUpdate', 'updated', 'beforeDestroy', 'destroyed'],
|
|
|
|
/**
|
|
* Max circular updates allowed in a batcher flush cycle.
|
|
*/
|
|
|
|
_maxUpdateCount: 100
|
|
};
|
|
|
|
// we have two separate queues: one for directive updates
|
|
// and one for user watcher registered via $watch().
|
|
// we want to guarantee directive updates to be called
|
|
// before user watchers so that when user watchers are
|
|
// triggered, the DOM would have already been in updated
|
|
// state.
|
|
|
|
var queueIndex;
|
|
var queue = [];
|
|
var userQueue = [];
|
|
var has = {};
|
|
var circular = {};
|
|
var waiting = false;
|
|
var internalQueueDepleted = false;
|
|
|
|
/**
|
|
* Reset the batcher's state.
|
|
*/
|
|
|
|
function resetBatcherState() {
|
|
queue = [];
|
|
userQueue = [];
|
|
has = {};
|
|
circular = {};
|
|
waiting = internalQueueDepleted = false;
|
|
}
|
|
|
|
/**
|
|
* Flush both queues and run the watchers.
|
|
*/
|
|
|
|
function flushBatcherQueue() {
|
|
queue.sort(queueSorter);
|
|
runBatcherQueue(queue);
|
|
internalQueueDepleted = true;
|
|
runBatcherQueue(userQueue);
|
|
resetBatcherState();
|
|
}
|
|
|
|
/**
|
|
* Sort queue before flush.
|
|
* This ensures components are updated from parent to child
|
|
* so there will be no duplicate updates, e.g. a child was
|
|
* pushed into the queue first and then its parent's props
|
|
* changed.
|
|
*/
|
|
|
|
function queueSorter(a, b) {
|
|
return a.id - b.id;
|
|
}
|
|
|
|
/**
|
|
* Run the watchers in a single queue.
|
|
*
|
|
* @param {Array} queue
|
|
*/
|
|
|
|
function runBatcherQueue(queue) {
|
|
// do not cache length because more watchers might be pushed
|
|
// as we run existing watchers
|
|
for (queueIndex = 0; queueIndex < queue.length; queueIndex++) {
|
|
var watcher = queue[queueIndex];
|
|
var id = watcher.id;
|
|
has[id] = null;
|
|
watcher.run();
|
|
// in dev build, check and stop circular updates.
|
|
if (process.env.NODE_ENV !== 'production' && has[id] != null) {
|
|
circular[id] = (circular[id] || 0) + 1;
|
|
if (circular[id] > config._maxUpdateCount) {
|
|
warn$1('You may have an infinite update loop for watcher ' + 'with expression "' + watcher.expression + '"', watcher.vm);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Push a watcher into the watcher queue.
|
|
* Jobs with duplicate IDs will be skipped unless it's
|
|
* pushed when the queue is being flushed.
|
|
*
|
|
* @param {Watcher} watcher
|
|
* properties:
|
|
* - {Number} id
|
|
* - {Function} run
|
|
*/
|
|
|
|
function pushWatcher(watcher) {
|
|
var id = watcher.id;
|
|
if (has[id] == null) {
|
|
if (internalQueueDepleted && !watcher.user) {
|
|
// an internal watcher triggered by a user watcher...
|
|
// let's run it immediately after current user watcher is done.
|
|
userQueue.splice(queueIndex + 1, 0, watcher);
|
|
} else {
|
|
// push watcher into appropriate queue
|
|
var q = watcher.user ? userQueue : queue;
|
|
has[id] = q.length;
|
|
q.push(watcher);
|
|
// queue the flush
|
|
if (!waiting) {
|
|
waiting = true;
|
|
nextTick(flushBatcherQueue);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var uid$1 = 0;
|
|
var prevTarget = void 0;
|
|
|
|
/**
|
|
* A watcher parses an expression, collects dependencies,
|
|
* and fires callback when the expression value changes.
|
|
* This is used for both the $watch() api and directives.
|
|
*
|
|
* @param {Vue} vm
|
|
* @param {String|Function} expOrFn
|
|
* @param {Function} cb
|
|
* @param {Object} options
|
|
* - {Array} filters
|
|
* - {Boolean} twoWay
|
|
* - {Boolean} deep
|
|
* - {Boolean} user
|
|
* - {Boolean} sync
|
|
* - {Boolean} lazy
|
|
* - {Function} [preProcess]
|
|
* - {Function} [postProcess]
|
|
* @constructor
|
|
*/
|
|
|
|
function Watcher(vm, expOrFn, cb, options) {
|
|
// mix in options
|
|
if (options) {
|
|
extend(this, options);
|
|
}
|
|
var isFn = typeof expOrFn === 'function';
|
|
this.vm = vm;
|
|
vm._watchers.push(this);
|
|
this.expression = expOrFn;
|
|
this.cb = cb;
|
|
this.id = ++uid$1; // uid for batching
|
|
this.active = true;
|
|
this.dirty = this.lazy; // for lazy watchers
|
|
this.deps = [];
|
|
this.newDeps = [];
|
|
this.depIds = new Set$1();
|
|
this.newDepIds = new Set$1();
|
|
// parse expression for getter
|
|
if (isFn) {
|
|
this.getter = expOrFn;
|
|
} else {
|
|
this.getter = parsePath(expOrFn);
|
|
if (!this.getter) {
|
|
this.getter = function () {};
|
|
process.env.NODE_ENV !== 'production' && warn$1('Failed watching path: ' + expOrFn + 'Watcher only accepts simple dot-delimited paths. ' + 'For full control, use a function instead.', vm);
|
|
}
|
|
}
|
|
this.value = this.lazy ? undefined : this.get();
|
|
// state for avoiding false triggers for deep and Array
|
|
// watchers during vm._digest()
|
|
this.queued = this.shallow = false;
|
|
}
|
|
|
|
/**
|
|
* Evaluate the getter, and re-collect dependencies.
|
|
*/
|
|
|
|
Watcher.prototype.get = function () {
|
|
this.beforeGet();
|
|
var value = this.getter.call(this.vm, this.vm);
|
|
// "touch" every property so they are all tracked as
|
|
// dependencies for deep watching
|
|
if (this.deep) {
|
|
traverse(value);
|
|
}
|
|
this.afterGet();
|
|
return value;
|
|
};
|
|
|
|
/**
|
|
* Prepare for dependency collection.
|
|
*/
|
|
|
|
Watcher.prototype.beforeGet = function () {
|
|
prevTarget = Dep.target;
|
|
Dep.target = this;
|
|
};
|
|
|
|
/**
|
|
* Add a dependency to this directive.
|
|
*
|
|
* @param {Dep} dep
|
|
*/
|
|
|
|
Watcher.prototype.addDep = function (dep) {
|
|
var id = dep.id;
|
|
if (!this.newDepIds.has(id)) {
|
|
this.newDepIds.add(id);
|
|
this.newDeps.push(dep);
|
|
if (!this.depIds.has(id)) {
|
|
dep.addSub(this);
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Clean up for dependency collection.
|
|
*/
|
|
|
|
Watcher.prototype.afterGet = function () {
|
|
Dep.target = prevTarget;
|
|
var i = this.deps.length;
|
|
while (i--) {
|
|
var dep = this.deps[i];
|
|
if (!this.newDepIds.has(dep.id)) {
|
|
dep.removeSub(this);
|
|
}
|
|
}
|
|
var tmp = this.depIds;
|
|
this.depIds = this.newDepIds;
|
|
this.newDepIds = tmp;
|
|
this.newDepIds.clear();
|
|
tmp = this.deps;
|
|
this.deps = this.newDeps;
|
|
this.newDeps = tmp;
|
|
this.newDeps.length = 0;
|
|
};
|
|
|
|
/**
|
|
* Subscriber interface.
|
|
* Will be called when a dependency changes.
|
|
*
|
|
* @param {Boolean} shallow
|
|
*/
|
|
|
|
Watcher.prototype.update = function (shallow) {
|
|
if (this.lazy) {
|
|
this.dirty = true;
|
|
} else if (this.sync) {
|
|
this.run();
|
|
} else {
|
|
// if queued, only overwrite shallow with non-shallow,
|
|
// but not the other way around.
|
|
this.shallow = this.queued ? shallow ? this.shallow : false : !!shallow;
|
|
this.queued = true;
|
|
pushWatcher(this);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Batcher job interface.
|
|
* Will be called by the batcher.
|
|
*/
|
|
|
|
Watcher.prototype.run = function () {
|
|
if (this.active) {
|
|
var value = this.get();
|
|
if (value !== this.value ||
|
|
// Deep watchers and watchers on Object/Arrays should fire even
|
|
// when the value is the same, because the value may
|
|
// have mutated; but only do so if this is a
|
|
// non-shallow update (caused by a vm digest).
|
|
(isObject(value) || this.deep) && !this.shallow) {
|
|
// set new value
|
|
var oldValue = this.value;
|
|
this.value = value;
|
|
this.cb.call(this.vm, value, oldValue);
|
|
}
|
|
this.queued = this.shallow = false;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Evaluate the value of the watcher.
|
|
* This only gets called for lazy watchers.
|
|
*/
|
|
|
|
Watcher.prototype.evaluate = function () {
|
|
// avoid overwriting another watcher that is being
|
|
// collected.
|
|
var current = Dep.target;
|
|
this.value = this.get();
|
|
this.dirty = false;
|
|
Dep.target = current;
|
|
};
|
|
|
|
/**
|
|
* Depend on all deps collected by this watcher.
|
|
*/
|
|
|
|
Watcher.prototype.depend = function () {
|
|
var i = this.deps.length;
|
|
while (i--) {
|
|
this.deps[i].depend();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Remove self from all dependencies' subcriber list.
|
|
*/
|
|
|
|
Watcher.prototype.teardown = function () {
|
|
if (this.active) {
|
|
// remove self from vm's watcher list
|
|
// this is a somewhat expensive operation so we skip it
|
|
// if the vm is being destroyed or is performing a v-for
|
|
// re-render (the watcher list is then filtered by v-for).
|
|
if (!this.vm._isBeingDestroyed && !this.vm._vForRemoving) {
|
|
remove(this.vm._watchers, this);
|
|
}
|
|
var i = this.deps.length;
|
|
while (i--) {
|
|
this.deps[i].removeSub(this);
|
|
}
|
|
this.active = false;
|
|
this.vm = this.cb = this.value = null;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Recrusively traverse an object to evoke all converted
|
|
* getters, so that every nested property inside the object
|
|
* is collected as a "deep" dependency.
|
|
*
|
|
* @param {*} val
|
|
* @param {Set} seen
|
|
*/
|
|
|
|
var seenObjects = new Set$1();
|
|
function traverse(val, seen) {
|
|
var i = void 0,
|
|
keys = void 0,
|
|
isA = void 0,
|
|
isO = void 0;
|
|
if (!seen) {
|
|
seen = seenObjects;
|
|
seen.clear();
|
|
}
|
|
isA = isArray(val);
|
|
isO = isObject(val);
|
|
if (isA || isO) {
|
|
if (val.__ob__) {
|
|
var depId = val.__ob__.dep.id;
|
|
if (seen.has(depId)) {
|
|
return;
|
|
} else {
|
|
seen.add(depId);
|
|
}
|
|
}
|
|
if (isA) {
|
|
i = val.length;
|
|
while (i--) {
|
|
traverse(val[i], seen);
|
|
}
|
|
} else if (isO) {
|
|
keys = Object.keys(val);
|
|
i = keys.length;
|
|
while (i--) {
|
|
traverse(val[keys[i]], seen);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var arrayProto = Array.prototype;
|
|
var arrayMethods = Object.create(arrayProto)
|
|
|
|
/**
|
|
* Intercept mutating methods and emit events
|
|
*/
|
|
|
|
;['push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse'].forEach(function (method) {
|
|
// cache original method
|
|
var original = arrayProto[method];
|
|
def(arrayMethods, method, function mutator() {
|
|
// avoid leaking arguments:
|
|
// http://jsperf.com/closure-with-arguments
|
|
var i = arguments.length;
|
|
var args = new Array(i);
|
|
while (i--) {
|
|
args[i] = arguments[i];
|
|
}
|
|
var result = original.apply(this, args);
|
|
var ob = this.__ob__;
|
|
var inserted;
|
|
switch (method) {
|
|
case 'push':
|
|
inserted = args;
|
|
break;
|
|
case 'unshift':
|
|
inserted = args;
|
|
break;
|
|
case 'splice':
|
|
inserted = args.slice(2);
|
|
break;
|
|
}
|
|
if (inserted) ob.observeArray(inserted);
|
|
// notify change
|
|
ob.dep.notify();
|
|
return result;
|
|
});
|
|
});
|
|
|
|
var arrayKeys = Object.getOwnPropertyNames(arrayMethods);
|
|
|
|
/**
|
|
* By default, when a reactive property is set, the new value is
|
|
* also converted to become reactive. However when passing down props,
|
|
* we don't want to force conversion because the value may be a nested value
|
|
* under a frozen data structure. Converting it would defeat the optimization.
|
|
*/
|
|
|
|
var observerState = {
|
|
shouldConvert: true
|
|
};
|
|
|
|
/**
|
|
* Observer class that are attached to each observed
|
|
* object. Once attached, the observer converts target
|
|
* object's property keys into getter/setters that
|
|
* collect dependencies and dispatches updates.
|
|
*
|
|
* @param {Array|Object} value
|
|
* @constructor
|
|
*/
|
|
|
|
function Observer(value) {
|
|
this.value = value;
|
|
this.dep = new Dep();
|
|
def(value, '__ob__', this);
|
|
if (isArray(value)) {
|
|
var augment = hasProto ? protoAugment : copyAugment;
|
|
augment(value, arrayMethods, arrayKeys);
|
|
this.observeArray(value);
|
|
} else {
|
|
this.walk(value);
|
|
}
|
|
}
|
|
|
|
// Instance methods
|
|
|
|
/**
|
|
* Walk through each property and convert them into
|
|
* getter/setters. This method should only be called when
|
|
* value type is Object.
|
|
*
|
|
* @param {Object} obj
|
|
*/
|
|
|
|
Observer.prototype.walk = function (obj) {
|
|
for (var key in obj) {
|
|
this.convert(key, obj[key]);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Observe a list of Array items.
|
|
*
|
|
* @param {Array} items
|
|
*/
|
|
|
|
Observer.prototype.observeArray = function (items) {
|
|
for (var i = 0, l = items.length; i < l; i++) {
|
|
observe(items[i]);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Convert a property into getter/setter so we can emit
|
|
* the events when the property is accessed/changed.
|
|
*
|
|
* @param {String} key
|
|
* @param {*} val
|
|
*/
|
|
|
|
Observer.prototype.convert = function (key, val) {
|
|
defineReactive(this.value, key, val);
|
|
};
|
|
|
|
/**
|
|
* Add an owner vm, so that when $set/$delete mutations
|
|
* happen we can notify owner vms to proxy the keys and
|
|
* digest the watchers. This is only called when the object
|
|
* is observed as an instance's root $data.
|
|
*
|
|
* @param {Vue} vm
|
|
*/
|
|
|
|
Observer.prototype.addVm = function (vm) {
|
|
(this.vms || (this.vms = [])).push(vm);
|
|
};
|
|
|
|
/**
|
|
* Remove an owner vm. This is called when the object is
|
|
* swapped out as an instance's $data object.
|
|
*
|
|
* @param {Vue} vm
|
|
*/
|
|
|
|
Observer.prototype.removeVm = function (vm) {
|
|
remove(this.vms, vm);
|
|
};
|
|
|
|
// helpers
|
|
|
|
/**
|
|
* Augment an target Object or Array by intercepting
|
|
* the prototype chain using __proto__
|
|
*
|
|
* @param {Object|Array} target
|
|
* @param {Object} src
|
|
*/
|
|
|
|
function protoAugment(target, src) {
|
|
/* eslint-disable no-proto */
|
|
target.__proto__ = src;
|
|
/* eslint-enable no-proto */
|
|
}
|
|
|
|
/**
|
|
* Augment an target Object or Array by defining
|
|
* hidden properties.
|
|
*
|
|
* @param {Object|Array} target
|
|
* @param {Object} proto
|
|
*/
|
|
|
|
function copyAugment(target, src, keys) {
|
|
for (var i = 0, l = keys.length; i < l; i++) {
|
|
var key = keys[i];
|
|
def(target, key, src[key]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Attempt to create an observer instance for a value,
|
|
* returns the new observer if successfully observed,
|
|
* or the existing observer if the value already has one.
|
|
*
|
|
* @param {*} value
|
|
* @param {Vue} [vm]
|
|
* @return {Observer|undefined}
|
|
* @static
|
|
*/
|
|
|
|
function observe(value, vm) {
|
|
if (!isObject(value)) {
|
|
return;
|
|
}
|
|
var ob;
|
|
if (hasOwn(value, '__ob__') && value.__ob__ instanceof Observer) {
|
|
ob = value.__ob__;
|
|
} else if (observerState.shouldConvert && (isArray(value) || isPlainObject(value)) && Object.isExtensible(value) && !value._isVue) {
|
|
ob = new Observer(value);
|
|
}
|
|
if (ob && vm) {
|
|
ob.addVm(vm);
|
|
}
|
|
return ob;
|
|
}
|
|
|
|
/**
|
|
* Define a reactive property on an Object.
|
|
*
|
|
* @param {Object} obj
|
|
* @param {String} key
|
|
* @param {*} val
|
|
*/
|
|
|
|
function defineReactive(obj, key, val) {
|
|
var dep = new Dep();
|
|
|
|
var property = Object.getOwnPropertyDescriptor(obj, key);
|
|
if (property && property.configurable === false) {
|
|
return;
|
|
}
|
|
|
|
// cater for pre-defined getter/setters
|
|
var getter = property && property.get;
|
|
var setter = property && property.set;
|
|
|
|
var childOb = observe(val);
|
|
Object.defineProperty(obj, key, {
|
|
enumerable: true,
|
|
configurable: true,
|
|
get: function reactiveGetter() {
|
|
var value = getter ? getter.call(obj) : val;
|
|
if (Dep.target) {
|
|
dep.depend();
|
|
if (childOb) {
|
|
childOb.dep.depend();
|
|
}
|
|
if (isArray(value)) {
|
|
for (var e, i = 0, l = value.length; i < l; i++) {
|
|
e = value[i];
|
|
e && e.__ob__ && e.__ob__.dep.depend();
|
|
}
|
|
}
|
|
}
|
|
return value;
|
|
},
|
|
set: function reactiveSetter(newVal) {
|
|
var value = getter ? getter.call(obj) : val;
|
|
if (newVal === value) {
|
|
return;
|
|
}
|
|
if (setter) {
|
|
setter.call(obj, newVal);
|
|
} else {
|
|
val = newVal;
|
|
}
|
|
childOb = observe(newVal);
|
|
dep.notify();
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Set a property on an object. Adds the new property and
|
|
* triggers change notification if the property doesn't
|
|
* already exist.
|
|
*
|
|
* @param {Object} obj
|
|
* @param {String} key
|
|
* @param {*} val
|
|
* @public
|
|
*/
|
|
|
|
function set(obj, key, val) {
|
|
if (isArray(obj)) {
|
|
return obj.splice(key, 1, val);
|
|
}
|
|
if (hasOwn(obj, key)) {
|
|
obj[key] = val;
|
|
return;
|
|
}
|
|
if (obj._isVue) {
|
|
set(obj._data, key, val);
|
|
return;
|
|
}
|
|
var ob = obj.__ob__;
|
|
if (!ob) {
|
|
obj[key] = val;
|
|
return;
|
|
}
|
|
ob.convert(key, val);
|
|
ob.dep.notify();
|
|
if (ob.vms) {
|
|
var i = ob.vms.length;
|
|
while (i--) {
|
|
var vm = ob.vms[i];
|
|
proxy(vm, key);
|
|
vm.$forceUpdate();
|
|
}
|
|
}
|
|
return val;
|
|
}
|
|
|
|
function proxy(vm, key) {
|
|
if (!isReserved(key)) {
|
|
Object.defineProperty(vm, key, {
|
|
configurable: true,
|
|
enumerable: true,
|
|
get: function proxyGetter() {
|
|
return vm._data[key];
|
|
},
|
|
set: function proxySetter(val) {
|
|
vm._data[key] = val;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function unproxy(vm, key) {
|
|
if (!isReserved(key)) {
|
|
delete vm[key];
|
|
}
|
|
}
|
|
|
|
function initState(vm) {
|
|
vm._watchers = [];
|
|
initProps(vm);
|
|
initData(vm);
|
|
initComputed(vm);
|
|
initMethods(vm);
|
|
initWatch(vm);
|
|
}
|
|
|
|
function initProps(vm) {
|
|
var props = vm.$options.props;
|
|
var propsData = vm.$options.propsData;
|
|
if (props) {
|
|
var keys = vm.$options.propKeys = Object.keys(props);
|
|
var isRoot = !vm.$parent;
|
|
// root instance props should be converted
|
|
observerState.shouldConvert = isRoot;
|
|
for (var i = 0; i < keys.length; i++) {
|
|
var key = keys[i];
|
|
defineReactive(vm, key, validateProp(vm, key, propsData));
|
|
}
|
|
observerState.shouldConvert = true;
|
|
}
|
|
}
|
|
|
|
function initData(vm) {
|
|
var data = vm.$options.data;
|
|
data = vm._data = typeof data === 'function' ? data() : data || {};
|
|
if (!isPlainObject(data)) {
|
|
data = {};
|
|
process.env.NODE_ENV !== 'production' && warn$1('data functions should return an object.', vm);
|
|
}
|
|
// proxy data on instance
|
|
var keys = Object.keys(data);
|
|
var i = keys.length;
|
|
while (i--) {
|
|
proxy(vm, keys[i]);
|
|
}
|
|
// observe data
|
|
observe(data, vm);
|
|
}
|
|
|
|
function noop() {}
|
|
|
|
function initComputed(vm) {
|
|
var computed = vm.$options.computed;
|
|
if (computed) {
|
|
for (var key in computed) {
|
|
var userDef = computed[key];
|
|
var def = {
|
|
enumerable: true,
|
|
configurable: true
|
|
};
|
|
if (typeof userDef === 'function') {
|
|
def.get = makeComputedGetter(userDef, vm);
|
|
def.set = noop;
|
|
} else {
|
|
def.get = userDef.get ? userDef.cache !== false ? makeComputedGetter(userDef.get, vm) : bind(userDef.get, vm) : noop;
|
|
def.set = userDef.set ? bind(userDef.set, vm) : noop;
|
|
}
|
|
Object.defineProperty(vm, key, def);
|
|
}
|
|
}
|
|
}
|
|
|
|
function makeComputedGetter(getter, owner) {
|
|
var watcher = new Watcher(owner, getter, null, {
|
|
lazy: true
|
|
});
|
|
return function computedGetter() {
|
|
if (watcher.dirty) {
|
|
watcher.evaluate();
|
|
}
|
|
if (Dep.target) {
|
|
watcher.depend();
|
|
}
|
|
return watcher.value;
|
|
};
|
|
}
|
|
|
|
function initMethods(vm) {
|
|
var methods = vm.$options.methods;
|
|
if (methods) {
|
|
for (var key in methods) {
|
|
vm[key] = bind(methods[key], vm);
|
|
}
|
|
}
|
|
}
|
|
|
|
function initWatch(vm) {
|
|
var watch = vm.$options.watch;
|
|
if (watch) {
|
|
for (var key in watch) {
|
|
var handler = watch[key];
|
|
if (isArray(handler)) {
|
|
for (var i = 0; i < handler.length; i++) {
|
|
createWatcher(vm, key, handler[i]);
|
|
}
|
|
} else {
|
|
createWatcher(vm, key, handler);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function createWatcher(vm, key, handler) {
|
|
var options = void 0;
|
|
if (isPlainObject(handler)) {
|
|
options = handler;
|
|
handler = handler.handler;
|
|
}
|
|
if (typeof handler === 'string') {
|
|
handler = vm[handler];
|
|
}
|
|
vm.$watch(key, handler, options);
|
|
}
|
|
|
|
function stateMixin(Vue) {
|
|
Object.defineProperty(Vue.prototype, '$data', {
|
|
get: function get() {
|
|
return this._data;
|
|
},
|
|
set: function set(newData) {
|
|
if (newData !== this._data) {
|
|
setData(this, newData);
|
|
}
|
|
}
|
|
});
|
|
|
|
Vue.prototype.$watch = function (fn, cb, options) {
|
|
options = options || {};
|
|
options.user = true;
|
|
var watcher = new Watcher(this, fn, cb, options) |