Guest User

Untitled

a guest
Mar 24th, 2017
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { DOCUMENT } from '@angular/platform-browser';
  2. import { Component, Inject, ViewChild, OnInit, AfterViewInit } from '@angular/core';
  3. import { FormControl } from '@angular/forms';
  4. import { MdSidenav, MdDialog } from '@angular/material';
  5. import { TreeComponent, TreeNode, TREE_ACTIONS, IActionMapping } from 'angular2-tree-component';
  6. import { Accordion } from 'primeng/primeng';
  7. import { Ol3MapDirective } from './directives/ol3-map.directive';
  8. import { OwlCarouselDirective } from './directives/owl-carousel.directive';
  9. import { HttpRequestHelper } from './shared/http-request-helper';
  10. import { MeasureDialogComponent } from './components/measure-dialog/measure-dialog.component';
  11. import { MessageDialogComponent } from './components/message-dialog/message-dialog.component';
  12.  
  13. import { sprintf, vsprintf } from 'sprintf-js';
  14.  
  15. import { Observable } from 'rxjs/Observable';
  16. import 'rxjs/add/operator/toPromise';
  17.  
  18. declare var jQuery: any;
  19. declare var screenfull: any;
  20. declare var app: any;
  21.  
  22. class Node {
  23.   id: number;
  24.   name: string;
  25.   children?: Node[];
  26.   url?: string;
  27.   urls?: string[];
  28.   isSelected?: boolean;
  29.   extendedSearch: number;
  30.   iconFileId: string;
  31.   options?: {
  32.     visibility: boolean;
  33.   };
  34.   params?: {
  35.     CQL_FILTER: string;
  36.     numZoomLevels: number;
  37.   };
  38.   order: number;
  39. }
  40.  
  41. const actionMapping: IActionMapping = {
  42.   mouse: {
  43.     click: TREE_ACTIONS.TOGGLE_EXPANDED
  44.   }
  45. };
  46.  
  47. @Component({
  48.   selector: 'app-root',
  49.   templateUrl: './app.component.html',
  50.   styleUrls: ['./app.component.scss']
  51. })
  52. export class AppComponent implements OnInit, AfterViewInit {
  53.   @ViewChild('sidenav')
  54.   private _sidenav: MdSidenav;
  55.  
  56.   @ViewChild('infoSidenav')
  57.   private _infoSidenav: MdSidenav;
  58.  
  59.   @ViewChild(OwlCarouselDirective)
  60.   private _owlCarousel: OwlCarouselDirective;
  61.  
  62.   @ViewChild(Accordion)
  63.   private _extendedSearchAccordion: Accordion;
  64.  
  65.   tabGroupSelectedIndex = 0;
  66.  
  67.   layersInfos$: Promise<any>;
  68.   searchResults$: Promise<any>;
  69.   extendedSearch$: Promise<any>;
  70.   extendedSearchResult$: Promise<any>;
  71.  
  72.   extendedSearchModel: { [layerId: string]: Object } = {};
  73.  
  74.   measureInfo: {
  75.     type: string,
  76.     text: string
  77.   };
  78.   measureInfoTitles: {
  79.     [type: string]: string
  80.   } = {
  81.     'Point': 'წერტილის კოორდინატები',
  82.     'LineString': 'სრული სიგრძე',
  83.     'Polygon': 'სრული ფართობი და პერიმეტრი'
  84.   };
  85.  
  86.   baseLayer: ol.layer.Tile;
  87.   baseLayerSwitcherVisible = false;
  88.  
  89.   baseLayerNodes$: Promise<Node[]>;
  90.   overlayLayerNodes$: Promise<Node[]>;
  91.  
  92.   @ViewChild(TreeComponent)
  93.   private _tree: TreeComponent;
  94.  
  95.   treeOptions = {
  96.     actionMapping
  97.   };
  98.  
  99.   @ViewChild(Ol3MapDirective)
  100.   private _ol3Map: Ol3MapDirective;
  101.  
  102.   private _drawingLayer = new ol.layer.Vector({
  103.     source: new ol.source.Vector({ wrapX: false }),
  104.     style: new ol.style.Style({
  105.       fill: new ol.style.Fill({
  106.         color: 'rgba(255, 255, 255, 0.2)'
  107.       }),
  108.       stroke: new ol.style.Stroke({
  109.         color: '#ffcc33',
  110.         width: 2
  111.       }),
  112.       image: new ol.style.Circle({
  113.         radius: 7,
  114.         fill: new ol.style.Fill({
  115.           color: '#ffcc33'
  116.         })
  117.       })
  118.     })
  119.   });
  120.   private _drawingInteraction: ol.interaction.Draw;
  121.  
  122.   ol3MapOptions: olx.MapOptions = {
  123.     layers: [
  124.       this._drawingLayer
  125.     ],
  126.     controls: ol.control.defaults({
  127.       rotate: false,
  128.       attribution: false,
  129.       zoom: false
  130.     }).extend([
  131.       new ol.control.Zoom({ zoomInLabel: '', zoomOutLabel: '' }),
  132.       new app.MapToolsControl({
  133.         actions: [{
  134.           icon: 'info',
  135.           title: 'ფენების ინფო',
  136.           handle: () => {
  137.             let interaction = this._addInteraction('Point');
  138.             interaction.on('drawstart', () => {
  139.               this._drawingLayer.getSource().clear();
  140.             });
  141.             interaction.on('drawend', e => {
  142.               this._sidenav.close().then(() => {
  143.                 let geometry = e.feature.getGeometry(),
  144.                   coordinates3857 = geometry.getCoordinates(),
  145.                   coordinates4326 = ol.proj.transform(coordinates3857, 'EPSG:3857', 'EPSG:4326'),
  146.                   layerIds = Object.keys(this.checkboxes).filter(id => this.checkboxes[id].isChecked && !!this.layers[id]);
  147.                 this._animateTo(coordinates3857, 18);
  148.                 this._infoSidenav.close().then(() => {
  149.                   if (layerIds.length > 0) {
  150.                     let requestBody = {
  151.                       sessionId: '12345abcd',
  152.                       clientToken: 'aceeb1c1-b378-439e-821a-843abcabaafc',
  153.                       params: {
  154.                         orgId: 1,
  155.                         viewLevel: 1,
  156.                         x: coordinates4326[0],
  157.                         y: coordinates4326[1],
  158.                         lrsIds: layerIds.join(',')
  159.                       }
  160.                     };
  161.                     this.layersInfos$ = this._http.post2('http://192.168.157.38:3000/proxy/tbilisimap-core/api/managelayersws/getLayersInfos', requestBody)
  162.                       .toPromise().then(response => response.data);
  163.                     this.layersInfos$.then(data => {
  164.                       if (data.length > 0) {
  165.                         this._infoSidenav.open();
  166.                         this._infoSidenav.onClose.first().subscribe(() => {
  167.                           this._drawingLayer.getSource().clear();
  168.                         });
  169.                       }
  170.                     });
  171.                   } else {
  172.                     let dialog = this._dialog.open(MessageDialogComponent, {
  173.                       data: {
  174.                         message: 'ინფორმაციის ძებნისთვის გთხოვთ ჩართოთ მინიმუმ ერთი დამფარავი ფენა.'
  175.                       }
  176.                     });
  177.  
  178.                     dialog.afterClosed().subscribe(() => {
  179.                       this._drawingLayer.getSource().clear();
  180.                     });
  181.                   }
  182.                 });
  183.               });
  184.             });
  185.           }
  186.         }, {
  187.           icon: 'fullscreen',
  188.           title: 'მთლიანი ეკრანი',
  189.           handle: () => {
  190.             screenfull.toggle(this._doc.body);
  191.           }
  192.         }, {
  193.           icon: 'fullscreen_exit',
  194.           title: 'მთლიანი ეკრანიდან გასვლა',
  195.           handle: () => {
  196.             screenfull.toggle(this._doc.body);
  197.           }
  198.         }, {
  199.           icon: 'place',
  200.           title: 'რუკის საწყისი ხედი',
  201.           handle: () => {
  202.             this._animateTo(ol.proj.transform([44.7805792, 41.7128489], 'EPSG:4326', 'EPSG:3857'));
  203.           }
  204.         }, {
  205.           icon: 'change_history',
  206.           title: 'გაზომვის ხელსაწყოები',
  207.           actions: [{
  208.             icon: 'timeline',
  209.             title: 'სიგრძის გაზომვა',
  210.             handle: () => {
  211.               this._addMesaureTool('LineString', e => {
  212.                 let geometry = e.feature.getGeometry();
  213.                 this._ol3Map.map.getView().fit(geometry.getExtent(), this._ol3Map.map.getSize());
  214.                 // this._dialog.open(MeasureDialogComponent, { data: e });
  215.                 // =========================
  216.                 let type = geometry.getType(),
  217.                   text = sprintf('%d მეტრი',
  218.                     MeasureDialogComponent.getLineLength(geometry));
  219.                 this._infoSidenav.close().then(() => {
  220.                   this.measureInfo = { type, text };
  221.                   this._infoSidenav.open();
  222.                 });
  223.               });
  224.             }
  225.           }, {
  226.             icon: 'signal_cellular_null',
  227.             title: 'ფართობის გაზომვა',
  228.             handle: () => {
  229.               this._addMesaureTool('Polygon', e => {
  230.                 let geometry = e.feature.getGeometry();
  231.                 // let g: ol.geom.Polygon = e.feature.getGeometry();
  232.                 // g.getLinearRing(0).getFirstCoordinate();
  233.                 // this._animateTo(e.feature.getGeometry().getFirstCoordinate(), 16);
  234.                 this._ol3Map.map.getView().fit(geometry.getExtent(), this._ol3Map.map.getSize());
  235.                 // this._dialog.open(MeasureDialogComponent, { data: e });
  236.                 // =========================
  237.                 let type = geometry.getType(),
  238.                   text = sprintf('ფართობი: %.2f კვადრატული მეტრი, პერიმეტრი: %.2f მეტრი',
  239.                     MeasureDialogComponent.getPolygonArea(geometry),
  240.                     MeasureDialogComponent.getPolygonLength(geometry));
  241.                 this._infoSidenav.close().then(() => {
  242.                   this.measureInfo = { type, text };
  243.                   this._infoSidenav.open();
  244.                 });
  245.               });
  246.             }
  247.           }, {
  248.             icon: 'place',
  249.             title: 'მდებარეობის კოორდინატები',
  250.             handle: () => {
  251.               this._addMesaureTool('Point', e => {
  252.                 let geometry = e.feature.getGeometry();
  253.                 this._animateTo(geometry.getCoordinates(), 16);
  254.                 // this._dialog.open(MeasureDialogComponent, { data: e });
  255.                 // =========================
  256.                 let type = geometry.getType(),
  257.                   text = vsprintf(`X=%s მეტრი Y=%s მეტრი`,
  258.                     geometry.getCoordinates().map(c => parseFloat(c).toFixed(0)));
  259.                 this._infoSidenav.close().then(() => {
  260.                   this.measureInfo = { type, text };
  261.                   this._infoSidenav.open();
  262.                 });
  263.               });
  264.             }
  265.           }]
  266.         }, {
  267.           icon: 'clear',
  268.           title: 'გრაფიკის გასუფთავება',
  269.           handle: () => {
  270.             this._removeInteraction();
  271.             this._drawingLayer.getSource().clear();
  272.             this._infoSidenav.close();
  273.           }
  274.         }, {
  275.           icon: 'pan_tool',
  276.           title: 'რუკის გადაადგილება',
  277.           handle: () => {
  278.             this._removeInteraction();
  279.           }
  280.         }]
  281.       }),
  282.       new ol.control.MousePosition({
  283.         projection: 'EPSG:4326',
  284.         coordinateFormat: ol.coordinate.createStringXY(4)
  285.       })
  286.     ]),
  287.     view: new ol.View({
  288.       zoom: 14,
  289.       maxZoom: 22,
  290.       center: ol.proj.transform([44.7805792, 41.7128489], 'EPSG:4326', 'EPSG:3857'),
  291.     })
  292.   };
  293.  
  294.   bxSliderOptions = {
  295.     pager: false,
  296.     infiniteLoop: false
  297.   };
  298.  
  299.   layers: {
  300.     [id: number]: ol.layer.Tile;
  301.   } = {};
  302.  
  303.   checkboxes: {
  304.     [id: number]: {
  305.       isChecked: boolean;
  306.       isIndeterminate: boolean;
  307.     };
  308.   } = {};
  309.  
  310.   options: string[] = [];
  311.   filteredOptions: Observable<string[]>;
  312.   myControl = new FormControl();
  313.  
  314.   filterOptions(val: string) {
  315.     return this.options.filter(option => new RegExp(val, 'gi').test(option));
  316.   }
  317.  
  318.   constructor(
  319.     @Inject(DOCUMENT) private _doc: any,
  320.     private _dialog: MdDialog,
  321.     private _http: HttpRequestHelper
  322.   ) {
  323.     this._drawingLayer.setZIndex(1);
  324.  
  325.     // Possible useful example for the open and closeAll events.
  326.     // Adding a class to the body if a dialog opens and
  327.     // removing it after all open dialogs are closed
  328.     _dialog.afterOpen.subscribe(() => {
  329.       _doc.body.classList.add('no-scroll');
  330.     });
  331.     _dialog.afterAllClosed.subscribe(() => {
  332.       _doc.body.classList.remove('no-scroll');
  333.     });
  334.   }
  335.  
  336.   ngOnInit() {
  337.     const randomId = () => Math.floor(new Date().valueOf() * Math.random());
  338.     const parseLayer = layer => ({
  339.       id: layer.lrId,
  340.       name: layer.name,
  341.       order: layer.lrOrder,
  342.       url: layer.sourceUrl,
  343.       urls: layer.sourceUrls ? layer.sourceUrls.split(',') : layer.sourceUrls,
  344.       isSelected: !!layer.isSelected,
  345.       extendedSearch: layer.extendedSearch,
  346.       iconFileId: layer.iconFileId,
  347.       options: Object.assign({}, layer.optionsJson),
  348.       params: Object.assign(layer.cqlFilterOl3 || {}, layer.paramsJson) // eval(`(${layer.cql})`) ||
  349.     });
  350.     const parseGroup = group => ({
  351.       id: randomId(),
  352.       name: group.groupName,
  353.       children: group.groups.map(parseGroup)
  354.         .concat(group.lrs.map(parseLayer))
  355.     });
  356.  
  357.     let requestBody = {
  358.       clientToken: 'aceeb1c1-b378-439e-821a-843abcabaafc',
  359.       params: {
  360.         orgId: 1,
  361.         viewLevel: 1
  362.       }
  363.     };
  364.     let layersObservable = this._http.post2('http://192.168.157.38:3000/proxy/tbilisimap-core/api/managelayersws/getLayersInGroups', requestBody)
  365.       .map(response => response.data.map(parseGroup))
  366.       .toPromise();
  367.  
  368.     this.baseLayerNodes$ = layersObservable
  369.       .then(response => response[0].children.filter(v => !v.params.type));
  370.     this.overlayLayerNodes$ = layersObservable
  371.       .then(response => response[1].children);
  372.  
  373.     this.myControl.valueChanges.subscribe(value => {
  374.       this._tree.treeModel.filterNodes(node => node.data.name.indexOf(value) >= 0);
  375.     });
  376.   }
  377.  
  378.   ngAfterViewInit() {
  379.     jQuery('.mat-tab-body, .sidenav-info .cdk-focus-trap-content').mCustomScrollbar({ theme: 'dark-thin' });
  380.  
  381.     this._sidenav.onOpenStart.subscribe(() => this._infoSidenav.close());
  382.  
  383.     this._sidenav.onOpenStart.subscribe(() => this._doc.body.classList.add('sidenav-open'));
  384.     this._sidenav.onCloseStart.subscribe(() => this._doc.body.classList.remove('sidenav-open'));
  385.  
  386.     this._infoSidenav.onOpenStart.subscribe(() => this._doc.body.classList.add('sidenav-open'));
  387.     this._infoSidenav.onCloseStart.subscribe(() => this._doc.body.classList.remove('sidenav-open'));
  388.  
  389.     this._sidenav.onOpenStart.subscribe(() => {
  390.       this.baseLayerSwitcherVisible = false;
  391.     });
  392.  
  393.     // this._infoSidenav.onOpenStart.subscribe(() => this._sidenav.close());
  394.  
  395.     this._infoSidenav.onClose.subscribe(() => {
  396.       this.measureInfo = undefined;
  397.       this.layersInfos$ = undefined;
  398.     });
  399.  
  400.     this._ol3Map.map.updateSize(); // http://gis.stackexchange.com/questions/124326/openlayers-dynamic-viewport-width-and-height-assignment
  401.  
  402.     this._ol3Map.map.on('click', () => {
  403.       this.baseLayerSwitcherVisible = false;
  404.     });
  405.  
  406.     this._ol3Map.map.on('moveend', () => {
  407.       let format = new ol.format.WKT(),
  408.         extent = this._ol3Map.map.getView().calculateExtent(this._ol3Map.map.getSize()),
  409.         wkt = format.writeGeometry(ol.geom.Polygon.fromExtent(extent));
  410.       let requestBody = {
  411.         sessionId: '12345abcd',
  412.         clientToken: 'aceeb1c1-b378-439e-821a-843abcabaafc',
  413.         params: {
  414.           bbox: wkt,
  415.           orgId: 1
  416.         }
  417.       };
  418.       this._http.post2('http://192.168.157.38:3000/proxy/tbilisimap-core/api/managelayersws/getLrUserObjsImgs', requestBody)
  419.         .toPromise().then(response => {
  420.           if (response.success) {
  421.             // objVal
  422.             // LayersController/downloadPicture.do?fileId={0}
  423.           }
  424.         });
  425.     });
  426.  
  427.     this.baseLayerNodes$.then(nodes => {
  428.       let flattenNodes = this._flatten(nodes);
  429.  
  430.       flattenNodes.sort((a, b) => a.order - b.order)
  431.         .forEach(node => {
  432.           if (node.url) {
  433.             this.layers[node.id] = new ol.layer.Tile({
  434.               visible: node.isSelected,
  435.               source: new ol.source.OSM({
  436.                 url: node.url.replace(/\$(\{[xyz]\})/g, '$1'),
  437.                 maxZoom: node.params.numZoomLevels,
  438.                 crossOrigin: null
  439.               })
  440.             });
  441.             if (node.isSelected) {
  442.               this.baseLayer = this.layers[node.id];
  443.             }
  444.             this._ol3Map.map.addLayer(this.layers[node.id]);
  445.           };
  446.         });
  447.     })
  448.  
  449.     this.overlayLayerNodes$.then(nodes => {
  450.       let flattenNodes = this._flatten(nodes);
  451.  
  452.       let extendedSearchPromises = flattenNodes.filter(node => node.extendedSearch)
  453.         .map(node => new Promise(resolve => {
  454.           let requestBody = {
  455.             sessionId: '12345abcd',
  456.             clientToken: 'aceeb1c1-b378-439e-821a-843abcabaafc',
  457.             params: {
  458.               orgId: 1,
  459.               useType: 7,
  460.               lrId: node.id
  461.             }
  462.           };
  463.           this._http.post2('http://192.168.157.38:3000/proxy/tbilisimap-core/api/managelayersws/getLayerTableColumnsConfig', requestBody)
  464.             .toPromise().then(response => {
  465.               let columnsConfigPromises = response.data.map(value => {
  466.                 if (value.dataType === 61) {
  467.                   let requestBody = {
  468.                     sessionId: '12345abcd',
  469.                     clientToken: 'aceeb1c1-b378-439e-821a-843abcabaafc',
  470.                     params: {
  471.                       orgId: 1,
  472.                       libId: value.libType
  473.                     }
  474.                   };
  475.                   return this._http.post2('http://192.168.157.38:3000/proxy/tbilisimap-core/api/appws/getLibObjects', requestBody)
  476.                     .toPromise().then(response => Object.assign({ libObjects: response.data }, value));
  477.                 } else {
  478.                   return new Promise(resolve => resolve(value));
  479.                 }
  480.               });
  481.  
  482.               Promise.all(columnsConfigPromises).then(columnsConfig => {
  483.                 resolve({ id: node.id, name: node.name, columnsConfig });
  484.               });
  485.             });
  486.         }));
  487.  
  488.       this.extendedSearch$ = Promise.all(extendedSearchPromises);
  489.  
  490.       this.extendedSearch$.then((results: any[]) => {
  491.         results.forEach(result => {
  492.           this.extendedSearchModel[result.id] = {};
  493.           result.columnsConfig.filter(config => config.configId).forEach(config => {
  494.             Object.assign(this.extendedSearchModel[result.id], {
  495.               [config.configId]: Object.assign({}, config)
  496.             });
  497.           });
  498.         });
  499.       });
  500.  
  501.       flattenNodes.sort((a, b) => a.name > b.name ? 1 : (a.name < b.name ? -1 : 0))
  502.         .forEach(node => this.options.push(node.name));
  503.  
  504.       flattenNodes.sort((a, b) => a.order - b.order)
  505.         .forEach(node => {
  506.           this.checkboxes[node.id] = { isChecked: false, isIndeterminate: false };
  507.           if (node.url || node.urls) {
  508.             this.layers[node.id] = new ol.layer.Tile({
  509.               visible: node.options.visibility || false,
  510.               source: new ol.source.TileWMS({
  511.                 url: node.url,
  512.                 urls: node.urls,
  513.                 projection: 'EPSG:3857',
  514.                 params: Object.assign({ 'FORMAT': 'image/png' }, node.params)
  515.               })
  516.             });
  517.             this.layers[node.id].set('name', node.name);
  518.           }
  519.         });
  520.  
  521.       this.filteredOptions = this.myControl.valueChanges
  522.         .startWith(null)
  523.         .map(val => val ? this.filterOptions(val) : this.options.slice());
  524.     });
  525.   }
  526.  
  527.   private _flatten(nodes: Node[]) {
  528.     let flattenArray: Node[] = [];
  529.     nodes.forEach(node => flattenArray.push(node, ...this._flatten(node.children || [])));
  530.     return flattenArray;
  531.   }
  532.  
  533.   private _addInteraction(type: ol.geom.GeometryType) {
  534.     this._removeInteraction();
  535.     let source = this._drawingLayer.getSource();
  536.     this._drawingInteraction = new ol.interaction.Draw({ type, source });
  537.     this._ol3Map.map.addInteraction(this._drawingInteraction);
  538.     return this._drawingInteraction;
  539.   }
  540.  
  541.   private _removeInteraction() {
  542.     if (this._drawingInteraction) {
  543.       this._ol3Map.map.removeInteraction(this._drawingInteraction);
  544.       this._drawingInteraction = null;
  545.     }
  546.   }
  547.  
  548.   private _addMesaureTool(type: ol.geom.GeometryType, onDrawEnd: Function) {
  549.     const clear = () => {
  550.       this._infoSidenav.close();
  551.       this._drawingLayer.getSource().clear();
  552.     };
  553.     clear();
  554.     let interaction = this._addInteraction(type);
  555.     interaction.on('drawstart', clear);
  556.     interaction.on('drawend', onDrawEnd);
  557.     return interaction;
  558.   }
  559.  
  560.   private _animateTo(center: ol.Coordinate, zoom: number = 14) {
  561.     this._ol3Map.map.getView().animate({ center, zoom });
  562.   }
  563.  
  564.   drawWkt(geometryWkt: string) {
  565.     this._drawingLayer.getSource().clear();
  566.     let format = new ol.format.WKT(),
  567.       geometry = format.readGeometry(geometryWkt.replace(/,\)$/g, ')')),
  568.       feature = new ol.Feature({ geometry });
  569.     this._drawingLayer.getSource().addFeature(feature);
  570.     if (geometry.getType() === 'Point') {
  571.       this._animateTo(geometry['getCoordinates'](), 18);
  572.     } else {
  573.       this._ol3Map.map.getView().fit(feature.getGeometry().getExtent(), this._ol3Map.map.getSize());
  574.     }
  575.   }
  576.  
  577.   search(event) {
  578.     let requestBody = {
  579.       sessionId: "123456abcdefg",
  580.       params: {
  581.         orgId: 1,
  582.         searchText: event.query,
  583.         page: 0,
  584.         size: 20
  585.       }
  586.     };
  587.     this.searchResults$ = this._http.post2('http://192.168.157.38:3000/proxy/tbilisimap-core/api/mapsearchws/search', requestBody)
  588.       .toPromise().then(response => response.data || []);
  589.   }
  590.  
  591.   extendedSearch(event: Event, layer: Node) {
  592.     event.stopPropagation();
  593.     this.tabGroupSelectedIndex = 3;
  594.     this._extendedSearchAccordion.tabs.forEach(tab => {
  595.       tab.selected = false;
  596.     });
  597.     setTimeout(() => {
  598.       this._extendedSearchAccordion.tabs.forEach(tab => {
  599.         if (tab.header === layer.name) {
  600.           tab.selected = true;
  601.         } else {
  602.           tab.selected = false;
  603.         }
  604.       });
  605.     }, 400);
  606.   }
  607.  
  608.   doExtendedSearch(
  609.     layerId: number,
  610.     recordsFrom: number = 1,
  611.     recordsTo: number = 10
  612.   ) {
  613.     const snake2Camel = (value: string) => value.toLowerCase().replace(/_\w/g, (m: string) => m[1].toUpperCase()),
  614.       transformFn = value => field => {
  615.         let obj = Object.assign({ columnValue: value[snake2Camel(field.columnName)] }, field);
  616.         if (field.dataType === 61) {
  617.           let libObject = field.libObjects.find(o => o.objId === obj.columnValue);
  618.           if (libObject) {
  619.             Object.assign(obj, { columnValue: libObject.objId });
  620.           }
  621.         }
  622.         return obj;
  623.       };
  624.  
  625.     if (this.extendedSearchModel[layerId]) {
  626.       let fields = Object.keys(this.extendedSearchModel[layerId])
  627.         .map(key => this.extendedSearchModel[layerId][key])
  628.         .sort((a, b) => a.configOrder - b.configOrder);
  629.  
  630.       let fieldWithValueList = fields.filter(field => field.fieldValue)
  631.         .map(field => ({ fieldName: field.columnName, fieldType: field.dbDataType, fieldValue: field.fieldValue }));
  632.  
  633.       let requestBody = {
  634.         sessionId: '12345abcd',
  635.         clientToken: 'aceeb1c1-b378-439e-821a-843abcabaafc',
  636.         params: {
  637.           orgId: 1,
  638.           lrId: layerId,
  639.           fieldWithValueList,
  640.           recordsFrom,
  641.           recordsTo
  642.         }
  643.       };
  644.       this.extendedSearchResult$ = this._http.post2('http://192.168.157.38:3000/proxy/tbilisimap-core/api/mapsearchws/searchLr', requestBody)
  645.         .toPromise().then(response => response.data.map(value => fields.map(transformFn(value))));
  646.     }
  647.   }
  648.  
  649.   clearExtendedSearchModel(layerId: number) {
  650.     if (this.extendedSearchModel[layerId]) {
  651.       Object.keys(this.extendedSearchModel[layerId]).forEach(configId => {
  652.         delete this.extendedSearchModel[layerId][configId].fieldValue;
  653.       });
  654.     }
  655.   }
  656.  
  657.   toggleBaseLayerSwitcherVisibility() {
  658.     this.baseLayerSwitcherVisible = !this.baseLayerSwitcherVisible;
  659.   }
  660.  
  661.   onSelectSearch(event) {
  662.     this._sidenav.close().then(() => {
  663.       this.drawWkt(event.geometryWkt);
  664.     });
  665.   }
  666.  
  667.   onClickCheckbox(event: Event) {
  668.     event.stopPropagation();
  669.   }
  670.  
  671.   setBaseLayer(node: Node) {
  672.     if (this.baseLayer) {
  673.       this.baseLayer.setVisible(false);
  674.     }
  675.     this.baseLayer = this.layers[node.id];
  676.     this.baseLayer.setVisible(true);
  677.   }
  678.  
  679.   onChangeCheckbox(node: TreeNode) {
  680.     const setChecked = (node: TreeNode, isChecked: boolean) => {
  681.       this.checkboxes[node.id].isChecked = isChecked;
  682.       this.checkboxes[node.id].isIndeterminate = false;
  683.  
  684.       if (this.layers[node.id]) {
  685.         if (isChecked) {
  686.           this._ol3Map.map.addLayer(this.layers[node.id]);
  687.         } else {
  688.           this._ol3Map.map.removeLayer(this.layers[node.id]);
  689.         }
  690.         this.layers[node.id].setVisible(isChecked);
  691.       }
  692.  
  693.       let children = node.children || [];
  694.       children.forEach(child => setChecked(child, isChecked));
  695.     };
  696.  
  697.     setChecked(node, !this.checkboxes[node.id].isChecked);
  698.  
  699.     let parent = node;
  700.     while (!parent.isRoot) {
  701.       parent = parent.parent;
  702.  
  703.       let checked = parent.children.filter(child => this.checkboxes[child.id].isChecked),
  704.         indeterminate = parent.children.filter(child => this.checkboxes[child.id].isIndeterminate);
  705.  
  706.       let isParentChecked = false,
  707.         isParentIndeterminate = false;
  708.  
  709.       if (checked.length === parent.children.length) {
  710.         isParentChecked = true;
  711.       } else if (checked.length > 0 || indeterminate.length > 0) {
  712.         isParentIndeterminate = true;
  713.       }
  714.  
  715.       this.checkboxes[parent.id].isChecked = isParentChecked;
  716.       this.checkboxes[parent.id].isIndeterminate = isParentIndeterminate;
  717.     }
  718.   }
  719. }
Advertisement
Add Comment
Please, Sign In to add comment