Skip to content

Commit

Permalink
Attribution control support custom (#2413)
Browse files Browse the repository at this point in the history
  • Loading branch information
deyihu authored Sep 5, 2024
1 parent cdf38b9 commit 0c3fcd0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/control/Control.Attribution.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createEl } from '../core/util/dom';
import Control, { ControlOptionsType } from './Control';
import Map from '../map/Map';
import { isString } from '../core/util';

/**
* @property {Object} options - options
Expand All @@ -14,7 +15,8 @@ const options: AttributionOptionsType = {
'bottom': 0,
'left': 0
},
'content': '<a href="http://maptalks.org" target="_blank">maptalks</a>'
'content': '<a href="http://maptalks.org" target="_blank">maptalks</a>',
'custom': false
};

const layerEvents = 'addlayer removelayer setbaselayer baselayerremove';
Expand Down Expand Up @@ -58,6 +60,16 @@ class Attribution extends Control {
return this._attributionContainer;
}

getContent() {
return this.options.content;
}

setContent(content: string | HTMLElement) {
this.options.content = content;
this._update();
return this;
}

onAdd() {
this.getMap().on(layerEvents, this._update, this);
}
Expand All @@ -66,8 +78,28 @@ class Attribution extends Control {
this.getMap().off(layerEvents, this._update, this);
}

//@internal
_updateContent() {
const container = this._attributionContainer;
const content = this.options.content || '';
if (container) {
//clear
container.innerHTML = '';
if (isString(content)) {
container.innerHTML = content;
} else if (content instanceof HTMLElement) {
container.appendChild(container);
}
}
return this;
}

//@internal
_update() {
if (this.options.custom) {
this._updateContent();
return;
}
const map = this.getMap();
if (!map) {
return;
Expand Down Expand Up @@ -99,5 +131,6 @@ Map.addOnLoadHook(function () {
export default Attribution;

export type AttributionOptionsType = {
content?: string;
content?: string | HTMLElement;
custom?: boolean;
} & ControlOptionsType;
8 changes: 8 additions & 0 deletions src/layer/Layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,14 @@ class Layer extends JSONAble(Eventable(Renderable(Class))) {
if (renderer && renderer.setToRedraw) {
renderer.setToRedraw();
}
//auto update attribution control
if ('attribution' in conf) {
const map = this.getMap();
if (map && map.attributionControl && map.attributionControl._update) {
map.attributionControl._update();
}
}

}
}

Expand Down
2 changes: 2 additions & 0 deletions src/map/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { computeDomPosition, MOUSEMOVE_THROTTLE_TIME } from '../core/util/dom';
import EPSG9807, { type EPSG9807ProjectionType } from '../geo/projection/Projection.EPSG9807.js';
import { AnimationOptionsType, EasingType } from '../core/Animation';
import { BBOX, bboxInBBOX, getDefaultBBOX, pointsBBOX } from '../core/util/bbox';
import { Attribution } from '../control';

const TEMP_COORD = new Coordinate(0, 0);
const TEMP_POINT = new Point(0, 0);
Expand Down Expand Up @@ -261,6 +262,7 @@ export class Map extends Handlerable(Eventable(Renderable(Class))) {
options: MapOptionsType;
static VERSION: string;
JSON_VERSION: '1.0';
attributionControl?: Attribution;


/**
Expand Down

0 comments on commit 0c3fcd0

Please sign in to comment.