8000 feat: support YMapClusterer by ddubrava · Pull Request #260 · ddubrava/angular-yandex-maps · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: support YMapClusterer #260

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
Aug 24, 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
34 changes: 34 additions & 0 deletions apps/angular-yandex-maps-v3-demo/src/app/app.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,37 @@
width: 1000px;
height: 500px;
}

.circle {
position: relative;
width: 40px;
height: 40px;
color: #7234c3;
border: 2px solid currentColor;
border-radius: 50%;
background-color: rgba(255, 255, 255, 0.7);
transform: translate(-50%, -50%);
}

.circle-content {
position: absolute;
top: 50%;
left: 50%;
display: flex;
justify-content: center;
align-items: center;
width: 70%;
height: 70%;
border-radius: 50%;
background-color: currentColor;
transform: translate3d(-50%, -50%, 0);
}

.circle-text {
font-size: 0.9em;
color: #fff;
}

.pin {
transform: translate(-50%, -100%);
}
33 changes: 33 additions & 0 deletions apps/angular-yandex-maps-v3-demo/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,36 @@ <h1>Hint</h1>
/>
</y-map>
</div>

<h1>Clusterer</h1>

<div class="container">
<y-map
[props]="{
location: {
center: [-0.127696, 51.507351],
zoom: 10,
},
}"
>
<y-map-default-scheme-layer />
<y-map-default-features-layer />

<y-map-clusterer [props]="{ features }">
<ng-template #marker let-context>
<img
src="https://yastatic.net/s3/front-maps-static/maps-front-jsapi-3/3.0.14707275/build/static/s3-cases/markers-clusterizer/pin.svg"
class="pin"
/>
</ng-template>

