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