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 | ||
16072ce1 TO |
20 | |
21 | /** | |
22 | * @var Civi\Angular\Manager | |
23 | */ | |
24 | protected $angular; | |
25 | ||
2f6c50d5 TO |
26 | /** |
27 | * @param string $title | |
28 | * Title of the page. | |
29 | * @param int $mode | |
30 | * Mode of the page. | |
31 | * @param CRM_Core_Resources|null $res | |
32 | * Resource manager. | |
33 | */ | |
34 | public function __construct($title = NULL, $mode = NULL, $res = NULL) { | |
35 | parent::__construct($title, $mode); | |
36 | $this->res = CRM_Core_Resources::singleton(); | |
16072ce1 | 37 | $this->angular = Civi\Core\Container::singleton()->get('angular'); |
2f6c50d5 TO |
38 | } |
39 | ||
b5c2afd0 EM |
40 | /** |
41 | * This function takes care of all the things common to all | |
42 | * pages. This typically involves assigning the appropriate | |
43 | * smarty variable :) | |
44 | * | |
a6c01b45 CW |
45 | * @return string |
46 | * The content generated by running this page | |
b5c2afd0 | 47 | */ |
00be9182 | 48 | public function run() { |
2f6c50d5 | 49 | $this->registerResources(); |
4b07d5bd TO |
50 | return parent::run(); |
51 | } | |
52 | ||
a0ee3941 | 53 | /** |
2f6c50d5 | 54 | * Register resources required by Angular. |
a0ee3941 | 55 | */ |
2f6c50d5 | 56 | public function registerResources() { |
16072ce1 | 57 | $modules = $this->angular->getModules(); |
e7ff7042 | 58 | |
2f6c50d5 | 59 | $this->res->addSettingsFactory(function () use (&$modules) { |
e7ff7042 TO |
60 | // TODO optimization; client-side caching |
61 | return array( | |
62 | 'resourceUrls' => CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(), | |
63 | 'angular' => array( | |
64 | 'modules' => array_merge(array('ngRoute'), array_keys($modules)), | |
a2dc0f82 | 65 | 'cacheCode' => $this->res->getCacheCode(), |
e7ff7042 | 66 | ), |
2b7de044 TO |
67 | 'crmAttachment' => array( |
68 | 'token' => CRM_Core_Page_AJAX_Attachment::createToken(), | |
69 | ), | |
e7ff7042 TO |
70 | ); |
71 | }); | |
72 | ||
2f6c50d5 TO |
73 | $this->res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE); |
74 | $this->res->addScriptFile('civicrm', 'bower_components/angular-route/angular-route.min.js', 110, 'html-header', FALSE); | |
1996b8b2 | 75 | $headOffset = 0; |
16072ce1 TO |
76 | foreach ($modules as $moduleName => $module) { |
77 | foreach ($this->angular->getStyleUrls($moduleName) as $url) { | |
78 | $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header'); | |
4b07d5bd | 79 | } |
16072ce1 TO |
80 | foreach ($this->angular->getScriptUrls($moduleName) as $url) { |
81 | $this->res->addScriptUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header'); | |
82 | // addScriptUrl() bypasses the normal string-localization of addScriptFile(), | |
83 | // but that's OK because all Angular strings (JS+HTML) will load via crmResource. | |
2f6c50d5 | 84 | } |
2f6c50d5 | 85 | } |
2f6c50d5 | 86 | } |
b5c2afd0 | 87 | } |