Commit | Line | Data |
---|---|---|
7f254ad8 AE |
1 | <?php |
2 | namespace Civi\Angular\Page; | |
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 | */ | |
10 | class Main extends \CRM_Core_Page { | |
11 | /** | |
12 | * The weight to assign to any Angular JS module files. | |
13 | */ | |
14 | const DEFAULT_MODULE_WEIGHT = 200; | |
15 | ||
16 | /** | |
17 | * The resource manager. | |
18 | * | |
19 | * Do not use publicly. Inject your own copy! | |
20 | * | |
21 | * @var \CRM_Core_Resources | |
22 | */ | |
23 | public $res; | |
24 | ||
25 | ||
26 | /** | |
27 | * The Angular module manager. | |
28 | * | |
29 | * Do not use publicly. Inject your own copy! | |
30 | * | |
31 | * @var \Civi\Angular\Manager | |
32 | */ | |
33 | public $angular; | |
34 | ||
35 | /** | |
36 | * @param string $title | |
37 | * Title of the page. | |
38 | * @param int $mode | |
39 | * Mode of the page. | |
40 | * @param \CRM_Core_Resources|null $res | |
41 | * Resource manager. | |
42 | */ | |
43 | public function __construct($title = NULL, $mode = NULL, $res = NULL) { | |
44 | parent::__construct($title, $mode); | |
45 | $this->res = \CRM_Core_Resources::singleton(); | |
46 | $this->angular = \Civi\Core\Container::singleton()->get('angular'); | |
47 | } | |
48 | ||
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 | * | |
54 | * @return string | |
55 | * The content generated by running this page | |
56 | */ | |
57 | public function run() { | |
58 | $this->registerResources(); | |
59 | return parent::run(); | |
60 | } | |
61 | ||
62 | /** | |
63 | * Register resources required by Angular. | |
64 | */ | |
65 | public function registerResources() { | |
66 | $modules = $this->angular->getModules(); | |
67 | $page = $this; // PHP 5.3 does not propagate $this to inner functions. | |
68 | ||
69 | $this->res->addSettingsFactory(function () use (&$modules, $page) { | |
70 | // TODO optimization; client-side caching | |
71 | return array_merge($page->angular->getResources(array_keys($modules), 'settings', 'settings'), array( | |
72 | 'resourceUrls' => \CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(), | |
73 | 'angular' => array( | |
74 | 'modules' => array_merge(array('ngRoute'), array_keys($modules)), | |
75 | 'cacheCode' => $page->res->getCacheCode(), | |
76 | ), | |
77 | )); | |
78 | }); | |
79 | ||
80 | $this->res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE); | |
81 | ||
82 | // FIXME: crmUi depends on loading ckeditor, but ckeditor doesn't work with this aggregation. | |
83 | $this->res->addScriptFile('civicrm', 'packages/ckeditor/ckeditor.js', 100, 'page-header', FALSE); | |
84 | ||
85 | $headOffset = 0; | |
86 | $config = \CRM_Core_Config::singleton(); | |
87 | if ($config->debug) { | |
88 | foreach ($modules as $moduleName => $module) { | |
89 | foreach ($this->angular->getResources($moduleName, 'css', 'cacheUrl') as $url) { | |
90 | $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header'); | |
91 | } | |
92 | foreach ($this->angular->getResources($moduleName, 'js', 'cacheUrl') as $url) { | |
93 | $this->res->addScriptUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header'); | |
94 | // addScriptUrl() bypasses the normal string-localization of addScriptFile(), | |
95 | // but that's OK because all Angular strings (JS+HTML) will load via crmResource. | |
96 | } | |
97 | } | |
98 | } | |
99 | else { | |
100 | // Note: addScriptUrl() bypasses the normal string-localization of addScriptFile(), | |
101 | // but that's OK because all Angular strings (JS+HTML) will load via crmResource. | |
102 | $aggScriptUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=js&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE); | |
103 | $this->res->addScriptUrl($aggScriptUrl, 120, 'html-header'); | |
104 | ||
105 | // FIXME: The following CSS aggregator doesn't currently handle path-adjustments - which can break icons. | |
106 | //$aggStyleUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=css&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE); | |
107 | //$this->res->addStyleUrl($aggStyleUrl, 120, 'html-header'); | |
108 | ||
109 | foreach ($this->angular->getResources(array_keys($modules), 'css', 'cacheUrl') as $url) { | |
110 | $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header'); | |
111 | } | |
112 | } | |
113 | } | |
114 | ||
115 | } |