Create an editable Movement layer that updates with area geometry.
This commit is contained in:
parent
baa8fd5329
commit
6f231744f1
|
@ -58,12 +58,22 @@ function farm_livestock_move_form($form, &$form_state) {
|
|||
'#title' => t('Moving to'),
|
||||
'#description' => t('Enter the name of the area that animals are moving to. A list of existing area options will appear as you type. If the area does not exist, a new one will be created.'),
|
||||
'#autocomplete_path' => 'taxonomy/autocomplete/field_farm_area',
|
||||
'#ajax' => array(
|
||||
'callback' => 'farm_livestock_move_form_next_location_ajax',
|
||||
),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
|
||||
// Load the Moving To location geom in a hidden text field.
|
||||
$form['move']['area']['next_location'] = array(
|
||||
'#type' => 'hidden',
|
||||
'#value' => '',
|
||||
'#prefix' => '<div id="next-location">',
|
||||
'#suffix' => '</div>',
|
||||
);
|
||||
|
||||
// Geometry
|
||||
// Add a farmOS map instance with the WKT and drawing controls.
|
||||
$wkt = '';
|
||||
// Add a farmOS map instance.
|
||||
$form['move']['area']['geometry'] = array(
|
||||
'#type' => 'fieldset',
|
||||
'#title' => t('Geometry'),
|
||||
|
@ -74,13 +84,11 @@ function farm_livestock_move_form($form, &$form_state) {
|
|||
$form['move']['area']['geometry']['map'] = array(
|
||||
'#type' => 'farm_map',
|
||||
'#map_name' => 'farm_movement',
|
||||
'#wkt' => $wkt,
|
||||
'#edit' => TRUE,
|
||||
);
|
||||
$form['move']['area']['geometry']['data'] = array(
|
||||
'#type' => 'textarea',
|
||||
'#title' => t('Data'),
|
||||
'#default_value' => $wkt,
|
||||
'#default_value' => '',
|
||||
);
|
||||
|
||||
// Observations
|
||||
|
@ -241,6 +249,42 @@ function farm_livestock_move_form_animal_location_ajax($form, $form_state) {
|
|||
return array('#type' => 'ajax', '#commands' => $commands);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajax callback for farm_livestock_move_form().
|
||||
*/
|
||||
function farm_livestock_move_form_next_location_ajax($form, $form_state) {
|
||||
|
||||
// If the location is available, load areas.
|
||||
$areas = array();
|
||||
if (!empty($form_state['values']['move']['area']['name'])) {
|
||||
$areas = farm_term_parse_names($form_state['values']['move']['area']['name'], 'farm_areas', FALSE);
|
||||
}
|
||||
|
||||
// Get the "wkt" form element and CSS selector.
|
||||
$element = $form['move']['area']['next_location'];
|
||||
$selector = '#next-location';
|
||||
|
||||
// Update hidden wkt with area geometry.
|
||||
$area_ids = array();
|
||||
foreach($areas as $area) {
|
||||
$area_ids[] = $area->tid;
|
||||
}
|
||||
$geom = farm_area_extract_geoms($area_ids);
|
||||
$element['#value'] = $geom;
|
||||
|
||||
// Assemble commands...
|
||||
$commands = array();
|
||||
|
||||
// Replace the hidden field.
|
||||
$commands[] = ajax_command_replace($selector, render($element));
|
||||
|
||||
// Execute Javascript to add WKT to the map.
|
||||
$commands[] = array('command' => 'updateMovementLayer');
|
||||
|
||||
// Return ajax commands.
|
||||
return array('#type' => 'ajax', '#commands' => $commands);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit function for movement quick form.
|
||||
*/
|
||||
|
|
|
@ -7,4 +7,13 @@
|
|||
farmOS.map.behaviors.move.previewCurrentLocation(wkt);
|
||||
}
|
||||
}
|
||||
|
||||
// Define a Drupal ajax command for loading the Movement To area wkt
|
||||
// from a hidden input field and preview it as an editable layer in the map.
|
||||
Drupal.ajax.prototype.commands.updateMovementLayer = function() {
|
||||
var wkt = $('#next-location input[name="move[area][next_location]"]').val();
|
||||
if (wkt) {
|
||||
farmOS.map.behaviors.move.updateMovementLayer(wkt);
|
||||
}
|
||||
}
|
||||
}(jQuery));
|
||||
|
|
|
@ -10,10 +10,17 @@
|
|||
*/
|
||||
this.instance = instance;
|
||||
|
||||
// When features are changed in the map, drop the WKT into the data field.
|
||||
instance.edit.wktOn('featurechange', function(wkt) {
|
||||
$('#' + instance.target).parent().find('textarea').val(wkt);
|
||||
});
|
||||
// Create an editable movement layer.
|
||||
// Init as an empty vector layer, the layer can be recreated
|
||||
// as an editable WKT layer later.
|
||||
var opts = {
|
||||
title: 'Movement',
|
||||
color: 'blue',
|
||||
};
|
||||
this.movementLayer = this.instance.addLayer('vector', opts);
|
||||
|
||||
// Make the layer editable.
|
||||
this.instance.addBehavior('edit', { layer: this.movementLayer });
|
||||
},
|
||||
|
||||
// Update the assets current location map layer.
|
||||
|
@ -37,6 +44,43 @@
|
|||
this.instance.zoomToLayer(this.currentLocationLayer);
|
||||
},
|
||||
|
||||
// Recreate the Movement map layer.
|
||||
updateMovementLayer: function (wkt) {
|
||||
|
||||
// Remove current location layer.
|
||||
if (this.movementLayer) {
|
||||
this.instance.map.removeLayer(this.movementLayer);
|
||||
this.movementLayer = null;
|
||||
}
|
||||
|
||||
// Create current location layer with the WKT.
|
||||
// Do not put the layer inside a group, because map.removeLayer() (used
|
||||
// above) does not recurse into layer groups.
|
||||
var opts = {
|
||||
title: 'Movement',
|
||||
color: 'blue',
|
||||
wkt: wkt,
|
||||
};
|
||||
this.movementLayer = this.instance.addLayer('wkt', opts);
|
||||
|
||||
// Make the layer editable.
|
||||
this.instance.addBehavior('edit', { layer: this.movementLayer });
|
||||
|
||||
// Zoom to the new layer.
|
||||
this.instance.zoomToLayer(this.movementLayer);
|
||||
|
||||
// Save the map instance ID.
|
||||
const target = this.instance.target;
|
||||
|
||||
// Update the data field with the selected areas WKT.
|
||||
$('#' + target).parent().find('textarea').val(wkt);
|
||||
|
||||
// When features are changed in the map, drop the WKT into the data field.
|
||||
this.instance.edit.wktOn('featurechange', function(wkt) {
|
||||
$('#' + target).parent().find('textarea').val(wkt);
|
||||
});
|
||||
},
|
||||
|
||||
// Make sure this runs after farmOS.map.behaviors.wkt.
|
||||
weight: 101,
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue