all files / vmodel/ ProxyArray.js

100% Statements 56/56
92.86% Branches 26/28
100% Functions 14/14
100% Lines 56/56
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        37×           31×                                         11×             72× 1152×         27×   27× 27×   27× 27× 27×       99× 72× 72× 72×   72× 72×   99× 99× 273× 273× 73×     99×    
import { avalon, ap, platform, modern, isObject } from '../seed/core'
import { Mutation } from './Mutation'
 
var _splice = ap.splice
var __array__ = {
    set: function(index, val) {
        if (((index >>> 0) === index) && this[index] !== val) {
            if (index > this.length) {
                throw Error(index + 'set方法的第一个参数不能大于原数组长度')
            }
            this.splice(index, 1, val)
        }
    },
    toJSON: function() {
        //为了解决IE6-8的解决,通过此方法显式地求取数组的$model
        return this.$model = platform.toJson(this)
    },
    contains: function(el) { //判定是否包含
        return this.indexOf(el) !== -1
    },
    ensure: function(el) {
        if (!this.contains(el)) { //只有不存在才push
            this.push(el)
            return true
        }
        return false
    },
    pushArray: function(arr) {
        return this.push.apply(this, arr)
    },
    remove: function(el) { //移除第一个等于给定值的元素
        return this.removeAt(this.indexOf(el))
    },
    removeAt: function(index) { //移除指定索引上的元素
        if ((index >>> 0) === index) {
            return this.splice(index, 1)
        }
        return []
    },
    clear: function() {
        this.removeAll()
        return this
    },
    removeAll: function(all) { //移除N个元素
        var size = this.length
        var eliminate = Array.isArray(all) ?
            function(el) {
                return all.indexOf(el) !== -1
            } : typeof all === 'function' ?
            all : false
 
        if (eliminate) {
            for (var i = this.length - 1; i >= 0; i--) {
                if (eliminate(this[i], i)) {
                    _splice.call(this, i, 1)
                }
            }
        } else {
            _splice.call(this, 0, this.length)
        }
        this.toJSON()
        this.$events.__dep__.notify()
    }
}
export function hijackMethods(array) {
    for (var i in __array__) {
        platform.hideProperty(array, i, __array__[i])
    }
}
var __method__ = ['push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse']
 
__method__.forEach(function(method) {
    var original = ap[method]
    __array__[method] = function() {
        // 继续尝试劫持数组元素的属性
        var core = this.$events
 
        var args = platform.listFactory(arguments, true, core.__dep__)
        var result = original.apply(this, args)
 
        this.toJSON()
        core.__dep__.notify(method)
        return result
    }
})
 
export function listFactory(array, stop, dd) {
    if (!stop) {
        hijackMethods(array)
        Eif (modern) {
            Object.defineProperty(array, '$model', platform.modelAccessor)
        }
        platform.hideProperty(array, '$hashcode', avalon.makeHashCode('$'))
        platform.hideProperty(array, '$events', { __dep__: dd || new Mutation })
    }
    var _dd = array.$events && array.$events.__dep__
    for (var i = 0, n = array.length; i < n; i++) {
        var item = array[i]
        if (isObject(item)) {
            array[i] = platform.createProxy(item, _dd)
        }
    }
    return array
}
 
platform.listFactory = listFactory