美文网首页周|总结
openlayers源码分析(1)

openlayers源码分析(1)

作者: solfKwolf | 来源:发表于2020-12-28 23:33 被阅读0次

    1 准备

    // 码云fork,速度快
    git clone https://gitee.com/solfKwolf/openlayers.git
    // 以v5.3版本为准
    git checkout v5.3.0
    

    2 入口文件

    src/ol/index.js

    /**
     * @module ol
     */
    
    export {default as AssertionError} from './AssertionError.js';
    export {default as Collection} from './Collection.js';
    export {default as Disposable} from './Disposable.js';
    export {default as Feature} from './Feature.js';
    export {default as Geolocation} from './Geolocation.js';
    export {default as Graticule} from './Graticule.js';
    export {default as Image} from './Image.js';
    export {default as ImageBase} from './ImageBase.js';
    export {default as ImageCanvas} from './ImageCanvas.js';
    export {default as ImageTile} from './ImageTile.js';
    export {default as Kinetic} from './Kinetic.js';
    export {default as Map} from './Map.js';
    export {default as MapBrowserEvent} from './MapBrowserEvent.js';
    export {default as MapBrowserEventHandler} from './MapBrowserEventHandler.js';
    export {default as MapBrowserPointerEvent} from './MapBrowserPointerEvent.js';
    export {default as MapEvent} from './MapEvent.js';
    export {default as Object} from './Object.js';
    export {default as Observable} from './Observable.js';
    export {default as Overlay} from './Overlay.js';
    export {default as PluggableMap} from './PluggableMap.js';
    export {default as Tile} from './Tile.js';
    export {default as TileCache} from './TileCache.js';
    export {default as TileQueue} from './TileQueue.js';
    export {default as TileRange} from './TileRange.js';
    export {default as VectorImageTile} from './VectorImageTile.js';
    export {default as VectorTile} from './VectorTile.js';
    export {default as View} from './View.js';
    export {default as WebGLMap} from './WebGLMap.js';
    
    export {getUid, inherits, VERSION} from './util.js';
    

    ol是将不同的模块拆分成不同的文件集中放置在ol文件下,再统一通过index.js入口文件暴露出去。

    将default暴露的模块重命名

    // xxx.js
    export default function() {
      // statement
    }
    // index.js
    export {default as xxx} from './xxx.js';
    
    // 相当于
    import { default as xxx } from './xxx.js'
    

    3 AssertionError

    openlayer自定义错误类(继承Error),目的是方便自定义错误描述与错误码相对应。

    示例截图

    class AssertionError extends Error {
    
      /**
       * @param {number} code Error code.
       */
      constructor(code) {
        const path = VERSION === 'latest' ? VERSION : 'v' + VERSION.split('-')[0];
        const message = 'Assertion failed. See https://openlayers.org/en/' + path +
        '/doc/errors/#' + code + ' for details.';
    
        super(message);
    
        /**
         * Error code. The meaning of the code can be found on
         * https://openlayers.org/en/latest/doc/errors/ (replace `latest` with
         * the version found in the OpenLayers script's header comment if a version
         * other than the latest is used).
         * @type {number}
         * @api
         */
        this.code = code;
    
        /**
         * @type {string}
         */
        this.name = 'AssertionError';
    
        // Re-assign message, see https://github.com/Rich-Harris/buble/issues/40
        this.message = message;
      }
    
    }
    

    如果需要自己开发类库,需要认真研读Error类,非常简单。

    5 Event

    openlayers库中定义的事件基类,只提供typetarget属性以及stopPropagationpreventDefault方法。方便后面的自定义事件。

    class Event {
    
      /**
       * @param {string} type Type.
       */
      constructor(type) {
    
        /**
         * @type {boolean}
         */
        this.propagationStopped;
    
        /**
         * The event type.
         * @type {string}
         * @api
         */
        this.type = type;
    
        /**
         * The event target.
         * @type {Object}
         * @api
         */
        this.target = null;
      }
    
      /**
       * Stop event propagation.
       * @api
       */
      preventDefault() {
        this.propagationStopped = true;
      }
    
      /**
       * Stop event propagation.
       * @api
       */
      stopPropagation() {
        this.propagationStopped = true;
      }
    
    }
    
    
    /**
     * @param {Event|import("./Event.js").default} evt Event
     */
    export function stopPropagation(evt) {
      evt.stopPropagation();
    }
    
    
    /**
     * @param {Event|import("./Event.js").default} evt Event
     */
    export function preventDefault(evt) {
      evt.preventDefault();
    }
    
    export default Event;
    

    6 Target

    对象类是配合事件类的。

    
    

    相关文章

      网友评论

        本文标题:openlayers源码分析(1)

        本文链接:https://www.haomeiwen.com/subject/jxlxoktx.html