Source: ext/InfoWindow/index.js

import { EasyMap } from "../../easyMap";
import * as maptalks from "../../libs/maptalks";
import Geom from "../geom";

/**
 *信息窗体类
 */
class InfoWindow {
    /**
     * Creates an instance of InfoWindow.
     * @param {*} options 信息窗体的配置项
     * @param {Boolean} options.autoPan 如果您不希望地图进行平移动画以适合打开的窗口,则将其设置为false
     * @param {Boolean} options.autoCloseOn 自动关闭地图事件的信息窗口,例如,“单击上下文菜单”将通过单击或右键单击地图来关闭信息窗口
     * @param {Boolean} options.autoOpenOn 在所有者的事件上自动打开信息窗口,例如“单击”将通过单击或右键单击窗口的所有者来打开信息窗口
     * @param {Number} options.width 默认宽度
     * @param {Number} options.minHeight 最小高度
     * @param {Boolean} options.custom 设置为true可以填入html代码或HTMLElement设置为内容
     * @param {String} options.title 信息窗口的标题
     * @param {String | HTMLElement	} options.content 信息窗口的内容
     * @param {Boolean} options.eventsPropagation
     * @param {Number} options.dx x轴上的像素偏移
     * @param {Number} options.dy y轴上的像素偏移
     * @param {Boolean} options.autoPanDuration 自动平移动画的持续时间
     * @param {Boolean} options.single 如果信息窗体是一个全局单一信息窗体,则设置为true只会同时显示一个信息窗体
     * @param {Boolean} options.animation fade | scale 在显示和隐藏时淡入,缩放,添加动画效果
     * @param {Number} options.animationDuration 动画持续时间(以毫秒为单位)
     * @param {Boolean} options.pitchWithMap 是否与地图倾斜
     * @param {Boolean} options.rotateWithMap 是否随地图旋转
     * @memberof InfoWindow
     */
    constructor(options) {
        this._handlers = [];
        if (options instanceof maptalks.ui.InfoWindow) {
            this._infoWindow = options;
        } else {
            this._infoWindow = new maptalks.ui.InfoWindow(options);
        }
    }

    /**
     *事件绑定
     *
     * @param {*} eventsOn 要注册的事件类型
     * @param {Function} handler 要调用的处理函数
     * @param {*} context 处理程序的上下文
     * @return {Function} 返回处理过的回调,用于移除绑定
     * @memberof InfoWindow
     */
    on(eventsOn, handler, context) {
        let h = (e) => {
            e.target = this;
            handler(e);
        };
        this._infoWindow.on(eventsOn, h, context);
        this._handlers.push({ name: eventsOn, handler: h });
        return h;
    }

    /**
     *事件绑定,别名
     *
     * @param {*} eventsOn 要注册的事件类型
     * @param {Function} handler 要调用的处理函数
     * @param {*} context 处理程序的上下文
     * @return {Function} 返回处理过的回调,用于移除绑定
     * @memberof InfoWindow
     */
    addMapEventListener(eventsOn, handler, context) {
        let h = (e) => {
            e.target = this;
            handler(e);
        };
        this._infoWindow.addEventListener(eventsOn, h, context);
        this._handlers.push({ name: eventsOn, handler: h });
        return h;
    }

    /**
     *单次事件绑定,调用一次后移除
     *
     * @param {*} eventsOn 要注册的事件类型
     * @param {Function} handler 要调用的处理函数
     * @param {*} context 处理程序的上下文
     * @memberof InfoWindow
     */
    once(eventsOn, handler, context) {
        let h = (e) => {
            e.target = this;
            handler(e);
        };
        this._infoWindow.once(eventsOn, h, context);
        this._handlers.push({ name: eventsOn, handler: h });
        return h;
    }

    /**
     *事件移除
     *
     * @param {*} eventsOn 要移除的事件类型
     * @param {Function} handler 要移除的处理函数,该函数为绑定事件时返回的函数,如不传既移除该事件所有绑定(可选)
     * @param {*} context 处理程序的上下文
     * @memberof InfoWindow
     */
    un(eventsOn, handler, context) {
        if (handler) {
            this._infoWindow.off(eventsOn, handler, context);
        } else {
            let len = this._handlers.length - 1;
            for (let i = len; i >= 0; i--) {
                if (eventsOn === this._handlers[i].name) {
                    this._infoWindow.off(
                        eventsOn,
                        this._handlers[i].handler,
                        context
                    );
                    this._handlers.splice(i, 1);
                }
            }
        }
    }

    /**
     *事件移除,别名
     *
     * @param {*} eventsOn 要移除的事件类型
     * @param {Function} handler 要移除的处理函数,该函数为绑定事件时返回的函数,如不传既移除该事件所有绑定(可选)
     * @param {*} context 处理程序的上下文
     * @memberof InfoWindow
     */
    removeMapEventListener(eventsOn, handler, context) {
        if (handler) {
            this._infoWindow.removeEventListener(eventsOn, handler, context);
        } else {
            let len = this._handlers.length - 1;
            for (let i = len; i >= 0; i--) {
                if (eventsOn === this._handlers[i].name) {
                    this._infoWindow.removeEventListener(
                        eventsOn,
                        this._handlers[i].handler,
                        context
                    );
                    this._handlers.splice(i, 1);
                }
            }
        }
    }

    /**
     *将窗体添加到覆盖物或地图
     *
     * @param {*} object 地图或者覆盖物对象
     * @memberof InfoWindow
     */
    addTo(object) {
        let obj;
        if (object instanceof Geom) {
            obj = object._geometry;
        } else if (object instanceof EasyMap) {
            obj = object.map;
        } else if (object instanceof maptalks.Map) {
            obj = object;
        }
        this._infoWindow.addTo(obj);
        return this
    }

    /**
     *修改信息窗体配置项,与现有配置项合并
     *
     * @param {*} options
     * @return {*}
     * @memberof InfoWindow
     */
    setOptions(options) {
        this._infoWindow.setOptions(options);
        return this;
    }

    /**
     *设置信息窗口的内容
     *
     * @param {*} content HTMLElement
     * @memberof InfoWindow
     */
    setContent(content) {
        this._infoWindow.setContent(content);
        return this;
    }

    /**
     *获取信息窗口的内容。
     *
     * @return {*}
     * @memberof InfoWindow
     */
    getContent() {
        return this._infoWindow.getContent();
    }

    /**
     *设置信息窗口的标题
     *
     * @param {*} t HTMLElement
     * @return {*}
     * @memberof InfoWindow
     */
    setTitle(t) {
        this._infoWindow.setTitle(t);
        return this;
    }

    /**
     *获取信息窗口的标题
     *
     * @return {*} 信息窗口的内容
     * @memberof InfoWindow
     */
    getTitle() {
        return this._infoWindow.getTitle();
    }

    /**
     *打开信息窗体
     *
     * @param {*} coordinate 坐标;例如[1,2]
     * @memberof InfoWindow
     */
    show(coordinate) {
        this._infoWindow.show(
            coordinate ? new maptalks.Coordinate(coordinate) : ""
        );
        return this;
    }

    /**
     *关闭信息窗体
     *
     * @return {*}
     * @memberof InfoWindow
     */
    hide() {
        this._infoWindow.hide();
        return this;
    }

    /**
     *确定窗体是否打开
     *
     * @return {boolean}
     * @memberof InfoWindow
     */
    isVisible() {
        return this._infoWindow.isVisible();
    }

    /**
     *移除自身
     *
     * @return {*}
     * @memberof InfoWindow
     */
    remove() {
        this._infoWindow.remove();
        return this;
    }
}
export default InfoWindow;