Consistent JavaScript style

This commit is contained in:
John Firebaugh 2013-06-12 11:34:43 -07:00
parent f1a4669c95
commit eebe1f1d3e
5 changed files with 112 additions and 138 deletions

View file

@ -75,12 +75,12 @@ $(document).ready(function () {
uiPane: uiPane uiPane: uiPane
}).addTo(map); }).addTo(map);
L.control.note({ L.OSM.note({
position: 'topright', position: 'topright',
uiPane: uiPane uiPane: uiPane
}).addTo(map); }).addTo(map);
L.control.share({ L.OSM.share({
getUrl: getShortUrl, getUrl: getShortUrl,
uiPane: uiPane uiPane: uiPane
}).addTo(map); }).addTo(map);

View file

@ -1,25 +1,19 @@
L.OSM.Key = L.Control.extend({ L.OSM.key = function(options) {
onAdd: function (map) { var control = L.control(options);
this._map = map;
this._initLayout();
return this.$container[0];
},
_initLayout: function () { control.onAdd = function (map) {
var map = this._map; var $container = $('<div>')
this.$container = $('<div>')
.attr('class', 'control-key'); .attr('class', 'control-key');
var link = $('<a>') $('<a>')
.attr('class', 'control-button') .attr('class', 'control-button')
.attr('href', '#') .attr('href', '#')
.attr('title', 'Map Key') .attr('title', 'Map Key')
.html('<span class="icon key"></span>') .html('<span class="icon key"></span>')
.appendTo(this.$container); .appendTo($container);
}
});
L.OSM.key = function(options) { return $container[0];
return new L.OSM.Key(options); };
return control;
}; };

View file

@ -1,15 +1,10 @@
L.OSM.Layers = L.Control.extend({ L.OSM.layers = function(options) {
onAdd: function (map) { var control = L.control(options);
this._map = map;
this._initLayout();
return this.$container[0];
},
_initLayout: function () { control.onAdd = function (map) {
var map = this._map, var layers = options.layers;
layers = this.options.layers;
this.$container = $('<div>') var $container = $('<div>')
.attr('class', 'control-layers'); .attr('class', 'control-layers');
var link = $('<a>') var link = $('<a>')
@ -17,20 +12,21 @@ L.OSM.Layers = L.Control.extend({
.attr('href', '#') .attr('href', '#')
.attr('title', 'Layers') .attr('title', 'Layers')
.html('<span class="icon layers"></span>') .html('<span class="icon layers"></span>')
.appendTo(this.$container); .on('click', toggle)
.appendTo($container);
var $ui = $('<div>')
.attr('class', 'layers-ui')
.appendTo(options.uiPane);
$('<h2>')
.text(I18n.t('javascripts.map.layers.header'))
.appendTo($ui);
if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') { if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
this.$ui = $('<div>')
.attr('class', 'layers-ui')
.appendTo(this.options.uiPane);
$('<h2>')
.text(I18n.t('javascripts.map.layers.header'))
.appendTo(this.$ui);
var overlaySection = $('<section>') var overlaySection = $('<section>')
.addClass('overlay-layers') .addClass('overlay-layers')
.appendTo(this.$ui); .appendTo($ui);
$('<p>') $('<p>')
.text(I18n.t('javascripts.map.layers.overlays')) .text(I18n.t('javascripts.map.layers.overlays'))
@ -72,7 +68,7 @@ L.OSM.Layers = L.Control.extend({
var baseSection = $('<section>') var baseSection = $('<section>')
.addClass('base-layers') .addClass('base-layers')
.appendTo(this.$ui); .appendTo($ui);
$('<p>') $('<p>')
.text(I18n.t('javascripts.map.layers.base')) .text(I18n.t('javascripts.map.layers.base'))
@ -128,29 +124,27 @@ L.OSM.Layers = L.Control.extend({
}); });
}); });
$(link).on('click', $.proxy(this.toggleLayers, this)); function toggle(e) {
}, e.stopPropagation();
e.preventDefault();
toggleLayers: function (e) { var controlContainer = $('.leaflet-control-container .leaflet-top.leaflet-right');
e.stopPropagation();
e.preventDefault();
var controlContainer = $('.leaflet-control-container .leaflet-top.leaflet-right'); if ($ui.is(':visible')) {
$(control.options.uiPane).hide();
controlContainer.css({paddingRight: '0'});
} else {
$(control.options.uiPane).show();
controlContainer.css({paddingRight: '230px'});
}
if (this.$ui.is(':visible')) { $ui.find('.base-layers .leaflet-container').each(function() {
$(this.options.uiPane).hide(); $(this).data('map').invalidateSize();
controlContainer.css({paddingRight: '0'}); });
} else {
$(this.options.uiPane).show();
controlContainer.css({paddingRight: '230px'});
} }
this.$ui.find('.base-layers .leaflet-container').each(function() { return $container[0];
$(this).data('map').invalidateSize(); };
});
}
});
L.OSM.layers = function(options) { return control;
return new L.OSM.Layers(options);
}; };

