Skip to content

Commit

Permalink
Add show field
Browse files Browse the repository at this point in the history
  • Loading branch information
z-song committed May 22, 2019
1 parent 4671ff0 commit 6318ed6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 35 deletions.
67 changes: 67 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,78 @@

namespace Encore\Admin\Latlong;

use Encore\Admin\Admin;
use Encore\Admin\Extension as BaseExtension;

class Extension extends BaseExtension
{
public $name = 'latlong';

public $views = __DIR__.'/../resources/views';

/**
* @var array
*/
protected static $providers = [
'baidu' => Map\Baidu::class,
'tencent' => Map\Tencent::class,
'amap' => Map\Amap::class,
'google' => Map\Google::class,
'yandex' => Map\Yandex::class,
];

/**
* @var Map\AbstractMap
*/
protected static $provider;

/**
* @param string $name
* @return Map\AbstractMap
*/
public static function getProvider($name = '')
{
if (static::$provider) {
return static::$provider;
}

$name = Extension::config('default', $name);
$args = Extension::config("providers.$name", []);

return static::$provider = new static::$providers[$name](...array_values($args));
}

/**
* @return \Closure
*/
public static function showField()
{
return function ($lat, $lng, $height = 300) {

return $this->unescape()->as(function () use ($lat, $lng, $height) {

$lat = $this->{$lat};
$lng = $this->{$lng};

$id = ['lat' => 'lat', 'lng' => 'lng'];

Admin::script(Extension::getProvider()->applyScript($id));

return <<<HTML
<div class="row">
<div class="col-md-3">
<input id="{$id['lat']}" class="form-control" value="{$lat}"/>
</div>
<div class="col-md-3">
<input id="{$id['lng']}" class="form-control" value="{$lng}"/>
</div>
</div>
<br>
<div id="map_{$id['lat']}{$id['lng']}" style="width: 100%;height: {$height}px"></div>
HTML;
});
};
}
}
37 changes: 2 additions & 35 deletions src/Latlong.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Encore\Admin\Latlong;

use Encore\Admin\Form\Field;
use Encore\Admin\Latlong\Map;

class Latlong extends Field
{
Expand All @@ -14,22 +13,6 @@ class Latlong extends Field
*/
protected $column = [];

/**
* @var array
*/
protected static $providers = [
'baidu' => Map\Baidu::class,
'tencent' => Map\Tencent::class,
'amap' => Map\Amap::class,
'google' => Map\Google::class,
'yandex' => Map\Yandex::class,
];

/**
* @var Map\AbstractMap
*/
protected static $provider;

/**
* @var string
*/
Expand All @@ -49,23 +32,7 @@ class Latlong extends Field
*/
public static function getAssets()
{
return ['js' => static::getProvider()->getAssets()];
}

/**
* @param string $name
* @return Map\AbstractMap
*/
public static function getProvider($name = '')
{
if (static::$provider) {
return static::$provider;
}

$name = Extension::config('default', $name);
$args = Extension::config("providers.$name", []);

return static::$provider = new static::$providers[$name](...array_values($args));
return ['js' => Extension::getProvider()->getAssets()];
}

/**
Expand Down Expand Up @@ -105,7 +72,7 @@ public function height($height = 300)
*/
public function render()
{
$this->script = static::getProvider()->applyScript($this->id);
$this->script = Extension::getProvider()->applyScript($this->id);

return parent::render()->with(['height' => $this->height]);
}
Expand Down
2 changes: 2 additions & 0 deletions src/LatlongServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Encore\Admin\Admin;
use Encore\Admin\Form;
use Encore\Admin\Show;
use Illuminate\Support\ServiceProvider;

class LatlongServiceProvider extends ServiceProvider
Expand All @@ -21,6 +22,7 @@ public function boot(Extension $extension)

Admin::booting(function () {
Form::extend('latlong', Latlong::class);
Show\Field::macro('latlong', Extension::showField());
});
}
}

0 comments on commit 6318ed6

Please sign in to comment.