<commit_before>import Ember from 'ember';

export default Ember.Component.extend({

  tagName: 'section',

  cartodbtable: 'cwd1981_2010_ave_hst_1415990012',

  didRender() {
    this.slideMapEl = this.$('#map7-2');
    this.setListeners();
  },

  setListeners: function() {
    Reveal.addEventListener('slidechanged', function( event ) {
      if (event.currentSlide.classList.contains('slide-map-7-2')) {
        this.initMap();
        this.initLegend();
      } else if (this.slideMap) {
        this.removeMap();
      }
    }.bind(this));
  },

  initMap(){
    this.slideMapEl.addClass('-loading');
    const mapOptions = {
      zoomControl: false,
      scrollWheelZoom:false,
      center: [38.280957,-122.457728],
      zoom: 7,
      basemapSpec: {
        url: 'http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png',
        options: {
          maxZoom: 18
        }
      }
    };

    if (!this.slideMap){
      this.slideMap = L.map('map7-2', mapOptions);
      L.tileLayer(mapOptions.basemapSpec.url, mapOptions.basemapSpec.options).addTo(this.slideMap);
      this.addRaster();
      if (this.bounds){
        this.fitBounds();
      } else {
        this.setBounds().done(function(data){
          let coordinates = JSON.parse(data.rows[0].bbox).coordinates[0];
          let southWest = L.latLng(coordinates[0][1],coordinates[0][0]),
          northEast = L.latLng(coordinates[2][1],coordinates[2][0]);
          this.bounds = L.latLngBounds(southWest, northEast);
          this.fitBounds();
        }.bind(this));
      }
    }
  },

  fitBounds: function(){
    this.slideMap.fitBounds(this.bounds,{padding:[15,15]});
  },

  setBounds: function(){
    let query  = 'SELECT ST_AsGeoJSON(ST_Envelope(ST_Union(ST_Transform(ST_Envelope(the_raster_webmercator), 4326)))) as bbox FROM '+ this.cartodbtable +' as t';
    return $.get('https://prep-admin.cartodb.com/api/v2/sql/?q='+query);
  },

  addRaster(){
    this.slideMapEl.addClass('-loading');
    var request = {
      layers: [{
        'user_name': 'prep-admin',
        'type': 'cartodb',
        'options': {
            'sql': 'SELECT * FROM '+this.cartodbtable,
            'cartocss': '#'+this.cartodbtable+' {raster-opacity:1; raster-colorizer-default-mode: linear; raster-colorizer-default-color: transparent; raster-colorizer-epsilon: 0.01; raster-colorizer-stops: stop(1, #c9946b ) stop(50, #c9946b ) stop(100, #85af43 ) stop(200, #497435 )}',
          'cartocss_version': '2.3.0',
          'geom_column': 'the_raster_webmercator',
          'geom_type': 'raster',
          'raster_band': 1
        }
      }]
    };

    $.ajax({
      type: 'POST',
      dataType: 'json',
      contentType: 'application/json; charset=UTF-8',
      url: 'https://prep-admin.cartodb.com/api/v1/map/',
      data: JSON.stringify(request),
      success: function(data) {
        var tileUrl = 'https://prep-admin.cartodb.com/api/v1/map/' + data.layergroupid + '/{z}/{x}/{y}.png';
        if (this.layer) {
          this.slideMap.removeLayer(this.layer);
          this.layer = null;
        }
        this.layer = L.tileLayer(tileUrl).addTo(this.slideMap, 1);
        this.layer.on('load',function() {
          this.slideMapEl.removeClass('-loading');
        }.bind(this));
      }.bind(this)
    });
  },

  initLegend(){
    var steps = this.$('.range span');
    steps.on('click', function(ev){
      var target = ev.currentTarget;
      var index = $(target).index();
      steps.each(function(index, item){
        item.classList.remove('-selected');
      });
      target.classList.add('-selected');
      slider.noUiSlider.set(index);
      this.updateLayer(index);
    }.bind(this));

    var slider = document.getElementById('timelineSlider');
    noUiSlider.create(slider, {
      start: [0],
    	snap: true,
    	range: {
    		'min': 0,
    		'50%': 1,
    		'max': 2
    	}
    });
  },

  updateLayer(index){
    switch (index){
      case 0:
        this.cartodbtable = 'cwd1981_2010_ave_hst_1415990012';
        break;
      case 1:
        this.cartodbtable = 'test';
        break;
      default:
        this.cartodbtable = 'cwd1981_2010_ave_hst_1415990012';
    }
    this.addRaster();
  },

  removeMap(){
    this.slideMap.remove();
    this.slideMap = null;
  }
<commit_msg>Fix slider already initialized<commit_after>import Ember from 'ember';

export default Ember.Component.extend({

  tagName: 'section',

  cartodbtable: 'cwd1981_2010_ave_hst_1415990012',

  didRender() {
    this.slideMapEl = this.$('#map7-2');
    this.setListeners();
  },

  setListeners: function() {
    Reveal.addEventListener('slidechanged', function( event ) {
      if (event.currentSlide.classList.contains('slide-map-7-2')) {
        this.initMap();
        this.initLegend();
      } else if (this.slideMap) {
        this.removeMap();
      }
    }.bind(this));
  },

  initMap(){
    this.slideMapEl.addClass('-loading');
    const mapOptions = {
      zoomControl: false,
      scrollWheelZoom:false,
      center: [38.280957,-122.457728],
      zoom: 7,
      basemapSpec: {
        url: 'http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png',
        options: {
          maxZoom: 18
        }
      }
    };

    if (!this.slideMap){
      this.slideMap = L.map('map7-2', mapOptions);
      L.tileLayer(mapOptions.basemapSpec.url, mapOptions.basemapSpec.options).addTo(this.slideMap);
      this.addRaster();
      if (this.bounds){
        this.fitBounds();
      } else {
        this.setBounds().done(function(data){
          let coordinates = JSON.parse(data.rows[0].bbox).coordinates[0];
          let southWest = L.latLng(coordinates[0][1],coordinates[0][0]),
          northEast = L.latLng(coordinates[2][1],coordinates[2][0]);
          this.bounds = L.latLngBounds(southWest, northEast);
          this.fitBounds();
        }.bind(this));
      }
    }
  },

  fitBounds: function(){
    this.slideMap.fitBounds(this.bounds,{padding:[15,15]});
  },

  setBounds: function(){
    let query  = 'SELECT ST_AsGeoJSON(ST_Envelope(ST_Union(ST_Transform(ST_Envelope(the_raster_webmercator), 4326)))) as bbox FROM '+ this.cartodbtable +' as t';
    return $.get('https://prep-admin.cartodb.com/api/v2/sql/?q='+query);
  },

  addRaster(){
    this.slideMapEl.addClass('-loading');
    var request = {
      layers: [{
        'user_name': 'prep-admin',
        'type': 'cartodb',
        'options': {
            'sql': 'SELECT * FROM '+this.cartodbtable,
            'cartocss': '#'+this.cartodbtable+' {raster-opacity:1; raster-colorizer-default-mode: linear; raster-colorizer-default-color: transparent; raster-colorizer-epsilon: 0.01; raster-colorizer-stops: stop(1, #c9946b ) stop(50, #c9946b ) stop(100, #85af43 ) stop(200, #497435 )}',
          'cartocss_version': '2.3.0',
          'geom_column': 'the_raster_webmercator',
          'geom_type': 'raster',
          'raster_band': 1
        }
      }]
    };

    $.ajax({
      type: 'POST',
      dataType: 'json',
      contentType: 'application/json; charset=UTF-8',
      url: 'https://prep-admin.cartodb.com/api/v1/map/',
      data: JSON.stringify(request),
      success: function(data) {
        var tileUrl = 'https://prep-admin.cartodb.com/api/v1/map/' + data.layergroupid + '/{z}/{x}/{y}.png';
        if (this.layer) {
          this.slideMap.removeLayer(this.layer);
          this.layer = null;
        }
        this.layer = L.tileLayer(tileUrl).addTo(this.slideMap, 1);
        this.layer.on('load',function() {
          this.slideMapEl.removeClass('-loading');
        }.bind(this));
      }.bind(this)
    });
  },

  initLegend() {
    if(!this.slider){
      var steps = this.$('.range span');
      steps.on('click', function(ev){
        var target = ev.currentTarget;
        var index = $(target).index();
        steps.each(function(index, item){
          item.classList.remove('-selected');
        });
        target.classList.add('-selected');
        this.slider.noUiSlider.set(index);
        this.updateLayer(index);
      }.bind(this));

      this.slider = document.getElementById('timelineSlider');
      noUiSlider.create(this.slider, {
        start: [0],
        snap: true,
        range: {
          'min': 0,
          '50%': 1,
          'max': 2
        }
      });
    }
  },

  updateLayer(index){
    switch (index){
      case 0:
        this.cartodbtable = 'cwd1981_2010_ave_hst_1415990012';
        break;
      case 1:
        this.cartodbtable = 'test';
        break;
      default:
        this.cartodbtable = 'cwd1981_2010_ave_hst_1415990012';
    }
    this.addRaster();
  },

  removeMap(){
    this.slideMap.remove();
    this.slideMap = null;
  }

{'fraction_non_alphanumeric': 0.16839318600368325, 'fraction_numerical': 0.03360957642725598, 'mean_word_length': 2.4714342788653614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, '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': '2877875', 'n_tokens_mistral': 3191, 'n_tokens_neox': 2963, 'n_words': 437}