1 //= require templates/map/layers
3 L.OSM.Layers = L.Control.extend({
4 onAdd: function (map) {
7 return this._container;
10 _initLayout: function () {
11 var className = 'leaflet-control-map-ui',
12 container = this._container = L.DomUtil.create('div', className);
14 var link = L.DomUtil.create('a', 'control-button', container);
15 link.innerHTML = "<span class='icon layers'></span>";
17 link.title = 'Layers';
19 this._ui = $(L.DomUtil.create('div', 'layers-ui', this.options.uiPane))
20 .html(JST["templates/map/layers"]());
22 var list = this._ui.find('.base-layers ul');
24 this.options.layers.forEach(function(layer) {
25 var item = $('<li></li>')
28 if (this._map.hasLayer(layer)) {
29 item.addClass('active');
32 var div = $('<div></div>')
35 this._map.whenReady(function() {
36 var map = L.map(div[0], {attributionControl: false, zoomControl: false})
37 .setView(this._map.getCenter(), Math.max(this._map.getZoom() - 2, 0))
38 .addLayer(new layer.constructor);
40 map.dragging.disable();
41 map.touchZoom.disable();
42 map.doubleClickZoom.disable();
43 map.scrollWheelZoom.disable();
46 var label = $('<label></label>')
47 .text(layer.options.name)
50 item.on('click', function() {
51 this.options.layers.forEach(function(other) {
52 if (other === layer) {
53 this._map.addLayer(other);
55 this._map.removeLayer(other);
60 this._map.on('layeradd', function(e) {
61 if (e.layer === layer) {
62 item.addClass('active');
64 }).on('layerremove', function(e) {
65 if (e.layer === layer) {
66 item.removeClass('active');
71 $(link).on('click', $.proxy(this.toggleLayers, this));
74 toggleLayers: function (e) {
78 var controlContainer = $('.leaflet-control-container .leaflet-top.leaflet-right');
80 if ($(this._ui).is(':visible')) {
81 $(this.options.uiPane).hide();
82 controlContainer.css({paddingRight: '0'});
84 $(this.options.uiPane).show();
85 controlContainer.css({paddingRight: '230px'});
90 L.OSM.layers = function(options) {
91 return new L.OSM.Layers(options);