-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(import-export): imported file can be styled from a style list (#571
- Loading branch information
Showing
12 changed files
with
251 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './style-list.service'; | ||
export * from './style-list.interface'; |
4 changes: 4 additions & 0 deletions
4
packages/geo/src/lib/import-export/style-list/style-list.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface StyleListOptions { | ||
default?: { [key: string]: any }; | ||
path?: string; | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/geo/src/lib/import-export/style-list/style-list.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { NgModule, ModuleWithProviders } from '@angular/core'; | ||
import { provideStyleListOptions, provideStyleListLoader } from './style-list.provider'; | ||
|
||
@NgModule({ | ||
imports: [], | ||
declarations: [], | ||
exports: [] | ||
}) | ||
export class IgoStyleListModule { | ||
static forRoot(): ModuleWithProviders { | ||
return { | ||
ngModule: IgoStyleListModule, | ||
providers: [provideStyleListOptions({}), provideStyleListLoader()] | ||
}; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/geo/src/lib/import-export/style-list/style-list.provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { APP_INITIALIZER, InjectionToken } from '@angular/core'; | ||
|
||
import { StyleListService } from './style-list.service'; | ||
import { StyleListOptions } from './style-list.interface'; | ||
|
||
export let STYLELIST_OPTIONS = new InjectionToken<StyleListOptions>('styleListOptions'); | ||
|
||
export function provideStyleListOptions(options: StyleListOptions) { | ||
return { | ||
provide: STYLELIST_OPTIONS, | ||
useValue: options | ||
}; | ||
} | ||
|
||
export function styleListFactory( | ||
styleListService: StyleListService, | ||
options: StyleListOptions | ||
) { | ||
return () => styleListService.load(options); | ||
} | ||
|
||
export function provideStyleListLoader() { | ||
return { | ||
provide: APP_INITIALIZER, | ||
useFactory: styleListFactory, | ||
multi: true, | ||
deps: [StyleListService, STYLELIST_OPTIONS] | ||
}; | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/geo/src/lib/import-export/style-list/style-list.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { TestBed, inject } from '@angular/core/testing'; | ||
import { HttpClientModule } from '@angular/common/http'; | ||
|
||
import { StyleListService } from './style-list.service'; | ||
|
||
describe('StyleListService', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientModule], | ||
providers: [StyleListService] | ||
}); | ||
}); | ||
|
||
it( | ||
'should ...', | ||
inject([StyleListService], (service: StyleListService) => { | ||
expect(service).toBeTruthy(); | ||
}) | ||
); | ||
}); |
53 changes: 53 additions & 0 deletions
53
packages/geo/src/lib/import-export/style-list/style-list.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Injectable, Injector } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { throwError } from 'rxjs'; | ||
import { catchError } from 'rxjs/operators'; | ||
|
||
import { ObjectUtils } from '@igo2/utils'; | ||
|
||
import { StyleListOptions } from './style-list.interface'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class StyleListService { | ||
private styleList: object = {}; | ||
|
||
constructor(private injector: Injector) {} | ||
|
||
/** | ||
* Use to get the data found in styleList file | ||
*/ | ||
public getStyleList(key: string): any { | ||
return ObjectUtils.resolve(this.styleList, key); | ||
} | ||
|
||
/** | ||
* This method loads "[path]" to get all styleList's variables | ||
*/ | ||
public load(options: StyleListOptions) { | ||
const baseStyleList = options.default || {}; | ||
if (!options.path) { | ||
this.styleList = baseStyleList; | ||
return true; | ||
} | ||
|
||
const http = this.injector.get(HttpClient); | ||
|
||
return new Promise((resolve, _reject) => { | ||
http | ||
.get(options.path) | ||
.pipe( | ||
catchError((error: any): any => { | ||
console.log(`StyleList file ${options.path} could not be read`); | ||
resolve(true); | ||
return throwError(error.error || 'Server error'); | ||
}) | ||
) | ||
.subscribe(styleListResponse => { | ||
this.styleList = ObjectUtils.mergeDeep(baseStyleList, styleListResponse); | ||
resolve(true); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters