Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-20981 - Allow custom base-pages with less crmApp boilerplate #10783

Merged
merged 4 commits into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions Civi/Angular/AngularLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class AngularLoader {
*/
protected $modules;

/**
* @var array|NULL
*/
protected $crmApp = NULL;

/**
* AngularLoader constructor.
*/
Expand All @@ -74,11 +79,32 @@ public function __construct() {

/**
* Register resources required by Angular.
*
* @return AngularLoader
*/
public function load() {
$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));
Expand Down Expand Up @@ -135,6 +161,45 @@ public function load() {
$res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), $this->getRegion());
}
}

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
* `<div ng-app="foo">`.
*
* Calling useApp() will replace the page's main body with the
* `<div ng-app="crmApp">...</div>` and apply some configuration options
* for the `crmApp` module.
*
* @param array $settings
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this documentation for $settings I think it would be helpful to have some example of values which can be supplied for activeRoute, defaultRoute, and region. This would help me understand whether I should doing things like using a preceding forward slash or not.

* A list of settings. Accepted values:
* - activeRoute: string, the route to open up immediately
* Ex: '/case/list'
* - defaultRoute: string, use this to redirect the default route (`/`) to another page
* Ex: '/case/list'
* - region: string, the place on the page where we should insert the angular app
* Ex: 'page-body'
* @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;
}

/**
Expand Down Expand Up @@ -170,9 +235,11 @@ public function getRes() {

/**
* @param \CRM_Core_Resources $res
* @return AngularLoader
*/
public function setRes($res) {
$this->res = $res;
return $this;
}

/**
Expand All @@ -184,9 +251,11 @@ public function getAngular() {

/**
* @param \Civi\Angular\Manager $angular
* @return AngularLoader
*/
public function setAngular($angular) {
$this->angular = $angular;
return $this;
}

/**
Expand All @@ -198,9 +267,11 @@ public function getRegion() {

/**
* @param string $region
* @return AngularLoader
*/
public function setRegion($region) {
$this->region = $region;
return $this;
}

/**
Expand All @@ -214,9 +285,21 @@ public function getPageName() {
/**
* @param string $pageName
* Ex: 'civicrm/a'.
* @return AngularLoader
*/
public function setPageName($pageName) {
$this->pageName = $pageName;
return $this;
}

/**
* @param array|string $modules
* @return AngularLoader
*/
public function addModules($modules) {
$modules = (array) $modules;
$this->modules = array_unique(array_merge($this->modules, $modules));
return $this;
}

/**
Expand All @@ -228,9 +311,11 @@ public function getModules() {

/**
* @param array $modules
* @return AngularLoader
*/
public function setModules($modules) {
$this->modules = $modules;
return $this;
}

}
21 changes: 4 additions & 17 deletions Civi/Angular/Page/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,12 @@ public function run() {
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';
}

}