feat: 1.BaseControl 升级

This commit is contained in:
yanxiong 2022-08-09 11:27:35 +08:00
parent 78f93c8e13
commit af1cc12950
5 changed files with 37 additions and 34 deletions

View File

@ -10,10 +10,10 @@
"browser": true,
"node": true
},
"parser": "babel-eslint",
"parserOptions": {
"sourceType": "module"
},
// "parser": "babel-eslint",
// "parserOptions": {
// "sourceType": "module"
// },
"plugins": [
"html"
],

View File

@ -11,14 +11,13 @@ import {
import { DOM } from '@antv/l7-utils';
import { EventEmitter } from 'eventemitter3';
import { Container } from 'inversify';
import { ControlEvent } from '../interface';
import { pick } from 'lodash';
export { PositionType } from '@antv/l7-core';
export default abstract class Control<
O extends IControlOption = IControlOption,
E extends string = ControlEvent
> extends EventEmitter<E> {
O extends IControlOption = IControlOption
> extends EventEmitter {
/**
*
* @protected
@ -63,32 +62,27 @@ export default abstract class Control<
/**
*
* @param newOption
* @param newOptions
*/
public setOption(newOption: Partial<O>): void {
const {
position: newPosition,
className: newClassName,
style: newStyle,
} = newOption;
public setOptions(newOptions: Partial<O>): void {
const {
position: oldPosition,
className: oldClassName,
style: oldStyle,
} = this.controlOption;
if (newPosition && newPosition !== oldPosition) {
this.setPosition(newPosition);
if ('position' in newOptions && newOptions.position !== oldPosition) {
this.setPosition(newOptions.position);
}
if (
(newClassName && newClassName !== oldClassName) ||
(newStyle && newStyle !== oldStyle)
('className' in newOptions && newOptions.className !== oldClassName) ||
('style' in newOptions && newOptions.style !== oldStyle)
) {
this.setContainerCSS({ className: newClassName, style: newStyle });
this.setContainerCSS(pick(newOptions, 'className', 'style'));
}
this.controlOption = {
...this.controlOption,
...newOption,
...newOptions,
};
}
@ -141,6 +135,7 @@ export default abstract class Control<
const container = this.container;
DOM.removeClass(container, 'l7-control-hide');
this.isShow = true;
this.emit('show', this);
}
/**
@ -163,6 +158,20 @@ export default abstract class Control<
} as O;
}
/**
* DOM
*/
public getContainer() {
return this.container;
}
/**
* Control
*/
public getIsShow() {
return this.isShow;
}
public _refocusOnMap(e: MouseEvent) {
// if map exists and event is not a keyboard event
if (this.mapsService && e && e.screenX > 0 && e.screenY > 0) {
@ -200,12 +209,14 @@ export default abstract class Control<
option: Pick<IControlOption, 'className' | 'style'>,
) {
const { className, style } = option;
if (className) {
if ('className' in option) {
const { className: oldClassName } = this.controlOption;
if (oldClassName) {
DOM.removeClass(this.container, oldClassName);
}
DOM.addClass(this.container, className);
if (className) {
DOM.addClass(this.container, className);
}
}
if (style) {
DOM.addStyle(this.container, style);

View File

@ -1,19 +1,13 @@
import { IControlOption, PositionType } from '@antv/l7-core';
import { bindAll, DOM } from '@antv/l7-utils';
import { ControlEvent, ILayerControlOption } from '../interface';
import { ILayerControlOption } from '../interface';
import Control from './BaseControl';
interface IInputItem extends HTMLInputElement {
layerId: string;
}
export type LayerControlEvent =
| ControlEvent
| 'overlayadd'
| 'overlayremove'
| 'baselayerchange';
export default class Layers extends Control<IControlOption, LayerControlEvent> {
export default class Layers extends Control<IControlOption> {
private layerControlInputs: any[];
private layers: any[];
private lastZIndex: number;

View File

@ -1,7 +1,5 @@
import { IControlOption } from '@antv/l7-core';
export type ControlEvent = 'show' | 'hide' | 'add' | 'remove';
export interface ILayerControlOption extends IControlOption {
collapsed: boolean;
autoZIndex: boolean;

View File

@ -36,7 +36,7 @@ export interface IControlCorners {
}
export interface IControl {
controlOption: IControlOption;
setOption: (newOption: Partial<IControlOption>) => void;
setOptions: (newOption: Partial<IControlOption>) => void;
addTo(sceneContainer: Container): void;
onAdd(): HTMLElement;
onRemove(): void;