-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: update API, add new GoogleMapField
- Loading branch information
Showing
10 changed files
with
297 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#map-field { | ||
height:500px; | ||
} | ||
|
||
.controls { | ||
background-color: #fff; | ||
border-radius: 2px; | ||
border: 1px solid transparent; | ||
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3); | ||
box-sizing: border-box; | ||
font-family: Roboto !important; | ||
font-size: 15px !important; | ||
font-weight: 300; | ||
height: 29px; | ||
margin-left: 17px; | ||
margin-top: 10px; | ||
outline: none; | ||
padding: 0 11px 0 13px; | ||
text-overflow: ellipsis; | ||
width: 400px; | ||
} | ||
|
||
.controls:focus { | ||
border-color: #4d90fe; | ||
} | ||
|
||
.title { | ||
font-weight: bold; | ||
} | ||
|
||
#map #infowindow-content { | ||
display: inline; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
function initMap() { | ||
const map = new google.maps.Map(document.getElementById("map-field"), { | ||
center: { lat: -33.8688, lng: 151.2195 }, | ||
zoom: 13, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
function initMap() { | ||
const position = { lat: -33.8688, lng: 151.2195 }; | ||
const existingPlace = document.getElementById("place-id"); | ||
|
||
const map = new google.maps.Map(document.getElementById("map-field"), { | ||
center: position, | ||
zoom: 13, | ||
mapId: "DEMO_MAP_ID" | ||
}); | ||
|
||
const input = document.getElementById("pac-input"); | ||
const autocomplete = new google.maps.places.Autocomplete(input); | ||
|
||
autocomplete.bindTo("bounds", map); | ||
|
||
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); | ||
|
||
const infowindow = new google.maps.InfoWindow({content: null, ariaLabel: 'Test'}); | ||
const infowindowContent = document.getElementById("infowindow-content"); | ||
|
||
infowindow.setContent(infowindowContent); | ||
|
||
const marker = new google.maps.marker.AdvancedMarkerElement({ | ||
map: map, | ||
position: null, | ||
}); | ||
|
||
if (existingPlace.value) { | ||
const geocoder = new google.maps.Geocoder(); | ||
geocoder | ||
.geocode({ placeId: existingPlace.value }) | ||
.then(({ results }) => { | ||
const existingCoords = results[0].geometry.location; | ||
marker.position = existingCoords; | ||
map.setCenter(existingCoords); | ||
}); | ||
} | ||
|
||
autocomplete.addListener("place_changed", function() { | ||
marker.position = null; | ||
infowindow.close(); | ||
|
||
const place = autocomplete.getPlace(); | ||
|
||
if (!place.geometry || !place.geometry.location) { | ||
return; | ||
} | ||
|
||
if (place.geometry.viewport) { | ||
map.fitBounds(place.geometry.viewport); | ||
} else { | ||
map.setCenter(place.geometry.location); | ||
map.setZoom(17); | ||
} | ||
|
||
marker.position = place.geometry.location; | ||
|
||
infowindowContent.children.namedItem("place-id").textContent = place.place_id; | ||
// set href | ||
infowindowContent.children.namedItem("place-id").href = `javascript:setPlaceValue('${place.place_id}')`; | ||
infowindowContent.children.namedItem("place-address").textContent = place.formatted_address; | ||
infowindow.open(map, marker); | ||
|
||
let heading = document.createElement('div'); | ||
heading.textContent = place.name; | ||
heading.style.fontWeight = 'bold'; | ||
|
||
infowindow.setHeaderContent(heading); | ||
}); | ||
} | ||
|
||
function setPlaceValue(placeId) { | ||
document.getElementById("Form_ItemEditForm_PlaceID").value = placeId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
namespace Iliain\GoogleConfig\Fields; | ||
|
||
use SilverStripe\Core\Environment; | ||
use SilverStripe\View\Requirements; | ||
use SilverStripe\Forms\DatalessField; | ||
|
||
class GoogleMapField extends DatalessField | ||
{ | ||
/** | ||
* The type of map to be displayed | ||
* @var string | ||
*/ | ||
protected $mapType; | ||
|
||
/** | ||
* An array of URL parameters to be appended to the Google Maps API URL | ||
* @var array | ||
*/ | ||
protected $urlParams; | ||
|
||
const TYPE_DEFAULT = 'default'; | ||
|
||
const TYPE_PLACES = 'places'; | ||
|
||
/** | ||
* @param string $name | ||
* @param string $mapType | ||
* @param array $urlParams | ||
*/ | ||
public function __construct($name, $mapType = null, $urlParams = []) | ||
{ | ||
$this->setMapType($mapType); | ||
$this->setUrlParams($urlParams); | ||
|
||
parent::__construct($name); | ||
} | ||
|
||
/** | ||
* @param string $type | ||
* | ||
* @return $this | ||
*/ | ||
public function setMapType($mapType) | ||
{ | ||
if ($mapType) { | ||
$this->mapType = $mapType; | ||
} else { | ||
$this->mapType = self::TYPE_DEFAULT; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getMapType() | ||
{ | ||
return $this->mapType; | ||
} | ||
|
||
/** | ||
* @param string $type | ||
* | ||
* @return $this | ||
*/ | ||
public function setUrlParams($urlParams) | ||
{ | ||
$urlParams['loading'] = 'async'; | ||
$urlParams['callback'] = 'initMap'; | ||
$urlParams['v'] = 'weekly'; | ||
|
||
if ($this->getMapType() === self::TYPE_PLACES) { | ||
$urlParams['libraries'] = 'places,marker'; | ||
} | ||
|
||
$this->urlParams = $urlParams; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getUrlParams() | ||
{ | ||
return $this->urlParams; | ||
} | ||
|
||
public function Field($properties = []) | ||
{ | ||
Requirements::css('iliain/silverstripe-google-config: client/css/map-field.css'); | ||
|
||
switch ($this->getMapType()) { | ||
case self::TYPE_PLACES: | ||
Requirements::javascript('iliain/silverstripe-google-config: client/javascript/places-map.js', ['defer' => true]); | ||
break; | ||
default: | ||
Requirements::javascript('iliain/silverstripe-google-config: client/javascript/default-map.js', ['defer' => true]); | ||
break; | ||
} | ||
|
||
Requirements::javascript($this->getMapURL(), ['async' => true, 'defer' => true]); | ||
|
||
$properties['MapType'] = $this->getMapType(); | ||
|
||
// Get the place ID from object loading this field | ||
$properties['PlaceID'] = $this->getForm()->getRecord()->PlaceID; | ||
|
||
return parent::Field($properties); | ||
} | ||
|
||
/** | ||
* Returns the URL for the Google Maps API with relevant parameters | ||
* @return string | ||
*/ | ||
public function getMapURL() | ||
{ | ||
if (!Environment::getEnv('GOOGLE_MAPS_API_KEY')) { | ||
throw new \Exception('No Google Maps API key set'); | ||
} | ||
|
||
$url = 'https://maps.googleapis.com/maps/api/js?key=' . Environment::getEnv('GOOGLE_MAPS_API_KEY'); | ||
|
||
if ($this->getUrlParams()) { | ||
$url .= '&' . http_build_query($this->getUrlParams()); | ||
} | ||
|
||
return $url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<div id="map-holder"> | ||
<% if $MapType == 'places' %> | ||
<div style="display: none"> | ||
<input | ||
id="pac-input" | ||
class="controls" | ||
type="text" | ||
placeholder="Enter a location" | ||
/> | ||
</div> | ||
<% end_if %> | ||
|
||
<% if $PlaceID %> | ||
<div style="display: none"> | ||
<input type="hidden" | ||
id="place-id" | ||
value="{$PlaceID}" | ||
/> | ||
</div> | ||
<% end_if %> | ||
|
||
<div id="map-field"><a href="javascript:window.location.href=window.location.href">Click to reload map</a></div> | ||
|
||
<% if $MapType == 'places' %> | ||
<div style="display: none;"> | ||
<div id="infowindow-content"> | ||
<span id="place-address"></span> | ||
<br /><br /> | ||
<strong>Place ID:</strong> <a href="" id="place-id"></a> | ||
</div> | ||
</div> | ||
<% end_if %> | ||
</div> |