Skip to content

Commit

Permalink
feat: finish form loading
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad-haji committed Aug 14, 2018
1 parent 8d040e9 commit 62c54eb
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 362 deletions.
4 changes: 3 additions & 1 deletion src/app/@core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {AnalyticsService} from './utils/analytics.service';
import {HttpResponse} from '@angular/common/http';
import {PageTitleTagsService} from "./services/page-title-tags.service";
import {FormLoaderService} from "./dynamic-form/service/form-loader.service";
import {MenuLoaderService} from "./services/menu-loader.service";

const socialLinks = [
{
Expand Down Expand Up @@ -70,7 +71,8 @@ const NB_CORE_PROVIDERS = [
declarations: [],
providers: [
PageTitleTagsService,
FormLoaderService
FormLoaderService,
MenuLoaderService
]
})
export class CoreModule {
Expand Down
18 changes: 1 addition & 17 deletions src/app/@core/dynamic-form/service/form-loader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,7 @@ export class FormLoaderService implements Resolve<Observable<any>> {
* @returns {any}
*/
resolve(route: ActivatedRouteSnapshot) {
// @FIXME use logic of menu loader
return this.loadForms(route);
}
}


// return Observable.forkJoin ([
// this._mockService.dynamicTitle('resolver'),
// this._mockService.request(withError)
// ])
// .map(results => ({
// dynamicTitle: results[0],
// records : results[1]
// }))
// .catch(error => {
// this._ngAlert.push({
// message: error.message,
// type: MessageType.error
// });
// return Observable.throw(error);
// });
68 changes: 68 additions & 0 deletions src/app/@core/services/menu-loader.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Created by Mohammad.hajiaghazadeh on 8/14/2018
*/
import {Injectable} from "@angular/core";
import {ActivatedRouteSnapshot, Resolve} from "@angular/router";
import {Observable} from "rxjs/Rx";
import {HttpClient} from "@angular/common/http";
import {NbLayoutDirection, NbLayoutDirectionService} from "@nebular/theme";
import {TranslateService} from "@ngx-translate/core";
import * as _ from 'lodash';

@Injectable()
export class MenuLoaderService implements Resolve<Observable<any>> {
private JSON_DIR = '/assets/data/menus';
currentDirection: NbLayoutDirection;
currentLang: string = 'fa';
requestList: any[] = [];
ngxMenuConfig: any = {};

constructor(private httpClient: HttpClient,
private directionService: NbLayoutDirectionService,
public translate: TranslateService) {
this.currentDirection = this.directionService.getDirection();
// @FIXME: get current lang better ex: from browser lang
this.currentLang = this.translate.currentLang || 'fa';
}


loadFormByLang(lang: string, menuType: string) {
return this.httpClient.get(`${this.JSON_DIR}/${this.ngxMenuConfig[menuType][lang]}`);
}

resolve(route: ActivatedRouteSnapshot) {
this.requestList.push(this.httpClient.get('/assets/config/menu/menu.config.json'));
return Observable.forkJoin(this.requestList)
.map(results => {
return results;
})
.flatMap((formConfig) => {
this.requestList = [];
let appMenuConfig = _.first(formConfig);
for (let menu_key of Object.keys(appMenuConfig)) {
route.data.menus.forEach(_menuKey => {
if (menu_key === _menuKey) {
this.requestList.push(this.httpClient.get(`${this.JSON_DIR}/${appMenuConfig[menu_key][this.currentLang]}`).map(res => res));
}
});
}
return Observable.forkJoin(this.requestList).map(menusResult => {
let obj = {};
Object.keys(appMenuConfig).forEach((menu_key, index) => {
route.data.menus.forEach(_menuKey => {
if (menu_key === _menuKey) {
obj[menu_key] = appMenuConfig[menu_key];
obj[menu_key].menuItems = {};
obj[menu_key].menuItems[this.currentLang] = menusResult[index];
}
});
});
this.ngxMenuConfig = obj;
return obj;
});
})
.catch(error => {
return Observable.throw(error);
})
}
}
173 changes: 0 additions & 173 deletions src/app/pages/pages-menu-en.ts

This file was deleted.

0 comments on commit 62c54eb

Please sign in to comment.