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['crmUi'] = array(
60 'js' => array('js/angular-crm-ui.js', 'packages/ckeditor/ckeditor.js'),
61 'partials' => array('partials/crmUi/*.html'),
63 $angularModules['crmUtil'] = array(
65 'js' => array('js/angular-crm-util.js'),
67 // https://github.com/jwstadler/angular-jquery-dialog-service
68 $angularModules['dialogService'] = array(
70 'js' => array('bower_components/angular-jquery-dialog-service/dialog-service.js'),
72 $angularModules['ngSanitize'] = array(
74 'js' => array('js/angular-sanitize.js'),
76 $angularModules['ui.utils'] = array(
78 'js' => array('bower_components/angular-ui-utils/ui-utils.min.js'),
80 $angularModules['ui.sortable'] = array(
82 'js' => array('bower_components/angular-ui-sortable/sortable.min.js'),
84 $angularModules['unsavedChanges'] = array(
86 'js' => array('bower_components/angular-unsavedChanges/dist/unsavedChanges.min.js'),
89 foreach (\CRM_Core_Component
::getEnabledComponents() as $component) {
90 $angularModules = array_merge($angularModules, $component->getAngularModules());
92 \CRM_Utils_Hook
::angularModules($angularModules);
93 $this->modules
= $this->resolvePatterns($angularModules);
96 return $this->modules
;
100 * Get the descriptor for an Angular module.
102 * @param string $name
105 * Details about the module:
106 * - ext: string, the name of the Civi extension which defines the module
107 * - js: array(string $relativeFilePath).
108 * - css: array(string $relativeFilePath).
109 * - partials: array(string $relativeFilePath).
112 public function getModule($name) {
113 $modules = $this->getModules();
114 if (!isset($modules[$name])) {
115 throw new \
Exception("Unrecognized Angular module");
117 return $modules[$name];
121 * Convert any globs in an Angular module to file names.
123 * @param array $modules
124 * List of Angular modules.
126 * Updated list of Angular modules
128 protected function resolvePatterns($modules) {
129 $newModules = array();
131 foreach ($modules as $moduleKey => $module) {
132 foreach (array('js', 'css', 'partials') as $fileset) {
133 if (!isset($module[$fileset])) {
136 $module[$fileset] = $this->res
->glob($module['ext'], $module[$fileset]);
138 $newModules[$moduleKey] = $module;
145 * Get the partial HTML documents for a module.
147 * @param string $name
148 * Angular module name.
150 * Array(string $extFilePath => string $html)
152 public function getPartials($name) {
153 $module = $this->getModule($name);
155 if (isset($module['partials'])) {
156 foreach ($module['partials'] as $file) {
157 $filename = $name . '/' . $file;
158 $result[$filename] = file_get_contents($this->res
->getPath($module['ext'], $file));
166 * Get list of translated strings for a module.
168 * @param string $name
169 * Angular module name.
171 * Translated strings: array(string $orig => string $translated).
173 public function getTranslatedStrings($name) {
175 $strings = $this->getStrings($name);
176 foreach ($strings as $string) {
177 // TODO: should we pass translation domain based on $module[ext] or $module[tsDomain]?
178 // It doesn't look like client side really supports the domain right now...
179 $translated = ts($string);
180 if ($translated != $string) {
181 $result[$string] = $translated;
188 * Get list of translatable strings for a module.
190 * @param string $name
191 * Angular module name.
193 * Translatable strings.
195 public function getStrings($name) {
196 $module = $this->getModule($name);
198 if (isset($module['js'])) {
199 foreach ($module['js'] as $file) {
200 $strings = $this->res
->getStrings()->get(
202 $this->res
->getPath($module['ext'], $file),
205 $result = array_unique(array_merge($result, $strings));
208 if (isset($module['partials'])) {
209 foreach ($module['partials'] as $file) {
210 $strings = $this->res
->getStrings()->get(
212 $this->res
->getPath($module['ext'], $file),
215 $result = array_unique(array_merge($result, $strings));
222 * @param string $name
228 public function getScriptUrls($name) {
229 $module = $this->getModule($name);
231 if (isset($module['js'])) {
232 foreach ($module['js'] as $file) {
233 $result[] = $this->res
->getUrl($module['ext'], $file, TRUE);
240 * @param string $name
246 public function getStyleUrls($name) {
247 $module = $this->getModule($name);
249 if (isset($module['css'])) {
250 foreach ($module['css'] as $file) {
251 $result[] = $this->res
->getUrl($module['ext'], $file, TRUE);