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['crmResource'] = array(
78 // 'js' => array('js/angular-crmResource/byModule.js'), // One HTTP request per module.
79 'js' => array('js/angular-crmResource/all.js'), // One HTTP request for all modules.
81 $angularModules['crmUi'] = array(
83 'js' => array('js/angular-crm-ui.js', 'packages/ckeditor/ckeditor.js'),
84 'partials' => array('partials/crmUi'),
86 $angularModules['crmUtil'] = array(
88 'js' => array('js/angular-crm-util.js'),
90 // https://github.com/jwstadler/angular-jquery-dialog-service
91 $angularModules['dialogService'] = array(
93 'js' => array('bower_components/angular-jquery-dialog-service/dialog-service.js'),
95 $angularModules['ngSanitize'] = array(
97 'js' => array('js/angular-sanitize.js'),
99 $angularModules['ui.utils'] = array(
101 'js' => array('bower_components/angular-ui-utils/ui-utils.min.js'),
103 $angularModules['ui.sortable'] = array(
105 'js' => array('bower_components/angular-ui-sortable/sortable.min.js'),
107 $angularModules['unsavedChanges'] = array(
109 'js' => array('bower_components/angular-unsavedChanges/dist/unsavedChanges.min.js'),
112 foreach (\CRM_Core_Component
::getEnabledComponents() as $component) {
113 $angularModules = array_merge($angularModules, $component->getAngularModules());
115 \CRM_Utils_Hook
::angularModules($angularModules);
116 $this->modules
= $this->resolvePatterns($angularModules);
119 return $this->modules
;
123 * Get the descriptor for an Angular module.
125 * @param string $name
128 * Details about the module:
129 * - ext: string, the name of the Civi extension which defines the module
130 * - js: array(string $relativeFilePath).
131 * - css: array(string $relativeFilePath).
132 * - partials: array(string $relativeFilePath).
135 public function getModule($name) {
136 $modules = $this->getModules();
137 if (!isset($modules[$name])) {
138 throw new \
Exception("Unrecognized Angular module");
140 return $modules[$name];
144 * Convert any globs in an Angular module to file names.
146 * @param array $modules
147 * List of Angular modules.
149 * Updated list of Angular modules
151 protected function resolvePatterns($modules) {
152 $newModules = array();
154 foreach ($modules as $moduleKey => $module) {
155 foreach (array('js', 'css', 'partials') as $fileset) {
156 if (!isset($module[$fileset])) {
159 $module[$fileset] = $this->res
->glob($module['ext'], $module[$fileset]);
161 $newModules[$moduleKey] = $module;
168 * Get the partial HTML documents for a module.
170 * @param string $name
171 * Angular module name.
173 * Array(string $extFilePath => string $html)
175 * Invalid partials configuration.
177 public function getPartials($name) {
178 $module = $this->getModule($name);
180 if (isset($module['partials'])) {
181 foreach ($module['partials'] as $partialDir) {
182 $partialDir = $this->res
->getPath($module['ext']) . '/' . $partialDir;
183 $files = \CRM_Utils_File
::findFiles($partialDir, '*.html', TRUE);
184 foreach ($files as $file) {
185 $filename = '~/' . $name . '/' . $file;
186 $result[$filename] = file_get_contents($partialDir . '/' . $file);
194 * Get list of translated strings for a module.
196 * @param string $name
197 * Angular module name.
199 * Translated strings: array(string $orig => string $translated).
201 public function getTranslatedStrings($name) {
202 $module = $this->getModule($name);
204 $strings = $this->getStrings($name);
205 foreach ($strings as $string) {
206 // TODO: should we pass translation domain based on $module[ext] or $module[tsDomain]?
207 // It doesn't look like client side really supports the domain right now...
208 $translated = ts($string, array(
209 'domain' => array($module['ext'], NULL),
211 if ($translated != $string) {
212 $result[$string] = $translated;
219 * Get list of translatable strings for a module.
221 * @param string $name
222 * Angular module name.
224 * Translatable strings.
226 public function getStrings($name) {
227 $module = $this->getModule($name);
229 if (isset($module['js'])) {
230 foreach ($module['js'] as $file) {
231 $strings = $this->res
->getStrings()->get(
233 $this->res
->getPath($module['ext'], $file),
236 $result = array_unique(array_merge($result, $strings));
239 if (isset($module['partials'])) {
240 foreach ($module['partials'] as $partialDir) {
241 $partialDir = $this->res
->getPath($module['ext']) . '/' . $partialDir;
242 $files = \CRM_Utils_File
::findFiles($partialDir, '*.html');
243 foreach ($files as $file) {
244 $strings = $this->res
->getStrings()->get(
249 $result = array_unique(array_merge($result, $strings));
257 * @param string $name
263 public function getScriptUrls($name) {
264 $module = $this->getModule($name);
266 if (isset($module['js'])) {
267 foreach ($module['js'] as $file) {
268 $result[] = $this->res
->getUrl($module['ext'], $file, TRUE);
275 * @param string $name
281 public function getStyleUrls($name) {
282 $module = $this->getModule($name);
284 if (isset($module['css'])) {
285 foreach ($module['css'] as $file) {
286 $result[] = $this->res
->getUrl($module['ext'], $file, TRUE);