-
Notifications
You must be signed in to change notification settings - Fork 2
/
Application.php
181 lines (150 loc) · 4.25 KB
/
Application.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
<?php
/**
*
*/
namespace bedezign\yii2\desktop;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
class Application
extends components\Component
implements DesktopEventHandler
{
/**
* Unique ID for this application. Used for serializing
* @var string
*/
public $id = null;
/**
* Application title
* @var string
*/
public $title = 'Application';
/**
* If this application is via an IFrame, this is the application route for that frame
* @var string
*/
public $route = null;
/**
* true if the application was running/should be running on desktop boot
* @var bool
*/
public $launched = false;
/**
* Positional data of the application window
* @var string
*/
public $windowState = null;
public $windowMaximised = false;
/**
* Stored session data for the application (if any). This is available in the desktop by calling desktop.session_data()
* @var mixed
*/
public $sessionData = null;
/**
* Application icon URL. If empty, the default icon is used.
* @var Icon
*/
protected $_icon = null;
public function init()
{
if (!$this->id)
throw new InvalidConfigException('"id" is required');
parent::init();
}
/**
* @param array[int, int] $size array with new width and height.
*/
public function setSize($size)
{
if (!$this->windowState)
// Nothing yet, fake a window position save first
$this->windowState = ['top' => 'auto', 'left' => 'auto', 'bottom' => 'auto', 'right' => 'auto'];
$this->windowState['width'] = str_replace('px', '', $size[0]) . 'px';
$this->windowState['height'] = str_replace('px', '', $size[1]) . 'px';
}
/**
* @param array[int, int] $position array with new x and y position (left and top)
*/
public function setPosition($position)
{
if (!$this->windowState)
// Nothing yet, fake a window position save first
$this->windowState = ['bottom' => 'auto', 'right' => 'auto', 'width' => '700px', 'height' => '300px'];
$this->windowState['left'] = str_replace('px', '', $position[0]) . 'px';
$this->windowState['top'] = str_replace('px', '', $position[1]) . 'px';
}
/**
* Converts the ID into an internally used application ID
* @return string
*/
public function getApplicationId()
{
return 'application_' . Html::encode($this->id);
}
public static function toRegularId($applicationId)
{
if (substr($applicationId, 0, 12) == 'application_')
return Html::decode(substr($applicationId, 12));
return $applicationId;
}
public function renderWindow()
{
return $this->desktop->render('_applicationWindow', ['application' => $this]);
}
/**
* This functions renders the actual window contents. By default this is an empty iframe with a data-url attribute.
* The javascript class looks for an iframe and will change its location to the specified url if found.
* You can override this function if you need to output literal HTML
*
* @return string
*/
public function renderContent()
{
return $this->desktop->render('_applicationWindowContent', ['application' => $this]);
}
/**
* Return the rendered dock button.
* This element ties the application window to an anchor that is used to control it.
*
* @return string
*/
public function renderDockButton()
{
$id = $this->applicationId;
$icon = $this->getIcon()->render(Icon::DISPLAY_DOCK);
$title = $this->title;
return <<<HTML
<li id="icon_dock_{$id}">
<a href="#window_{$id}" id="{$id}" class="application_dock_button">
$icon <span class="title">$title</span>
</a>
</li>
HTML;
}
public function canHandleEvent($type)
{
return in_array($type, ['application.launched', 'application.closed', 'application.session-updated', 'window.changed']);
}
public function event($data)
{
$action = $data['action'];
unset($data['action'], $data['application']);
switch ($action) {
case 'application.launched' :
$this->launched = true;
return true;
case 'application.closed' :
$this->launched = false;
return true;
case 'application.session-updated' :
$this->sessionData = $data['session'];
return true;
case 'window.changed' :
$this->windowMaximised = isset($data['maximized']) && $data['maximized'] == 'true';
unset($data['maximized']);
$this->windowState = $data;
return true;
}
return false;
}
}