Skip to content

Commit bf8117e

Browse files
authored
Merge pull request #12 from helsingborg-stad/feat/create-popup-class
feat: create popup class
2 parents 5c9fd50 + a388c82 commit bf8117e

File tree

7 files changed

+55
-32
lines changed

7 files changed

+55
-32
lines changed

js/addableInterface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LayerGroup, Map as LeafletMap } from 'leaflet';
22

33
export interface Addable {
4-
getAddable(): LeafletMap|LayerGroup;
4+
getAddable(): LeafletMap | LayerGroup;
55
}

js/bindableInterface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ImageOverlay, Layer, LayerGroup, Map as LeafletMap, Marker } from 'leaflet';
2+
import { PopupInterface } from './features/popup/popupInterface';
23

34
export interface Bindable {
45
getBindable(): Layer;

js/features/createRectangle/rectangle.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import L, { Rectangle as LeafletRectangle } from "leaflet";
22
import { RectangleInterface } from "./rectangleInterface";
33
import { Addable } from "../../addableInterface";
44
import { LatLngBoundsObject, InteractionEvent, InteractionEventCallback } from "../../types";
5+
import { PopupInterface } from "../popup/popupInterface";
56

67
export class Rectangle implements RectangleInterface {
78
private listeners: { [key: string]: InteractionEventCallback[] } = {};

js/features/popup/createPopup.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import L from "leaflet";
2+
import { Bindable } from "../../bindableInterface";
3+
import { Popup } from "./popup";
4+
import { PopupInterface, PopupOptions } from "./popupInterface";
5+
import { CreatePopupInterface } from "./createPopupInterface";
6+
7+
export class CreatePopup implements CreatePopupInterface {
8+
public create(popupOptions: PopupOptions = {}): PopupInterface {
9+
const popup = L.popup(popupOptions);
10+
return new Popup(popup);
11+
}
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Bindable } from "../../bindableInterface";
2+
import { PopupInterface, PopupOptions } from "./popupInterface";
3+
4+
export interface CreatePopupInterface {
5+
create(popupOptions?: PopupOptions): PopupInterface;
6+
}

js/features/popup/popup.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
1+
import { Popup as LeafletPopup } from "leaflet";
12
import { Bindable } from "../../bindableInterface";
23
import { PopupInterface, PopupOptions } from "./popupInterface";
34

5+
46
export class Popup implements PopupInterface {
5-
public bindTo(bindable: Bindable, content: string | HTMLElement, options: PopupOptions = {}): Bindable {
6-
bindable.getBindable().bindPopup(content, options);
7-
return bindable;
7+
constructor(private popup: LeafletPopup) { }
8+
9+
public bindTo(bindable: Bindable, options: PopupOptions = {}): this {
10+
bindable.getBindable().bindPopup(this.popup, options);
11+
return this;
812
}
913

10-
public unbindPopup(bindable: Bindable): Bindable {
11-
bindable.getBindable().unbindPopup();
12-
return bindable;
14+
public unbind(): this {
15+
this.popup.unbindPopup();
16+
return this;
1317
}
1418

15-
public openPopup(bindable: Bindable): Bindable {
16-
bindable.getBindable().openPopup();
17-
return bindable;
19+
public getElement(): HTMLElement | null {
20+
return this.popup.getElement() ?? null;
1821
}
1922

20-
public closePopup(bindable: Bindable): Bindable {
21-
bindable.getBindable().closePopup();
22-
return bindable;
23+
public open(): this {
24+
this.popup.openPopup();
25+
return this;
2326
}
2427

25-
public togglePopup(bindable: Bindable): Bindable {
26-
bindable.getBindable().togglePopup();
27-
return bindable;
28+
public close(): this {
29+
this.popup.close();
30+
return this;
2831
}
2932

30-
public isPopupOpen(bindable: Bindable): boolean {
31-
return bindable.getBindable().isPopupOpen();
33+
public toggle(): this {
34+
this.popup.toggle();
35+
return this;
3236
}
3337

34-
public setPopupContent(bindable: Bindable, content: string | HTMLElement): Bindable {
35-
bindable.getBindable().setPopupContent(content);
36-
return bindable;
38+
public isOpen(): boolean {
39+
return this.popup.isOpen();
3740
}
3841

39-
public getPopup(bindable: Bindable): this | null {
40-
const popup = bindable.getBindable().getPopup();
41-
return popup ? this : null;
42+
public setContent(content: string | HTMLElement): this {
43+
this.popup.setContent(content);
44+
return this;
4245
}
4346
}

js/features/popup/popupInterface.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Bindable } from "../../bindableInterface";
22

33
export interface PopupInterface {
4-
setPopupContent(bindable: Bindable, content: string | HTMLElement): Bindable;
5-
bindTo(bindable: Bindable, content: string | HTMLElement, options?: PopupOptions): Bindable;
6-
unbindPopup(bindable: Bindable): Bindable;
7-
openPopup(bindable: Bindable): Bindable;
8-
closePopup(bindable: Bindable): Bindable;
9-
togglePopup(bindable: Bindable): Bindable;
10-
isPopupOpen(bindable: Bindable): boolean;
11-
getPopup(bindable: Bindable): this | null;
4+
setContent(content: string | HTMLElement): this;
5+
bindTo(bindable: Bindable, options?: PopupOptions): this;
6+
unbind(): this;
7+
getElement(): HTMLElement | null;
8+
open(): this;
9+
close(): this;
10+
toggle(): this;
11+
isOpen(): boolean;
1212
}
1313

1414
export type PopupOptions = {

0 commit comments

Comments
 (0)