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['ui.utils'] = array(
102 'js' => array('bower_components/angular-ui-utils/ui-utils.min.js'),
104 $angularModules['ui.sortable'] = array(
106 'js' => array('bower_components/angular-ui-sortable/sortable.min.js'),
108 $angularModules['unsavedChanges'] = array(
110 'js' => array('bower_components/angular-unsavedChanges/dist/unsavedChanges.min.js'),
113 foreach (\CRM_Core_Component
::getEnabledComponents() as $component) {
114 $angularModules = array_merge($angularModules, $component->getAngularModules());
116 \CRM_Utils_Hook
::angularModules($angularModules);
117 $this->modules
= $this->resolvePatterns($angularModules);
120 return $this->modules
;
124 * Get the descriptor for an Angular module.
126 * @param string $name
129 * Details about the module:
130 * - ext: string, the name of the Civi extension which defines the module
131 * - js: array(string $relativeFilePath).
132 * - css: array(string $relativeFilePath).
133 * - partials: array(string $relativeFilePath).
136 public function getModule($name) {
137 $modules = $this->getModules();
138 if (!isset($modules[$name])) {
139 throw new \
Exception("Unrecognized Angular module");
141 return $modules[$name];
145 * Convert any globs in an Angular module to file names.
147 * @param array $modules
148 * List of Angular modules.
150 * Updated list of Angular modules
152 protected function resolvePatterns($modules) {
153 $newModules = array();
155 foreach ($modules as $moduleKey => $module) {
156 foreach (array('js', 'css', 'partials') as $fileset) {
157 if (!isset($module[$fileset])) {
160 $module[$fileset] = $this->res
->glob($module['ext'], $module[$fileset]);
162 $newModules[$moduleKey] = $module;
169 * Get the partial HTML documents for a module.
171 * @param string $name
172 * Angular module name.
174 * Array(string $extFilePath => string $html)
176 * Invalid partials configuration.
178 public function getPartials($name) {
179 $module = $this->getModule($name);
181 if (isset($module['partials'])) {
182 foreach ($module['partials'] as $partialDir) {
183 $partialDir = $this->res
->getPath($module['ext']) . '/' . $partialDir;
184 $files = \CRM_Utils_File
::findFiles($partialDir, '*.html', TRUE);
185 foreach ($files as $file) {
186 $filename = '~/' . $name . '/' . $file;
187 $result[$filename] = file_get_contents($partialDir . '/' . $file);
195 * Get list of translated strings for a module.
197 * @param string $name
198 * Angular module name.
200 * Translated strings: array(string $orig => string $translated).
202 public function getTranslatedStrings($name) {
203 $module = $this->getModule($name);
205 $strings = $this->getStrings($name);
206 foreach ($strings as $string) {
207 // TODO: should we pass translation domain based on $module[ext] or $module[tsDomain]?
208 // It doesn't look like client side really supports the domain right now...
209 $translated = ts($string, array(
210 'domain' => array($module['ext'], NULL),
212 if ($translated != $string) {
213 $result[$string] = $translated;
220 * Get list of translatable strings for a module.
222 * @param string $name
223 * Angular module name.
225 * Translatable strings.
227 public function getStrings($name) {
228 $module = $this->getModule($name);
230 if (isset($module['js'])) {
231 foreach ($module['js'] as $file) {
232 $strings = $this->res
->getStrings()->get(
234 $this->res
->getPath($module['ext'], $file),
237 $result = array_unique(array_merge($result, $strings));
240 if (isset($module['partials'])) {
241 foreach ($module['partials'] as $partialDir) {
242 $partialDir = $this->res
->getPath($module['ext']) . '/' . $partialDir;
243 $files = \CRM_Utils_File
::findFiles($partialDir, '*.html');
244 foreach ($files as $file) {
245 $strings = $this->res
->getStrings()->get(
250 $result = array_unique(array_merge($result, $strings));
258 * @param string $name
264 public function getScriptUrls($name) {
265 $module = $this->getModule($name);
267 if (isset($module['js'])) {
268 foreach ($module['js'] as $file) {
269 $result[] = $this->res
->getUrl($module['ext'], $file, TRUE);
276 * @param string $name
282 public function getStyleUrls($name) {
283 $module = $this->getModule($name);
285 if (isset($module['css'])) {
286 foreach ($module['css'] as $file) {
287 $result[] = $this->res
->getUrl($module['ext'], $file, TRUE);