<ng-template #cluster let-context>
<div class="circle">
<div class="circle-content">
<span class="circle-text">{{ context.features.length }}</span>
</div>
</div>
</ng-template>
</y-map-clusterer>
</y-map>
</div>
25 changes: 25 additions & 0 deletions apps/angular-yandex-maps-v3-demo/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ChangeDetectionStrategy, Component, DoCheck } from '@angular/core';
import type { YMapFeature, YMapHotspot, YMapMarker } from '@yandex/ymaps3-types/imperative';
import { Feature } from '@yandex/ymaps3-types/packages/clusterer';
import {
YConfig,
YMapClustererDirective,
YMapComponent,
YMapControlButtonDirective,
YMapControlCommonButtonDirective,
Expand Down Expand Up @@ -42,6 +44,7 @@ import { config$ } from './app.config';
YMapOpenMapsButtonDirective,
YMapControlDirective,
YMapHintDirective,
YMapClustererDirective,
],
selector: 'app-root',
templateUrl: './app.component.html',
Expand All @@ -52,6 +55,28 @@ import { config$ } from './app.config';
export class AppComponent implements DoCheck {
lang: YConfig['lang'] = 'ru_RU';

coordinates: [number, number][] = [
[-0.597696, 51.907351],
[0.412304, 51.287351],
[-0.087696, 51.947351],
[-0.477696, 51.117351],
[0.152304, 51.867351],
[-0.627696, 51.347351],
[0.402304, 51.737351],
[-0.057696, 51.107351],
[-0.457696, 51.787351],
[0.362304, 51.627351],
];

features: Feature[] = this.coordinates.map((coordinates, i) => ({
type: 'Feature',
id: i.toString(),
geometry: {
type: 'Point',
coordinates,
},
}));

ngDoCheck() {
console.log('do check');
}
Expand Down
1 change: 1 addition & 0 deletions libs/angular-yandex-maps-v3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// Components
export * from './lib/components/common/y-map/y-map.component';
export * from './lib/components/common/y-map-clusterer/y-map-clusterer.di ED4F rective';
export * from './lib/components/common/y-map-default-marker/y-map-default-marker.directive';
export * from './lib/components/common/y-map-feature/y-map-feature.directive';
export * from './lib/components/common/y-map-feature-data-source/y-map-feature-data-source.directive';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { Component, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { YMapClustererProps } from '@yandex/ymaps3-types/packages/clusterer';
import { BehaviorSubject } from 'rxjs';

import {
mockClusterByGrid,
mockYMapClustererConstructor,
mockYMapClustererInstance,
mockYMapInstance,
} from '../../../../test-utils';
import { Optional } from '../../../types/utilities/optional';
import { YReadyEvent } from '../../../types/y-ready-event';
import { YMapComponent } from '../y-map/y-map.component';
import { YMapClustererDirective } from './y-map-clusterer.directive';

type OptionalYMapClustererProps = Optional<YMapClustererProps, 'marker' | 'cluster' | 'method'>;

@Component({
standalone: true,
imports: [YMapClustererDirective],
template: `
<y-map-clusterer [props]="props">
<ng-template #marker />
<ng-template #cluster />
</y-map-clusterer>
`,
})
class MockHostComponent {
@ViewChild(YMapClustererDirective, { static: true })
clusterer!: YMapClustererDirective;

props: OptionalYMapClustererProps = {
features: [
{
type: 'Feature',
id: '0',
geometry: {
type: 'Point',
coordinates: [0, 1],
},
},
],
};
}

describe('YMapClustererDirective', () => {
let component: YMapClustererDirective;
let mockComponent: MockHostComponent;
let fixture: ComponentFixture<MockHostComponent>;

let mapInstance: ReturnType<typeof mockYMapInstance>;
let clustererInstance: ReturnType<typeof mockYMapClustererInstance>;
let clustererConstructorMock: jest.Mock;

beforeEach(async () => {
mapInstance = mockYMapInstance();

await TestBed.configureTestingModule({
imports: [MockHostComponent],
providers: [
{
provide: YMapComponent,
useValue: {
map$: new BehaviorSubject(mapInstance),
},
},
],
}).compileComponents();

fixture = TestBed.createComponent(MockHostComponent);
mockComponent = fixture.componentInstance;
component = mockComponent.clusterer;

// Two instances are returned from the import function.
clustererInstance = mockYMapClustererInstance();
clustererConstructorMock = mockYMapClustererConstructor(clustererInstance);
mockClusterByGrid();
});

afterEach(() => {
(window as any).ymaps3 = undefined;
});

it('should create entity', async () => {
fixture.detectChanges();

expect((window as any).ymaps3.import).toHaveBeenCalledWith('@yandex/ymaps3-clusterer@0.0.1');

// ymaps3.import is async, wait for it
await new Promise(process.nextTick);

expect(clustererConstructorMock).toHaveBeenCalledWith({
marker: expect.any(Function),
cluster: expect.any(Function),
method: [],
...mockComponent.props,
});

expect(mapInstance.addChild).toHaveBeenCalledWith(clustererInstance);
});

it('should emit ready on load', async () => {
jest.spyOn(component.ready, 'emit');
fixture.detectChanges();

// ymaps3.import is async, wait for it
await new Promise(process.nextTick);

const readyEvent: YReadyEvent = {
ymaps3: (window as any).ymaps3,
entity: clustererInstance,
};

expect(component.ready.emit).toHaveBeenCalledWith(readyEvent);
});

it('should pass inputs to constructor', async () => {
const props: OptionalYMapClustererProps = {
features: [],
method: { render: () => [] },
};

mockComponent.props = props;

fixture.detectChanges();

// ymaps3.import is async, wait for it
await new Promise(process.nextTick);

expect(clustererConstructorMock).toHaveBeenCalledWith(expect.objectContaining(props));
});

it('should update props input after init', async () => {
fixture.detectChanges();

// ymaps3.import is async, wait for it
await new Promise(process.nextTick);

const props: OptionalYMapClustererProps = {
features: [
{
type: 'Feature',
id: '10',
geometry: {
type: 'Point',
coordinates: [50, 50],
},
},
],
};

mockComponent.props = props;

fixture.detectChanges();

expect(clustererInstance.update).toHaveBeenCalledWith(expect.objectContaining(props));
});
});
Loading
0