forked from digitick/yii-jquery-gmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EGmap3ActionBase.php
193 lines (176 loc) · 3.66 KB
/
EGmap3ActionBase.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* EGmap3 Yii extension
*
* Object oriented PHP interface to GMAP3 Javascript library for
* Google Maps.
*
* @copyright © Digitick <www.digitick.net> 2011
* @license GNU Lesser General Public License v3.0
* @author Ianaré Sévi
*
*/
/**
* Base class for all actions.
*/
abstract class EGmap3ActionBase extends EGmap3ObjectBase
{
protected $action;
/**
* @var EGmap3OptionBase
*/
protected $options;
protected $callback = array();
protected $events = array();
protected $onces = array();
/**
* Create a new action.
* @param mixed $options Associative array or Options object
*/
public function __construct($options=null)
{
if ($options) {
$this->setOptions($options);
}
}
/**
* Add a JS function to call on a specified event.
*
* The event will fire every time.
*
* @param string $event Event type, ex 'mouseover', 'click'.
* @param string $function Javascript function to execute.
*/
public function addEvent($event, $function)
{
$this->events[$event] = $function;
}
/**
* Get all events.
* @return array
*/
public function getEvents()
{
return $this->events;
}
/**
* Add a JS function to call on a specified event.
*
* The event will only fire once.
*
* @param string $event Event type, ex 'mouseover', 'click'.
* @param string $function Javascript function to execute.
*/
public function addEventOnce($event, $function)
{
$this->onces[$event] = $function;
}
/**
* Get all events that fire once.
* @return array
*/
public function getEventOnces()
{
return $this->onces;
}
protected function eventsToJS(array $events){
$js = 'js:{';
foreach ($events as $event => $function){
$js .= "'$event':$function,";
}
$js = rtrim($js, ',');
$js .= '}';
return $js;
}
/**
* Convert the action object into a Javascript string.
* @return string
*/
public function toJS()
{
if (!empty($this->events)) {
$this->events = $this->eventsToJS($this->events);
}
if (!empty($this->onces)) {
$this->onces = $this->eventsToJS($this->onces);
}
return parent::toJS();
}
/**
* Add a Javascript function to call at the end of the action.
* @param string $callback
*/
public function addCallback($callback)
{
$this->callback[] = 'js:' . $callback;
}
/**
* Get all callbacks.
* @return array
*/
public function getCallbacks()
{
return $this->callback;
}
/**
* Get the options container for this action.
* @return EGmap3OptionBase
*/
public function getOptions()
{
return $this->options;
}
/**
* Set options to apply when creating target object.
* @param mixed $options Associative array or EMapMarkerOptions object
*/
public function setOptions($options)
{
if (is_object($options)) {
$this->setOptionsObject($options);
}
else if (is_array($options)) {
$this->setOptionsArray($options);
}
else {
throw new CException('Inavlid type given for options.');
}
if ($this->options->isEmpty()) {
unset($this->options);
}
else {
$this->options->verifyOptions();
}
}
/**
* Set options object from array.
* @param array $options
*/
protected function setOptionsArray(array $options)
{
if (!is_object($this->options)) {
$optionClass = $this->getOptionsClass();
$this->options = new $optionClass();
}
foreach ($options as $k => $v) {
$this->options->$k = $v;
}
}
protected function getOptionsClass()
{
return get_class($this) . 'Options';
}
/**
* Set options object.
* @param EGmap3OptionBase $options
*/
protected function setOptionsObject(EGmap3OptionBase $options)
{
if (is_object($this->options)) {
$this->options->merge($options);
}
else {
$this->options = $options;
}
}
}