app-keep-alive.vue 6.6 KB
Newer Older
1
<script>
2 3
import { ViewTool } from 'ibiz-core';
import qs from 'qs';
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
export default {
    name: 'app-keep-alive',
    render: function render() {
        let _this = this;
        let slot = _this.$slots.default;
        let vnode = _this.getFirstComponentChild(slot);
        let componentOptions = vnode && vnode.componentOptions;
        if (componentOptions) {
            // check pattern
            let name = _this.getComponentName(componentOptions);
            let ref = _this;
            let include = ref.include;
            let exclude = ref.exclude;
            let routerList = ref.routerList;
            let route = ref.$route;
19 20 21 22 23 24 25 26 27 28 29 30 31
            let fullPath = route.fullPath;
            if (fullPath && fullPath.indexOf('?') > -1) {
                const { startPath, viewParams } = ViewTool.getViewParamsByPath(fullPath);
                if (viewParams.hasOwnProperty('srfnav')) {
                    delete viewParams.srfnav;
                    if (Object.keys(viewParams).length > 0) {
                        fullPath = `${startPath}?${encodeURIComponent(qs.stringify(viewParams, { delimiter: ';' }))}`;
                    } else {
                        fullPath = startPath;
                    }
                    
                }
            }
32 33 34 35 36
            if (
                // not included
                (include && (!name || !_this.matches(include, name))) ||
                // excluded
                (exclude && name && _this.matches(exclude, name)) ||
37
                (routerList && (!fullPath && !_this.matches(routerList, fullPath)))
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
            ) {
                return vnode
            }

            let ref$1 = _this;
            let cache = ref$1.cache;
            let keys = ref$1.keys;
            let key = vnode.key == null
                // same constructor may get registered as different local components
                // so cid alone is not enough (#3269)
                ? componentOptions.Ctor.cid + (componentOptions.tag ? ("::" + (componentOptions.tag)) : '')
                : vnode.key;
            if (cache[key]) {
                vnode.componentInstance = cache[key].componentInstance;
                // make current key freshest
                _this.remove(keys, key);
                keys.push(key);
            } else {
                cache[key] = vnode;
                keys.push(key);
                // prune oldest entry
                if (_this.max && keys.length > parseInt(_this.max)) {
                    _this.pruneCacheEntry(cache, keys[0], keys, _this._vnode);
                }
            }

            vnode.data.keepAlive = true;
65
            vnode.data.curPath = fullPath;
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
        }
        return vnode || (slot && slot[0])
    },
    props: {
        include: [String, RegExp, Array],
        exclude: [String, RegExp, Array],
        max: [String, Number],
        routerList: [Array]
    },
    data: function(){
        return {
            _toString: Object.prototype.toString
        }
    },
    created: function () {
        this.cache = Object.create(null);
        this.keys = [];
    },
    destroyed: function () {
        let _this = this;
        for (let key in _this.cache) {
            _this.pruneCacheEntry(_this.cache, key, _this.keys);
        }
    },
    watch: {
        'include': function (val) {
            let _this = this;
            _this.pruneCache(function (name) {
                return _this.matches(val, name);
            });
        },
        'exclude': function (val) {
            let _this = this;
            _this.pruneCache(function (name) {
                return !_this.matches(val, name);
            });
        },
        'routerList': function(val) {
            let _this = this;
            _this.pruneCache2(function (name) {
                return !_this.matches(val, name);
            });
        }
    },
    methods: {
        pruneCacheEntry(cache, key, keys, current) {
            let cached = cache[key];
            if (cached) {
                cached.componentInstance.$destroy();
            }
            cache[key] = null;
            this.remove(keys, key);
        },
        pruneCache(filter) {
            let _this = this;
            let cache = _this.cache;
            let keys = _this.keys;
            let _vnode = _this._vnode;
            for (let key in cache) {
                let cachedNode = cache[key];
                if (cachedNode) {
                    let name = _this.getComponentName(cachedNode.componentOptions);
                    if (name && !filter(name)) {
                        _this.pruneCacheEntry(cache, key, keys, _vnode);
                    }
                }
            }
        },
        pruneCache2(filter) {
            let _this = this;
            let cache = _this.cache;
            let keys = _this.keys;
            let _vnode = _this._vnode;
            for (let key in cache) {
                let cachedNode = cache[key];
                if (cachedNode) {
                    let name = cachedNode.data.curPath;
                    if (name && filter(name)) {
                        _this.pruneCacheEntry(cache, key, keys, _vnode);
                    }
                }
            }
        },
        matches(pattern, name) {
            if (Array.isArray(pattern)) {
                return pattern.indexOf(name) > -1
            } else if (typeof pattern === 'string') {
                return pattern.split(',').indexOf(name) > -1
            } else if (this.isRegExp(pattern)) {
                return pattern.test(name)
            }
            /* istanbul ignore next */
            return false
        },
        getComponentName(opts) {
            return opts && (opts.Ctor.options.name || opts.tag)
        },
        getFirstComponentChild(children) {
            let _this = this;
            if (Array.isArray(children)) {
                for (let i = 0; i < children.length; i++) {
                    let c = children[i];
                    if (_this.isDef(c) && (_this.isDef(c.componentOptions) || _this.isAsyncPlaceholder(c))) {
                        return c
                    }
                }
            }
        },
        isAsyncPlaceholder(node) {
            return node.isComment && node.asyncFactory
        },
        isDef(v) {
            return v !== undefined && v !== null
        },
        isRegExp(v) {
            return this._toString.call(v) === '[object RegExp]'
        },
        remove(arr, item) {
            if (arr.length) {
                let index = arr.indexOf(item);
                if (index > -1) {
                    return arr.splice(index, 1)
                }
            }
        }
    }
}
</script>

<style lang="less">

</style>