Skip to content

Attribution control support custom #2413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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