This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathOverlayTrait.php
123 lines (113 loc) · 3.15 KB
/
OverlayTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*
*
* @copyright Copyright (c) 2013-2019 2amigos
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*
*/
namespace dosamigos\google\maps;
use dosamigos\google\maps\overlays\InfoWindow;
use dosamigos\google\maps\overlays\Marker;
use yii\helpers\ArrayHelper;
use yii\web\JsExpression;
/**
* OverlayTrait
*
* Common methods of overlays
*
* @author Antonio Ramirez <[email protected]>
*
* @link http://www.2amigos.us/
* @package dosamigos\google\maps
*/
trait OverlayTrait
{
/**
*
* Info window object attached to the object
* @var InfoWindow
*/
protected $infoWindow;
/**
*
* If the Info window is shared or not
* @var boolean
*/
protected $isInfoWindowShared = false;
/**
* Attaches a info window to the object
*
* @param InfoWindow $infoWindow
* @param bool $shared
*/
public function attachInfoWindow(InfoWindow $infoWindow, $shared = true)
{
$this->infoWindow = $infoWindow;
$this->isInfoWindowShared = $shared;
}
/**
* Returns the attached info window object
* @return InfoWindow|null
*/
public function getInfoWindow()
{
return $this->infoWindow;
}
/**
* Returns whether the info window attached is shared or not
* @return bool
*/
public function getIsInfoWindowShared()
{
return $this->isInfoWindowShared;
}
/**
* Sets the map on which to display the overlay. The map is assumed to be the variable map name.
*
* @param $map
*/
public function setMap($map)
{
$this->options['map'] = new JsExpression($map);
}
/**
* Returns the initialization code from info window (if any)
* @return array
*/
protected function getInfoWindowJs()
{
$map = ArrayHelper::getValue($this->options, 'map');
if ($this->infoWindow !== null) {
if ($this->isInfoWindowShared) {
$infoWindowName = $map . 'infoWindow';
if ($this instanceof Marker) {
$js =
"$infoWindowName.setContent('{$this->infoWindow->content}');\n" .
"$infoWindowName.open($map, this);\n";
} else {
$js =
"$infoWindowName.close();\n" .
"$infoWindowName.setContent('{$this->infoWindow->content}');\n" .
"$infoWindowName.setPosition({$this->getCenterOfBounds()->getJs()});\n" .
"$infoWindowName.open($map);\n";
}
$this->addEvent(
new Event([
'trigger' => 'click',
'js' => $js
])
);
} else {
$this->addEvent(
new Event([
'trigger' => 'click',
'js' => "{$this->infoWindow->getName()}.open($map, this);\n"
])
);
return [$this->infoWindow->getJs()];
}
}
return [];
}
}