Refactor farm_plan_map to use a farmOS-map behavior.
This commit is contained in:
parent
0eea90dec0
commit
d7fd542069
|
@ -1,35 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* farm_plan_map.default_openlayers_components.inc
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_default_openlayers_components().
|
||||
*/
|
||||
function farm_plan_map_default_openlayers_components() {
|
||||
$export = array();
|
||||
|
||||
$javascript = file_get_contents(drupal_get_path('module', 'farm_plan_map') . '/js/farm_plan_map_layers.js');
|
||||
|
||||
// Define an inline javascript component that adds area and asset layers to
|
||||
// a plan map.
|
||||
/**
|
||||
* @todo
|
||||
* This is just a placeholder until we migrate to
|
||||
* https://github.com/mstenta/farmOS-map
|
||||
*/
|
||||
$ol_component = new stdClass();
|
||||
$ol_component->disabled = FALSE; /* Edit this to true to make a default ol_component disabled initially */
|
||||
$ol_component->api_version = 1;
|
||||
$ol_component->machine_name = 'farm_plan_map_layers';
|
||||
$ol_component->name = 'Farm Plan Map: Layers';
|
||||
$ol_component->description = 'Adds plan asset and area layers to the map via inline JavaScript, so that the plan ID can be appended to the GeoJSON source URLs.';
|
||||
$ol_component->factory_service = 'openlayers.Component:InlineJS';
|
||||
$ol_component->options = array(
|
||||
'javascript' => $javascript,
|
||||
);
|
||||
$export['farm_plan_map_layers'] = $ol_component;
|
||||
|
||||
return $export;
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* farm_plan_map.default_openlayers_maps.inc
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_default_openlayers_maps().
|
||||
*/
|
||||
function farm_plan_map_default_openlayers_maps() {
|
||||
$export = array();
|
||||
|
||||
// Create a plan map.
|
||||
$machine_name = 'farm_plan_map';
|
||||
$ol_map = new stdClass();
|
||||
$ol_map->disabled = FALSE; /* Edit this to true to make a default ol_map disabled initially */
|
||||
$ol_map->api_version = 1;
|
||||
$ol_map->machine_name = $machine_name;
|
||||
$ol_map->name = 'Farm Plan Map';
|
||||
$ol_map->description = '';
|
||||
$ol_map->factory_service = 'openlayers.Map:OLMap';
|
||||
$ol_map->options = array(
|
||||
'width' => 'auto',
|
||||
'height' => '500px',
|
||||
'provideBlockLayerSwitcher' => 0,
|
||||
'view' => array(
|
||||
'center' => array(
|
||||
'lat' => 0,
|
||||
'lon' => 0,
|
||||
),
|
||||
'rotation' => 0,
|
||||
'zoom' => 2,
|
||||
'minZoom' => 0,
|
||||
'maxZoom' => 24,
|
||||
),
|
||||
'renderer' => 'canvas',
|
||||
'layers' => array(),
|
||||
'controls' => array(
|
||||
0 => 'openlayers_control_zoom',
|
||||
1 => 'openlayers_control_scaleline',
|
||||
2 => 'farm_map_control_layer_switcher',
|
||||
3 => 'farm_map_control_geolocate',
|
||||
4 => 'farm_map_control_geocoder',
|
||||
),
|
||||
'interactions' => array(
|
||||
0 => 'openlayers_interaction_doubleclickzoom',
|
||||
1 => 'openlayers_interaction_dragpan',
|
||||
2 => 'openlayers_interaction_mousewheelzoom',
|
||||
3 => 'openlayers_interaction_pinchzoom',
|
||||
),
|
||||
'components' => array(
|
||||
0 => 'farm_plan_map_layers',
|
||||
1 => 'farm_map_component_progress_bar',
|
||||
),
|
||||
);
|
||||
$export[$machine_name] = $ol_map;
|
||||
|
||||
return $export;
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Farm Map hooks implemented by the Farm Plan Map module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_farm_map_behaviors().
|
||||
*/
|
||||
function farm_plan_map_farm_map_behaviors() {
|
||||
return array(
|
||||
'plan' => array(
|
||||
'js' => 'js/farmOS.map.behaviors.plan.js',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_farm_map_view().
|
||||
*/
|
||||
function farm_plan_map_farm_map_view($name, $element) {
|
||||
|
||||
// Add the plan behavior to farm plan maps.
|
||||
if ($name == 'farm_plan_map') {
|
||||
if (!empty($element['#plan_id'])) {
|
||||
farm_map_add_behavior('plan', array('plan_id' => $element['#plan_id'], 'zoom' => TRUE));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,18 +4,6 @@
|
|||
* farm_plan_map.features.inc
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_ctools_plugin_api().
|
||||
*/
|
||||
function farm_plan_map_ctools_plugin_api($module = NULL, $api = NULL) {
|
||||
if ($module == "openlayers" && $api == "default_openlayers_components") {
|
||||
return array("version" => "1");
|
||||
}
|
||||
if ($module == "openlayers" && $api == "default_openlayers_maps") {
|
||||
return array("version" => "1");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_views_api().
|
||||
*/
|
||||
|
|
|
@ -5,10 +5,5 @@ package = farmOS (beta)
|
|||
dependencies[] = farm_area
|
||||
dependencies[] = farm_map
|
||||
dependencies[] = farm_movement
|
||||
dependencies[] = openlayers
|
||||
features[ctools][] = views:views_default:3.0
|
||||
features[ctools][] = openlayers:default_openlayers_components:1
|
||||
features[ctools][] = openlayers:default_openlayers_maps:1
|
||||
features[features_api][] = api:2
|
||||
features[openlayers_components][] = farm_plan_map_layers
|
||||
features[openlayers_maps][] = farm_plan_map
|
||||
|
|
|
@ -29,10 +29,9 @@ function farm_plan_map_entity_view_alter(&$build, $type) {
|
|||
return;
|
||||
}
|
||||
|
||||
// Add the plan ID as a Javascript setting, so that the farm_plan_map_layers
|
||||
// InlineJS component can add it to the GeoJSON URLs.
|
||||
drupal_add_js(array('farm_plan_map' => array('plan_id' => $plan->id)), 'setting');
|
||||
|
||||
// Build the areas map and add it to the page content.
|
||||
$build['farm_plan_map'] = farm_map_build('farm_plan_map');
|
||||
|
||||
// Add the plan ID to the element.
|
||||
$build['farm_plan_map']['#plan_id'] = $plan->id;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
(function () {
|
||||
farmOS.map.behaviors.plan = {
|
||||
attach: function (instance) {
|
||||
|
||||
// Get the plan ID from Drupal.settings.
|
||||
var planId = Drupal.settings.farm_map.behaviors.plan.plan_id;
|
||||
|
||||
// Define the GeoJSON layers that we will include.
|
||||
var layers = [
|
||||
{
|
||||
title: 'Areas',
|
||||
url: '/farm/areas/geojson/all/' + planId,
|
||||
color: 'purple',
|
||||
group: 'Plan',
|
||||
},
|
||||
{
|
||||
title: 'Assets',
|
||||
url: '/farm/assets/geojson/full/all/' + planId,
|
||||
color: 'green',
|
||||
group: 'Plan',
|
||||
},
|
||||
];
|
||||
|
||||
// Add layers to the map.
|
||||
for (var i = 0; i < layers.length; i++) {
|
||||
var layer = instance.addLayer('geojson', layers[i]);
|
||||
|
||||
// If zoom is true, zoom to all vector layers when they load.
|
||||
if (Drupal.settings.farm_map.behaviors.plan.zoom) {
|
||||
var source = layer.getSource();
|
||||
source.on('change', function () {
|
||||
instance.zoomToVectors();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}());
|
|
@ -1,86 +0,0 @@
|
|||
// Get the plan ID from Drupal.settings.
|
||||
var planId = Drupal.settings.farm_plan_map.plan_id;
|
||||
|
||||
// Get the map object.
|
||||
var map = data.map;
|
||||
|
||||
// Define the GeoJSON layers that we will include.
|
||||
var farmPlanMapLayers = [
|
||||
{
|
||||
url: "/farm/areas/geojson/all/" + planId,
|
||||
color: "purple",
|
||||
},
|
||||
{
|
||||
url: "/farm/assets/geojson/full/all/" + planId,
|
||||
color: "green",
|
||||
},
|
||||
];
|
||||
|
||||
// Define color styles.
|
||||
var colors = {
|
||||
purple: "rgba(204,51,102,1)",
|
||||
green: "rgba(51,153,51,1)",
|
||||
};
|
||||
|
||||
// Start an empty extent which we will extend as sources are loaded.
|
||||
var extent = ol.extent.createEmpty();
|
||||
|
||||
// Define a function for zooming to all vector sources within the map.
|
||||
function zoomToVectorSources() {
|
||||
map.getLayers().forEach(function(layer) {
|
||||
if (typeof layer.getSource === "function") {
|
||||
var source = layer.getSource();
|
||||
if (source !== "null" && source instanceof ol.source.Vector) {
|
||||
if (source.getState() === "ready" && source.getFeatures().length > 0) {
|
||||
ol.extent.extend(extent, source.getExtent());
|
||||
var fitOptions = {
|
||||
size: map.getSize(),
|
||||
constrainResolution: false,
|
||||
padding: [20, 20, 20, 20],
|
||||
};
|
||||
map.getView().fit(extent, fitOptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Build each source, layer, and style.
|
||||
for (var i = 0; i < farmPlanMapLayers.length; i++) {
|
||||
|
||||
// Create the style.
|
||||
var fill = new ol.style.Fill({
|
||||
color: "rgba(0,0,0,0)"
|
||||
});
|
||||
var stroke = new ol.style.Stroke({
|
||||
color: colors[farmPlanMapLayers[i].color],
|
||||
width: 2
|
||||
});
|
||||
var style = [
|
||||
new ol.style.Style({
|
||||
image: new ol.style.Circle({
|
||||
fill: fill,
|
||||
stroke: stroke,
|
||||
radius: 4
|
||||
}),
|
||||
fill: fill,
|
||||
stroke: stroke
|
||||
})
|
||||
];
|
||||
|
||||
// Create the source.
|
||||
var source = new ol.source.Vector({
|
||||
url: farmPlanMapLayers[i].url,
|
||||
format: new ol.format.GeoJSON(),
|
||||
});
|
||||
|
||||
// Create the layer and add it to the map.
|
||||
var layer = new ol.layer.Vector({
|
||||
source: source,
|
||||
style: style,
|
||||
});
|
||||
map.addLayer(layer);
|
||||
|
||||
// Zoom to the combined extent of all sources as they are loaded.
|
||||
source.on("change", zoomToVectorSources);
|
||||
}
|
Loading…
Reference in New Issue