日期:2014-05-16  浏览次数:20493 次

基于OpenLayer工具栏管理类的Javascript类定义

基于OpenLayer工具栏管理类的Javascript类定义

mapCwgisToolBarClass.js

//定义工具栏管理类
//mapCwgisToolBarClass.js
//vp:hsg
//create date:2013-07-31
//modify date:2013-08-01
//定义全局变量
var cmToolbar = null;
//插件调用方法cmToolbar.addItem(pluginCmdTool);
//定义工具栏管理类
mapCwgisToolBarClass = OpenLayers.Class(
{
    //
    m_ToolBarItems: null,
    isScan: false,
    //初始化函数
    initialize: function () {
        this.m_ToolBarItems = [];
        
    },
    //添加插件项
    addItem: function (item) {
        if (item != null) {
            this.m_ToolBarItems.push(item);
        }
    },
    //反激活插件集合
    deactivateItems: function () {
        //清除全局地图事件鼠标监听者
        mapWrap.map.events.clearMouseListener();
        //反激活所有全局工具
        if (this.m_ToolBarItems == null) return;
        for (var item in this.m_ToolBarItems) {
            if (item != null) {
                try {
                    this.m_ToolBarItems[item].dispose();
                }
                catch (e) { }
                this.m_ToolBarItems[item].deactivate();
            }
        }
    },
    //启动扫描
    startScan: function () {
        this.isScan = true;
        window.setInterval('loopScanItemEvent()', 1000); //1000为1秒钟,时间可以自己设
    },
    //循环扫描项事件
    loopScanItemEvent: function () {
        if (this.isScan == true) {
            for (var item in this.m_ToolBarItems) {
                if (this.isScan == false) break;
                if (item != null) {
                    item.enabled;
                }
            }
        }
    },
    //释放类
    dispose: function () {
        this.isScan = false;
        this.deactivateItems();
        this.m_ToolBarItems = null;
        OpenLayers.Class.prototype.destroy.apply(this, arguments);
    },
    //类名称
    CLASS_NAME: "mapCwgisToolBarClass"
});
//实例化一个全局工具条管理类
cmToolBar = new mapCwgisToolBarClass();

//定义插件Command基类  继承于OpenLayers.Class(OpenLayers.Control)
//基类抽象类名称:mapCwgisPluginCommand
mapCwgisPluginCommand = OpenLayers.Class(OpenLayers.Control,
{
    //定义属性
    mapWrap: null,
    //定义类  初始化函数
    init: function (p_mapCwgisClass) {
        this.mapWrap = p_mapCwgisClass;
        this.setMap(this.mapWrap.map);
    },
    initialize: function (p_mapCwgisClass, options) {
        this.init(p_mapCwgisClass);
        //
        this.displayClass =
            this.CLASS_NAME.replace("OpenLayers.", "ol").replace(/\./g, "");

        OpenLayers.Util.extend(this, options);
        //
        this.events = new OpenLayers.Events(this, null, this.EVENT_TYPES);
        if (this.eventListeners instanceof Object) {
            this.events.on(this.eventListeners);
        }
        if (this.id == null) {
            this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
        }
    },
    //---------------------------------------------
    /**
    * Method: activate
    * Activates the control.
    * 
    * Returns:
    * {Boolean} The control was effectively activated.
    */
    activate: function () {
        if (!this.active) {
            for (var i in this.handlers) {
                this.handlers[i].activate();
            }
        }
        return OpenLayers.Control.prototype.activate.apply(
            this, arguments
        );
    },

    /**
    * Method: deactivate
    * Deactivates the control.
    * 
    * Returns:
    * {Boolean} The control was effectively deactivated.
    */
    deactivate: function () {
        if (this.active) {
            for (var i in this.handlers) {
                this.handlers[i].deactivate();
            }
        }
        return OpenLayers.Control.prototype.deactivate.apply(
            this, arguments
        );
    },
    //---------------------------------------------
    //释放类
    dispose: function () {
        this.deactivate();
    },
    //定义单击事件
    onClick: function () {
        if (