2 namespace Civi\Angular
;
5 * Manage Angular resources.
7 * @package Civi\Angular
12 * @var \CRM_Core_Resources
14 protected $res = NULL;
18 * Each item has some combination of these keys:
20 * The Civi extension which defines the Angular module.
21 * - js: array(string $relativeFilePath)
22 * List of JS files (relative to the extension).
23 * - css: array(string $relativeFilePath)
24 * List of CSS files (relative to the extension).
25 * - partials: array(string $relativeFilePath)
26 * A list of partial-HTML folders (relative to the extension).
27 * This will be mapped to "~/moduleName" by crmResource.
29 protected $modules = NULL;
32 * @param \CRM_Core_Resources $res
33 * The resource manager.
35 public function __construct($res) {
40 * Get a list of AngularJS modules which should be autoloaded.
43 * Each item has some combination of these keys:
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.
54 public function getModules() {
55 if ($this->modules
=== NULL) {
57 $angularModules = array();
58 $angularModules['angularFileUpload'] = array(
60 'js' => array('bower_components/angular-file-upload/angular-file-upload.min.js'),
62 $angularModules['crmApp'] = array(
64 'js' => array('js/angular-crmApp.js'),
66 $angularModules['crmAttachment'] = array(
68 'js' => array('js/angular-crmAttachment.js'),
69 'css' => array('css/angular-crmAttachment.css'),
70 'partials' => array('partials/crmAttachment'),
72 $angularModules['crmAutosave'] = array(
74 'js' => array('js/angular-crmAutosave.js'),
76 //$angularModules['crmExample'] = array(
77 // 'ext' => 'civicrm',
78 // 'js' => array('js/angular-crmExample.js'),
79 // 'partials' => array('partials/crmExample'),
81 $angularModules['crmResource'] = array(
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.
86 $angularModules['crmUi'] = array(
88 'js' => array('js/angular-crm-ui.js', 'packages/ckeditor/ckeditor.js'),
89 'partials' => array('partials/crmUi'),
91 $angularModules['crmUtil'] = array(
93 'js' => array('js/angular-crm-util.js'),
95 // https://github.com/jwstadler/angular-jquery-dialog-service
96 $angularModules['dialogService'] = array(
98 'js' => array('bower_components/angular-jquery-dialog-service/dialog-service.js'),
100 $angularModules['ngSanitize'] = array(
102 'js' => array('js/angular-sanitize.js'),
104 $angularModules['ui.utils'] = array(
106 'js' => array('bower_components/angular-ui-utils/ui-utils.min.js'),
108 $angularModules['ui.sortable'] = array(
110 'js' => array('bower_components/angular-ui-sortable/sortable.min.js'),
112 $angularModules['unsavedChanges'] = array(
114 'js' => array('bower_components/angular-unsavedChanges/dist/unsavedChanges.min.js'),
117 foreach (\CRM_Core_Component
::getEnabledComponents() as $component) {
118 $angularModules = array_merge($angularModules, $component->getAngularModules());
120 \CRM_Utils_Hook
::angularModules($angularModules);
121 $this->modules
= $this->resolvePatterns($angularModules);
124 return $this->modules
;
128 * Get the descriptor for an Angular module.
130 * @param string $name
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).
140 public function getModule($name) {
141 $modules = $this->getModules();
142 if (!isset($modules[$name])) {
143 throw new \
Exception("Unrecognized Angular module");
145 return $modules[$name];
149 * Convert any globs in an Angular module to file names.
151 * @param array $modules
152 * List of Angular modules.
154 * Updated list of Angular modules
156 protected function resolvePatterns($modules) {
157 $newModules = array();
159 foreach ($modules as $moduleKey => $module) {
160 foreach (array('js', 'css', 'partials') as $fileset) {
161 if (!isset($module[$fileset])) {
164 $module[$fileset] = $this->res
->glob($module['ext'], $module[$fileset]);
166 $newModules[$moduleKey] = $module;
173 * Get the partial HTML documents for a module.
175 * @param string $name
176 * Angular module name.
178 * Array(string $extFilePath => string $html)
180 * Invalid partials configuration.
182 public function getPartials($name) {
183 $module = $this->getModule($name);
185 if (isset($module['partials'])) {
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);
199 * Get list of translated strings for a module.
201 * @param string $name
202 * Angular module name.
204 * Translated strings: array(string $orig => string $translated).
206 public function getTranslatedStrings($name) {
207 $module = $this->getModule($name);
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...
213 $translated = ts($string, array(
214 'domain' => array($module['ext'], NULL),
216 if ($translated != $string) {
217 $result[$string] = $translated;
224 * Get list of translatable strings for a module.
226 * @param string $name
227 * Angular module name.
229 * Translatable strings.
231 public function getStrings($name) {
232 $module = $this->getModule($name);
234 if (isset($module['js'])) {
235 foreach ($module['js'] as $file) {
236 $strings = $this->res
->getStrings()->get(
238 $this->res
->getPath($module['ext'], $file),
241 $result = array_unique(array_merge($result, $strings));
244 if (isset($module['partials'])) {
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(
254 $result = array_unique(array_merge($result, $strings));
262 * @param string $name
268 public function getScriptUrls($name) {
269 $module = $this->getModule($name);
271 if (isset($module['js'])) {
272 foreach ($module['js'] as $file) {
273 $result[] = $this->res
->getUrl($module['ext'], $file, TRUE);
280 * @param string $name
286 public function getStyleUrls($name) {
287 $module = $this->getModule($name);
289 if (isset($module['css'])) {
290 foreach ($module['css'] as $file) {
291 $result[] = $this->res
->getUrl($module['ext'], $file, TRUE);