[build] Build library

This commit is contained in:
amercader
2012-04-25 12:50:58 +01:00
parent 33cf962a5b
commit 4f675cb6aa

View File

@@ -1462,6 +1462,11 @@ my.Map = Backbone.View.extend({
<div class="editor-buttons"> \ <div class="editor-buttons"> \
<button class="btn editor-update-map">Update</button> \ <button class="btn editor-update-map">Update</button> \
</div> \ </div> \
<div class="editor-options" > \
<label class="checkbox"> \
<input type="checkbox" id="editor-auto-zoom" checked="checked" /> \
Auto zoom to features</label> \
</div> \
<input type="hidden" class="editor-id" value="map-1" /> \ <input type="hidden" class="editor-id" value="map-1" /> \
</div> \ </div> \
</form> \ </form> \
@@ -1479,7 +1484,8 @@ my.Map = Backbone.View.extend({
// Define here events for UI elements // Define here events for UI elements
events: { events: {
'click .editor-update-map': 'onEditorSubmit', 'click .editor-update-map': 'onEditorSubmit',
'change .editor-field-type': 'onFieldTypeChange' 'change .editor-field-type': 'onFieldTypeChange',
'change #editor-auto-zoom': 'onAutoZoomChange'
}, },
initialize: function(options) { initialize: function(options) {
@@ -1498,15 +1504,27 @@ my.Map = Backbone.View.extend({
// Listen to changes in the documents // Listen to changes in the documents
this.model.currentDocuments.bind('add', function(doc){self.redraw('add',doc)}); this.model.currentDocuments.bind('add', function(doc){self.redraw('add',doc)});
this.model.currentDocuments.bind('change', function(doc){
self.redraw('remove',doc);
self.redraw('add',doc);
});
this.model.currentDocuments.bind('remove', function(doc){self.redraw('remove',doc)}); this.model.currentDocuments.bind('remove', function(doc){self.redraw('remove',doc)});
this.model.currentDocuments.bind('reset', function(){self.redraw('reset')}); this.model.currentDocuments.bind('reset', function(){self.redraw('reset')});
this.bind('view:show',function(){
// If the div was hidden, Leaflet needs to recalculate some sizes // If the div was hidden, Leaflet needs to recalculate some sizes
// to display properly // to display properly
this.bind('view:show',function(){
if (self.map){ if (self.map){
self.map.invalidateSize(); self.map.invalidateSize();
if (self._zoomPending && self.autoZoom) {
self._zoomToFeatures();
self._zoomPending = false;
} }
}
self.visible = true;
});
this.bind('view:hide',function(){
self.visible = false;
}); });
var stateData = _.extend({ var stateData = _.extend({
@@ -1518,6 +1536,7 @@ my.Map = Backbone.View.extend({
); );
this.state = new recline.Model.ObjectState(stateData); this.state = new recline.Model.ObjectState(stateData);
this.autoZoom = true;
this.mapReady = false; this.mapReady = false;
this.render(); this.render();
}, },
@@ -1583,6 +1602,13 @@ my.Map = Backbone.View.extend({
this.features.clearLayers(); this.features.clearLayers();
this._add(this.model.currentDocuments.models); this._add(this.model.currentDocuments.models);
} }
if (action != 'reset' && this.autoZoom){
if (this.visible){
this._zoomToFeatures();
} else {
this._zoomPending = true;
}
}
} }
}, },
@@ -1629,6 +1655,10 @@ my.Map = Backbone.View.extend({
} }
}, },
onAutoZoomChange: function(e){
this.autoZoom = !this.autoZoom;
},
// Private: Add one or n features to the map // Private: Add one or n features to the map
// //
// For each document passed, a GeoJSON geometry will be extracted and added // For each document passed, a GeoJSON geometry will be extracted and added
@@ -1656,7 +1686,9 @@ my.Map = Backbone.View.extend({
// TODO: mustache? // TODO: mustache?
html = '' html = ''
for (key in doc.attributes){ for (key in doc.attributes){
html += '<div><strong>' + key + '</strong>: '+ doc.attributes[key] + '</div>' if (!(self.state.get('geomField') && key == self.state.get('geomField'))){
html += '<div><strong>' + key + '</strong>: '+ doc.attributes[key] + '</div>';
}
} }
feature.properties = {popupContent: html}; feature.properties = {popupContent: html};
@@ -1707,19 +1739,22 @@ my.Map = Backbone.View.extend({
_getGeometryFromDocument: function(doc){ _getGeometryFromDocument: function(doc){
if (this.geomReady){ if (this.geomReady){
if (this.state.get('geomField')){ if (this.state.get('geomField')){
var value = doc.get(this.state.get('geomField'));
if (typeof(value) === 'string'){
// We have a GeoJSON string representation
return $.parseJSON(value);
} else {
// We assume that the contents of the field are a valid GeoJSON object // We assume that the contents of the field are a valid GeoJSON object
return doc.attributes[this.state.get('geomField')]; return value;
}
} else if (this.state.get('lonField') && this.state.get('latField')){ } else if (this.state.get('lonField') && this.state.get('latField')){
// We'll create a GeoJSON like point object from the two lat/lon fields // We'll create a GeoJSON like point object from the two lat/lon fields
var lon = doc.get(this.state.get('lonField')); var lon = doc.get(this.state.get('lonField'));
var lat = doc.get(this.state.get('latField')); var lat = doc.get(this.state.get('latField'));
if (lon && lat) { if (!isNaN(parseFloat(lon)) && !isNaN(parseFloat(lat))) {
return { return {
type: 'Point', type: 'Point',
coordinates: [ coordinates: [lon,lat]
doc.attributes[this.state.get('lonField')],
doc.attributes[this.state.get('latField')]
]
}; };
} }
} }
@@ -1761,6 +1796,18 @@ my.Map = Backbone.View.extend({
return null; return null;
}, },
// Private: Zoom to map to current features extent if any, or to the full
// extent if none.
//
_zoomToFeatures: function(){
var bounds = this.features.getBounds();
if (bounds){
this.map.fitBounds(bounds);
} else {
this.map.setView(new L.LatLng(0, 0), 2);
}
},
// Private: Sets up the Leaflet map control and the features layer. // Private: Sets up the Leaflet map control and the features layer.
// //
// The map uses a base layer from [MapQuest](http://www.mapquest.com) based // The map uses a base layer from [MapQuest](http://www.mapquest.com) based
@@ -1785,6 +1832,24 @@ my.Map = Backbone.View.extend({
} }
}); });
// This will be available in the next Leaflet stable release.
// In the meantime we add it manually to our layer.
this.features.getBounds = function(){
var bounds = new L.LatLngBounds();
this._iterateLayers(function (layer) {
if (layer instanceof L.Marker){
bounds.extend(layer.getLatLng());
} else {
if (layer.getBounds){
bounds.extend(layer.getBounds().getNorthEast());
bounds.extend(layer.getBounds().getSouthWest());
}
}
}, this);
return (typeof bounds.getNorthEast() !== 'undefined') ? bounds : null;
}
this.map.addLayer(this.features); this.map.addLayer(this.features);
this.map.setView(new L.LatLng(0, 0), 2); this.map.setView(new L.LatLng(0, 0), 2);
@@ -3404,6 +3469,8 @@ this.recline.Backend = this.recline.Backend || {};
var options = options || {}; var options = options || {};
var trm = options.trim; var trm = options.trim;
var separator = options.separator || ','; var separator = options.separator || ',';
var delimiter = options.delimiter || '"';
var cur = '', // The character we are currently processing. var cur = '', // The character we are currently processing.
inQuote = false, inQuote = false,
@@ -3451,8 +3518,8 @@ this.recline.Backend = this.recline.Backend || {};
field = ''; field = '';
fieldQuoted = false; fieldQuoted = false;
} else { } else {
// If it's not a ", add it to the field buffer // If it's not a delimiter, add it to the field buffer
if (cur !== '"') { if (cur !== delimiter) {
field += cur; field += cur;
} else { } else {
if (!inQuote) { if (!inQuote) {
@@ -3460,9 +3527,9 @@ this.recline.Backend = this.recline.Backend || {};
inQuote = true; inQuote = true;
fieldQuoted = true; fieldQuoted = true;
} else { } else {
// Next char is ", this is an escaped " // Next char is delimiter, this is an escaped delimiter
if (s.charAt(i + 1) === '"') { if (s.charAt(i + 1) === delimiter) {
field += '"'; field += delimiter;
// Skip the next char // Skip the next char
i += 1; i += 1;
} else { } else {