Skip to content

Commit

Permalink
Breaking: Use new GeoMapEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnitto committed Nov 5, 2023
1 parent 8e51f55 commit 3df2944
Show file tree
Hide file tree
Showing 20 changed files with 228 additions and 296 deletions.
40 changes: 40 additions & 0 deletions Classes/Migrations/LocationMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Carbon\GeoMap\Migrations;

use Neos\ContentRepository\Domain\Model\NodeData;
use Neos\ContentRepository\Migration\Transformations\AbstractTransformation;
use Neos\Neos\Controller\CreateContentContextTrait;

class LocationMigration extends AbstractTransformation
{
use CreateContentContextTrait;

/**
* @param NodeData $node
* @return boolean
*/
public function isTransformable(NodeData $node)
{
if (!$node->hasProperty('lat') && !$node->hasProperty('lng')) {
return false;
}
return true;
}

/**
* @param NodeData $node
* @return void
*/
public function execute(NodeData $node)
{
$lat = (float)$node->getProperty('lat');
$lng = (float)$node->getProperty('lng');
$node->setProperty('location', [
'lat' => $lat,
'lng' => $lng,
]);
$node->removeProperty('lat');
$node->removeProperty('lng');
}
}
10 changes: 4 additions & 6 deletions Classes/Service/GeocodingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public function nodePropertyChanged(
return;
}

$node->setProperty('lat', '');
$node->setProperty('lng', '');
$node->setProperty('location', null);

if (empty($value) && $propertyName != 'country') {
return;
Expand Down Expand Up @@ -82,8 +81,7 @@ public function nodePropertyChanged(
$latLng = $this->geocodeLatLngFromAddress($address);

if ($latLng) {
$node->setProperty('lat', $latLng['lat']);
$node->setProperty('lng', $latLng['lng']);
$node->setProperty('location', $latLng);
}
}

Expand All @@ -104,8 +102,8 @@ public function geocodeLatLngFromAddress(string $address): ?array
$json = json_decode($jsonContent, true);
if (isset($json[0]) && isset($json[0]['lat']) && isset($json[0]['lon'])) {
return [
'lat' => $json[0]['lat'],
'lng' => $json[0]['lon'],
'lat' => (float)$json[0]['lat'],
'lng' => (float)$json[0]['lon'],
];
}
}
Expand Down
15 changes: 15 additions & 0 deletions Migrations/ContentRepository/Version20231105203142.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
up:
comments: 'Save lat and lng properties to location property'
migration:
- filters:
- type: 'NodeType'
settings:
nodeType: 'Carbon.GeoMap:Mixin.Address'
withSubTypes: true
transformations:
- type: 'Carbon\GeoMap\Migrations\LocationMigration'
settings: []


down:
comments: 'No down migration available'
87 changes: 3 additions & 84 deletions NPM/dist/module.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const DOCUMENT = document;

/*jshint -W030 */
const globalSettings = JSON.parse(
DOCUMENT.currentScript.dataset?.settings || null
DOCUMENT.currentScript.dataset?.settings || null,
);
/*jshint +W030 */

Expand All @@ -25,6 +25,7 @@ function getAddresses(canvas) {
return null;
}
return {
element,
html: element.outerHTML,
popup: !!element.innerHTML,
lat: coordinate.lat,
Expand All @@ -34,98 +35,16 @@ function getAddresses(canvas) {
.filter((element) => element !== null);
}

function getEditor(element, key) {
return element.querySelector(
`.carbon-geomap-coordinatestable__${key} .neos-inline-editable`
)?.firstElementChild;
}

function getLatLngEditors(element) {
const lat = getEditor(element, "lat");
const lng = getEditor(element, "lng");

return lat && lng ? { lat, lng } : null;
}

function updateEditor(editor, value) {
editor.innerText = value.toString();
}

function updateLatLngEditors(editors, values) {
updateEditor(editors.lat, values.lat);
updateEditor(editors.lng, values.lng);
}

function initFrontend({ className, initFunction }) {
[...DOCUMENT.querySelectorAll(`.carbon-geomap.${className}`)].forEach(
(element) => initFunction({ element, live: true })
initFunction,
);
}

function initBackend({ className, initFunction, nodeType }) {
if (
typeof className !== "string" ||
typeof initFunction !== "function" ||
typeof nodeType !== "string"
) {
console.error(
"Invalid backend edit initialization: You need to set className, initFunction and nodeType correctly",
{ className, initFunction, nodeType }
);
return;
}

const liveClassName = `.carbon-geomap--live.${className}`;
const editClassName = `.carbon-geomap--edit.${className}`;
const liveNodes = [...DOCUMENT.querySelectorAll(liveClassName)];
const editContainerNodes = liveNodes.map(
(element) => element.nextElementSibling
);

// Init the live maps
liveNodes.forEach((element) => initFunction({ element, live: true }));

// Observer if something inside the edit view changes
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
const element = mutation.addedNodes[0];
if (element && element.matches(editClassName)) {
initFunction({ element, live: false });
break;
}
}
});

// Re-initialize the edit map if something inside the edit view changes
editContainerNodes.forEach((element) =>
observer.observe(element, { childList: true })
);

