Commit | Line | Data |
---|---|---|
e7ff7042 | 1 | <?php |
39c3d5e9 | 2 | namespace Civi\Angular\Page; |
e7ff7042 TO |
3 | |
4 | /** | |
5 | * This page is simply a container; any Angular modules defined by CiviCRM (or by CiviCRM extensions) | |
6 | * will be activated on this page. | |
7 | * | |
8 | * @link https://issues.civicrm.org/jira/browse/CRM-14479 | |
9 | */ | |
39c3d5e9 | 10 | class Main extends \CRM_Core_Page { |
e7ff7042 TO |
11 | /** |
12 | * The weight to assign to any Angular JS module files | |
13 | */ | |
14 | const DEFAULT_MODULE_WEIGHT = 200; | |
15 | ||
2f6c50d5 | 16 | /** |
39c3d5e9 | 17 | * @var \CRM_Core_Resources |
2f6c50d5 TO |
18 | */ |
19 | protected $res; | |
20 | ||
16072ce1 TO |
21 | |
22 | /** | |
39c3d5e9 | 23 | * @var \Civi\Angular\Manager |
16072ce1 TO |
24 | */ |
25 | protected $angular; | |
26 | ||
2f6c50d5 TO |
27 | /** |
28 | * @param string $title | |
29 | * Title of the page. | |
30 | * @param int $mode | |
31 | * Mode of the page. | |
39c3d5e9 | 32 | * @param \CRM_Core_Resources|null $res |
2f6c50d5 TO |
33 | * Resource manager. |
34 | */ | |
35 | public function __construct($title = NULL, $mode = NULL, $res = NULL) { | |
36 | parent::__construct($title, $mode); | |
39c3d5e9 TO |
37 | $this->res = \CRM_Core_Resources::singleton(); |
38 | $this->angular = \Civi\Core\Container::singleton()->get('angular'); | |
2f6c50d5 TO |
39 | } |
40 | ||
b5c2afd0 EM |
41 | /** |
42 | * This function takes care of all the things common to all | |
43 | * pages. This typically involves assigning the appropriate | |
44 | * smarty variable :) | |
45 | * | |
a6c01b45 CW |
46 | * @return string |
47 | * The content generated by running this page | |
b5c2afd0 | 48 | */ |
00be9182 | 49 | public function run() { |
2f6c50d5 | 50 | $this->registerResources(); |
4b07d5bd TO |
51 | return parent::run(); |
52 | } | |
53 | ||
a0ee3941 | 54 | /** |
2f6c50d5 | 55 | * Register resources required by Angular. |
a0ee3941 | 56 | */ |
2f6c50d5 | 57 | public function registerResources() { |
16072ce1 | 58 | $modules = $this->angular->getModules(); |
e7ff7042 | 59 | |
2f6c50d5 | 60 | $this->res->addSettingsFactory(function () use (&$modules) { |
e7ff7042 TO |
61 | // TODO optimization; client-side caching |
62 | return array( | |
39c3d5e9 | 63 | 'resourceUrls' => \CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(), |
e7ff7042 TO |
64 | 'angular' => array( |
65 | 'modules' => array_merge(array('ngRoute'), array_keys($modules)), | |
a2dc0f82 | 66 | 'cacheCode' => $this->res->getCacheCode(), |
e7ff7042 | 67 | ), |
2b7de044 | 68 | 'crmAttachment' => array( |
39c3d5e9 | 69 | 'token' => \CRM_Core_Page_AJAX_Attachment::createToken(), |
2b7de044 | 70 | ), |
e7ff7042 TO |
71 | ); |
72 | }); | |
73 | ||
2f6c50d5 TO |
74 | $this->res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE); |
75 | $this->res->addScriptFile('civicrm', 'bower_components/angular-route/angular-route.min.js', 110, 'html-header', FALSE); | |
1996b8b2 | 76 | $headOffset = 0; |
16072ce1 TO |
77 | foreach ($modules as $moduleName => $module) { |
78 | foreach ($this->angular->getStyleUrls($moduleName) as $url) { | |
79 | $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header'); | |
4b07d5bd | 80 | } |
16072ce1 TO |
81 | foreach ($this->angular->getScriptUrls($moduleName) as $url) { |
82 | $this->res->addScriptUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header'); | |
83 | // addScriptUrl() bypasses the normal string-localization of addScriptFile(), | |
84 | // but that's OK because all Angular strings (JS+HTML) will load via crmResource. | |
2f6c50d5 | 85 | } |
2f6c50d5 | 86 | } |
2f6c50d5 | 87 | } |
cbcb7579 | 88 | |
b5c2afd0 | 89 | } |