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 | 11 | /** |
fe482240 | 12 | * The weight to assign to any Angular JS module files. |
e7ff7042 TO |
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() { |
81916bee TL |
58 | $snippet = (array_key_exists('snippet', $_GET) && str_replace("/", "", $_GET['snippet']) != "0"); |
59 | $this->registerResources($snippet); | |
4b07d5bd TO |
60 | return parent::run(); |
61 | } | |
62 | ||
a0ee3941 | 63 | /** |
2f6c50d5 | 64 | * Register resources required by Angular. |
a0ee3941 | 65 | */ |
81916bee TL |
66 | public function registerResources($snippet = false) { |
67 | $scripts = array(); | |
68 | $styles = array(); | |
16072ce1 | 69 | $modules = $this->angular->getModules(); |
4d93c42f | 70 | $page = $this; // PHP 5.3 does not propagate $this to inner functions. |
e7ff7042 | 71 | |
4d93c42f | 72 | $this->res->addSettingsFactory(function () use (&$modules, $page) { |
e7ff7042 | 73 | // TODO optimization; client-side caching |
1da632e0 | 74 | return array_merge($page->angular->getResources(array_keys($modules), 'settings', 'settings'), array( |
81916bee TL |
75 | 'resourceUrls' => \CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(), |
76 | 'angular' => array( | |
77 | 'modules' => array_merge(array('ngRoute'), array_keys($modules)), | |
78 | 'cacheCode' => $page->res->getCacheCode(), | |
79 | ), | |
1da632e0 | 80 | )); |
e7ff7042 TO |
81 | }); |
82 | ||
81916bee TL |
83 | $config = \CRM_Core_Config::singleton(); |
84 | ||
2f6c50d5 | 85 | $this->res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE); |
81916bee TL |
86 | $scripts[] = $config->userFrameworkResourceURL .'bower_components/angular/angular.min.js'; |
87 | ||
27a90ef6 | 88 | |
6d57b745 | 89 | $headOffset = 0; |
27a90ef6 | 90 | if ($config->debug) { |
27a90ef6 TO |
91 | foreach ($modules as $moduleName => $module) { |
92 | foreach ($this->angular->getResources($moduleName, 'css', 'cacheUrl') as $url) { | |
93 | $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header'); | |
81916bee | 94 | $styles[] = $url; |
27a90ef6 TO |
95 | } |
96 | foreach ($this->angular->getResources($moduleName, 'js', 'cacheUrl') as $url) { | |
97 | $this->res->addScriptUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header'); | |
81916bee | 98 | $scripts[] = $url; |
27a90ef6 TO |
99 | // addScriptUrl() bypasses the normal string-localization of addScriptFile(), |
100 | // but that's OK because all Angular strings (JS+HTML) will load via crmResource. | |
101 | } | |
2f6c50d5 | 102 | } |
2f6c50d5 | 103 | } |
27a90ef6 TO |
104 | else { |
105 | // Note: addScriptUrl() bypasses the normal string-localization of addScriptFile(), | |
106 | // but that's OK because all Angular strings (JS+HTML) will load via crmResource. | |
107 | $aggScriptUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=js&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE); | |
108 | $this->res->addScriptUrl($aggScriptUrl, 120, 'html-header'); | |
81916bee | 109 | $scripts[] = $aggScriptUrl; |
27a90ef6 | 110 | |
6d57b745 TO |
111 | // FIXME: The following CSS aggregator doesn't currently handle path-adjustments - which can break icons. |
112 | //$aggStyleUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=css&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE); | |
113 | //$this->res->addStyleUrl($aggStyleUrl, 120, 'html-header'); | |
114 | ||
115 | foreach ($this->angular->getResources(array_keys($modules), 'css', 'cacheUrl') as $url) { | |
116 | $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header'); | |
81916bee | 117 | $styles[] = $url; |
6d57b745 | 118 | } |
27a90ef6 | 119 | } |
81916bee TL |
120 | |
121 | //This allows angular apps to be loaded as snippets in tabs | |
122 | $this->assign("snippet", $snippet); | |
123 | if($snippet) { | |
124 | $this->assign("route", $_REQUEST['route']); | |
125 | $this->assign("scripts", $scripts); | |
126 | $this->assign("styles", $styles); | |
127 | } | |
128 | ||
2f6c50d5 | 129 | } |
cbcb7579 | 130 | |
b5c2afd0 | 131 | } |