'use strict';

var config                 = require('./config')()
var dataServiceFactory     = require('./dataService')
var mapFactory             = require('../wrappers/map')
var popupPresenterFactory  = require('./popupPresenter')
var filterFactory          = require('./geojsonFeatureFilterer')
var stylePresenterFactory  = require('./layerStylePresenter')
var key                    = require('./key')
var tileLayerManager       = require('./tileLayerManager')
var mapKeyController       = require('./keyController')
var geojsonLayerController = require('./geojsonLayerController')
var tileLayerController    = require('./tileLayerController')

var mapConfig = config.getMap()
var keyConfig = config.getKey()
var map       = mapFactory(mapConfig)

tileLayerController({
  map: map,
  manager: tileLayerManager(mapConfig.tileLayers)
})

var keyController = mapKeyController({
  map: map,
  key: key(keyConfig),
  layerNames: config.getLayerNames()
})

var layerConfigs = config.getLayers()

geojsonLayerController({
  map: map,
  keyController: keyController,
  dataServiceFactory: dataServiceFactory,
  popupPresenterFactory: popupPresenterFactory,
  filterFactory: filterFactory,
  stylePresenterFactory: stylePresenterFactory
}).addLayersFromConfig(layerConfigs)

function clickLayer(layerConfig) {
  var layerStylePresenter = stylePresenterFactory(layerConfig.styles.layer)

  map.on('click', (event) => {
    var lng         = event.latlng.lng
    var lat         = event.latlng.lat
    var config      = JSON.parse(JSON.stringify(layerConfig.dataSource))
    var dataService

    config.url  = config.url.replace('{x}', lng).replace('{y}', lat)
    dataService = dataServiceFactory(config)
    dataService.on('data', () => {
      map.setGeojsonLayer(layerConfig.name, {
        geojson: dataService.getData(),
        style: (properties) => layerStylePresenter.present(properties)
      })
      map.showGeojsonLayer(layerConfig.name)
    })

    dataService.start()
  })
}

//add feature layers
layerConfigs.forEach((layerConfig) => {
  if (layerConfig.type === 'click')
    clickLayer(layerConfig)
})

'use strict';

var config                 = require('./config')()
var dataServiceFactory     = require('./dataService')
var mapFactory             = require('../wrappers/map')
var popupPresenterFactory  = require('./popupPresenter')
var filterFactory          = require('./geojsonFeatureFilterer')
var stylePresenterFactory  = require('./layerStylePresenter')
var key                    = require('./key')
var tileLayerManager       = require('./tileLayerManager')
var mapKeyController       = require('./keyController')
var geojsonLayerController = require('./geojsonLayerController')
var tileLayerController    = require('./tileLayerController')

var mapConfig    = config.getMap()
var keyConfig    = config.getKey()
var layerConfigs = config.getLayers()
var map          = mapFactory(mapConfig)

tileLayerController({
  map: map,
  manager: tileLayerManager(mapConfig.tileLayers)
})

var keyController = mapKeyController({
  map: map,
  key: key(keyConfig),
  layerNames: config.getLayerNames()
})

geojsonLayerController({
  map: map,
  keyController: keyController,
  dataServiceFactory: dataServiceFactory,
  popupPresenterFactory: popupPresenterFactory,
  filterFactory: filterFactory,
  stylePresenterFactory: stylePresenterFactory
}).addLayersFromConfig(layerConfigs)

function clickLayer(layerConfig) {
  var layerStylePresenter = stylePresenterFactory(layerConfig.styles.layer)

  map.on('click', (event) => {
    var lng         = event.latlng.lng
    var lat         = event.latlng.lat
    var config      = JSON.parse(JSON.stringify(layerConfig.dataSource))
    var dataService

    config.url  = config.url.replace('{x}', lng).replace('{y}', lat)
    dataService = dataServiceFactory(config)
    dataService.on('data', () => {
      map.setGeojsonLayer(layerConfig.name, {
        geojson: dataService.getData(),
        style: (properties) => layerStylePresenter.present(properties)
      })
      map.showGeojsonLayer(layerConfig.name)
    })

    dataService.start()
  })
}

//add feature layers
layerConfigs.forEach((layerConfig) => {
  if (layerConfig.type === 'click')
    clickLayer(layerConfig)
})

Update main.js file formatting