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
}).addTo(map);
L.control.note({
L.OSM.note({
position: 'topright',
uiPane: uiPane
}).addTo(map);
L.control.share({
L.OSM.share({
getUrl: getShortUrl,
uiPane: uiPane
}).addTo(map);

View file

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

View file

@ -1,15 +1,10 @@
L.OSM.Layers = L.Control.extend({
onAdd: function (map) {
this._map = map;
this._initLayout();
return this.$container[0];
},
L.OSM.layers = function(options) {
var control = L.control(options);
_initLayout: function () {
var map = this._map,
layers = this.options.layers;
control.onAdd = function (map) {
var layers = options.layers;
this.$container = $('<div>')
var $container = $('<div>')
.attr('class', 'control-layers');
var link = $('<a>')
@ -17,20 +12,21 @@ L.OSM.Layers = L.Control.extend({
.attr('href', '#')
.attr('title', 'Layers')
.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') {
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>')
.addClass('overlay-layers')
.appendTo(this.$ui);
.appendTo($ui);
$('<p>')
.text(I18n.t('javascripts.map.layers.overlays'))
@ -72,7 +68,7 @@ L.OSM.Layers = L.Control.extend({
var baseSection = $('<section>')
.addClass('base-layers')
.appendTo(this.$ui);
.appendTo($ui);
$('<p>')
.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) {
e.stopPropagation();
e.preventDefault();
var controlContainer = $('.leaflet-control-container .leaflet-top.leaflet-right');
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')) {
$(this.options.uiPane).hide();
controlContainer.css({paddingRight: '0'});
} else {
$(this.options.uiPane).show();
controlContainer.css({paddingRight: '230px'});
$ui.find('.base-layers .leaflet-container').each(function() {
$(this).data('map').invalidateSize();
});
}
this.$ui.find('.base-layers .leaflet-container').each(function() {
$(this).data('map').invalidateSize();
});
}
});
return $container[0];
};
L.OSM.layers = function(options) {
return new L.OSM.Layers(options);
return control;
};

View file

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

View file

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