| 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 | |
| 15 | /** |
| 16 | * @var CRM_Core_Resources |
| 17 | */ |
| 18 | protected $res; |
| 19 | |
| 20 | |
| 21 | /** |
| 22 | * @var Civi\Angular\Manager |
| 23 | */ |
| 24 | protected $angular; |
| 25 | |
| 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(); |
| 37 | $this->angular = Civi\Core\Container::singleton()->get('angular'); |
| 38 | } |
| 39 | |
| 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 | * |
| 45 | * @return string |
| 46 | * The content generated by running this page |
| 47 | */ |
| 48 | public function run() { |
| 49 | $this->registerResources(); |
| 50 | return parent::run(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Register resources required by Angular. |
| 55 | */ |
| 56 | public function registerResources() { |
| 57 | $modules = $this->angular->getModules(); |
| 58 | |
| 59 | $this->res->addSettingsFactory(function () use (&$modules) { |
| 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)), |
| 65 | 'cacheCode' => $this->res->getCacheCode(), |
| 66 | ), |
| 67 | 'crmAttachment' => array( |
| 68 | 'token' => CRM_Core_Page_AJAX_Attachment::createToken(), |
| 69 | ), |
| 70 | ); |
| 71 | }); |
| 72 | |
| 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); |
| 75 | $headOffset = 0; |
| 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'); |
| 79 | } |
| 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. |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |