List.js 4.4 KB
Newer Older
L
lang 已提交
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
define(function(require) {
    'use strict';

    var zrUtil = require('zrender/core/util');

    function createArrayIterWithDepth(maxDepth, properties, cb, context, iterType) {
        // Simple optimization to avoid read the undefined value in properties array
        var nestedProperties = properties.length > 0;
        return function eachAxis(array, depth) {
            if (depth === maxDepth) {
                return zrUtil[iterType](array, cb, context);
            }
            else {
                if (array) {
                    var property = properties[i];
                    for (var i = 0; i < array.length; i++) {
                        var item = array[i];
                        // Access property of each item
                        if (nestedProperties && property && item) {
                            item = item[property];
                        }
                        array[i] = eachAxis(item, depth);
                    }
                }
            }
        }
    }

    function List() {

        this.elements = this.elements || [];

        // Depth and properties is useful in nested Array.
        // For example in eventRiver, data structure is a nested 2d array as following
        // [{evolution: []}, {evolution: []}]
        // In this situation. depth should be 2 and properties should be ['evolution']
        this.depth = 1;

        this.properties = [];
    }

    List.prototype = {

        constructor: List,

        each: function (cb, context) {
            context = context || this;
            if (this.depth > 1) {
                createArrayIterWithDepth(
                    this.depth, this.properties, cb, context, 'each'
                )(this.elements, 0);
            }
            else {
                zrUtil.each(this.elements, cb, context);
            }
        },

        /**
         * In-place filter
         */
        filter: function (cb, context) {
            context = context || this;
            if (this.depth > 1) {
                createArrayIterWithDepth(
                    this.depth, this.properties, cb, context, 'filter'
                )(this.elements, 0);
            }
            else {
                this.elements = zrUtil.filter(this.elements, cb, context);
            }
        },

        /**
         * In-place map
         */
        map: function (cb, context) {
            context = context || this;
            if (this.depth > 1) {
                createArrayIterWithDepth(
                    this.depth, this.properties, cb, context, 'map'
                )(this.elements, 0);
            }
            else {
                this.elements = zrUtil.map(this.elements, cb, context);
            }
        },

/*
PENDING
        flatten: function () {

        },

        group: function () {

        },
*/

L
lang 已提交
99 100 101 102 103 104 105 106
        getItemByName: function (name) {
            var elements = this.elements;
            for (var i = 0; i < elements.length; i++) {
                if (elements[i].name === name) {
                    return elements[i];
                }
            }
        },
L
lang 已提交
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
        /**
         * Get x of single data item by a given data index.
         * can be overwritten
         * @param {number} idx
         * @return {number}
         */
        getX: function (idx) {

        },

        /**
         * Get y of single data item by a given data index.
         * can be overwritten
         * @param {number} idx
         * @return {number}
         */
        getY: function (idx) {

        },

        /**
         * Get z of single data item by a given data index.
         * can be overwritten
         * @param {number} idx
         * @return {number}
         */
        getZ: function (idx) {

        },

        /**
         * Get value of single data item by a given data index.
         * can be overwritten
         * @param {number} idx
         * @return {number}
         */
        getValue: function (idx) {

        },

        clone: function () {
            // Clone with depth
        }
    };

    List.fromArray = function (data) {
        var list = new List();
        // Normalize data
        // 2D Array
        list.elements = zrUtil.map(data, function (dataItem) {
            if (dataItem !== Object(dataItem)) {
                return {
                    value: dataItem
                };
            }
            return dataItem;
        });
        return list;
    }

    return List;
});