Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
],
"dependencies": {
"jquery": ">=2.0.x",
"leaflet": ">=0.7.x"
"leaflet": "0.7.7",
"Sortable": "sortablejs#^1.4.2"
},

"devDependencies": {
"bootstrap": "3.2.x"
},

"ignore": [
"**/.*",
"src",
Expand Down
4 changes: 1 addition & 3 deletions dist/css/leaflet.control.orderlayers.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 34 additions & 9 deletions dist/leaflet.control.orderlayers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ L.Control.OrderLayers = L.Control.Layers.extend({
title: 'Layer Manager',
// Values: ['normal', 'qgis']
order: 'normal',
showBaselayers: true
showBaselayers: true,
draggging: false
},

onAdd: function (map) {
this._initLayout();
this._update();

if(this.options.draggging) {
this._addDranAndDrop();
}

map
.on('layeradd', this._onLayerChange, this)
Expand Down Expand Up @@ -133,6 +138,20 @@ L.Control.OrderLayers = L.Control.Layers.extend({
this._separator.style.display = overlaysPresent && baseLayersPresent ? '' : 'none';
},

_addDranAndDrop: function() {
var self=this;
Sortable.create(this._overlaysList, {
onMove: function (evt, originalEvent) {
var objDragged = self._layers[evt.dragged.layerId];
var objRelated = self._layers[evt.related.layerId];
var zIndexDragged = self._getZIndex(objDragged);
var zIndexRelated = self._getZIndex(objRelated);
objDragged.layer.setZIndex(zIndexRelated);
objRelated.layer.setZIndex(zIndexDragged);
}
});
},

_addItem: function (obj) {
var row = L.DomUtil.create('div', 'leaflet-row');

Expand All @@ -151,6 +170,10 @@ L.Control.OrderLayers = L.Control.Layers.extend({

input.layerId = L.stamp(obj.layer);
input.id = 'lf.'+ input.layerId;

if(this.options.draggging) {
row.layerId = input.layerId;
}

L.DomEvent.on(input, 'click', this._onInputClick, this);

Expand All @@ -171,14 +194,16 @@ L.Control.OrderLayers = L.Control.Layers.extend({

var container;
if(obj.overlay) {
col = L.DomUtil.create('div', 'leaflet-up');
L.DomEvent.on(col, 'click', (this.options.order === 'normal'? this._onUpClick:this._onDownClick), this);
col.layerId = input.layerId;
row.appendChild(col);
col = L.DomUtil.create('div', 'leaflet-down');
col.layerId = input.layerId;
L.DomEvent.on(col, 'click', (this.options.order === 'normal'? this._onDownClick:this._onUpClick), this);
row.appendChild(col);
if(!this.options.draggging) {
col = L.DomUtil.create('div', 'leaflet-up');
L.DomEvent.on(col, 'click', (this.options.order === 'normal'? this._onUpClick:this._onDownClick), this);
col.layerId = input.layerId;
row.appendChild(col);
col = L.DomUtil.create('div', 'leaflet-down');
col.layerId = input.layerId;
L.DomEvent.on(col, 'click', (this.options.order === 'normal'? this._onDownClick:this._onUpClick), this);
row.appendChild(col);
}
container = this._overlaysList;
} else {
container = this._baseLayersList;
Expand Down
4 changes: 2 additions & 2 deletions dist/leaflet.control.orderlayers.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions examples/simple-example-drag-and-drop.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<html>
<head>
<script src="../bower_components/jquery/dist/jquery.js"></script>
<script src="../bower_components/leaflet/dist/leaflet-src.js"></script>
<script src="../bower_components/Sortable/Sortable.min.js"></script>
<script src="../dist/leaflet.control.orderlayers.min.js"></script>
<link rel="stylesheet" href="../bower_components/leaflet/dist/leaflet.css" />
<link rel="stylesheet" href="../dist/css/leaflet.control.orderlayers.css" />
<link rel="stylesheet" href="css/styles.css" />
<script>
jQuery(function() {
var map = L.map('map').setView([4.598056, -74.075833], 13);

var b1 = L.tileLayer('http://a.tiles.mapbox.com/v3/elesdoar.map-856j4yqh/{z}/{x}/{y}.png', {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 20
}).addTo(map);

var b2 = L.tileLayer('http://a.tiles.mapbox.com/v3/elesdoar.map-lfwx2j04/{z}/{x}/{y}.png', {
attribution: 'Map data © OpenStreetMap contributors, CC-BY-SA, Imagery © Mapbox',
maxZoom: 20
});

var fire = L.tileLayer('http://openfiremap.org/hytiles/{z}/{x}/{y}.png', {
attribution: '© OpenFireMap contributors - © OpenStreetMap contributors',
continuousWorld: true
}).addTo(map);

var overlayLayers = {
'OpenFireMap': fire,
'Streets': b1,
'Night': b2
};

var controls = L.control.orderlayers(null, overlayLayers, {collapsed: false, draggging:true});
controls.addTo(map);

map.setView([4.649, -74.086], 11);
});
</script>
</head>
<body>
<div id="map">
</div>
</body>
</html>
43 changes: 34 additions & 9 deletions src/leaflet.control.orderlayers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ L.Control.OrderLayers = L.Control.Layers.extend({
title: 'Layer Manager',
// Values: ['normal', 'qgis']
order: 'normal',
showBaselayers: true
showBaselayers: true,
draggging: false
},

onAdd: function (map) {
this._initLayout();
this._update();

if(this.options.draggging) {
this._addDranAndDrop();
}

map
.on('layeradd', this._onLayerChange, this)
Expand Down Expand Up @@ -133,6 +138,20 @@ L.Control.OrderLayers = L.Control.Layers.extend({
this._separator.style.display = overlaysPresent && baseLayersPresent ? '' : 'none';
},

_addDranAndDrop: function() {
var self=this;
Sortable.create(this._overlaysList, {
onMove: function (evt, originalEvent) {
var objDragged = self._layers[evt.dragged.layerId];
var objRelated = self._layers[evt.related.layerId];
var zIndexDragged = self._getZIndex(objDragged);
var zIndexRelated = self._getZIndex(objRelated);
objDragged.layer.setZIndex(zIndexRelated);
objRelated.layer.setZIndex(zIndexDragged);
}
});
},

_addItem: function (obj) {
var row = L.DomUtil.create('div', 'leaflet-row');

Expand All @@ -151,6 +170,10 @@ L.Control.OrderLayers = L.Control.Layers.extend({

input.layerId = L.stamp(obj.layer);
input.id = 'lf.'+ input.layerId;

if(this.options.draggging) {
row.layerId = input.layerId;
}

L.DomEvent.on(input, 'click', this._onInputClick, this);

Expand All @@ -171,14 +194,16 @@ L.Control.OrderLayers = L.Control.Layers.extend({

var container;
if(obj.overlay) {
col = L.DomUtil.create('div', 'leaflet-up');
L.DomEvent.on(col, 'click', (this.options.order === 'normal'? this._onUpClick:this._onDownClick), this);
col.layerId = input.layerId;
row.appendChild(col);
col = L.DomUtil.create('div', 'leaflet-down');
col.layerId = input.layerId;
L.DomEvent.on(col, 'click', (this.options.order === 'normal'? this._onDownClick:this._onUpClick), this);
row.appendChild(col);
if(!this.options.draggging) {
col = L.DomUtil.create('div', 'leaflet-up');
L.DomEvent.on(col, 'click', (this.options.order === 'normal'? this._onUpClick:this._onDownClick), this);
col.layerId = input.layerId;
row.appendChild(col);
col = L.DomUtil.create('div', 'leaflet-down');
col.layerId = input.layerId;
L.DomEvent.on(col, 'click', (this.options.order === 'normal'? this._onDownClick:this._onUpClick), this);
row.appendChild(col);
}
container = this._overlaysList;
} else {
container = this._baseLayersList;
Expand Down