From: Tim Otten Date: Sat, 29 Jul 2017 00:01:10 +0000 (-0700) Subject: CRM-20981 - Move crmApp init from `Page\Main::run()` to `AngularLoader::useApp()` X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=3e8823e3e2c8c9f020306cc7dcc21e96cf9cf408;p=civicrm-core.git CRM-20981 - Move crmApp init from `Page\Main::run()` to `AngularLoader::useApp()` --- diff --git a/Civi/Angular/AngularLoader.php b/Civi/Angular/AngularLoader.php index bf18b30697..80cd4906e7 100644 --- a/Civi/Angular/AngularLoader.php +++ b/Civi/Angular/AngularLoader.php @@ -61,6 +61,11 @@ class AngularLoader { */ protected $modules; + /** + * @var array|NULL + */ + protected $crmApp = NULL; + /** * AngularLoader constructor. */ @@ -81,6 +86,25 @@ class AngularLoader { $angular = $this->getAngular(); $res = $this->getRes(); + if ($this->crmApp !== NULL) { + $this->addModules($this->crmApp['modules']); + $region = \CRM_Core_Region::instance($this->crmApp['region']); + $region->update('default', array('disabled' => TRUE)); + $region->add(array('template' => $this->crmApp['file'], 'weight' => 0)); + $this->res->addSetting(array( + 'crmApp' => array( + 'defaultRoute' => $this->crmApp['defaultRoute'], + ), + )); + + // If trying to load an Angular page via AJAX, the route must be passed as a + // URL parameter, since the server doesn't receive information about + // URL fragments (i.e, what comes after the #). + $this->res->addSetting(array( + 'angularRoute' => $this->crmApp['activeRoute'], + )); + } + $moduleNames = $this->findActiveModules(); if (!$this->isAllModules($moduleNames)) { $assetParams = array('modules' => implode(',', $moduleNames)); @@ -141,6 +165,40 @@ class AngularLoader { return $this; } + /** + * Use Civi's generic "application" module. + * + * This is suitable for use on a basic, standalone Angular page + * like `civicrm/a`. (If you need to integrate Angular with pre-existing, + * non-Angular pages... then this probably won't help.) + * + * The Angular bootstrap process requires an HTML directive like + * `
`. + * + * Calling useApp() will replace the page's main body with the + * `
...
` and apply some configuration options + * for the `crmApp` module. + * + * @param array $settings + * A list of settings. Accepted values: + * - activeRoute: string, the route to open up immediately + * - defaultRoute: string, use this to redirect the default route to another page + * - region: string, the place on the page where we should insert the angular app + * @return AngularLoader + * @link https://code.angularjs.org/1.5.11/docs/guide/bootstrap + */ + public function useApp($settings = array()) { + $defaults = array( + 'modules' => array('crmApp'), + 'activeRoute' => NULL, + 'defaultRoute' => NULL, + 'region' => 'page-body', + 'file' => 'Civi/Angular/Page/Main.tpl', + ); + $this->crmApp = array_merge($defaults, $settings); + return $this; + } + /** * Get a list of all Angular modules which should be activated on this * page. diff --git a/Civi/Angular/Page/Main.php b/Civi/Angular/Page/Main.php index a13a18c068..aa31fef711 100644 --- a/Civi/Angular/Page/Main.php +++ b/Civi/Angular/Page/Main.php @@ -76,25 +76,12 @@ class Main extends \CRM_Core_Page { public function registerResources() { $loader = new \Civi\Angular\AngularLoader(); $loader->setPageName('civicrm/a'); - $loader->setModules(array('crmApp')); - $loader->load(); - - // If trying to load an Angular page via AJAX, the route must be passed as a - // URL parameter, since the server doesn't receive information about - // URL fragments (i.e, what comes after the #). - \CRM_Core_Resources::singleton()->addSetting(array( - 'crmApp' => array( - 'defaultRoute' => NULL, - ), - 'angularRoute' => \CRM_Utils_Request::retrieve('route', 'String'), + $loader->useApp(array( + 'activeRoute' => \CRM_Utils_Request::retrieve('route', 'String'), + 'defaultRoute' => NULL, )); - } + $loader->load(); - /** - * @inheritdoc - */ - public function getTemplateFileName() { - return 'Civi/Angular/Page/Main.tpl'; } }