View file

@ -1,39 +1,31 @@
L.Control.Note = L.Control.extend({ L.OSM.note = function (options) {
options: { var control = L.control(options);
position: 'topright',
title: 'Notes',
},
onAdd: function (map) { control.onAdd = function (map) {
var className = 'control-note', var $container = $('<div>')
container = L.DomUtil.create('div', className); .attr('class', 'control-note');
var link = L.DomUtil.create('a', 'control-button', container); $('<a>')
link.innerHTML = "<span class='icon note'></span>"; .attr('class', 'control-button')
link.href = '#'; .attr('href', '#')
link.title = this.options.title; .attr('title', 'Notes')
.html('<span class="icon note"></span>')
.on('click', toggle)
.appendTo($container);
L.DomEvent function toggle(e) {
.on(link, 'click', L.DomEvent.stopPropagation) e.stopPropagation();
.on(link, 'click', L.DomEvent.preventDefault) e.preventDefault();
.on(link, 'click', this._toggle, this)
.on(link, 'dblclick', L.DomEvent.stopPropagation);
this.map = map; if (map.hasLayer(map.noteLayer)) {
map.removeLayer(map.noteLayer);
return container; } else {
}, map.addLayer(map.noteLayer);
}
// TODO: this relies on notesLayer on the map
_toggle: function() {
if (this.map.hasLayer(this.map.noteLayer)) {
this.map.removeLayer(this.map.noteLayer);
} else {
this.map.addLayer(this.map.noteLayer);
}
} }
});
L.control.note = function(options) { return $container[0];
return new L.Control.Note(options); };
return control;
}; };

View file

@ -1,60 +1,54 @@
L.Control.Share = L.Control.extend({ L.OSM.share = function (options) {
options: { var control = L.control(options);
position: 'topright',
title: 'Share',
url: function(map) {
return '';
}
},
onAdd: function (map) { control.onAdd = function (map) {
var className = 'control-share', var $container = $('<div>')
container = L.DomUtil.create('div', className); .attr('class', 'control-share');
var link = L.DomUtil.create('a', 'control-button', container); $('<a>')
link.innerHTML = "<span class='icon share'></span>"; .attr('class', 'control-button')
link.href = '#'; .attr('href', '#')
link.title = this.options.title; .attr('title', 'Share')
.html('<span class="icon share"></span>')
.on('click', toggle)
.appendTo($container);
this._uiPane = this.options.uiPane; var $ui = $('<div>')
.attr('class', 'share-ui')
.appendTo(options.uiPane);
this._map = map; $('<h2>')
.text(I18n.t('javascripts.share.title'))
.appendTo($ui);
var h2 = L.DomUtil.create('h2', '', this._uiPane); var $input = $('<input>')
h2.innerHTML = I18n.t('javascripts.share.title'); .appendTo($ui);
this._linkInput = L.DomUtil.create('input', '', this._uiPane); map.on('moveend layeradd layerremove', update);
L.DomEvent function toggle(e) {
.on(link, 'click', L.DomEvent.stopPropagation) e.stopPropagation();
.on(link, 'click', L.DomEvent.preventDefault) e.preventDefault();
.on(link, 'click', this._toggle, this)
.on(link, 'dblclick', L.DomEvent.stopPropagation);
map.on('moveend layeradd layerremove', this._update, this); var controlContainer = $('.leaflet-control-container .leaflet-top.leaflet-right');
return container; if ($ui.is(':visible')) {
}, $(control.options.uiPane).hide();
controlContainer.css({paddingRight: '0'});
_update: function (e) { } else {
var center = this._map.getCenter().wrap(); $(control.options.uiPane).show();
var layers = getMapLayers(this._map); controlContainer.css({paddingRight: '200px'});
this._linkInput.value = this.options.getUrl(this._map); }
},
_toggle: function() {
var controlContainer = $('.leaflet-control-container .leaflet-top.leaflet-right');
if ($(this._uiPane).is(':visible')) {
$(this._uiPane).hide();
controlContainer.css({paddingRight: '0'});
} else {
$(this._uiPane).show();
controlContainer.css({paddingRight: '200px'});
}
} }
});
L.control.share = function(options) { function update() {
return new L.Control.Share(options); var center = map.getCenter().wrap();
var layers = getMapLayers(map);
$input.val(options.getUrl(map));
}
return $container[0];
};
return control;
}; };