Commit | Line | Data |
---|---|---|
e7ff7042 TO |
1 | <?php |
2 | ||
3 | /** | |
4 | * This page is simply a container; any Angular modules defined by CiviCRM (or by CiviCRM extensions) | |
5 | * will be activated on this page. | |
6 | * | |
7 | * @link https://issues.civicrm.org/jira/browse/CRM-14479 | |
8 | */ | |
9 | class CRM_Core_Page_Angular extends CRM_Core_Page { | |
10 | /** | |
11 | * The weight to assign to any Angular JS module files | |
12 | */ | |
13 | const DEFAULT_MODULE_WEIGHT = 200; | |
14 | ||
2f6c50d5 TO |
15 | /** |
16 | * @var CRM_Core_Resources | |
17 | */ | |
18 | protected $res; | |
19 | ||
20 | /** | |
21 | * @param string $title | |
22 | * Title of the page. | |
23 | * @param int $mode | |
24 | * Mode of the page. | |
25 | * @param CRM_Core_Resources|null $res | |
26 | * Resource manager. | |
27 | */ | |
28 | public function __construct($title = NULL, $mode = NULL, $res = NULL) { | |
29 | parent::__construct($title, $mode); | |
30 | $this->res = CRM_Core_Resources::singleton(); | |
31 | } | |
32 | ||
b5c2afd0 EM |
33 | /** |
34 | * This function takes care of all the things common to all | |
35 | * pages. This typically involves assigning the appropriate | |
36 | * smarty variable :) | |
37 | * | |
a6c01b45 CW |
38 | * @return string |
39 | * The content generated by running this page | |
b5c2afd0 | 40 | */ |
00be9182 | 41 | public function run() { |
2f6c50d5 | 42 | $this->registerResources(); |
4b07d5bd TO |
43 | return parent::run(); |
44 | } | |
45 | ||
a0ee3941 | 46 | /** |
2f6c50d5 | 47 | * Register resources required by Angular. |
a0ee3941 | 48 | */ |
2f6c50d5 TO |
49 | public function registerResources() { |
50 | $modules = $this->getAngularModules(); | |
e7ff7042 | 51 | |
2f6c50d5 | 52 | $this->res->addSettingsFactory(function () use (&$modules) { |
e7ff7042 TO |
53 | // TODO optimization; client-side caching |
54 | return array( | |
55 | 'resourceUrls' => CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(), | |
56 | 'angular' => array( | |
57 | 'modules' => array_merge(array('ngRoute'), array_keys($modules)), | |
58 | ), | |
2b7de044 TO |
59 | 'crmAttachment' => array( |
60 | 'token' => CRM_Core_Page_AJAX_Attachment::createToken(), | |
61 | ), | |
e7ff7042 TO |
62 | ); |
63 | }); | |
64 | ||
2f6c50d5 TO |
65 | $this->res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE); |
66 | $this->res->addScriptFile('civicrm', 'bower_components/angular-route/angular-route.min.js', 110, 'html-header', FALSE); | |
1996b8b2 | 67 | $headOffset = 0; |
e7ff7042 | 68 | foreach ($modules as $module) { |
d08319ae TO |
69 | if (!empty($module['css'])) { |
70 | foreach ($module['css'] as $file) { | |
2f6c50d5 | 71 | $this->res->addStyleFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header', TRUE); |
d08319ae | 72 | } |
e7ff7042 | 73 | } |
d08319ae TO |
74 | if (!empty($module['js'])) { |
75 | foreach ($module['js'] as $file) { | |
2f6c50d5 | 76 | $this->res->addScriptFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header', TRUE); |
4b07d5bd TO |
77 | } |
78 | } | |
e7ff7042 | 79 | } |
e7ff7042 TO |
80 | } |
81 | ||
82 | /** | |
83 | * Get a list of AngularJS modules which should be autoloaded | |
84 | * | |
a6c01b45 CW |
85 | * @return array |
86 | * (string $name => array('ext' => string $key, 'js' => array $paths, 'css' => array $paths)) | |
e7ff7042 | 87 | */ |
2f6c50d5 | 88 | public function getAngularModules() { |
e7ff7042 | 89 | $angularModules = array(); |
353ffa53 TO |
90 | $angularModules['angularFileUpload'] = array( |
91 | 'ext' => 'civicrm', | |
d5cc0fc2 | 92 | 'js' => array('bower_components/angular-file-upload/angular-file-upload.min.js'), |
353ffa53 | 93 | ); |
416abe87 | 94 | $angularModules['crmApp'] = array('ext' => 'civicrm', 'js' => array('js/angular-crmApp.js')); |
353ffa53 TO |
95 | $angularModules['crmAttachment'] = array( |
96 | 'ext' => 'civicrm', | |
97 | 'js' => array('js/angular-crmAttachment.js'), | |
d5cc0fc2 | 98 | 'css' => array('css/angular-crmAttachment.css'), |
2f6c50d5 | 99 | 'partials' => array('partials/crmAttachment/*.html'), |
353ffa53 TO |
100 | ); |
101 | $angularModules['crmUi'] = array( | |
102 | 'ext' => 'civicrm', | |
d5cc0fc2 | 103 | 'js' => array('js/angular-crm-ui.js', 'packages/ckeditor/ckeditor.js'), |
2f6c50d5 | 104 | 'partials' => array('partials/crmUi/*.html'), |
353ffa53 | 105 | ); |
60ebf0a5 | 106 | $angularModules['crmUtil'] = array('ext' => 'civicrm', 'js' => array('js/angular-crm-util.js')); |
d89d6ff7 | 107 | // https://github.com/jwstadler/angular-jquery-dialog-service |
353ffa53 TO |
108 | $angularModules['dialogService'] = array( |
109 | 'ext' => 'civicrm', | |
d5cc0fc2 | 110 | 'js' => array('bower_components/angular-jquery-dialog-service/dialog-service.js'), |
353ffa53 | 111 | ); |
efd95528 | 112 | $angularModules['ngSanitize'] = array('ext' => 'civicrm', 'js' => array('js/angular-sanitize.js')); |
353ffa53 TO |
113 | $angularModules['ui.utils'] = array( |
114 | 'ext' => 'civicrm', | |
d5cc0fc2 | 115 | 'js' => array('bower_components/angular-ui-utils/ui-utils.min.js'), |
353ffa53 TO |
116 | ); |
117 | $angularModules['ui.sortable'] = array( | |
118 | 'ext' => 'civicrm', | |
d5cc0fc2 | 119 | 'js' => array('bower_components/angular-ui-sortable/sortable.min.js'), |
353ffa53 TO |
120 | ); |
121 | $angularModules['unsavedChanges'] = array( | |
122 | 'ext' => 'civicrm', | |
d5cc0fc2 | 123 | 'js' => array('bower_components/angular-unsavedChanges/dist/unsavedChanges.min.js'), |
353ffa53 | 124 | ); |
c45daff0 | 125 | |
e7ff7042 TO |
126 | foreach (CRM_Core_Component::getEnabledComponents() as $component) { |
127 | $angularModules = array_merge($angularModules, $component->getAngularModules()); | |
128 | } | |
129 | CRM_Utils_Hook::angularModules($angularModules); | |
2f6c50d5 | 130 | $angularModules = $this->resolvePatterns($angularModules); |
e7ff7042 TO |
131 | return $angularModules; |
132 | } | |
133 | ||
2f6c50d5 TO |
134 | /** |
135 | * @param array $modules | |
136 | * List of Angular modules. | |
137 | * @return array | |
138 | * Updated list of Angular modules | |
139 | */ | |
140 | public function resolvePatterns($modules) { | |
141 | $newModules = array(); | |
142 | ||
143 | foreach ($modules as $moduleKey => $module) { | |
144 | foreach (array('js', 'css', 'partials') as $fileset) { | |
145 | if (!isset($module[$fileset])) { | |
146 | continue; | |
147 | } | |
148 | $module[$fileset] = $this->res->glob($module['ext'], $module[$fileset]); | |
149 | } | |
150 | $newModules[$moduleKey] = $module; | |
151 | } | |
152 | ||
153 | return $newModules; | |
154 | } | |
155 | ||
b5c2afd0 | 156 | } |