1 | <?php |
||
2 | |||
3 | /** |
||
4 | * DialogLibraryTrait.php |
||
5 | * |
||
6 | * Common functions for javascript dialog libraries. |
||
7 | * |
||
8 | * @package jaxon-core |
||
9 | * @author Thierry Feuzeu <[email protected]> |
||
10 | * @copyright 2016 Thierry Feuzeu <[email protected]> |
||
11 | * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
||
12 | * @link https://github.com/jaxon-php/jaxon-dialogs |
||
13 | */ |
||
14 | |||
15 | namespace Jaxon\App\Dialog\Library; |
||
16 | |||
17 | use Jaxon\Response\Response; |
||
18 | |||
19 | trait DialogLibraryTrait |
||
20 | { |
||
21 | /** |
||
22 | * The dialog library helper |
||
23 | * |
||
24 | * @var DialogLibraryHelper |
||
25 | */ |
||
26 | protected $xHelper; |
||
27 | |||
28 | /** |
||
29 | * @var Response |
||
30 | */ |
||
31 | protected $xResponse = null; |
||
32 | |||
33 | /** |
||
34 | * For MessageInterface, tells if the calls to the functions shall |
||
35 | * add commands to the response or return the js code. By default, they add commands. |
||
36 | * |
||
37 | * @var bool |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
38 | */ |
||
39 | protected $bReturnCode = false; |
||
40 | |||
41 | /** |
||
42 | * Get the library name |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | abstract public function getName(): string; |
||
47 | |||
48 | /** |
||
49 | * Get the helper |
||
50 | * |
||
51 | * @return DialogLibraryHelper |
||
52 | */ |
||
53 | final public function helper(): DialogLibraryHelper |
||
54 | { |
||
55 | return $this->xHelper; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Set the response to attach the messages to. |
||
60 | * |
||
61 | * @param Response $xResponse Whether to return the code |
||
62 | * |
||
63 | * @return void |
||
64 | */ |
||
65 | final public function setResponse(Response $xResponse) |
||
66 | { |
||
67 | $this->xResponse = $xResponse; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Get the <Jaxon\Response\Response> object |
||
72 | * |
||
73 | * @return Response|null |
||
74 | */ |
||
75 | final protected function response(): ?Response |
||
76 | { |
||
77 | return $this->xResponse; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param bool $bReturnCode |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | final public function setReturnCode(bool $bReturnCode) |
||
86 | { |
||
87 | $this->bReturnCode = $bReturnCode; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Check if the library should return the js code or run it in the browser. |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | final protected function returnCode(): bool |
||
96 | { |
||
97 | return $this->bReturnCode; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Add a client side plugin command to the response object |
||
102 | * |
||
103 | * @param array $aAttributes The attributes of the command |
||
104 | * @param mixed $xData The data to be added to the command |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | final public function addCommand(array $aAttributes, $xData) |
||
109 | { |
||
110 | // This is usually the response plugin name. We set the library name instead. |
||
111 | $aAttributes['plg'] = $this->getName(); |
||
112 | $this->xResponse->addCommand($aAttributes, $xData); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Get the library base URI |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getUri(): string |
||
121 | { |
||
122 | return 'https://cdn.jaxon-php.org/libs'; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Get the library subdir for the URI |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | public function getSubdir(): string |
||
131 | { |
||
132 | return ''; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Get the library version for the URI |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getVersion(): string |
||
141 | { |
||
142 | return ''; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Get the CSS header code and file includes |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getJs(): string |
||
151 | { |
||
152 | return ''; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Get the javascript header code and file includes |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | public function getCss(): string |
||
161 | { |
||
162 | return ''; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Get the javascript code to be printed into the page |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | public function getScript(): string |
||
171 | { |
||
172 | return ''; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Get the javascript code to be executed on page load |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getReadyScript(): string |
||
181 | { |
||
182 | return ''; |
||
183 | } |
||
184 | } |
||
185 |