Merge "refactor devicemanager-core"
[ccsdk/features.git] / sdnr / wt / odlux / apps / networkMapApp / src / services / mapImagesService.ts
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 import * as mapboxgl from 'mapbox-gl';
20 import apartment from '../../icons/apartment.png';
21 import datacenter from '../../icons/datacenter.png';
22 import factory from '../../icons/factory.png';
23 import lamp from '../../icons/lamp.png';
24 import datacenterred from '../../icons/datacenterred.png';
25 import factoryred from '../../icons/factoryred.png';
26 import lampred from '../../icons/lampred.png';
27
28
29 type ImagesLoaded = (allImagesLoaded: boolean) => void;
30 type MapImages = {name: string, url: string}
31
32 export const Images : MapImages[]  = [
33     {name: 'data-center', url: datacenter}, 
34     {name: 'house', url: apartment}, 
35     {name: 'factory', url: factory},
36     {name: 'lamp', url: lamp},
37     {name: 'data-center-red', url: datacenterred}, 
38     {name: 'factory-red', url: factoryred},
39     {name: 'lamp-red', url: lampred},
40 ] ;
41
42 export const addImages = (map: mapboxgl.Map, callback?: ImagesLoaded) =>{
43
44     Images.forEach(image => {
45        
46         map.loadImage(
47             image.url,
48             function (error: any, img: any) {
49                 if (error) throw error;
50                 map.addImage(image.name, img);
51                 allImagesLoaded(map, callback);
52             });
53     });
54 }
55
56 const allImagesLoaded = (map: mapboxgl.Map, callback?: ImagesLoaded) =>{
57
58     const loadedImages = Images.map(image =>{
59         return map.hasImage(image.name);
60     });
61
62     const allImagesLoaded = loadedImages.filter(el => !el);
63     if(allImagesLoaded.length===0){
64         callback && callback(true);
65     }
66 }