Skip to content

Commit

Permalink
feat(icherche): get types allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Sep 18, 2019
1 parent bc9f062 commit 03cbc64
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 50 deletions.
4 changes: 2 additions & 2 deletions demo/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ export const environment: Environment = {
enabled: true
},
icherche: {
searchUrl: '/apis/icherche/geocode',
searchUrl: '/apis/icherche',
order: 2,
enabled: true,
params: {
limit: '8'
}
},
icherchereverse: {
searchUrl: '/apis/territoires/locate',
searchUrl: '/apis/territoires',
order: 3,
enabled: true
},
Expand Down
4 changes: 2 additions & 2 deletions packages/geo/src/lib/print/shared/print.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class PrintService {
}
const scaleText = translate.instant('igo.geo.printForm.scale');
const mapScale = map.viewController.getScale(dpi);
textProjScale += scaleText + ' ~ 1 ' + formatScale(mapScale);
textProjScale += scaleText + ': ~ 1 ' + formatScale(mapScale);
}
doc.setFont('courier');
doc.setFontSize(projScaleSize);
Expand Down Expand Up @@ -493,7 +493,7 @@ export class PrintService {
const mapScale = map.viewController.getScale(resolution);
newContext.textAlign = 'start';
newContext.fillText(
scaleText + ' ~ 1 : ' + formatScale(mapScale),
scaleText + ': ~ 1 : ' + formatScale(mapScale),
positionWProjScale,
positionHProjScale
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@
<button mat-raised-button
(click)="checkUncheckAll($event, source, setting)">{{setting.allEnabled || setting.allEnabled === undefined ? ('igo.geo.search.searchSources.settings.unselectAll' | translate): ('igo.geo.search.searchSources.settings.selectAll' | translate)}}</button>
</div>
<mat-checkbox class="igo-search-settings-radio-group"
<mat-checkbox *ngFor="let settingValue of getAvailableValues(setting)"
class="mat-menu-item"
[checked]="settingValue.enabled"
[value]="setting"
(click)="$event.stopPropagation()"
(change)="settingsValueCheckedCheckbox($event, source, setting, settingValue)"
*ngFor="let settingValue of setting.values">{{settingValue.title}}
(change)="settingsValueCheckedCheckbox($event, source, setting, settingValue)">
{{settingValue.title}}
</mat-checkbox>
</span>
</mat-menu>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

.checkAllButton{
text-align: center;
padding: 0 5px;
}

.igo-search-settings-button ::ng-deep div.mat-button-ripple-round {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {MatCheckboxChange, MatRadioChange } from '@angular/material';
import { MatCheckboxChange, MatRadioChange } from '@angular/material';

import {
Component,
Expand All @@ -9,7 +9,10 @@ import {

import { SearchSourceService } from '../shared/search-source.service';
import { SearchSource } from '../shared/sources/source';
import { SearchSourceSettings, SettingOptions } from '../shared/sources/source.interfaces';
import {
SearchSourceSettings,
SettingOptions
} from '../shared/sources/source.interfaces';

/**
* This component allows a user to select a search type yo enable. In it's
Expand All @@ -25,9 +28,7 @@ import { SearchSourceSettings, SettingOptions } from '../shared/sources/source.i
styleUrls: ['./search-settings.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})

export class SearchSettingsComponent {

/**
* Event emitted when the enabled search source changes
*/
Expand All @@ -40,7 +41,9 @@ export class SearchSettingsComponent {
* @internal
*/
getSearchSources(): SearchSource[] {
return this.searchSourceService.getSources().filter(s => s.available && s.getId() !== 'map');
return this.searchSourceService
.getSources()
.filter(s => s.available && s.getId() !== 'map');
}

/**
Expand Down Expand Up @@ -80,11 +83,7 @@ export class SearchSettingsComponent {
* Triggered when the check all / uncheck all type is clicked,
* @internal
*/
checkUncheckAll(
event,
source: SearchSource,
setting: SearchSourceSettings
) {
checkUncheckAll(event, source: SearchSource, setting: SearchSourceSettings) {
event.stopPropagation();
this.computeCheckAllBehavior(setting);
setting.values.forEach(settingValue => {
Expand All @@ -104,7 +103,7 @@ export class SearchSettingsComponent {
setting: SearchSourceSettings,
settingValue: SettingOptions
) {
setting.values.forEach( conf => {
setting.values.forEach(conf => {
if (conf.value !== settingValue.value) {
conf.enabled = !event.source.checked;
} else {
Expand All @@ -120,4 +119,7 @@ export class SearchSettingsComponent {
this.searchSourceChange.emit(source);
}

getAvailableValues(setting: SearchSourceSettings) {
return setting.values.filter(s => s.available !== false);
}
}
1 change: 0 additions & 1 deletion packages/geo/src/lib/search/shared/search.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Observable } from 'rxjs';

import { MetadataLayerOptions } from './../../metadata/shared/metadata.interface';
import { SearchSource } from './sources/source';

export interface Research {
Expand Down
17 changes: 11 additions & 6 deletions packages/geo/src/lib/search/shared/sources/icherche.providers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Injector } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { ConfigService, LanguageService } from '@igo2/core';
Expand Down Expand Up @@ -37,12 +38,14 @@ export function provideDefaultIChercheSearchResultFormatter() {
export function ichercheSearchSourceFactory(
http: HttpClient,
config: ConfigService,
formatter: IChercheSearchResultFormatter
formatter: IChercheSearchResultFormatter,
injector: Injector
) {
return new IChercheSearchSource(
http,
config.getConfig(`searchSources.${IChercheSearchSource.id}`),
formatter
formatter,
injector
);
}

Expand All @@ -54,7 +57,7 @@ export function provideIChercheSearchSource() {
provide: SearchSource,
useFactory: ichercheSearchSourceFactory,
multi: true,
deps: [HttpClient, ConfigService, IChercheSearchResultFormatter]
deps: [HttpClient, ConfigService, IChercheSearchResultFormatter, Injector]
};
}

Expand All @@ -64,11 +67,13 @@ export function provideIChercheSearchSource() {
*/
export function ichercheReverseSearchSourceFactory(
http: HttpClient,
config: ConfigService
config: ConfigService,
injector: Injector
) {
return new IChercheReverseSearchSource(
http,
config.getConfig(`searchSources.${IChercheReverseSearchSource.id}`)
config.getConfig(`searchSources.${IChercheReverseSearchSource.id}`),
injector
);
}

Expand All @@ -80,6 +85,6 @@ export function provideIChercheReverseSearchSource() {
provide: SearchSource,
useFactory: ichercheReverseSearchSourceFactory,
multi: true,
deps: [HttpClient, ConfigService]
deps: [HttpClient, ConfigService, Injector]
};
}
69 changes: 57 additions & 12 deletions packages/geo/src/lib/search/shared/sources/icherche.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable, Inject } from '@angular/core';
import { Injectable, Inject, Injector } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';

import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';

import { AuthService } from '@igo2/auth';
import { LanguageService } from '@igo2/core';

import { ObjectUtils } from '@igo2/utils';

import { FEATURE, Feature } from '../../../feature';
Expand Down Expand Up @@ -47,9 +47,19 @@ export class IChercheSearchSource extends SearchSource implements TextSearch {
private http: HttpClient,
@Inject('options') options: SearchSourceOptions,
@Inject(IChercheSearchResultFormatter)
private formatter: IChercheSearchResultFormatter
private formatter: IChercheSearchResultFormatter,
injector: Injector
) {
super(options);

const authService = injector.get(AuthService);
if (!authService) {
this.getAllowedTypes();
} else {
authService.authenticate$.subscribe(() => {
this.getAllowedTypes();
});
}
}

getId(): string {
Expand All @@ -58,8 +68,8 @@ export class IChercheSearchSource extends SearchSource implements TextSearch {

protected getDefaultOptions(): SearchSourceOptions {
return {
title: 'ICherche Québec',
searchUrl: 'https://geoegl.msp.gouv.qc.ca/apis/icherche/geocode',
title: 'iCherche',
searchUrl: 'https://geoegl.msp.gouv.qc.ca/apis/icherche',
settings: [
{
type: 'checkbox',
Expand Down Expand Up @@ -87,13 +97,14 @@ export class IChercheSearchSource extends SearchSource implements TextSearch {
title: 'Route',
value: 'routes',
enabled: false,
available: false,
hashtags: ['route']
},
{
title: 'Municipalité',
value: 'municipalites',
enabled: true,
hashtags: ['municipalité']
hashtags: ['municipalité', 'mun']
},
// {
// title: 'Ancienne municipalité',
Expand Down Expand Up @@ -127,6 +138,7 @@ export class IChercheSearchSource extends SearchSource implements TextSearch {
title: 'Entreprise',
value: 'entreprises',
enabled: false,
available: false,
hashtags: ['entreprise']
}
]
Expand Down Expand Up @@ -212,7 +224,7 @@ export class IChercheSearchSource extends SearchSource implements TextSearch {
if (!params.get('type').length) {
return of([]);
}
return this.http.get(this.searchUrl, { params }).pipe(
return this.http.get(`${this.searchUrl}/geocode`, { params }).pipe(
map((response: IChercheResponse) => this.extractResults(response)),
catchError(err => {
err.error.toDisplay = true;
Expand All @@ -222,6 +234,17 @@ export class IChercheSearchSource extends SearchSource implements TextSearch {
);
}

private getAllowedTypes() {
return this.http
.get(`${this.searchUrl}/types`)
.subscribe((types: string[]) => {
const typeSetting = this.settings.find(s => s.name === 'type');
typeSetting.values.forEach(v => {
v.available = types.indexOf(v.value as string) > -1;
});
});
}

private computeRequestParams(
term: string,
options: TextSearchOptions
Expand Down Expand Up @@ -348,13 +371,23 @@ export class IChercheReverseSearchSource extends SearchSource
implements ReverseSearch {
static id = 'icherchereverse';
static type = FEATURE;
static propertiesBlacklist: string[] = ['doc_type'];
static propertiesBlacklist: string[] = [];

constructor(
private http: HttpClient,
@Inject('options') options: SearchSourceOptions
@Inject('options') options: SearchSourceOptions,
injector: Injector
) {
super(options);

const authService = injector.get(AuthService);
if (!authService) {
this.getAllowedTypes();
} else {
authService.authenticate$.subscribe(() => {
this.getAllowedTypes();
});
}
}

getId(): string {
Expand All @@ -364,7 +397,7 @@ export class IChercheReverseSearchSource extends SearchSource
protected getDefaultOptions(): SearchSourceOptions {
return {
title: 'Territoire (Géocodage inversé)',
searchUrl: 'https://geoegl.msp.gouv.qc.ca/apis/territoires/locate',
searchUrl: 'https://geoegl.msp.gouv.qc.ca/apis/territoires',
settings: [
{
type: 'checkbox',
Expand All @@ -379,7 +412,8 @@ export class IChercheReverseSearchSource extends SearchSource
{
title: 'Route',
value: 'routes',
enabled: false
enabled: false,
available: false
},
{
title: 'Arrondissement',
Expand Down Expand Up @@ -418,13 +452,24 @@ export class IChercheReverseSearchSource extends SearchSource
options?: ReverseSearchOptions
): Observable<SearchResult<Feature>[]> {
const params = this.computeRequestParams(lonLat, options || {});
return this.http.get(this.searchUrl, { params }).pipe(
return this.http.get(`${this.searchUrl}/locate`, { params }).pipe(
map((response: IChercheReverseResponse) => {
return this.extractResults(response);
})
);
}

private getAllowedTypes() {
return this.http
.get(`${this.searchUrl}/types`)
.subscribe((types: string[]) => {
const typeSetting = this.settings.find(s => s.name === 'type');
typeSetting.values.forEach(v => {
v.available = types.indexOf(v.value as string) > -1;
});
});
}

private computeRequestParams(
lonLat: [number, number],
options?: ReverseSearchOptions
Expand Down
Loading

0 comments on commit 03cbc64

Please sign in to comment.