Skip to content

Commit

Permalink
Refactor map area handling to allow extending map shapes
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Nov 14, 2024
1 parent fcb489d commit fc7a1dc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
17 changes: 9 additions & 8 deletions app/admin/formwidgets/MapArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Admin\Traits\FormModelWidget;
use Admin\Traits\ValidatesForm;
use Igniter\Flame\Exception\ApplicationException;
use Igniter\Flame\Html\HtmlFacade as Html;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;

Expand Down Expand Up @@ -102,7 +101,7 @@ public function loadAssets()
if (strlen($key = setting('maps_api_key'))) {
$url = 'https://maps.googleapis.com/maps/api/js?key=%s&libraries=geometry';
$this->addJs(sprintf($url, $key),
['name' => 'google-maps-js', 'async' => null, 'defer' => null]
['name' => 'google-maps-js', 'async' => null, 'defer' => null],
);
}

Expand Down Expand Up @@ -175,14 +174,14 @@ public function onSaveRecord()

$modelsToSave = $this->prepareModelsToSave($model, $saveData);

DB::transaction(function () use ($modelsToSave) {
DB::transaction(function() use ($modelsToSave) {
foreach ($modelsToSave as $modelToSave) {
$modelToSave->saveOrFail();
}
});

flash()->success(sprintf(lang('admin::lang.alert_success'),
'Area '.($form->context == 'create' ? 'created' : 'updated')
'Area '.($form->context == 'create' ? 'created' : 'updated'),
))->now();

$this->formField->value = null;
Expand Down Expand Up @@ -216,11 +215,11 @@ public function onDeleteArea()
];
}

public function getMapShapeAttributes($area)
public function getMapAreaShapes($area)
{
$areaColor = $area->color;

$attributes = [
$attributes = collect()->push([
'data-id' => $area->area_id ?? 1,
'data-name' => $area->name ?? '',
'data-default' => $area->type ?? 'address',
Expand All @@ -234,9 +233,11 @@ public function getMapShapeAttributes($area)
'strokeColor' => $areaColor,
'distanceUnit' => setting('distance_unit'),
]),
];
]);

$this->fireSystemEvent('maparea.extendMapAreaShapes', [$area, $attributes]);

return Html::attributes($attributes);
return $attributes;
}

protected function getMapAreas()
Expand Down
4 changes: 3 additions & 1 deletion app/admin/formwidgets/maparea/area_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-hidden="true"></button>
</div>
<input type="hidden" name="areaId" value="{{ $formAreaId }}">
<input type="hidden" data-map-shape {!! $this->getMapShapeAttributes($formWidget->model) !!}>
@foreach($this->getMapAreaShapes($formWidget->model) as $mapShape)
<input type="hidden" data-map-shape {!! Html::attributes($mapShape) !!}>
@endforeach
<div class="modal-body">
<div class="form-fields p-0">
@foreach ($formWidget->getFields() as $field)
Expand Down
4 changes: 2 additions & 2 deletions app/admin/formwidgets/mapview/assets/js/mapview.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@
this.$mapView.css('height', this.options.mapHeight)

var mapCenter = new google.maps.LatLng(
parseFloat(this.options.mapCenter.lat),
parseFloat(this.options.mapCenter.lng)
parseFloat(this.options.mapCenter.lat),
parseFloat(this.options.mapCenter.lng)
),
mapOptions = {
zoom: this.options.mapZoom,
Expand Down

0 comments on commit fc7a1dc

Please sign in to comment.