First commit.

This commit is contained in:
Lasse S. Haslev
2020-10-25 18:34:21 +01:00
commit 97065c0377
22 changed files with 26186 additions and 0 deletions

28
src/FieldServiceProvider.php Executable file
View File

@@ -0,0 +1,28 @@
<?php
namespace Lassehaslev\NovaMapFields;
use Laravel\Nova\Nova;
use Laravel\Nova\Events\ServingNova;
use Illuminate\Support\ServiceProvider;
class FieldServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot()
{
Nova::serving(function (ServingNova $event) {
Nova::script('nova-map-fields', __DIR__.'/../dist/js/field.js');
Nova::style('nova-map-fields', __DIR__.'/../dist/css/field.css');
});
}
/**
* Register any application services.
*/
public function register()
{
}
}

109
src/MapField.php Executable file
View File

@@ -0,0 +1,109 @@
<?php
namespace Lassehaslev\NovaMapFields;
use Laravel\Nova\Fields\Field;
use Illuminate\Support\Str;
abstract class MapField extends Field
{
/**
* The field's component.
*
* @var string
*/
public $component = 'nova-map-fields';
/**
* Hide from index field.
*
* @var bool
*/
public $showOnIndex = false;
/**
* Create a new field.
* And set default properties.
*
* @param string $name
* @param string|null $attribute
* @param mixed|null $resolveCallback
*/
public function __construct($name, $attribute = null, $resolveCallback = null)
{
$this->setHeight('250px');
$this->setCenter(0, 0);
$this->setZoom(13);
$this->withMapMeta('type', $this->getMapFieldType());
parent::__construct($name, $attribute = null, $resolveCallback);
}
/**
* Set the height for the map.
*
* @param mixed $height
*
* @return $this
*/
public function setHeight($height)
{
return $this->withMapMeta('height', $height);
}
/**
* Set the zoom for the map.
*
* @param mixed $level
*
* @return $this
*/
public function setZoom($level)
{
return $this->withMapMeta('zoom', $level);
}
/**
* Set the position for the map.
*
* @param mixed $latitude
* @param mixed $longitude
*
* @return $this
*/
public function setCenter($latitude, $longitude)
{
return $this->withMapMeta('center', [
'latitude' => $latitude,
'longitude' => $longitude,
]);
}
/**
* Add meta data for the map.
*
* @param mixed $key
* @param mixed $value
*/
protected function withMapMeta($key, $value)
{
$existingMapMeta = $this->meta['map'] ?? [];
return $this->withMeta([
'map' => array_merge($existingMapMeta, [
$key => $value,
]),
]);
}
/**
* Get component name.
*
* @return string
*/
public function getMapFieldType()
{
return Str::kebab(class_basename($this));
}
}

85
src/Marker.php Executable file
View File

@@ -0,0 +1,85 @@
<?php
namespace Lassehaslev\NovaMapFields;
use Laravel\Nova\Http\Requests\NovaRequest;
class Marker extends MapField
{
/**
* Create a new field.
* And set default properties.
*
* @param string $name
* @param string|null $attribute
* @param mixed|null $resolveCallback
*/
public function __construct($name, $attribute = null, $resolveCallback = null)
{
$this->help('Drag the marker to change the point.');
parent::__construct($name, $attribute = null, $resolveCallback);
}
/**
* Set the latutude field.
*
* @param mixed $fieldName
*
* @return $this
*/
public function setLatitude($fieldName)
{
return $this->withMeta([
'latitudeField' => $fieldName,
]);
}
/**
* Set the longitude field.
*
* @param mixed $fieldName
*
* @return $this
*/
public function setLongitude($fieldName)
{
return $this->withMeta([
'longitudeField' => $fieldName,
]);
}
/**
* Resolve the attribute before sending to frontend.
*
* @param mixed $resource
* @param mixed|null $attribute
*
* @return array
*/
public function resolveAttribute($resource, $attribute = null)
{
return [
'lat' => $resource->{$this->meta['latitudeField']},
'lng' => $resource->{$this->meta['longitudeField']},
];
}
/**
* Hydrate the given attribute on the model based on the incoming request.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param string $requestAttribute
* @param object $model
* @param string $attribute
*/
protected function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute)
{
if ($request->exists($requestAttribute)) {
$latLng = json_decode($request[$requestAttribute]);
$model->{$this->meta['latitudeField']} = $latLng->lat;
$model->{$this->meta['longitudeField']} = $latLng->lng;
}
}
}

39
src/Polyline.php Executable file
View File

@@ -0,0 +1,39 @@
<?php
namespace Lassehaslev\NovaMapFields;
class Polyline extends MapField
{
/**
* Create a new field.
* And set default properties.
*
* @param string $name
* @param string|null $attribute
* @param mixed|null $resolveCallback
*/
public function __construct($name, $attribute = null, $resolveCallback = null)
{
$this->help('Click on the map to create a new point. Drag a marker to change the point. When the map is selected, you can press [backspace] to remove markers.');
parent::__construct($name, $attribute = null, $resolveCallback);
}
/**
* Resolve the attribute before sending to frontend.
*
* @param mixed $resource
* @param mixed|null $attribute
*
* @return array
*/
public function resolveAttribute($resource, $attribute = null)
{
return json_decode($resource->{$attribute});
return [
'lat' => $resource->{$this->meta['latitudeField']},
'lng' => $resource->{$this->meta['longitudeField']},
];
}
}