Commit | Line | Data |
---|---|---|
16072ce1 TO |
1 | <?php |
2 | namespace Civi\Angular; | |
3 | ||
4 | /** | |
5 | * Manage Angular resources. | |
6 | * | |
7 | * @package Civi\Angular | |
8 | */ | |
9 | class Manager { | |
10 | ||
11 | /** | |
12 | * @var \CRM_Core_Resources | |
13 | */ | |
14 | protected $res = NULL; | |
15 | ||
16 | /** | |
17 | * @var array|NULL | |
18 | * Each item has some combination of these keys: | |
19 | * - ext: string | |
8671b4f2 | 20 | * The Civi extension which defines the Angular module. |
16072ce1 | 21 | * - js: array(string $relativeFilePath) |
8671b4f2 | 22 | * List of JS files (relative to the extension). |
16072ce1 | 23 | * - css: array(string $relativeFilePath) |
8671b4f2 | 24 | * List of CSS files (relative to the extension). |
16072ce1 | 25 | * - partials: array(string $relativeFilePath) |
8671b4f2 TO |
26 | * A list of partial-HTML folders (relative to the extension). |
27 | * This will be mapped to "~/moduleName" by crmResource. | |
1da632e0 TO |
28 | * - settings: array(string $key => mixed $value) |
29 | * List of settings to preload. | |
16072ce1 TO |
30 | */ |
31 | protected $modules = NULL; | |
32 | ||
33 | /** | |
34 | * @param \CRM_Core_Resources $res | |
35 | * The resource manager. | |
36 | */ | |
37 | public function __construct($res) { | |
38 | $this->res = $res; | |
39 | } | |
40 | ||
41 | /** | |
fe482240 | 42 | * Get a list of AngularJS modules which should be autoloaded. |
16072ce1 TO |
43 | * |
44 | * @return array | |
8671b4f2 TO |
45 | * Each item has some combination of these keys: |
46 | * - ext: string | |
47 | * The Civi extension which defines the Angular module. | |
48 | * - js: array(string $relativeFilePath) | |
49 | * List of JS files (relative to the extension). | |
50 | * - css: array(string $relativeFilePath) | |
51 | * List of CSS files (relative to the extension). | |
52 | * - partials: array(string $relativeFilePath) | |
53 | * A list of partial-HTML folders (relative to the extension). | |
54 | * This will be mapped to "~/moduleName" by crmResource. | |
1da632e0 TO |
55 | * - settings: array(string $key => mixed $value) |
56 | * List of settings to preload. | |
16072ce1 TO |
57 | */ |
58 | public function getModules() { | |
59 | if ($this->modules === NULL) { | |
1da632e0 | 60 | $config = \CRM_Core_Config::singleton(); |
16072ce1 TO |
61 | |
62 | $angularModules = array(); | |
63 | $angularModules['angularFileUpload'] = array( | |
64 | 'ext' => 'civicrm', | |
65 | 'js' => array('bower_components/angular-file-upload/angular-file-upload.min.js'), | |
66 | ); | |
67 | $angularModules['crmApp'] = array( | |
68 | 'ext' => 'civicrm', | |
94bc1db0 | 69 | 'js' => array('ang/crmApp.js'), |
16072ce1 TO |
70 | ); |
71 | $angularModules['crmAttachment'] = array( | |
72 | 'ext' => 'civicrm', | |
ad16ac09 TO |
73 | 'js' => array('ang/crmAttachment.js'), |
74 | 'css' => array('ang/crmAttachment.css'), | |
75 | 'partials' => array('ang/crmAttachment'), | |
1da632e0 TO |
76 | 'settings' => array( |
77 | 'token' => \CRM_Core_Page_AJAX_Attachment::createToken(), | |
78 | ), | |
16072ce1 | 79 | ); |
2386ec88 TO |
80 | $angularModules['crmAutosave'] = array( |
81 | 'ext' => 'civicrm', | |
19464351 | 82 | 'js' => array('ang/crmAutosave.js'), |
2386ec88 | 83 | ); |
098de400 TO |
84 | $angularModules['crmCxn'] = array( |
85 | 'ext' => 'civicrm', | |
86 | 'js' => array('ang/crmCxn.js', 'ang/crmCxn/*.js'), | |
87 | 'css' => array('ang/crmCxn.css'), | |
88 | 'partials' => array('ang/crmCxn'), | |
89 | ); | |
56f158de TO |
90 | //$angularModules['crmExample'] = array( |
91 | // 'ext' => 'civicrm', | |
8f1f2525 TO |
92 | // 'js' => array('ang/crmExample.js'), |
93 | // 'partials' => array('ang/crmExample'), | |
56f158de | 94 | //); |
a2dc0f82 TO |
95 | $angularModules['crmResource'] = array( |
96 | 'ext' => 'civicrm', | |
97 | // 'js' => array('js/angular-crmResource/byModule.js'), // One HTTP request per module. | |
98 | 'js' => array('js/angular-crmResource/all.js'), // One HTTP request for all modules. | |
99 | ); | |
16072ce1 TO |
100 | $angularModules['crmUi'] = array( |
101 | 'ext' => 'civicrm', | |
2bcf742a TO |
102 | 'js' => array('ang/crmUi.js'), |
103 | 'partials' => array('ang/crmUi'), | |
16072ce1 TO |
104 | ); |
105 | $angularModules['crmUtil'] = array( | |
106 | 'ext' => 'civicrm', | |
bf61d18d | 107 | 'js' => array('ang/crmUtil.js'), |
16072ce1 TO |
108 | ); |
109 | // https://github.com/jwstadler/angular-jquery-dialog-service | |
110 | $angularModules['dialogService'] = array( | |
111 | 'ext' => 'civicrm', | |
112 | 'js' => array('bower_components/angular-jquery-dialog-service/dialog-service.js'), | |
113 | ); | |
1c03fdf8 TO |
114 | $angularModules['ngRoute'] = array( |
115 | 'ext' => 'civicrm', | |
116 | 'js' => array('bower_components/angular-route/angular-route.min.js'), | |
117 | ); | |
28949a14 TO |
118 | $angularModules['ngSanitize'] = array( |
119 | 'ext' => 'civicrm', | |
120 | 'js' => array('bower_components/angular-sanitize/angular-sanitize.min.js'), | |
121 | ); | |
16072ce1 TO |
122 | $angularModules['ui.utils'] = array( |
123 | 'ext' => 'civicrm', | |
124 | 'js' => array('bower_components/angular-ui-utils/ui-utils.min.js'), | |
125 | ); | |
126 | $angularModules['ui.sortable'] = array( | |
127 | 'ext' => 'civicrm', | |
128 | 'js' => array('bower_components/angular-ui-sortable/sortable.min.js'), | |
129 | ); | |
130 | $angularModules['unsavedChanges'] = array( | |
131 | 'ext' => 'civicrm', | |
132 | 'js' => array('bower_components/angular-unsavedChanges/dist/unsavedChanges.min.js'), | |
133 | ); | |
134 | ||
c0f7f681 NM |
135 | $angularModules['statuspage'] = array( |
136 | 'ext' => 'civicrm', | |
137 | 'js' => array('ang/crmStatusPage.js', 'ang/crmStatusPage/*.js'), | |
138 | 'css' => array('ang/crmStatusPage.css'), | |
5d73c55a | 139 | 'partials' => array('ang/crmStatusPage'), |
c0f7f681 NM |
140 | 'settings' => array(), |
141 | ); | |
142 | ||
16072ce1 TO |
143 | foreach (\CRM_Core_Component::getEnabledComponents() as $component) { |
144 | $angularModules = array_merge($angularModules, $component->getAngularModules()); | |
145 | } | |
146 | \CRM_Utils_Hook::angularModules($angularModules); | |
147 | $this->modules = $this->resolvePatterns($angularModules); | |
148 | } | |
149 | ||
150 | return $this->modules; | |
151 | } | |
152 | ||
153 | /** | |
154 | * Get the descriptor for an Angular module. | |
155 | * | |
156 | * @param string $name | |
157 | * Module name. | |
158 | * @return array | |
159 | * Details about the module: | |
160 | * - ext: string, the name of the Civi extension which defines the module | |
161 | * - js: array(string $relativeFilePath). | |
162 | * - css: array(string $relativeFilePath). | |
163 | * - partials: array(string $relativeFilePath). | |
164 | * @throws \Exception | |
165 | */ | |
166 | public function getModule($name) { | |
167 | $modules = $this->getModules(); | |
168 | if (!isset($modules[$name])) { | |
169 | throw new \Exception("Unrecognized Angular module"); | |
170 | } | |
171 | return $modules[$name]; | |
172 | } | |
173 | ||
174 | /** | |
175 | * Convert any globs in an Angular module to file names. | |
176 | * | |
177 | * @param array $modules | |
178 | * List of Angular modules. | |
179 | * @return array | |
180 | * Updated list of Angular modules | |
181 | */ | |
182 | protected function resolvePatterns($modules) { | |
183 | $newModules = array(); | |
184 | ||
185 | foreach ($modules as $moduleKey => $module) { | |
186 | foreach (array('js', 'css', 'partials') as $fileset) { | |
187 | if (!isset($module[$fileset])) { | |
188 | continue; | |
189 | } | |
190 | $module[$fileset] = $this->res->glob($module['ext'], $module[$fileset]); | |
191 | } | |
192 | $newModules[$moduleKey] = $module; | |
193 | } | |
194 | ||
195 | return $newModules; | |
196 | } | |
197 | ||
198 | /** | |
199 | * Get the partial HTML documents for a module. | |
200 | * | |
201 | * @param string $name | |
202 | * Angular module name. | |
203 | * @return array | |
204 | * Array(string $extFilePath => string $html) | |
a2dc0f82 TO |
205 | * @throws \Exception |
206 | * Invalid partials configuration. | |
16072ce1 TO |
207 | */ |
208 | public function getPartials($name) { | |
209 | $module = $this->getModule($name); | |
210 | $result = array(); | |
211 | if (isset($module['partials'])) { | |
a2dc0f82 TO |
212 | foreach ($module['partials'] as $partialDir) { |
213 | $partialDir = $this->res->getPath($module['ext']) . '/' . $partialDir; | |
214 | $files = \CRM_Utils_File::findFiles($partialDir, '*.html', TRUE); | |
215 | foreach ($files as $file) { | |
216 | $filename = '~/' . $name . '/' . $file; | |
217 | $result[$filename] = file_get_contents($partialDir . '/' . $file); | |
218 | } | |
16072ce1 TO |
219 | } |
220 | } | |
221 | return $result; | |
222 | } | |
223 | ||
16072ce1 TO |
224 | /** |
225 | * Get list of translated strings for a module. | |
226 | * | |
227 | * @param string $name | |
228 | * Angular module name. | |
229 | * @return array | |
230 | * Translated strings: array(string $orig => string $translated). | |
231 | */ | |
232 | public function getTranslatedStrings($name) { | |
e3d90d6c | 233 | $module = $this->getModule($name); |
16072ce1 TO |
234 | $result = array(); |
235 | $strings = $this->getStrings($name); | |
236 | foreach ($strings as $string) { | |
237 | // TODO: should we pass translation domain based on $module[ext] or $module[tsDomain]? | |
238 | // It doesn't look like client side really supports the domain right now... | |
e3d90d6c TO |
239 | $translated = ts($string, array( |
240 | 'domain' => array($module['ext'], NULL), | |
241 | )); | |
16072ce1 TO |
242 | if ($translated != $string) { |
243 | $result[$string] = $translated; | |
244 | } | |
245 | } | |
246 | return $result; | |
247 | } | |
248 | ||
249 | /** | |
250 | * Get list of translatable strings for a module. | |
251 | * | |
252 | * @param string $name | |
253 | * Angular module name. | |
254 | * @return array | |
255 | * Translatable strings. | |
256 | */ | |
257 | public function getStrings($name) { | |
258 | $module = $this->getModule($name); | |
259 | $result = array(); | |
260 | if (isset($module['js'])) { | |
261 | foreach ($module['js'] as $file) { | |
262 | $strings = $this->res->getStrings()->get( | |
263 | $module['ext'], | |
264 | $this->res->getPath($module['ext'], $file), | |
265 | 'text/javascript' | |
266 | ); | |
267 | $result = array_unique(array_merge($result, $strings)); | |
268 | } | |
269 | } | |
270 | if (isset($module['partials'])) { | |
a2dc0f82 TO |
271 | foreach ($module['partials'] as $partialDir) { |
272 | $partialDir = $this->res->getPath($module['ext']) . '/' . $partialDir; | |
273 | $files = \CRM_Utils_File::findFiles($partialDir, '*.html'); | |
274 | foreach ($files as $file) { | |
275 | $strings = $this->res->getStrings()->get( | |
276 | $module['ext'], | |
277 | $file, | |
278 | 'text/html' | |
279 | ); | |
280 | $result = array_unique(array_merge($result, $strings)); | |
281 | } | |
16072ce1 TO |
282 | } |
283 | } | |
284 | return $result; | |
285 | } | |
286 | ||
287 | /** | |
27a90ef6 TO |
288 | * Get resources for one or more modules. |
289 | * | |
290 | * @param string|array $moduleNames | |
291 | * List of module names. | |
292 | * @param string $resType | |
1da632e0 | 293 | * Type of resource ('js', 'css', 'settings'). |
27a90ef6 | 294 | * @param string $refType |
1da632e0 | 295 | * Type of reference to the resource ('cacheUrl', 'rawUrl', 'path', 'settings'). |
16072ce1 | 296 | * @return array |
27a90ef6 TO |
297 | * List of URLs or paths. |
298 | * @throws \CRM_Core_Exception | |
16072ce1 | 299 | */ |
27a90ef6 | 300 | public function getResources($moduleNames, $resType, $refType) { |
16072ce1 | 301 | $result = array(); |
27a90ef6 TO |
302 | $moduleNames = (array) $moduleNames; |
303 | foreach ($moduleNames as $moduleName) { | |
304 | $module = $this->getModule($moduleName); | |
305 | if (isset($module[$resType])) { | |
306 | foreach ($module[$resType] as $file) { | |
307 | switch ($refType) { | |
308 | case 'path': | |
309 | $result[] = $this->res->getPath($module['ext'], $file); | |
310 | break; | |
16072ce1 | 311 | |
27a90ef6 TO |
312 | case 'rawUrl': |
313 | $result[] = $this->res->getUrl($module['ext'], $file); | |
314 | break; | |
315 | ||
316 | case 'cacheUrl': | |
317 | $result[] = $this->res->getUrl($module['ext'], $file, TRUE); | |
318 | break; | |
319 | ||
1da632e0 TO |
320 | case 'settings': |
321 | if (!empty($module[$resType])) { | |
322 | $result[$moduleName] = $module[$resType]; | |
323 | } | |
324 | break; | |
325 | ||
27a90ef6 TO |
326 | default: |
327 | throw new \CRM_Core_Exception("Unrecognized resource format"); | |
328 | } | |
329 | } | |
16072ce1 TO |
330 | } |
331 | } | |
332 | return $result; | |
333 | } | |
8671b4f2 | 334 | |
16072ce1 | 335 | } |