cms/app-client/app/components/visu-carto.js
author Chloe Laisne <chloe.laisne@gmail.com>
Thu, 22 Sep 2016 22:57:06 +0200
changeset 281 618aec1734d5
parent 278 f2c2c80a49f7
child 290 ca990333f838
permissions -rw-r--r--
Set step colors with geoname counts

import Ember from 'ember';
import AmCharts from 'ammaps';
import ENV from 'app-client/config/environment';

export default Ember.Component.extend({

    map: null,

    continentsMapVar: AmCharts.maps.continentsLow,
    continentsAreas: [],
    countriesMapVar: AmCharts.maps.worldLow,
    countriesAreas: [],
    franceMapVar: AmCharts.maps.france2016Low,
    franceAreas: [],

    locationQueryParam: 'world',

    color: 'rgba(113,132,141,.2)',

    filter: Ember.inject.service(),
    locationObserver: Ember.observer('filter.location', function() {
        if(!this.get('filter').get('location')) {
            this.get('map').selectObject();
        }
    }),

    geostats: [],
    observerGeostats: Ember.observer('geostats', function() {
        if(this.get('map')) {
            if(this.get('map').selectedObject.id) {
                if(this.get('map').selectedObject.parentObject.mapVar === this.get('countriesMapVar')) {
                    if(this.get('map').selectedObject.id === 'FR') {
                        this.setFranceAreas();
                        this.get('map')['dataProvider'] = {
                            'mapVar': this.get('franceMapVar'),
                            'getAreasFromMap': true,
                            'areas': this.get('franceAreas')
                        };
                        this.get('map').validateData();
                    }
                } else if(this.get('map').selectedObject.parentObject.mapVar === this.get('continentsMapVar')) {
                    this.setCountriesAreas();
                    this.get('map')['dataProvider'] = {
                        'mapVar': this.get('countriesMapVar'),
                        'getAreasFromMap': true,
                        'areas': this.get('countriesAreas'),
                        'zoomLevel': this.get('map').zoomLevel(),
                        'zoomLatitude': this.get('map').zoomLatitude(),
                        'zoomLongitude': this.get('map').zoomLongitude()
                    };
                    this.get('map').validateData();
                }
            } else {
                this.createAmMap();
            }
        }
    }),

    init: function() {
        this._super(...arguments);
        if (ENV.environment === 'development') {
            this.set('color', 'rgba(141,113,113,.2)');
        }
    },

    setFranceAreas: function() {
        var self = this;
        var france2016LowAreas = [];
        this.get('franceMapVar')['svg']['g']['path'].forEach(function(area) {
            var object = {
                'id': area.id,
                'passZoomValuesToTarget': false,
                'autoZoom': false
            };
            var departments = self.get('geostats').find(geostat => geostat.id === area.id);
            if(typeof departments === 'undefined') {
                object['mouseEnabled'] = false,
                object['color'] = self.get('color');
            } else {
                object['value'] = departments.get('count');
            }
            france2016LowAreas.push(object);
        });
        this.set('franceAreas', france2016LowAreas);
    },

    setCountriesAreas: function() {
        var self = this;
        var worldLowAreas = [];
        this.get('countriesMapVar')['svg']['g']['path'].forEach(function(area) {
            var countries = self.get('geostats').find(geostat => geostat.id === area.id);
            if(typeof countries === 'undefined') {
                worldLowAreas.push({
                    'id': area.id,
                    'mouseEnabled': false,
                    'color': self.get('color')
                });
            } else {
                if(typeof worldLowAreas.find(country => country.id === area.id) === 'undefined') {
                    var object = {
                        'id': area.id,
                        'selectable': true,
                        'value': countries.get('count')
                    };
                    if(area.id === 'FR') {
                        object['autoZoom'] = true;
                    }
                    worldLowAreas.push(object);
                }
            }
        });
        this.set('countriesAreas', worldLowAreas);
    },

    setContinentsAreas: function() {
        var self = this;
        var continentsLowAreas = [];
        this.get('continentsMapVar')['svg']['g']['path'].forEach(function(area) {
            var object = {
                'id': area.id,
                'passZoomValuesToTarget': true,
                'selectable': true,
                'mouseEnabled': true,
                'autoZoom': true
            };
            var continent = self.get('geostats').find(geostat => geostat.id === area.id);
            if(typeof continent === 'undefined') {
                object['mouseEnabled'] = false,
                object['color'] = self.get('color');
            } else {
                object['value'] = continent.get('count');
            }
            continentsLowAreas.push(object);
        });
        this.set('continentsAreas', continentsLowAreas);
    },

    didInsertElement: function(){
        this.$('#mapdiv').height(Ember.$('.corpus-app-container').height());
        this.createAmMap();
    },

    createAmMap: function() {
        var self = this;
        this.setContinentsAreas();
        this.set('map', AmCharts.makeChart('mapdiv', {
            'type': 'map',
            'fontFamily': 'sans-serif',
            'fontSize': '12px',
            'dragMap': false,
            'zoomOnDoubleClick': false,
            'language': 'fr',
            'areasSettings': {
                'autoZoom': false,
                'selectable': true,
                'color': '#becfd4',
                'colorSolid': '#71848d',
                'colorOutline': '#253946',
                'selectedColor': '#253946',
                'rollOverOutlineColor': '#253946'
            },
            'zoomControl': {
                'zoomControlEnabled': false,
            },
            'dataProvider': {
                'mapVar': this.get('continentsMapVar'),
                'areas': this.get('continentsAreas')
            },
            listeners: [{
                'event':'clickMapObject',
                'method': Ember.run.bind(this, 'clickMapObject')
            }, {
                'event':'homeButtonClicked',
                'method': Ember.run.bind(this, 'homeButtonClicked')
            }]
        }));
    },

    clickMapObject: function(event) {
        this.set('locationQueryParam', event.mapObject.id);
        this.sendAction('setLocation', event.mapObject.id);
        this.get('filter').set('location', event.mapObject.title);
    },

    homeButtonClicked: function(event) {
        if(event.chart.dataProvider.map !== 'continentsLow') {
            this.sendAction('setLocation', 'world');
        }
    }

});