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 * - js: array(string $relativeFilePath)
21 * - css: array(string $relativeFilePath)
22 * - partials: array(string $relativeFilePath)
24 protected $modules = NULL;
27 * @param \CRM_Core_Resources $res
28 * The resource manager.
30 public function __construct($res) {
35 * Get a list of AngularJS modules which should be autoloaded
38 * (string $name => array('ext' => string $key, 'js' => array $paths, 'css' => array $paths))
40 public function getModules() {
41 if ($this->modules
=== NULL) {
43 $angularModules = array();
44 $angularModules['angularFileUpload'] = array(
46 'js' => array('bower_components/angular-file-upload/angular-file-upload.min.js'),
48 $angularModules['crmApp'] = array(
50 'js' => array('js/angular-crmApp.js'),
52 $angularModules['crmAttachment'] = array(
54 'js' => array('js/angular-crmAttachment.js'),
55 'css' => array('css/angular-crmAttachment.css'),
56 'partials' => array('partials/crmAttachment/*.html'),
58 $angularModules['crmResource'] = array(
60 // 'js' => array('js/angular-crmResource/byModule.js'), // One HTTP request per module.
61 'js' => array('js/angular-crmResource/all.js'), // One HTTP request for all modules.
63 $angularModules['crmUi'] = array(
65 'js' => array('js/angular-crm-ui.js', 'packages/ckeditor/ckeditor.js'),
66 'partials' => array('partials/crmUi/*.html'),
68 $angularModules['crmUtil'] = array(
70 'js' => array('js/angular-crm-util.js'),
72 // https://github.com/jwstadler/angular-jquery-dialog-service
73 $angularModules['dialogService'] = array(
75 'js' => array('bower_components/angular-jquery-dialog-service/dialog-service.js'),
77 $angularModules['ngSanitize'] = array(
79 'js' => array('js/angular-sanitize.js'),
81 $angularModules['ui.utils'] = array(
83 'js' => array('bower_components/angular-ui-utils/ui-utils.min.js'),
85 $angularModules['ui.sortable'] = array(
87 'js' => array('bower_components/angular-ui-sortable/sortable.min.js'),
89 $angularModules['unsavedChanges'] = array(
91 'js' => array('bower_components/angular-unsavedChanges/dist/unsavedChanges.min.js'),
94 foreach (\CRM_Core_Component
::getEnabledComponents() as $component) {
95 $angularModules = array_merge($angularModules, $component->getAngularModules());
97 \CRM_Utils_Hook
::angularModules($angularModules);
98 $this->modules
= $this->resolvePatterns($angularModules);
101 return $this->modules
;
105 * Get the descriptor for an Angular module.
107 * @param string $name
110 * Details about the module:
111 * - ext: string, the name of the Civi extension which defines the module
112 * - js: array(string $relativeFilePath).
113 * - css: array(string $relativeFilePath).
114 * - partials: array(string $relativeFilePath).
117 public function getModule($name) {
118 $modules = $this->getModules();
119 if (!isset($modules[$name])) {
120 throw new \
Exception("Unrecognized Angular module");
122 return $modules[$name];
126 * Convert any globs in an Angular module to file names.
128 * @param array $modules
129 * List of Angular modules.
131 * Updated list of Angular modules
133 protected function resolvePatterns($modules) {
134 $newModules = array();
136 foreach ($modules as $moduleKey => $module) {
137 foreach (array('js', 'css', 'partials') as $fileset) {
138 if (!isset($module[$fileset])) {
141 $module[$fileset] = $this->res
->glob($module['ext'], $module[$fileset]);
143 $newModules[$moduleKey] = $module;
150 * Get the partial HTML documents for a module.
152 * @param string $name
153 * Angular module name.
155 * Array(string $extFilePath => string $html)
157 * Invalid partials configuration.
159 public function getPartials($name) {
160 $module = $this->getModule($name);
162 if (isset($module['partials'])) {
163 foreach ($module['partials'] as $partialDir) {
164 $partialDir = $this->res
->getPath($module['ext']) . '/' . $partialDir;
165 $files = \CRM_Utils_File
::findFiles($partialDir, '*.html', TRUE);
166 foreach ($files as $file) {
167 $filename = '~/' . $name . '/' . $file;
168 $result[$filename] = file_get_contents($partialDir . '/' . $file);
176 * Get list of translated strings for a module.
178 * @param string $name
179 * Angular module name.
181 * Translated strings: array(string $orig => string $translated).
183 public function getTranslatedStrings($name) {
185 $strings = $this->getStrings($name);
186 foreach ($strings as $string) {
187 // TODO: should we pass translation domain based on $module[ext] or $module[tsDomain]?
188 // It doesn't look like client side really supports the domain right now...
189 $translated = ts($string);
190 if ($translated != $string) {
191 $result[$string] = $translated;
198 * Get list of translatable strings for a module.
200 * @param string $name
201 * Angular module name.
203 * Translatable strings.
205 public function getStrings($name) {
206 $module = $this->getModule($name);
208 if (isset($module['js'])) {
209 foreach ($module['js'] as $file) {
210 $strings = $this->res
->getStrings()->get(
212 $this->res
->getPath($module['ext'], $file),
215 $result = array_unique(array_merge($result, $strings));
218 if (isset($module['partials'])) {
219 foreach ($module['partials'] as $partialDir) {
220 $partialDir = $this->res
->getPath($module['ext']) . '/' . $partialDir;
221 $files = \CRM_Utils_File
::findFiles($partialDir, '*.html');
222 foreach ($files as $file) {
223 $strings = $this->res
->getStrings()->get(
228 $result = array_unique(array_merge($result, $strings));
236 * @param string $name
242 public function getScriptUrls($name) {
243 $module = $this->getModule($name);
245 if (isset($module['js'])) {
246 foreach ($module['js'] as $file) {
247 $result[] = $this->res
->getUrl($module['ext'], $file, TRUE);
254 * @param string $name
260 public function getStyleUrls($name) {
261 $module = $this->getModule($name);
263 if (isset($module['css'])) {
264 foreach ($module['css'] as $file) {
265 $result[] = $this->res
->getUrl($module['ext'], $file, TRUE);