// If the user clicks to edit, init the edit map and remove the live map
DOCUMENT.addEventListener("carbonCBD", (event) => {
const { type, mode, element } = event.detail;

if (!type === nodeType || mode === "live") {
return;
}

// Wait for the DOM to be ready
setTimeout(() => {
// Initialize the edit map
[...element.querySelectorAll(editClassName)].forEach((element) =>
initFunction({ element, live: false })
);
}, 10);
});
}

export {
globalSettings,
iconSettings,
getMapCanvas,
getAddresses,
getLatLngEditors,
updateLatLngEditors,
initBackend,
initFrontend,
};
2 changes: 1 addition & 1 deletion NPM/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "carbon-geomap",
"description": "Dependencies for Carbon.GeoMap",
"version": "0.1.2",
"version": "0.2.0",
"main": "dist/module.mjs",
"module": "dist/module.mjs",
"exports": {
Expand Down
12 changes: 6 additions & 6 deletions NodeTypes/Mixin/Address.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'Carbon.GeoMap:Mixin.Address':
abstract: true
superTypes:
'Carbon.GeoMap:Mixin.LatLng': true
'Carbon.GeoMap:Mixin.Location': true
ui:
inspector:
groups:
Expand All @@ -14,29 +14,29 @@
type: string
ui:
label: i18n
reloadIfChanged: true
reloadPageIfChanged: true
showInCreationDialog: true
inspector:
group: address
position: 10
validation:
'Neos.Neos/Validation/NotEmptyValidator': []
'Neos.Neos/Validation/NotEmptyValidator': {}
city:
type: string
ui:
label: i18n
reloadIfChanged: true
reloadPageIfChanged: true
showInCreationDialog: true
inspector:
group: address
position: 20
validation:
'Neos.Neos/Validation/NotEmptyValidator': []
'Neos.Neos/Validation/NotEmptyValidator': {}
country:
type: string
ui:
label: i18n
reloadIfChanged: true
reloadPageIfChanged: true
showInCreationDialog: true
inspector:
group: address
Expand Down
39 changes: 9 additions & 30 deletions NodeTypes/Mixin/AddressElement/AddressElement.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,22 @@ prototype(Carbon.GeoMap:Mixin.AddressElement) < prototype(Neos.Neos:ContentCompo
street = ${q(node).property('street')}
city = ${q(node).property('city')}
country = ${q(node).property('country')}
lat = ${q(node).property('lat')}
lng = ${q(node).property('lng')}
location = ${q(node).property('location')}
content = ${q(node).property('text')}
renderPopup = ${q(node).property('renderPopup')}
renderAddress = ${q(node).property('renderAddress')}
renderCountry = ${q(node).property('renderCountry')}
editable = ${node.context.inBackend}


addressType = 'Carbon.GeoMap:Presentation.Address'
mapType = ${null}
type = 'Carbon.GeoMap:Presentation.Address'

@if.hasAddressTypeAndMapType = ${this.addressType && this.mapType}
@if.hasType = ${this.type}

renderer = Neos.Fusion:Renderer {
type = ${props.addressType}
element = Neos.Fusion:DataStructure {
@apply.props = ${props}
}
@process.wrap = Neos.Fusion:Renderer {
@if.inCBDModeAndHasCoordinates = ${props.cbd && props.lat && props.lng}
type = ${props.mapType}
element.mode = 'edit'
element.editable = true
element.coordinates = ${[{'lat': props.lat, 'lng': props.lng}]}
element.showPopup = ${value}
element.mapStyle = ${q(node).parent().property('mapStyle')}
element.inlineEditor = Carbon.GeoMap:Presentation.CoordinatesTable {
lat = Neos.Neos:Editable {
property = 'lat'
block = false
}
lng = Neos.Neos:Editable {
property = 'lng'
block = false
}
}
}
}
renderer = afx`
<Neos.Fusion:Renderer
type={props.type}
[email protected]={props}
/>
`
}
9 changes: 8 additions & 1 deletion NodeTypes/Mixin/AddressElement/AddressElement.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'Carbon.GeoMap:Mixin.AddressElement':
abstract: true
superTypes:
'Carbon.CBD:Mixin.Element.Item': true
'Neos.Neos:Content': true
'Carbon.GeoMap:Mixin.Name': true
'Carbon.GeoMap:Mixin.Address': true
'Carbon.GeoMap:Mixin.Text': true
label: "${Neos.Node.labelForNode(node).properties('title', 'name', 'text')}"
ui:
label: i18n
icon: map-marker
inlineEditable: true
inspector:
groups:
settings:
Expand Down Expand Up @@ -49,3 +50,9 @@
ui:
inspector:
hidden: 'ClientEval:node.properties.renderPopup == false'


'Neos.Neos:ContentCollection':
constraints:
nodeTypes:
'Carbon.GeoMap:Mixin.AddressElement': false
11 changes: 0 additions & 11 deletions NodeTypes/Mixin/LatLng.yaml

This file was deleted.

15 changes: 15 additions & 0 deletions NodeTypes/Mixin/Location.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'Carbon.GeoMap:Mixin.Location':
abstract: true
properties:
location:
type: array
ui:
label: i18n
reloadPageIfChanged: true
inspector:
group: address
position: end
editor: 'Carbon.GeoMapEditor/Editor'
hidden: 'ClientEval: node.properties.location && node.properties.location.lat && node.properties.location.lng ? false : true'
editorOptions:
searchBar: false
Loading

0 comments on commit 3df2944

Please sign in to comment.