-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base.php
153 lines (134 loc) · 3.53 KB
/
Base.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
<?php
/**
* Base Class file
*
* @author Chris Yates
* @copyright Copyright © 2015 BeastBytes - All Rights Reserved
* @license BSD 3-Clause
* @package Leaflet
*/
namespace beastbytes\leaflet;
use yii\base\Component;
use yii\base\InvalidParamException;
use yii\helpers\Json;
use yii\web\JsExpression;
/**
* Base class for all Leaflet components.
*/
abstract class Base extends Component
{
/**
* @param array options
*/
protected $options = [];
/**
* @param array events
*/
protected $events = [];
/**
* @var JsExpression JavaScript variable name. If set the generated JavaScript will assign the Leaflet object to this variable.
*/
private $_jsVar;
public function __isset($name)
{
if ($name == 'jsVar') {
return isset($this->_jsVar);
}
return parent::__isset($name);
}
/**
* Gets the events
*
* @return array The events
*/
public function getEvents()
{
return $this->events;
}
/**
* Gets jsVar
*
* @return string jsVar
*/
public function getjsVar()
{
if (empty($this->_jsVar)) {
$this->setJsVar(true);
}
return $this->_jsVar;
}
/**
* Gets the options
*
* @return array The options
*/
public function getOptions()
{
return $this->options;
}
/**
* Sets an event
*
* @param string $name Event name
* @param string $handler Event handler
*/
public function setEvent($name, $handler)
{
$this->events[$name] = $handler;
}
/**
* Sets the events
*
* @param array $events Events for the object. Each event is in the format $name => $handler
*/
public function setEvents($events)
{
foreach ($events as $name => $handler) {
$this->setEvent($name, $handler);
};
}
/**
* Sets the JavaScript variable name for the object.
* If $value is boolean TRUE a name is generated.
*
* @param boolean|string|JsExpression $value TRUE, The JavaScript variable name, or a JsExpression containing the JavaScript variable name
* @throws InvalidParamExpression If $value is not or cannot be converted to a JsExpression
*/
public function setJsVar($value)
{
if ($value === true) {
$value = $this->jsVar();
}
if (is_string($value)) {
$value = new JsExpression($value);
}
if (!($value instanceof JsExpression)) {
throw new InvalidParamException(strtr('Invalid type for $value ({type}). $value must be a string that is a valid JavaScript variable name or a JsExpression of such a string.', [
'{type}' => (is_scalar($value) ? gettype($value) : get_class($value))
]));
}
$this->_jsVar = $value;
}
/**
* Sets a specified option
*
* @param string $name Option name
* @param mixed $value Option value
*/
public function setOption($name, $value)
{
$this->options[$name] = $value;
}
/**
* Sets the options
*
* @param array $options Options for the object
*/
public function setOptions($options)
{
$this->options = $options;
}
abstract public function jsVar(); // Creates a JavaScript variable name for the object
abstract public function toJs($map); // Generates JavaScript code
abstract protected function toJsExpression($js); // Generates the final JavaScript expression
}