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 pathEvent.php
144 lines (133 loc) · 3.52 KB
/
Event.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
<?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 yii\base\BaseObject;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
/**
* Event
*
* Google maps event
*
* @author Antonio Ramirez <[email protected]>
*
* @link http://www.2amigos.us/
* @package dosamigos\google\maps
*/
class Event extends BaseObject
{
/**
* @var string the action that will trigger the event
*/
public $trigger;
/**
* @var string the javascript code to be executed
*/
public $js;
/**
* @var bool whether to wrap the js code within a javascript function (ie `"function(){ $js }"`)
*/
public $wrap = true;
/**
* @var string the type of event. Defaults to [[EventType::DEFAULT_EVENT]]
*/
private $_type = EventType::DEFAULT_EVENT;
/**
* @inheritdoc
*/
public function init()
{
if (empty($this->trigger)) {
throw new InvalidConfigException('"$trigger" cannot be null.');
}
if (empty($this->js)) {
throw new InvalidConfigException('"js" cannot be null.');
}
parent::init();
}
/**
* Sets the type of event, by default Google Event
*
* @param string $value
*
* @throws \yii\base\InvalidParamException
*/
public function setType($value)
{
if (!EventType::getIsValid($value)) {
throw new InvalidParamException('Unrecognized event type');
}
$this->_type = $value;
}
/**
* Returns type of event
* @return string
*/
public function getType()
{
return $this->_type;
}
/**
* Returns the js function to be executed
* @return string
*/
public function getFunction()
{
return $this->wrap
? "function(event){{$this->js}}"
: $this->js;
}
/**
* Returns the javascript code for attaching a Google event to a javascript object
*
* @param string $name the javascript object name to attach the event to
* @param bool $once whether to make a one time call event or not
*
* @return string
*/
public function getEventJs($name, $once = false)
{
$once = ($once) ? 'Once' : '';
return "google.maps.event.addListener$once($name, '{$this->trigger}', {$this->getFunction()});";
}
/**
* Returns the javascript code for attaching a dom event to a javascript object
*
* @param string $name
* @param bool $once
*
* @return string
*/
public function getDomEventJs($name, $once = false)
{
$once = ($once) ? 'Once' : '';
return "google.maps.event.addDomListener$once($name, '{$this->trigger}', {$this->getFunction()});";
}
/**
* Returns the js code to attach a Google event or a Dom event to a js object
*
* @param string $name the object name
*
* @return string the js event
*/
public function getJs($name)
{
switch ($this->getType()) {
case EventType::DEFAULT_ONCE:
return $this->getEventJs($name, true);
case EventType::DOM:
return $this->getDomEventJs($name);
case EventType::DOM_ONCE:
return $this->getDomEventJs($name, true);
case EventType::DEFAULT_EVENT:
default:
return $this->getEventJs($name);
}
}
}