@rustle/grass
Version:
A simple mvvm library
1,784 lines (1,718 loc) • 101 kB
JavaScript
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var defineProperty = function (obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
};
var slicedToArray = function () {
function sliceIterator(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"]) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
return function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else if (Symbol.iterator in Object(arr)) {
return sliceIterator(arr, i);
} else {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
};
}();
var toConsumableArray = function (arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
} else {
return Array.from(arr);
}
};
function typeOf(val) {
return Object.prototype.toString.call(val);
}
function isObject(obj) {
return obj !== null && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object';
}
function isPlainObject(obj) {
return typeOf(obj) === '[object Object]';
}
function isNumber(num) {
return typeOf(num) === '[object Number]' && !isNaN(num);
}
function isPrimitive(value) {
return typeof value === 'string' || typeof value === 'number' || (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'symbol' || typeof value === 'boolean';
}
function isGeneratorFunction(fun) {
var constructor = fun.constructor;
if (!constructor) return false;
if (constructor.name === 'GeneratorFunction' || constructor.displayName === 'GeneratorFunction') {
return true;
}
var prototype = constructor.prototype;
return typeof prototype.next === 'function' && typeof prototype.throw === 'function';
}
function each(arr, cb) {
if (arr.length) {
for (var i = 0, len = arr.length; i < len; i++) {
if (cb(arr[i], i, i) === false) return;
}
return;
}
if (isPlainObject(arr)) {
var keys = Object.keys(arr);
for (var _i = 0, _len = keys.length; _i < _len; _i++) {
if (cb(arr[keys[_i]], keys[_i], _i) === false) return;
}
}
}
var hasOwnProperty = Object.prototype.hasOwnProperty;
function hasOwn(obj, key) {
return hasOwnProperty.call(obj, key);
}
function remove(arr, item) {
if (arr.length) {
var index = arr.indexOf(item);
if (index > -1) {
return arr.splice(index, 1);
}
}
}
function toString(val) {
return val == null ? '' : (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' ? JSON.stringify(val, null, 2) : String(val);
}
function extend(to, _from) {
for (var key in _from) {
to[key] = _from[key];
}
return to;
}
function isEmptyObj(obj) {
for (var val in obj) {
return false;
}
return true;
}
function setOnlyReadAttr(obj, key, val) {
Object.defineProperty(obj, key, {
get: function get$$1() {
return val;
}
});
}
function isUndef(val) {
return val === undefined || val === null;
}
function isDef(val) {
return val !== undefined && val !== null;
}
function isNative(Ctor) {
return typeof Ctor === 'function' && /native code/.test(Ctor.toString());
}
var hasSymbol = typeof Symbol !== 'undefined' && isNative(Symbol) && typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys);
function once(fun) {
var called = false;
return function () {
if (!called) {
called = true;
return fun.apply(this, arguments);
}
};
}
function warn(msg, noError) {
var errorInfor = '[Grass tip]: ' + msg;
if (noError) {
console.warn(errorInfor);
return;
}
throw Error(errorInfor);
}
function grassWarn(msg, compName, noError) {
var errorInfor = msg + ' \n\n ---> ' + (compName || 'unknow') + '\n';
warn(errorInfor, noError);
}
function cached(fn) {
var cache = Object.create(null);
return function cachedFn(str) {
var hit = cache[str];
return hit || (cache[str] = fn(str));
};
}
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];
};
}
function sendDirectWarn(direct, compName) {
warn('Cannot make "' + direct + '" directives on the root node of a component\uFF0C\n Maybe you can specify the "' + direct + '" command on "<' + compName + ' ' + direct + '="xxx" />"\n \n\n ---> ' + compName + '\n');
}
function deepClone(obj, similarArr) {
var res = void 0;
if (isPlainObject(obj)) {
res = new obj.constructor();
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
var val = obj[keys[i]];
if (val === obj) continue;
res[keys[i]] = canUse(val) ? val : deepClone(val, similarArr);
}
return res;
}
if (Array.isArray(obj) || similarArr) {
res = new obj.constructor();
for (var _i = 0; _i < obj.length; _i++) {
var _val = obj[_i];
if (_val === obj) continue;
res[_i] = canUse(_val) ? _val : deepClone(_val, similarArr);
}
return res;
}
function canUse(val) {
return isPrimitive(val) || val == null || typeof val === 'function';
}
return obj;
}
function isClass(fun) {
var proto = fun.prototype;
if (!proto || isGeneratorFunction(fun)) {
return false;
}
if (isEmptyObj(proto)) {
var _constructor = proto.constructor;
if (_constructor && _constructor === fun) {
var descriptors = Object.getOwnPropertyDescriptors(proto);
return Object.keys(descriptors).length > 1 ? true : false;
}
return true;
}
return true;
}
var version = '2';
function isVNode(x) {
return x && x.type === 'VirtualNode' && x.version === version;
}
function isVText(x) {
return x && x.type === 'VirtualText' && x.version === version;
}
function isWidget(w) {
return w && w.type === 'Widget';
}
var inBrowser = typeof window !== 'undefined';
var UA = inBrowser && window.navigator.userAgent.toLowerCase();
var isIE = UA && /msie|trident/.test(UA);
var isIE9 = UA && UA.indexOf('msie 9.0') > 0;
var isEdge = UA && UA.indexOf('edge/') > 0;
var isAndroid = UA && UA.indexOf('android') > 0;
var isIOS = UA && /iphone|ipad|ipod|ios/.test(UA);
var isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge;
var isHTMLTag = makeMap('html,body,base,head,link,meta,style,title,' + 'address,article,aside,footer,header,h1,h2,h3,h4,h5,h6,hgroup,nav,section,' + 'div,dd,dl,dt,figcaption,figure,picture,hr,img,li,main,ol,p,pre,ul,' + 'a,b,abbr,bdi,bdo,br,cite,code,data,dfn,em,i,kbd,mark,q,rp,rt,rtc,ruby,' + 's,samp,small,span,strong,sub,sup,time,u,var,wbr,area,audio,map,track,video,' + 'embed,object,param,source,canvas,script,noscript,del,ins,' + 'caption,col,colgroup,table,thead,tbody,td,th,tr,' + 'button,datalist,fieldset,form,input,label,legend,meter,optgroup,option,' + 'output,progress,select,textarea,' + 'details,dialog,menu,menuitem,summary,' + 'content,element,shadow,template,blockquote,iframe,tfoot');
var isSVG = makeMap('svg,animate,circle,clippath,cursor,defs,desc,ellipse,filter,font-face,' + 'foreignObject,g,glyph,image,line,marker,mask,missing-glyph,path,pattern,' + 'polygon,polyline,rect,switch,symbol,text,textpath,tspan,use,view', true);
var isInternelTag = makeMap('slot');
function isReservedTag(tag) {
return isHTMLTag(tag) || isSVG(tag);
}
function noop() {}
var isWidget$1 = isWidget;
var isOriginVNode = isVNode;
var isVNode$1 = function isVNode$$1(v) {
return isVNode(v) || isVText(v) || isWidget(v);
};
var noProperties = {};
var noChildren = [];
function VirtualNode(tagName, properties, children, key, namespace) {
this.tagName = tagName;
this.properties = properties || noProperties;
this.children = children || noChildren;
this.key = !isUndef$1(key) ? String(key) : undefined;
this.namespace = typeof namespace === 'string' ? namespace : null;
var count = children && children.length || 0;
var descendants = 0;
var hasWidgets = false;
for (var i = 0; i < count; i++) {
var child = children[i];
if (isVNode(child)) {
descendants += child.count || 0;
if (!hasWidgets && child.hasWidgets) {
hasWidgets = true;
}
} else if (!hasWidgets && isWidget(child)) {
if (typeof child.destroy === 'function') {
hasWidgets = true;
}
}
}
this.count = count + descendants;
this.hasWidgets = hasWidgets;
}
function isUndef$1(v) {
return v === undefined || v === null;
}
VirtualNode.prototype.version = version;
VirtualNode.prototype.type = 'VirtualNode';
function VirtualText(text) {
this.text = String(text);
}
VirtualText.prototype.version = version;
VirtualText.prototype.type = 'VirtualText';
function h(tagName, properties, children, elementCreated) {
var childNodes = [];
var tag = void 0,
props = void 0,
key = void 0,
namespace = void 0;
if (!children && isChildren(properties)) {
children = properties;
props = {};
}
props = props || properties || {};
tag = parseTag(tagName, props);
if (props.hasOwnProperty('key')) {
key = props.key;
props.key = undefined;
}
if (props.hasOwnProperty('namespace')) {
namespace = props.namespace;
props.namespace = undefined;
}
if (tag === 'INPUT' && !namespace && props.hasOwnProperty('value') && props.value !== undefined) {
if (props.value !== null && typeof props.value !== 'string') {
throw new Error('virtual-dom: "INPUT" value must be a "string" or "null"');
}
}
if (!isUndef$2(children)) {
addChild(children, childNodes);
}
var vNode = new VirtualNode(tag, props, childNodes, key, namespace);
if (typeof elementCreated === 'function') {
vNode.elementCreated = elementCreated;
}
return vNode;
}
function addChild(c, childNodes) {
if (typeof c === 'string') {
childNodes.push(new VirtualText(c));
} else if (typeof c === 'number') {
childNodes.push(new VirtualText(String(c)));
} else if (isChild(c)) {
childNodes.push(c);
} else if (Array.isArray(c)) {
for (var i = 0, len = c.length; i < len; i++) {
addChild(c[i], childNodes);
}
} else if (isUndef$2(c)) {
return;
} else {
throw new Error('Unexpected value type for input passed to h()');
}
}
function parseTag(tagName, props) {
if (!tagName) {
return 'DIV';
}
return props.namespace ? tagName : tagName.toUpperCase();
}
function isChild(x) {
return isVNode(x) || isVText(x) || isWidget(x);
}
function isChildren(x) {
return typeof x === 'string' || Array.isArray(x) || isChild(x);
}
function isUndef$2(v) {
return v === undefined || v === null;
}
VirtualPatch.NONE = 0;
VirtualPatch.VTEXT = 1;
VirtualPatch.VNODE = 2;
VirtualPatch.WIDGET = 3;
VirtualPatch.PROPS = 4;
VirtualPatch.ORDER = 5;
VirtualPatch.INSERT = 6;
VirtualPatch.REMOVE = 7;
VirtualPatch.THUNK = 8;
function VirtualPatch(type, vNode, patch) {
this.type = Number(type);
this.vNode = vNode;
this.patch = patch;
}
VirtualPatch.prototype.version = version;
VirtualPatch.prototype.type = 'VirtualPatch';
function diffProps(a, b) {
var diff = void 0;
for (var aKey in a) {
if (!(aKey in b)) {
diff = diff || {};
diff[aKey] = undefined;
continue;
}
var aValue = a[aKey];
var bValue = b[aKey];
if (aValue === bValue) {
if (aKey === 'className' || aKey === 'styleName') {
diff = diff || {};
diff[aKey] = bValue;
}
continue;
} else if (isObject$1(aValue) && isObject$1(bValue)) {
if (Object.getPrototypeOf(aValue) === Object.getPrototypeOf(bValue)) {
var objectDiff = diffProps(aValue, bValue);
if (objectDiff) {
diff = diff || {};
diff[aKey] = objectDiff;
}
} else {
diff = diff || {};
diff[aKey] = bValue;
}
} else {
diff = diff || {};
diff[aKey] = bValue;
}
}
for (var bKey in b) {
if (!(bKey in a)) {
diff = diff || {};
diff[bKey] = b[bKey];
}
}
return diff;
}
function isObject$1(x) {
return (typeof x === 'undefined' ? 'undefined' : _typeof(x)) === 'object' && x !== null;
}
function reorder(aChildren, bChildren) {
var bChildIndex = keyIndex(bChildren);
var bKeys = bChildIndex.keys;
var bFree = bChildIndex.free;
if (bFree.length === bChildren.length) {
return {
children: bChildren,
moves: null
};
}
var aChildIndex = keyIndex(aChildren);
var aKeys = aChildIndex.keys;
var aFree = aChildIndex.free;
if (aFree.length === aChildren.length) {
return {
children: bChildren,
moves: null
};
}
var newChildren = [];
var freeCount = bFree.length;
var freeIndex = 0;
var deletedItems = 0;
for (var i = 0, len = aChildren.length; i < len; i++) {
var aItem = aChildren[i];
var itemIndex = void 0;
if (aItem.key) {
if (bKeys.hasOwnProperty(aItem.key)) {
itemIndex = bKeys[aItem.key];
newChildren.push(bChildren[itemIndex]);
} else {
deletedItems++;
newChildren.push(null);
}
} else {
if (freeIndex < freeCount) {
itemIndex = bFree[freeIndex++];
newChildren.push(bChildren[itemIndex]);
} else {
deletedItems++;
newChildren.push(null);
}
}
}
var lastFreeIndex = freeIndex >= bFree.length ? bChildren.length : bFree[freeIndex];
for (var j = 0, _len = bChildren.length; j < _len; j++) {
var newItem = bChildren[j];
if (newItem.key) {
if (!aKeys.hasOwnProperty(newItem.key)) {
newChildren.push(newItem);
}
} else {
if (j >= lastFreeIndex) {
newChildren.push(newItem);
}
}
}
var simulate = newChildren.slice();
var removes = [];
var inserts = [];
var simulateIndex = 0;
var simulateItem = void 0;
for (var k = 0, _len2 = bChildren.length; k < _len2;) {
var wantedItem = bChildren[k];
simulateItem = simulate[simulateIndex];
while (simulateItem === null && simulate.length) {
removes.push(remove$1(simulate, simulateIndex, null));
simulateItem = simulate[simulateIndex];
}
if (simulateItem && simulateItem.key === wantedItem.key) {
k++;
simulateIndex++;
} else {
if (wantedItem.key) {
if (simulateItem && simulateItem.key) {
var positionInBkeys = bKeys[simulateItem.key];
if (positionInBkeys === k + 1) {
inserts.push({ key: wantedItem.key, to: k });
} else {
removes.push(remove$1(simulate, simulateIndex, simulateItem.key));
simulateItem = simulate[simulateIndex];
if (simulateItem && simulateItem.key === wantedItem.key) {
simulateIndex++;
} else {
inserts.push({ key: wantedItem.key, to: k });
}
}
} else {
inserts.push({ key: wantedItem.key, to: k });
}
k++;
} else if (simulateItem && simulateItem.key) {
removes.push(remove$1(simulate, simulateIndex, simulateItem.key));
}
}
}
while (simulateIndex < simulate.length) {
simulateItem = simulate[simulateIndex];
removes.push(remove$1(simulate, simulateIndex, simulateItem && simulateItem.key));
}
if (removes.length === deletedItems && !inserts.length) {
return {
children: newChildren,
moves: null
};
}
return {
children: newChildren,
moves: {
removes: removes,
inserts: inserts
}
};
}
function remove$1(arr, index, key) {
arr.splice(index, 1);
return {
key: key,
from: index
};
}
function keyIndex(children) {
var keys = {};
var free = [];
var length = children.length;
for (var i = 0; i < length; i++) {
var child = children[i];
if (child.key) {
keys[child.key] = i;
} else {
free.push(i);
}
}
return { keys: keys, free: free };
}
function diff(a, b) {
var patch = { a: a };
var index = 0;
walk(a, b, patch, index);
return patch;
}
function walk(a, b, patch, index) {
if (a === b) {
return;
}
var apply = patch[index];
var applyClear = false;
if (isUndef$3(b)) {
if (!isWidget(a)) {
destroyWidgets(a, patch, index);
apply = patch[index];
}
apply = appendPatch(apply, new VirtualPatch(VirtualPatch.REMOVE, a, b));
} else if (isVNode(b)) {
if (isVNode(a) && isSameVnode(a, b)) {
var propsPatch = diffProps(a.properties, b.properties);
if (propsPatch) {
apply = appendPatch(apply, new VirtualPatch(VirtualPatch.PROPS, a, propsPatch));
}
apply = diffChildren(a, b, patch, apply, index);
} else {
applyClear = true;
apply = appendPatch(apply, new VirtualPatch(VirtualPatch.VNODE, a, b));
}
} else if (isVText(b)) {
if (!isVText(a)) {
apply = appendPatch(apply, new VirtualPatch(VirtualPatch.VTEXT, a, b));
applyClear = true;
} else if (a.text !== b.text) {
apply = appendPatch(apply, new VirtualPatch(VirtualPatch.VTEXT, a, b));
}
} else if (isWidget(b)) {
if (!isWidget(a)) {
applyClear = true;
}
apply = appendPatch(apply, new VirtualPatch(VirtualPatch.WIDGET, a, b));
}
if (apply) {
patch[index] = apply;
}
if (applyClear) {
destroyWidgets(a, patch, index);
}
}
function destroyWidgets(vNode, patch, index) {
if (isWidget(vNode)) {
if (typeof vNode.destroy === 'function') {
patch[index] = appendPatch(patch[index], new VirtualPatch(VirtualPatch.REMOVE, vNode, null));
}
} else if (isVNode(vNode) && vNode.hasWidgets) {
var children = vNode.children;
for (var i = 0, len = children.length; i < len; i++) {
var child = children[i];
index++;
destroyWidgets(child, patch, index);
if (isVNode(child) && child.count) {
index += child.count;
}
}
}
}
function diffChildren(a, b, patch, apply, index) {
var aChildren = a.children;
var orderedSet = reorder(aChildren, b.children);
var bChildren = orderedSet.children;
var aLen = aChildren.length;
var bLen = bChildren.length;
var len = aLen > bLen ? aLen : bLen;
for (var i = 0; i < len; i++) {
var leftNode = aChildren[i];
var rightNode = bChildren[i];
index++;
if (leftNode) {
walk(leftNode, rightNode, patch, index);
} else {
if (rightNode) {
apply = appendPatch(apply, new VirtualPatch(VirtualPatch.INSERT, null, rightNode));
}
}
if (isVNode(leftNode) && leftNode.count) {
index += leftNode.count;
}
}
if (orderedSet.moves) {
apply = appendPatch(apply, new VirtualPatch(VirtualPatch.ORDER, a, orderedSet.moves));
}
return apply;
}
function appendPatch(apply, patch) {
if (apply) {
Array.isArray(apply) ? apply.push(patch) : apply = [apply, patch];
return apply;
}
return patch;
}
function isSameVnode(a, b) {
return a.tagName === b.tagName && a.namespace === b.namespace && a.key === b.key;
}
function isUndef$3(v) {
return v === undefined || v === null;
}
var raf = window.requestAnimationFrame ? window.requestAnimationFrame.bind(window) : setTimeout;
function nextFrame(fn) {
raf(function () {
raf(fn);
});
}
var autoCssTransition = cached(function (name) {
return {
enterClass: name + '-enter',
enterToClass: name + '-enter-to',
enterActiveClass: name + '-enter-active',
leaveClass: name + '-leave',
leaveToClass: name + '-leave-to',
leaveActiveClass: name + '-leave-active'
};
});
var TRANSITION = 'transition';
var ANIMATION = 'animation';
var hasTransition = inBrowser && !isIE9;
var transitionProp = 'transition';
var transitionEndEvent = 'transitionend';
var animationEndEvent = 'animationend';
if (hasTransition) {
if (window.ontransitionend === undefined && window.onwebkittransitionend !== undefined) {
transitionProp = 'WebkitTransition';
transitionEndEvent = 'webkitTransitionEnd';
}
if (window.onanimationend === undefined && window.onwebkitanimationend !== undefined) {
animationEndEvent = 'webkitAnimationEnd';
}
}
function enter(node, vnode, rm) {
var _ref = vnode.data || {},
vTransitionType = _ref.vTransitionType,
vTransitionData = _ref.vTransitionData;
if (!vTransitionType) {
rm();
return;
}
if (isDef(node._leaveCb)) {
node._leaveCb();
}
if (node._enterCb) {
rm();
return;
}
var name = vTransitionData.name,
hookFuns = vTransitionData.hookFuns;
var type = vTransitionType === 'transition' ? TRANSITION : ANIMATION;
if (callHook(hookFuns, node, 'beforeEnter') === false) {
rm();
return;
}
var _autoCssTransition = autoCssTransition(name),
enterClass = _autoCssTransition.enterClass,
enterActiveClass = _autoCssTransition.enterActiveClass,
enterToClass = _autoCssTransition.enterToClass;
var cb = node._enterCb = once(function () {
removeTransitionClass(node, enterToClass);
removeTransitionClass(node, enterActiveClass);
callHook(hookFuns, node, 'afterEnter');
node._enterCb = null;
rm();
});
addTransitionClass(node, enterClass);
addTransitionClass(node, enterActiveClass);
nextFrame(function () {
addTransitionClass(node, enterToClass);
removeTransitionClass(node, enterClass);
whenTransitionEnds(node, type, cb);
});
}
function leave(node, vnode, rm) {
var _ref2 = vnode.data || {},
vTransitionType = _ref2.vTransitionType,
vTransitionData = _ref2.vTransitionData;
if (!vTransitionType) {
rm();
return;
}
if (isDef(node._enterCb)) {
node._enterCb();
}
if (node._leaveCb) {
rm();
return;
}
var name = vTransitionData.name,
hookFuns = vTransitionData.hookFuns;
var type = vTransitionType === 'transtion' ? TRANSITION : ANIMATION;
if (callHook(hookFuns, node, 'beforeLeave') === false) {
rm();
return;
}
var _autoCssTransition2 = autoCssTransition(name),
leaveClass = _autoCssTransition2.leaveClass,
leaveActiveClass = _autoCssTransition2.leaveActiveClass,
leaveToClass = _autoCssTransition2.leaveToClass;
if (node.parentNode) {
if (!node.parentNode._pending) {
node.parentNode._pending = [];
}
var index = node.parentNode._pending.length;
node._index = index;
node.parentNode._pending[index] = node;
}
var cb = node._leaveCb = once(function (noRemove) {
if (!noRemove && node.parentNode && node.parentNode._pending) {
node.parentNode._pending[node._index] = null;
}
removeTransitionClass(node, leaveToClass);
removeTransitionClass(node, leaveActiveClass);
callHook(hookFuns, node, 'afterLeave');
node._leaveCb = null;
rm();
});
addTransitionClass(node, leaveClass);
addTransitionClass(node, leaveActiveClass);
nextFrame(function () {
addTransitionClass(node, leaveToClass);
removeTransitionClass(node, leaveClass);
whenTransitionEnds(node, type, cb);
});
}
function addTransitionClass(node, cls) {
var transitionClasses = node._transitionClasses || (node._transitionClasses = []);
if (transitionClasses.indexOf(cls) < 0) {
transitionClasses.push(cls);
addClass(node, cls);
}
}
function removeTransitionClass(node, cls) {
if (node._transitionClasses) {
remove(node._transitionClasses, cls);
}
removeClass(node, cls);
}
function whenTransitionEnds(node, type, cb) {
var ended = 0;
var _getTransitionInfo = getTransitionInfo(node),
propCount = _getTransitionInfo.propCount,
timeout = _getTransitionInfo.timeout;
var event = type === TRANSITION ? transitionEndEvent : animationEndEvent;
var end = function end() {
node.removeEventListener(event, onEnd);
cb();
};
var onEnd = function onEnd(e) {
if (++ended >= propCount) {
end();
}
};
setTimeout(function () {
if (ended < propCount) {
end();
}
}, timeout + 1);
node.addEventListener(event, onEnd);
}
function getTransitionInfo(node) {
var styles = window.getComputedStyle(node);
var transitionDelays = styles[transitionProp + 'Delay'].split(', ');
var transitionDurations = styles[transitionProp + 'Duration'].split(', ');
var transitionTimeout = getTimeout(transitionDelays, transitionDurations);
var propCount = transitionDurations.length;
var timeout = transitionTimeout;
return { propCount: propCount, timeout: timeout };
}
function getTimeout(delays, durations) {
while (delays.length < durations.length) {
delays = delays.concat(delays);
}
return Math.max.apply(null, durations.map(function (d, i) {
return toMs(d) + toMs(delays[i]);
}));
}
function toMs(s) {
return Number(s.slice(0, -1)) * 1000;
}
function applyPendingNode(parentNode) {
var pendingNode = parentNode && parentNode._pending;
if (pendingNode && pendingNode.length) {
for (var i = 0, len = pendingNode.length; i < len; i++) {
var node = pendingNode[i];
node && node._leaveCb && node._leaveCb(true);
}
parentNode._pending = [];
}
}
function addClass(node, cls) {
if (!cls || !(cls = cls.trim())) {
return;
}
if (node.classList) {
if (cls.indexOf(' ') > -1) {
cls.split(/\s+/).forEach(function (c) {
return node.classList.add(c);
});
} else {
node.classList.add(cls);
}
} else {
var cur = ' ' + (node.getAttribute('class') || '') + ' ';
if (cur.indexOf(' ' + cls + ' ') < 0) {
node.setAttribute('class', (cur + cls).trim());
}
}
}
function removeClass(node, cls) {
if (!cls || !(cls = cls.trim())) {
return;
}
if (node.classList) {
if (cls.indexOf(' ') > -1) {
cls.split(/\s+/).forEach(function (c) {
return node.classList.remove(c);
});
} else {
node.classList.remove(cls);
}
if (!node.classList.length) {
node.removeAttribute('class');
}
} else {
var cur = ' ' + (node.getAttribute('class') || '') + ' ';
var tar = ' ' + cls + ' ';
while (cur.indexOf(tar) >= 0) {
cur = cur.replace(tar, ' ');
}
cur = cur.trim();
if (cur) {
node.setAttribute('class', cur);
} else {
node.removeAttribute('class');
}
}
}
function callHook(funs, node, type) {
var fun = funs['v-' + type];
if (typeof fun === 'function') {
return fun(node);
}
}
function applyProperties(node, vnode, props, previous) {
if (hasOwn(props, 'className')) {
node.className = props.className;
}
var _loop = function _loop(propName) {
var propValue = props[propName];
if (propName === 'className') {
return 'continue';
}
if (propValue === undefined) {
removeProperty(node, propName, propValue, previous);
} else if (isObject$2(propValue)) {
patchObject(node, propName, propValue, previous);
} else {
if (propName === 'style' && vnode.data.haveShowTag) {
transition(node, vnode, propValue, function () {
node[propName] = propValue;
});
} else if (isAllow(propName)) {
node[propName] = propValue;
}
}
};
for (var propName in props) {
var _ret = _loop(propName);
if (_ret === 'continue') continue;
}
}
function removeProperty(node, propName, previous) {
if (!previous) {
return;
}
var previousValue = previous[propName];
if (propName === 'attributes') {
for (var attrName in previousValue) {
node.removeAttribute(attrName);
}
} else if (propName === 'style') {
for (var styleName in previousValue) {
node.style[styleName] = '';
}
} else if (typeof previousValue === 'string') {
node[propName] = '';
} else {
node[propName] = null;
}
}
function patchObject(node, propName, propValue, previous) {
var previousValue = previous ? previous[propName] : undefined;
if (propName === 'attributes') {
for (var attrName in propValue) {
var attrValue = propValue[attrName];
if (attrValue === undefined) {
node.removeAttribute(attrName);
} else {
attrName === 'value' && node.getAttribute('value') != null ? node.value = attrValue : node.setAttribute(attrName, attrValue);
}
}
return;
}
if (previousValue && isObject$2(previousValue)) {
if (Object.getPrototypeOf(previousValue) !== Object.getPrototypeOf(propValue)) {
node[propName] = propValue;
return;
}
}
if (!isObject$2(node[propName])) {
node[propName] = {};
}
var replacer = propName === 'style' ? '' : undefined;
for (var key in propValue) {
var value = propValue[key];
node[propName][key] = value === undefined ? replacer : value;
}
}
function transition(node, vnode, propValue, callback) {
var isShow = !propValue;
if (isShow) {
applyPendingNode(node.parentNode);
callback();
enter(node, vnode, noop);
} else {
leave(node, vnode, callback);
}
}
function isObject$2(x) {
return (typeof x === 'undefined' ? 'undefined' : _typeof(x)) === 'object' && x !== null;
}
function isAllow(x) {
return x !== 'slot';
}
function createElement(vnode) {
if (isWidget(vnode)) {
var _node = vnode.init();
if (typeof vnode.elementCreated === 'function') {
vnode.elementCreated(_node, vnode);
}
return _node;
} else if (isVText(vnode)) {
return document.createTextNode(vnode.text);
} else if (!isVNode(vnode)) {
console.error('virtual-dom: Item is not a valid virtual dom node');
return null;
}
var node = vnode.namespace === null ? document.createElement(vnode.tagName) : document.createElementNS(vnode.namespace, vnode.tagName);
var properties = vnode.properties,
children = vnode.children;
vnode.el = node;
applyProperties(node, vnode, properties);
for (var i = 0, len = children.length; i < len; i++) {
var childNode = createElement(children[i]);
if (childNode) {
node.appendChild(childNode);
}
}
if (typeof vnode.elementCreated === 'function') {
vnode.elementCreated(node, vnode);
}
if (!vnode.data.haveShowTag) {
enter(node, vnode, noop);
}
return node;
}
var noChild = {};
function domIndex(rootNode, tree, indices) {
if (!indices || !indices.length) {
return {};
}
indices.sort(function (a, b) {
return a > b ? 1 : -1;
});
return recurse(rootNode, tree, indices, null, 0);
}
function recurse(rootNode, tree, indices, nodes, rootIndex) {
nodes = nodes || {};
if (rootNode) {
if (indexInRange(indices, rootIndex, rootIndex)) {
nodes[rootIndex] = rootNode;
}
var vChildren = tree.children;
if (vChildren) {
var childNodes = rootNode.childNodes;
for (var i = 0, len = vChildren.length; i < len; i++) {
rootIndex++;
var vChild = vChildren[i] || noChild;
var nextIndex = rootIndex + (vChild.count || 0);
if (indexInRange(indices, rootIndex, nextIndex)) {
recurse(childNodes[i], vChild, indices, nodes, rootIndex);
}
rootIndex = nextIndex;
}
}
}
return nodes;
}
function indexInRange(indices, left, right) {
if (!indices.length) {
return false;
}
var minIndex = 0;
var maxIndex = indices.length - 1;
var currentIndex = void 0;
var currentItem = void 0;
while (minIndex <= maxIndex) {
currentIndex = (maxIndex + minIndex) / 2 >> 0;
currentItem = indices[currentIndex];
if (minIndex === maxIndex) {
return currentItem >= left && currentItem <= right;
} else if (currentItem < left) {
minIndex = currentIndex + 1;
} else if (currentItem > right) {
maxIndex = currentIndex - 1;
} else {
return true;
}
}
return false;
}
function applyPatch(vpatch, domNode, renderOptions) {
var type = vpatch.type,
vNode = vpatch.vNode,
patch = vpatch.patch;
switch (type) {
case VirtualPatch.REMOVE:
return removeNode(domNode, vNode);
case VirtualPatch.INSERT:
return insertNode(domNode, patch, renderOptions);
case VirtualPatch.VTEXT:
return stringPatch(domNode, patch, renderOptions);
case VirtualPatch.WIDGET:
return widgetPatch(domNode, vNode, patch, renderOptions);
case VirtualPatch.VNODE:
return vNodePatch(domNode, patch, renderOptions);
case VirtualPatch.ORDER:
reorderChildren(domNode, patch);
return domNode;
case VirtualPatch.PROPS:
applyProperties(domNode, vNode, patch, vNode.properties);
return domNode;
default:
return domNode;
}
}
function removeNode(domNode, vNode) {
var remove$$1 = once(function () {
var parentNode = domNode.parentNode;
if (parentNode) {
parentNode.removeChild(domNode);
}
destroyWidget(domNode, vNode);
});
leave(domNode, vNode, remove$$1);
return null;
}
function insertNode(parentNode, vNode, renderOptions) {
applyPendingNode(parentNode);
var newNode = renderOptions.render(vNode);
if (parentNode) {
parentNode.appendChild(newNode);
}
return parentNode;
}
function stringPatch(domNode, vText, renderOptions) {
if (domNode.nodeType === 3) {
domNode.replaceData(0, domNode.length, vText.text);
return domNode;
}
var parentNode = domNode.parentNode;
var newNode = renderOptions.render(vText);
if (parentNode && newNode !== domNode) {
parentNode.replaceChild(newNode, domNode);
}
return newNode;
}
function widgetPatch(domNode, leftVNode, widget, renderOptions) {
var updating = updateWidget(leftVNode, widget);
var newNode = updating ? widget.update(leftVNode, domNode) || domNode : renderOptions.render(widget);
var parentNode = domNode.parentNode;
if (parentNode && newNode !== domNode) {
parentNode.replaceChild(newNode, domNode);
}
if (!updating) {
destroyWidget(domNode, widget);
}
return newNode;
}
function vNodePatch(domNode, vNode, renderOptions) {
var parentNode = domNode.parentNode;
var newNode = renderOptions.render(vNode);
if (parentNode && newNode !== domNode) {
parentNode.replaceChild(newNode, domNode);
}
return newNode;
}
function reorderChildren(domNode, moves) {
var childNodes = domNode.childNodes;
var removes = moves.removes,
inserts = moves.inserts;
var keyMap = {};
for (var i = 0, len = removes.length; i < len; i++) {
var remove$$1 = removes[i];
var node = childNodes[remove$$1.from];
if (remove$$1.key) {
keyMap[remove$$1.key] = node;
}
domNode.removeChild(node);
}
var length = childNodes.length;
for (var j = 0, _len = inserts.length; j < _len; j++) {
var insert = inserts[j];
var _node = keyMap[insert.key];
domNode.insertBefore(_node, insert.to >= length++ ? null : childNodes[insert.to]);
}
}
function destroyWidget(domNode, w) {
if (typeof w.destroy === 'function' && isWidget(w)) {
w.destroy(domNode);
}
}
function updateWidget(a, b) {
if (isWidget(a) && isWidget(b)) {
return 'name' in a && 'name' in b ? a.id === b.id : a.init === b.init;
}
return false;
}
function patch(rootNode, patches) {
var renderOptions = {};
renderOptions.patch = patchRecursive;
renderOptions.render = createElement;
return renderOptions.patch(rootNode, patches, renderOptions);
}
function patchRecursive(rootNode, patches, renderOptions) {
var indices = patchIndices(patches);
if (!indices.length) {
return rootNode;
}
var index = domIndex(rootNode, patches.a, indices);
renderOptions.document = rootNode.ownerDocument;
for (var i = 0, len = indices.length; i < len; i++) {
var nodeIndex = indices[i];
rootNode = applyPatch$1(rootNode, index[nodeIndex], patches[nodeIndex], renderOptions);
}
return rootNode;
}
function applyPatch$1(rootNode, domNode, patchList, renderOptions) {
if (!domNode) {
return rootNode;
}
if (Array.isArray(patchList)) {
for (var i = 0, len = patchList.length; i < len; i++) {
applySinglePatch(patchList[i]);
}
} else {
applySinglePatch(patchList);
}
return rootNode;
function applySinglePatch(_patch) {
var newNode = applyPatch(_patch, domNode, renderOptions);
if (rootNode === domNode) {
rootNode = newNode;
}
}
}
function patchIndices(patches) {
var indices = [];
for (var key in patches) {
if (key !== 'a') {
indices.push(Number(key));
}
}
return indices;
}
var uid = 0;
var Dep = function () {
function Dep() {
classCallCheck(this, Dep);
this.id = uid++;
this.subs = [];
this.subsIds = new Set();
}
createClass(Dep, [{
key: "addSub",
value: function addSub(sub) {
var obj = isObject(sub);
if (obj && !this.subsIds.has(sub.id)) {
this.subsIds.add(sub.id);
this.subs.push(sub);
} else if (!obj) {
this.subs.push(sub);
}
}
}, {
key: "removeSub",
value: function removeSub(sub) {
if (isObject(sub)) {
this.subsIds.delete(sub.id);
}
remove(this.subs, sub);
}
}, {
key: "depend",
value: function depend() {
if (Dep.target) {
Dep.target.addDep(this);
}
}
}, {
key: "notify",
value: function notify(newValue, oldValue) {
var subs = this.subs.slice();
for (var i = 0, len = subs.length; i < len; i++) {
subs[i].update(newValue, oldValue);
}
}
}]);
return Dep;
}();
Dep.target = null;
var targetStack = [];
function pushTarget(_target) {
if (Dep.target) {
targetStack.push(_target);
}
Dep.target = _target;
}
function clearTarget() {
Dep.target = targetStack.pop();
}
var bailRE = /[^\w.$]/;
function parsePath(path) {
if (bailRE.test(path)) {
return;
}
var segments = path.split('.');
return function (obj) {
for (var i = 0; i < segments.length; i++) {
if (!obj) return;
obj = obj[segments[i]];
}
return obj;
};
}
function def(obj, key, val, enumerable) {
Object.defineProperty(obj, key, {
value: val,
enumerable: !!enumerable,
writable: true,
configurable: true
});
}
function protoAugment(target, src, keys) {
target.__proto__ = src;
}
function copyAugment(target, src, keys) {
for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i];
def(target, key, src[key]);
}
}
var arrayProto = Array.prototype;
var arrayMethods = Object.create(arrayProto);
var methodsToPatch = ['push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse'];
var _loop = function _loop(i, len) {
var method = methodsToPatch[i];
var original = arrayProto[method];
def(arrayMethods, method, function mutator() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var result = original.apply(this, args);
var ob = this.__ob__;
var inserted = void 0;
switch (method) {
case 'push':
case 'unshift':
inserted = args;
break;
case 'splice':
inserted = args.slice(2);
break;
}
if (inserted) {
ob.observeArray(inserted);
}
ob.dep.notify();
return result;
});
};
for (var i = 0, len = methodsToPatch.length; i < len; i++) {
_loop(i, len);
}
var hasProto = '__proto__' in {};
var arrayKeys = Object.getOwnPropertyNames(arrayMethods);
var Observer = function () {
function Observer(value) {
classCallCheck(this, Observer);
this.value = value;
this.dep = new Dep();
def(value, '__ob__', this);
if (Array.isArray(value)) {
var augment = hasProto ? protoAugment : copyAugment;
augment(value, arrayMethods, arrayKeys);
this.observeArray(value);
} else {
this.walk(value);
}
}
createClass(Observer, [{
key: 'walk',
value: function walk(obj) {
var keys = Object.keys(obj);
for (var i = 0, len = keys.length; i < len; i++) {
defineReactive(obj, keys[i], obj[keys[i]]);
}
}
}, {
key: 'observeArray',
value: function observeArray(items) {
for (var i = 0, len = items.length; i < len; i++) {
var item = items[i];
observe(item);
}
}
}]);
return Observer;
}();
function defineReactive(obj, key, val) {
var dep = new Dep();
var property = Object.getOwnPropertyDescriptor(obj, key);
if (property && property.configurable === false) {
return;
}
var getter = property && property.get;
var setter = property && property.set;
if ((!getter || setter) && arguments.length === 2) {
val = obj[key];
}
var childOb = observe(val);
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get: function get$$1() {
var value = getter ? getter.call(obj) : val;
if (Dep.target) {
dep.depend();
if (childOb) {
childOb.dep.depend();
if (Array.isArray(value)) {
dependArray(value);
}
}
}
return value;
},
set: function set$$1(newVal) {
var value = getter ? getter.call(obj) : val;
var oldValue = value;
if (newVal === value || newVal !== newVal && value !== value) {
return;
}
if (setter) {
setter.call(obj, newVal);
} else {
val = newVal;
}
childOb = observe(newVal);
dep.notify(newVal, oldValue);
}
});
}
function observe(value) {
if (!isObject(value) || isVNode$1(value)) {
return;
}
var ob = void 0;
if (hasOwn(value, '__ob__') && value.__ob__ instanceof Observer) {
ob = value.__ob__;
} else if ((Array.isArray(value) || isPlainObject(value)) && Object.isExtensible(value)) {
ob = new Observer(value);
}
return ob;
}
function dependArray(value) {
for (var i = 0, len = value.length; i < len; i++) {
var v = value[i];
if (v && v.__ob__) {
v.__ob__.dep.depend();
}
if (Array.isArray(v)) {
dependArray(v);
}
}
}
function initWatchState(data) {
observe(data);
}
var attribute = /^\s*([^\s"'<>\/=]+)(?:\s*(=)\s*(?:"([^"]*)"+|'([^']*)'+|([^\s"'=<>`]+)))?/;
var ncname = '[a-zA-Z_][\\w\\-\\.]*';
var qnameCapture = '((?:' + ncname + '\\:)?' + ncname + ')';
var startTagOpen = new RegExp('^<' + qnameCapture);
var startTagClose = /^\s*(\/?)>/;
var endTag = new RegExp('^<\\/' + qnameCapture + '[^>]*>');
var textREG = /[^<]*/;
var defaultTagRE = /\{\{((?:.|\n)+?)\}\}/;
var doctype = /^<!DOCTYPE [^>]+>/i;
var comment = /^<!--/;
var conditionalComment = /^<!\[/;
var TEXT = 0;
var STATICTAG = 1;
var TAG = 2;
function parseTemplate(html, compName) {
var index = 0;
var searchEndCount = 0;
var ast = [];
var scope = ast;
filter();
while (html) {
searchEndCount++;
parseStart();
parseEnd();
if (searchEndCount > html.length / 4) {
grassWarn('Parsing template error\n\n Missing end tag', compName);
}
}
return ast[0];
function parseStart() {
var match = html.match(startTagOpen);
if (match && match[0]) {
var indexKey = void 0,
parent = void 0,
container = void 0;
var tagStr = match[0];
var tagName = match[1];
var isRoot = scope === ast;
if (isRoot) {
parent = null;
indexKey = toString(ast.length);
container = ast;
} else {
parent = scope;
indexKey = toString(scope.children.length);
container = scope.children;
}
var tagNode = createTag(tagName, indexKey, parent);
container.push(tagNode);
scope = tagNode;
advance(tagStr.length);
var end = void 0,
attr = void 0,
attrName = void 0,
attrValue = void 0;
while (!(end = html.match(startTagClose)) && (attr = html.match(attribute))) {
advance(attr[0].length);
attrName = attr[1];
attrValue = attr[3] || attr[4] || attr[5];
if (/^v-|@|:+/.test(attrName)) {
conversionDirection(defineProperty({}, attrName, attrValue));
} else {
scope.attrs[attrName] = attrValue;
}
}
if (end[1]) {
scope.isUnaryTag = true;
scope.end = index;
scope = scope.parent;
searchEndCount = 0;
} else {
scope.isUnaryTag = false;
}
advance(end[0].length);
while (parseStaticTag()) {}
}
}
function parseStaticTag() {
filter();
var match = html.match(textREG);
var text = void 0;
if (!match || !match[0]) return false;
if (match && (text = match[0])) {
if (!defaultTagRE.test(text)) {
var textNode = createStaticNode(text, scope);
advance(text.length);
textNode.end = index;
if (scope === null) {
grassWarn('Component can only have one root node', compName);
}
scope.children.push(textNode);
} else {
var expression = parseTextExpression(text);
var staticTag = createStaticTag(text, expression, scope);
advance(text.length);
staticTag.end = index;
scope.children.push(staticTag);
}
}
return true;
}
function parseTextExpression(text) {
var l = 0;
var first = true;
var match = null;
var resultText = '';
var reg = new RegExp(defaultTagRE, 'g');
while (match = reg.exec(text)) {
resultText += first ? '`' + text.slice(l, match.index) + '` + _s(' + match[1] + ') ' : '+ `' + text.slice(l, match.index) + '` + _s(' + match[1] + ') ';
l = match.index + match[0].length;
first && (first = false);
}
if (l === text.length) return resultText;
resultText += '+ `' + text.slice(l, text.length) + '`';
return resultText;
}
function parseEnd() {
var match = html.match(endTag);
if (match && match[0]) {
var _match = slicedToArray(match, 2),
tagStr = _match[0],
tagName = _match[1];
if (scope.type === TAG && scope.tagName === tagName) {
searchEndCount = 0;
advance(tagStr.length);
scope.end = index;
scope = scope.parent;
while (parseStaticTag()) {}
}
}
}
function filter() {
if (comment.test(html)) {
var commentEnd = html.indexOf('-->');
if (commentEnd >= 0) {
advance(commentEnd + 3);
}
}
if (conditionalComment.test(html)) {
var conditionalEnd = html.indexOf(']>');
if (conditionalEnd >= 0) {
advance(conditionalEnd + 2);
}
}
var doctypeMatch = html.match(doctype);
if (doctypeMatch) {
advance(doctypeMatch[0].length);
}
}
function advance(n) {
index += n;
html = html.substring(n);
}
function getForArgs(attr) {
var args = /((\w+)|(\([^\(]+\)))\s+of\s+([\w\.\(\)\[\]\|\&\s]+)/g.exec(attr['v-for']);
if (args) {
var key = args[1];
if (key.includes(',')) {
key = key.replace(/[\(\)]/g, '').split(',').map(function (val) {
return val.trim();
});
}
return {
key: key,
data: args[4],
isMultiple: Array.isArray(key)
};
}
return null;
}
function conversionDirection(vAttr) {
var bind = void 0,
on = void 0;
var key = Object.keys(vAttr)[0];
if (key === 'v-for' && vAttr[key]) {
var args = getForArgs(vAttr);
scope.for = true;
scope.forArgs = args;
scope.forMultipleArg = Array.isArray(args);
scope.watcherCollectList = {};
}
if (key === 'v-if') {
scope.if = true;
}
if (bind = key.match(/^(:)(.+)/)) {
vAttr = defineProperty({}, 'v-bind' + key, vAttr[key]);
}
if (on = key.match(/^@(.+)/)) {
vAttr = defineProperty({}, 'v-on:' + on[1], vAttr[key]);
}
scope.direction.push(vAttr);
}
function createTag(tagName, indexKey, parent) {
var root = parent ? false : true;
return {
type: TAG,
tagName: tagName,
bindState: [],
children: [],
attrs: {},
start: index,
indexKey: indexKey,
end: null,
parent: parent,
root: root,
isUnaryTag: null,
direction: [],
hasBindings: function hasBindings() {
return !!this.direction.length;
}
};
}
function createStaticTag(content, expression, parent) {
return {
type: STATICTAG,
start: index,
bindState: [],
parent: parent,
end: null,
expression: expression,
content: content
};
}
function createStaticNode(content, parent) {
return {
type: TEXT,
start: index,
parent: parent,
end: null,
content: content,
static: true
};
}
}
var Container = function () {
function Container(val) {
classCallCheck(this, Container);
this._value = val;
}
createClass(Container, [{