This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
EGMapEvent.php
154 lines (146 loc) · 5.13 KB
/
EGMapEvent.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
<?php
/**
*
* EGMapEvent
*
* Modified by Antonio Ramirez
* @since 2011-01-22
* @link http://www.ramirezcobos.com
*
* changeLog:
*
* 2011-07-02 Included support for addDOMEventListenerOnce and addEventListenerOnce
* modified by: Johnatan -http://www.yiiframework.com/forum/index.php?/user/7513-johnatan/)
* 2011-01-22 Included toJs for custom event type selection (DOM or DEFAULT)
*
* A googleMap Event
* @author Fabrice Bernhard
*
*
* @copyright
* info as this library is a modified version of Fabrice Bernhard
*
* Copyright (c) 2008 Fabrice Bernhard
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
class EGMapEvent
{
const TYPE_EVENT_DEFAULT = 'DEFAULT';
const TYPE_EVENT_DEFAULT_ONCE = 'DEFAULT_ONCE';
const TYPE_EVENT_DOM = 'DOM';
const TYPE_EVENT_DOM_ONCE = 'DOM_ONCE';
protected $trigger;
protected $function;
protected $encapsulate_function;
protected $type = self::TYPE_EVENT_DEFAULT;
/**
* @param string $trigger action that will trigger the event
* @param string $function the javascript function to be executed
* @param string $encapsulate_function
* @author Fabrice Bernhard
*/
public function __construct($trigger,$function,$encapsulate_function=true, $type=self::TYPE_EVENT_DEFAULT)
{
$this->trigger = $trigger;
$this->function = $function;
$this->encapsulate_function = $encapsulate_function;
$this->setType($type);
}
/**
*
* Sets the type of event, by default Google Event
* @param string $type
* @throws CException
*/
public function setType( $type ){
if( $type !== self::TYPE_EVENT_DEFAULT && $type !== self::TYPE_EVENT_DEFAULT_ONCE &&
$type !== self::TYPE_EVENT_DOM && $type !== self::TYPE_EVENT_DOM_ONCE )
throw new CException( Yii::t('EGMap', 'Unrecognized Event type') );
$this->type = $type;
}
/**
*
* Returns type of event
* @return string
*/
public function getType( ){
return $this->type;
}
/**
* @return string $trigger action that will trigger the event
*/
public function getTrigger()
{
return $this->trigger;
}
/**
* @return string $function the javascript function to be executed
*/
public function getFunction()
{
if (!$this->encapsulate_function)
return $this->function;
else
return 'function() {'.$this->function.'}';
}
/**
* returns the javascript code for attaching a Google event to a javascript_object
*
* @param string $js_object_name
* @return string
* @author Fabrice Bernhard
* @since 2011-07-02 Johnatan added Once support
*/
public function getEventJs($js_object_name, $once=false)
{
$once = ($once)?'Once':'';
return 'google.maps.event.addListener'.$once.'('.$js_object_name.', "'.$this->getTrigger().'", '.$this->getFunction().');'.PHP_EOL;
}
/**
* returns the javascript code for attaching a dom event to a javascript_object
*
* @param string $js_object_name
* @return string
* @author Fabrice Bernhard
* @since 2011-07-02 Johnatan
*/
public function getDomEventJs($js_object_name, $once=false)
{
$once = ($once)?'Once':'';
return 'google.maps.event.addDomListener'.$once.'('.$js_object_name.', "'.$this->getTrigger().'", '.$this->getFunction().');'.PHP_EOL;
}
/**
* returns the javascript code for attaching a Google event or a dom event to a javascript_object
*
* @param string $js_object_name
* @return string of event type
* @author Antonio Ramirez
* @since 2011-07-02 Johnatan added Once Support
*/
public function toJs( $js_object_name ){
switch ($this->type) {
case self::TYPE_EVENT_DEFAULT_ONCE:
return $this->getEventJs($js_object_name, true);
case self::TYPE_EVENT_DOM:
return $this->getDomEventJs($js_object_name);
case self::TYPE_EVENT_DOM_ONCE:
return $this->getDomEventJs($js_object_name, true);
case self::TYPE_EVENT_DEFAULT:
default:
return $this->getEventJs($js_object_name);
}
}
}