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. | |
16072ce1 TO |
28 | */ |
29 | protected $modules = NULL; | |
30 | ||
31 | /** | |
32 | * @param \CRM_Core_Resources $res | |
33 | * The resource manager. | |
34 | */ | |
35 | public function __construct($res) { | |
36 | $this->res = $res; | |
37 | } | |
38 | ||
39 | /** | |
fe482240 | 40 | * Get a list of AngularJS modules which should be autoloaded. |
16072ce1 TO |
41 | * |
42 | * @return array | |
8671b4f2 TO |
43 | * Each item has some combination of these keys: |
44 | * - ext: string | |
45 | * The Civi extension which defines the Angular module. | |
46 | * - js: array(string $relativeFilePath) | |
47 | * List of JS files (relative to the extension). | |
48 | * - css: array(string $relativeFilePath) | |
49 | * List of CSS files (relative to the extension). | |
50 | * - partials: array(string $relativeFilePath) | |
51 | * A list of partial-HTML folders (relative to the extension). | |
52 | * This will be mapped to "~/moduleName" by crmResource. | |
16072ce1 TO |
53 | */ |
54 | public function getModules() { | |
55 | if ($this->modules === NULL) { | |
56 | ||
57 | $angularModules = array(); | |
58 | $angularModules['angularFileUpload'] = array( | |
59 | 'ext' => 'civicrm', | |
60 | 'js' => array('bower_components/angular-file-upload/angular-file-upload.min.js'), | |
61 | ); | |
62 | $angularModules['crmApp'] = array( | |
63 | 'ext' => 'civicrm', | |
64 | 'js' => array('js/angular-crmApp.js'), | |
65 | ); | |
66 | $angularModules['crmAttachment'] = array( | |
67 | 'ext' => 'civicrm', | |
68 | 'js' => array('js/angular-crmAttachment.js'), | |
69 | 'css' => array('css/angular-crmAttachment.css'), | |
ad7abea2 | 70 | 'partials' => array('partials/crmAttachment'), |
16072ce1 | 71 | ); |
2386ec88 TO |
72 | $angularModules['crmAutosave'] = array( |
73 | 'ext' => 'civicrm', | |
74 | 'js' => array('js/angular-crmAutosave.js'), | |
75 | ); | |
56f158de TO |
76 | //$angularModules['crmExample'] = array( |
77 | // 'ext' => 'civicrm', | |
78 | // 'js' => array('js/angular-crmExample.js'), | |
79 | // 'partials' => array('partials/crmExample'), | |
80 | //); | |
a2dc0f82 TO |
81 | $angularModules['crmResource'] = array( |
82 | 'ext' => 'civicrm', | |
83 | // 'js' => array('js/angular-crmResource/byModule.js'), // One HTTP request per module. | |
84 | 'js' => array('js/angular-crmResource/all.js'), // One HTTP request for all modules. | |
85 | ); | |
16072ce1 TO |
86 | $angularModules['crmUi'] = array( |
87 | 'ext' => 'civicrm', | |
88 | 'js' => array('js/angular-crm-ui.js', 'packages/ckeditor/ckeditor.js'), | |
ad7abea2 | 89 | 'partials' => array('partials/crmUi'), |
16072ce1 TO |
90 | ); |
91 | $angularModules['crmUtil'] = array( | |
92 | 'ext' => 'civicrm', | |
93 | 'js' => array('js/angular-crm-util.js'), | |
94 | ); | |
95 | // https://github.com/jwstadler/angular-jquery-dialog-service | |
96 | $angularModules['dialogService'] = array( | |
97 | 'ext' => 'civicrm', | |
98 | 'js' => array('bower_components/angular-jquery-dialog-service/dialog-service.js'), | |
99 | ); | |
100 | $angularModules['ngSanitize'] = array( | |
101 | 'ext' => 'civicrm', | |
102 | 'js' => array('js/angular-sanitize.js'), | |
103 | ); | |
104 | $angularModules['ui.utils'] = array( | |
105 | 'ext' => 'civicrm', | |
106 | 'js' => array('bower_components/angular-ui-utils/ui-utils.min.js'), | |
107 | ); | |
108 | $angularModules['ui.sortable'] = array( | |
109 | 'ext' => 'civicrm', | |
110 | 'js' => array('bower_components/angular-ui-sortable/sortable.min.js'), | |
111 | ); | |
112 | $angularModules['unsavedChanges'] = array( | |
113 | 'ext' => 'civicrm', | |
114 | 'js' => array('bower_components/angular-unsavedChanges/dist/unsavedChanges.min.js'), | |
115 | ); | |
116 | ||
117 | foreach (\CRM_Core_Component::getEnabledComponents() as $component) { | |
118 | $angularModules = array_merge($angularModules, $component->getAngularModules()); | |
119 | } | |
120 | \CRM_Utils_Hook::angularModules($angularModules); | |
121 | $this->modules = $this->resolvePatterns($angularModules); | |
122 | } | |
123 | ||
124 | return $this->modules; | |
125 | } | |
126 | ||
127 | /** | |
128 | * Get the descriptor for an Angular module. | |
129 | * | |
130 | * @param string $name | |
131 | * Module name. | |
132 | * @return array | |
133 | * Details about the module: | |
134 | * - ext: string, the name of the Civi extension which defines the module | |
135 | * - js: array(string $relativeFilePath). | |
136 | * - css: array(string $relativeFilePath). | |
137 | * - partials: array(string $relativeFilePath). | |
138 | * @throws \Exception | |
139 | */ | |
140 | public function getModule($name) { | |
141 | $modules = $this->getModules(); | |
142 | if (!isset($modules[$name])) { | |
143 | throw new \Exception("Unrecognized Angular module"); | |
144 | } | |
145 | return $modules[$name]; | |
146 | } | |
147 | ||
148 | /** | |
149 | * Convert any globs in an Angular module to file names. | |
150 | * | |
151 | * @param array $modules | |
152 | * List of Angular modules. | |
153 | * @return array | |
154 | * Updated list of Angular modules | |
155 | */ | |
156 | protected function resolvePatterns($modules) { | |
157 | $newModules = array(); | |
158 | ||
159 | foreach ($modules as $moduleKey => $module) { | |
160 | foreach (array('js', 'css', 'partials') as $fileset) { | |
161 | if (!isset($module[$fileset])) { | |
162 | continue; | |
163 | } | |
164 | $module[$fileset] = $this->res->glob($module['ext'], $module[$fileset]); | |
165 | } | |
166 | $newModules[$moduleKey] = $module; | |
167 | } | |
168 | ||
169 | return $newModules; | |
170 | } | |
171 | ||
172 | /** | |
173 | * Get the partial HTML documents for a module. | |
174 | * | |
175 | * @param string $name | |
176 | * Angular module name. | |
177 | * @return array | |
178 | * Array(string $extFilePath => string $html) | |
a2dc0f82 TO |
179 | * @throws \Exception |
180 | * Invalid partials configuration. | |
16072ce1 TO |
181 | */ |
182 | public function getPartials($name) { | |
183 | $module = $this->getModule($name); | |
184 | $result = array(); | |
185 | if (isset($module['partials'])) { | |
a2dc0f82 TO |
186 | foreach ($module['partials'] as $partialDir) { |
187 | $partialDir = $this->res->getPath($module['ext']) . '/' . $partialDir; | |
188 | $files = \CRM_Utils_File::findFiles($partialDir, '*.html', TRUE); | |
189 | foreach ($files as $file) { | |
190 | $filename = '~/' . $name . '/' . $file; | |
191 | $result[$filename] = file_get_contents($partialDir . '/' . $file); | |
192 | } | |
16072ce1 TO |
193 | } |
194 | } | |
195 | return $result; | |
196 | } | |
197 | ||
16072ce1 TO |
198 | /** |
199 | * Get list of translated strings for a module. | |
200 | * | |
201 | * @param string $name | |
202 | * Angular module name. | |
203 | * @return array | |
204 | * Translated strings: array(string $orig => string $translated). | |
205 | */ | |
206 | public function getTranslatedStrings($name) { | |
e3d90d6c | 207 | $module = $this->getModule($name); |
16072ce1 TO |
208 | $result = array(); |
209 | $strings = $this->getStrings($name); | |
210 | foreach ($strings as $string) { | |
211 | // TODO: should we pass translation domain based on $module[ext] or $module[tsDomain]? | |
212 | // It doesn't look like client side really supports the domain right now... | |
e3d90d6c TO |
213 | $translated = ts($string, array( |
214 | 'domain' => array($module['ext'], NULL), | |
215 | )); | |
16072ce1 TO |
216 | if ($translated != $string) { |
217 | $result[$string] = $translated; | |
218 | } | |
219 | } | |
220 | return $result; | |
221 | } | |
222 | ||
223 | /** | |
224 | * Get list of translatable strings for a module. | |
225 | * | |
226 | * @param string $name | |
227 | * Angular module name. | |
228 | * @return array | |
229 | * Translatable strings. | |
230 | */ | |
231 | public function getStrings($name) { | |
232 | $module = $this->getModule($name); | |
233 | $result = array(); | |
234 | if (isset($module['js'])) { | |
235 | foreach ($module['js'] as $file) { | |
236 | $strings = $this->res->getStrings()->get( | |
237 | $module['ext'], | |
238 | $this->res->getPath($module['ext'], $file), | |
239 | 'text/javascript' | |
240 | ); | |
241 | $result = array_unique(array_merge($result, $strings)); | |
242 | } | |
243 | } | |
244 | if (isset($module['partials'])) { | |
a2dc0f82 TO |
245 | foreach ($module['partials'] as $partialDir) { |
246 | $partialDir = $this->res->getPath($module['ext']) . '/' . $partialDir; | |
247 | $files = \CRM_Utils_File::findFiles($partialDir, '*.html'); | |
248 | foreach ($files as $file) { | |
249 | $strings = $this->res->getStrings()->get( | |
250 | $module['ext'], | |
251 | $file, | |
252 | 'text/html' | |
253 | ); | |
254 | $result = array_unique(array_merge($result, $strings)); | |
255 | } | |
16072ce1 TO |
256 | } |
257 | } | |
258 | return $result; | |
259 | } | |
260 | ||
261 | /** | |
262 | * @param string $name | |
263 | * Module name. | |
264 | * @return array | |
265 | * List of URLs. | |
266 | * @throws \Exception | |
267 | */ | |
268 | public function getScriptUrls($name) { | |
269 | $module = $this->getModule($name); | |
270 | $result = array(); | |
271 | if (isset($module['js'])) { | |
272 | foreach ($module['js'] as $file) { | |
273 | $result[] = $this->res->getUrl($module['ext'], $file, TRUE); | |
274 | } | |
275 | } | |
276 | return $result; | |
277 | } | |
278 | ||
279 | /** | |
280 | * @param string $name | |
281 | * Module name. | |
282 | * @return array | |
283 | * List of URLs. | |
284 | * @throws \Exception | |
285 | */ | |
286 | public function getStyleUrls($name) { | |
287 | $module = $this->getModule($name); | |
288 | $result = array(); | |
289 | if (isset($module['css'])) { | |
290 | foreach ($module['css'] as $file) { | |
291 | $result[] = $this->res->getUrl($module['ext'], $file, TRUE); | |
292 | } | |
293 | } | |
294 | return $result; | |
295 | } | |
8671b4f2 | 296 | |
16072ce1 | 297 | } |