Merge pull request #4534 from totten/master-civimail-abtest
[civicrm-core.git] / CRM / Core / Page / Angular.php
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
15 /**
16 * This function takes care of all the things common to all
17 * pages. This typically involves assigning the appropriate
18 * smarty variable :)
19 *
20 * @return string The content generated by running this page
21 */
22 function run() {
23 $this->registerResources(CRM_Core_Resources::singleton());
24 return parent::run();
25 }
26
27 /**
28 * @param CRM_Core_Resources $res
29 */
30 public function registerResources(CRM_Core_Resources $res) {
31 $modules = self::getAngularModules();
32
33 $res->addSettingsFactory(function () use (&$modules) {
34 // TODO optimization; client-side caching
35 return array(
36 'resourceUrls' => CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(),
37 'angular' => array(
38 'modules' => array_merge(array('ngRoute'), array_keys($modules)),
39 ),
40 );
41 });
42
43 $res->addScriptFile('civicrm', 'packages/bower_components/angular/angular.min.js', 100, 'html-header', FALSE);
44 $res->addScriptFile('civicrm', 'packages/bower_components/angular-route/angular-route.min.js', 110, 'html-header', FALSE);
45 $headOffset = 0;
46 foreach ($modules as $module) {
47 if (!empty($module['css'])) {
48 foreach ($module['css'] as $file) {
49 $res->addStyleFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header', TRUE);
50 }
51 }
52 if (!empty($module['js'])) {
53 foreach ($module['js'] as $file) {
54 $res->addScriptFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header', TRUE);
55 }
56 }
57 }
58 }
59
60 /**
61 * Get a list of AngularJS modules which should be autoloaded
62 *
63 * @return array (string $name => array('ext' => string $key, 'js' => array $paths, 'css' => array $paths))
64 */
65 public static function getAngularModules() {
66 $angularModules = array();
67 $angularModules['ui.utils'] = array('ext' => 'civicrm', 'js' => array('packages/bower_components/angular-ui-utils/ui-utils.min.js'));
68 $angularModules['ui.sortable'] = array('ext' => 'civicrm', 'js' => array('packages/bower_components/angular-ui-sortable/sortable.min.js'));
69 $angularModules['unsavedChanges'] = array('ext' => 'civicrm', 'js' => array('packages/bower_components/angular-unsavedChanges/dist/unsavedChanges.min.js'));
70 // https://github.com/jwstadler/angular-jquery-dialog-service
71 $angularModules['dialogService'] = array('ext' => 'civicrm' , 'js' => array('packages/bower_components/angular-jquery-dialog-service/dialog-service.js'));
72 $angularModules['crmUi'] = array('ext' => 'civicrm', 'js' => array('js/angular-crm-ui.js'));
73
74 foreach (CRM_Core_Component::getEnabledComponents() as $component) {
75 $angularModules = array_merge($angularModules, $component->getAngularModules());
76 }
77 CRM_Utils_Hook::angularModules($angularModules);
78 return $angularModules;
79 }
80
81 }