<commit_before>import { Component, OnInit, Input, Output, EventEmitter, SimpleChanges} from '@angular/core';
import { OptionMap, AbaMap } from '../core/map';
import { LayerType } from '../core/layer';
import { MapService } from '../core/map.service';
import ArcgisSearch = require('esri/dijit/Search');
const img_loading = require("file?name=./assets/[name].[ext]!./img/spin.gif");

import 'rxjs/add/operator/toPromise';
import { DrawType } from '../editor/drawMap';
import Extent = require("esri/geometry/Extent");
import Graphic = require("esri/graphic");

@Component({
  selector: 'aba-map',
  templateUrl: 'map.component.html',
  styleUrls: ['map.component.css']
})
export class MapComponent implements OnInit {

  optionMaps: OptionMap[];
  map : AbaMap;
  search: ArcgisSearch;

  mapLoading : boolean = false;
  imgLoading : string = img_loading;

  needZoom : boolean = false;

  @Output() mapInstancied = new EventEmitter();
  @Input() drawType : string;

  readonly ZOOM_LEVEL_MINIMUM : number = 16;

  constructor(private mapService: MapService) {
  }

  getDefaultMap(): void {
    this.mapService
        .map(160)
        .subscribe(
          optionMap => {
            //this.optionMaps = optionMaps;
            this.initMap(optionMap);
          }
        );
  }

  ngOnInit(): void {
    this.getDefaultMap();
  }

  setLayerType(layerType : LayerType): boolean {
    if (this.map){
      this.map.setLayerVisible(layerType);
      return true;
    }
    return false;
  }


  initMap(optionMap: OptionMap): void {

    this.map = AbaMap.fromOptionMap("esri-map", optionMap);

    this.applyDefaultCallbackToTheMap();

    this.search = new ArcgisSearch(
      {
        map: this.map,
        /* useMapExtent:false, */
        enableHighlight: false
      },
      "search"
    );

    this.mapInstancied.emit(optionMap);
  }

  private applyDefaultCallbackToTheMap(): void {

    this.map.on("update-start", () => this.mapLoading = true);
    this.map.on("update-end", () => this.mapLoading = false);

    // Zoom restriction
    this.map.on("zoom-end", (event: { level : number}) => {
        if(event.level){
          // Show or hide 'need zoom' message
          this.needZoom = (event.level < this.ZOOM_LEVEL_MINIMUM);

          // If yes, stop loading
          if(this.needZoom)
            this.mapLoading = false;
        }
      }
    );
  }

  public selectMapId(id: number): void {
    this.mapService.map(id).subscribe(
      optionMap => {

        // Fixed: Must destroy before attributing a new instance
        this.map.destroy();
        this.map = AbaMap.fromOptionMap("esri-map", optionMap);

        this.applyDefaultCallbackToTheMap();

        this.setLayerType(optionMap.layerType);

      }
    );
  }
  public saveMapWithTitle(title: string): void {
    this.map.title = title;
    this.mapService.add(this.map.toOptionMap()).subscribe( i => this.map.uid = i );
  }
}
<commit_msg>Fix bug #58<commit_after>import { Component, OnInit, Input, Output, EventEmitter, SimpleChanges} from '@angular/core';
import { OptionMap, AbaMap } from '../core/map';
import { LayerType } from '../core/layer';
import { MapService } from '../core/map.service';
import ArcgisSearch = require('esri/dijit/Search');
const img_loading = require("file?name=./assets/[name].[ext]!./img/spin.gif");

import 'rxjs/add/operator/toPromise';
import { DrawType } from '../editor/drawMap';
import Extent = require("esri/geometry/Extent");
import Graphic = require("esri/graphic");

@Component({
  selector: 'aba-map',
  templateUrl: 'map.component.html',
  styleUrls: ['map.component.css']
})
export class MapComponent implements OnInit {

  optionMaps: OptionMap[];
  map : AbaMap;
  search: ArcgisSearch;

  mapLoading : boolean = false;
  imgLoading : string = img_loading;

  needZoom : boolean = false;

  @Output() mapInstancied = new EventEmitter();
  @Input() drawType : string;

  readonly ZOOM_LEVEL_MINIMUM : number = 16;

  constructor(private mapService: MapService) {
  }

  getDefaultMap(): void {
    this.mapService
        .map(160)
        .subscribe(
          optionMap => {
            //this.optionMaps = optionMaps;
            this.initMap(optionMap);
          }
        );
  }

  ngOnInit(): void {
    this.getDefaultMap();
  }

  setLayerType(layerType : LayerType): boolean {
    if (this.map){
      this.map.setLayerVisible(layerType);
      return true;
    }
    return false;
  }


  initMap(optionMap: OptionMap): void {

    this.map = AbaMap.fromOptionMap("esri-map", optionMap);

    this.applyDefaultCallbackToTheMap();

    this.search = new ArcgisSearch(
      {
        map: this.map,
        /* useMapExtent:false, */
        enableHighlight: false
      },
      "search"
    );

    this.mapInstancied.emit(optionMap);
  }

  private applyDefaultCallbackToTheMap(): void {

    this.map.on("update-start", () => this.mapLoading = true);
    this.map.on("update-end", () => this.mapLoading = false);

    // Zoom restriction
    this.map.on("zoom-end", (event: { level : number}) => {
        if(event.level){
          // Show or hide 'need zoom' message
          this.needZoom = (event.level < this.ZOOM_LEVEL_MINIMUM);

          // If yes, stop loading
          if(this.needZoom)
            this.mapLoading = false;
        }
      }
    );
  }

  public selectMapId(id: number): void {
    this.mapService.map(id).subscribe(
      optionMap => {

        // Fixed: Must destroy before attributing a new instance
        this.map.destroy();
        this.map = AbaMap.fromOptionMap("esri-map", optionMap);

        // Call mapInstancied event to prevent others components of new map
        this.mapInstancied.emit(optionMap);

        this.applyDefaultCallbackToTheMap();

        this.setLayerType(optionMap.layerType);

      }
    );
  }
  public saveMapWithTitle(title: string): void {
    this.map.title = title;
    this.mapService.add(this.map.toOptionMap()).subscribe( i => this.map.uid = i );
  }
}

{'fraction_non_alphanumeric': 0.1409205020920502, 'fraction_numerical': 0.0020083682008368202, 'mean_word_length': 2.749058971141782, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2097830', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1863, 'n_words': 377}