components/layer/fillLayer.js

/*``
 * @Author: tjc
 * @Date:   2017-04-20 17:03:05
 * @Last Modified by:   hydata
 * @Last Modified time: 2018-05-10 17:10:51
 * @Email: 304861063@qq.com
 * @File Path: F:\work\hyMap\src\components\layer\buildingLayer.js
 * @File Name: clustersLayer.js
 * @Descript:多边形图层
 */
import styleModel from '../../model/fillStyle';
import baseLayer from './baseLayer';
import dataUtil from '../../util/dataUtil';


/**
 * 填充层
 * @extends baseLayer
 */
class FillLayer extends baseLayer {
    /**
     * @param {*} options 
     */
    constructor(options) {

        options.layerType = 'fill';
        super(options);
        options.serie.symbolStyle.normal = Object.assign({}, styleModel, options.serie.symbolStyle.normal);
        this.data = options.serie;
        this.geojson = this.init(options.serie);
        this.listenerArray = [];

    }


    /**
     * 初始化
     * @param {*} serie 
     */
    init(serie) {

        let json = this.buildJson(serie);

        this.map.on('load', () => {

            this.addLayer(json);

        });

        this.map.on('mouseenter', this.id, () => {

            this.map.getCanvas().style.cursor = 'pointer';

        });

        this.map.on('mouseleave', this.id, () => {

            this.map.getCanvas().style.cursor = '';

        });

        return json;

    }

    /**
     * 注册事件
     * @param {*} type 
     * @param {*} listener 
     */
    on(type, listener) {

        this.map.on(type, this.id, listener);

    }

    /**
     * 取消注册事件
     * @param {*} type 
     * @param {*} listener 
     */
    off(type, listener) {

        this.map.off(type, this.id, listener);

    }

    /*on(type, listener) {

        this.listenerArray.push(listener);
        this.map.on(type, this.id, e => {

            let popup = new mapboxgl.Popup({
                closeButton: false
            });
            let feature = e.features[0];
            popup.setLngLat(e.lngLat)
                .setText('')
                .addTo(e.target);

            popup['_container'].innerHTML = '';
            let obj = {
                div: popup['_container'],
                e
            };
            //listener(obj);
            this.listenerArray.forEach((value) => {

                    if (value === listener) {

                        listener(obj);

                    }

                }

            );
            e.originalEvent.preventDefault();
            e.originalEvent.stopPropagation();

        });

    }

    off(type, listener) {

        this.listenerArray.forEach((value, index) => {

            if (listener === value) {

                this.listenerArray.splice(index, 0, 1);

            }

        });

    }*/

    /**
     * 获取json
     */
    getJson() {

        return this.geojson;

    }

    /**
     * 构造json
     * @param {*} serie 
     */
    buildJson(serie) {

        //构建数据
        let dataObject = dataUtil.buildData(serie.data);
        serie.max = dataObject.max;
        serie.min = dataObject.min;
        //构建样式
        let style = this.buildStyle(serie);
        let json = {
            'id': this.id,
            'source': dataObject.geojson
        };
        Object.assign(json, style);
        return json;

    }

    /**
     * 构造样式
     */
    buildStyle(serie) {

        let normal = Object.assign({}, serie.symbolStyle.normal);
        let visibility = normal.visibility;
        delete normal['visibility'];
        return {
            'type': 'fill',
            'paint': normal,
            'layout': {
                'visibility': visibility
            }
        };


    }
}
export default FillLayer