Commit 81916bee authored by Tobias Lounsbury's avatar Tobias Lounsbury

Added ability for angular to be loaded as a snippet

parent 8a80aaf4
......@@ -55,39 +55,47 @@ class Main extends \CRM_Core_Page {
* The content generated by running this page
*/
public function run() {
$this->registerResources();
$snippet = (array_key_exists('snippet', $_GET) && str_replace("/", "", $_GET['snippet']) != "0");
$this->registerResources($snippet);
return parent::run();
}
/**
* Register resources required by Angular.
*/
public function registerResources() {
public function registerResources($snippet = false) {
$scripts = array();
$styles = array();
$modules = $this->angular->getModules();
$page = $this; // PHP 5.3 does not propagate $this to inner functions.
$this->res->addSettingsFactory(function () use (&$modules, $page) {
// TODO optimization; client-side caching
return array_merge($page->angular->getResources(array_keys($modules), 'settings', 'settings'), array(
'resourceUrls' => \CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(),
'angular' => array(
'modules' => array_merge(array('ngRoute'), array_keys($modules)),
'cacheCode' => $page->res->getCacheCode(),
),
'resourceUrls' => \CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(),
'angular' => array(
'modules' => array_merge(array('ngRoute'), array_keys($modules)),
'cacheCode' => $page->res->getCacheCode(),
),
));
});
$config = \CRM_Core_Config::singleton();
$this->res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE);
$scripts[] = $config->userFrameworkResourceURL .'bower_components/angular/angular.min.js';
$headOffset = 0;
$config = \CRM_Core_Config::singleton();
if ($config->debug) {
foreach ($modules as $moduleName => $module) {
foreach ($this->angular->getResources($moduleName, 'css', 'cacheUrl') as $url) {
$this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header');
$styles[] = $url;
}
foreach ($this->angular->getResources($moduleName, 'js', 'cacheUrl') as $url) {
$this->res->addScriptUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header');
$scripts[] = $url;
// addScriptUrl() bypasses the normal string-localization of addScriptFile(),
// but that's OK because all Angular strings (JS+HTML) will load via crmResource.
}
......@@ -98,6 +106,7 @@ class Main extends \CRM_Core_Page {
// but that's OK because all Angular strings (JS+HTML) will load via crmResource.
$aggScriptUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=js&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE);
$this->res->addScriptUrl($aggScriptUrl, 120, 'html-header');
$scripts[] = $aggScriptUrl;
// FIXME: The following CSS aggregator doesn't currently handle path-adjustments - which can break icons.
//$aggStyleUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=css&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE);
......@@ -105,8 +114,18 @@ class Main extends \CRM_Core_Page {
foreach ($this->angular->getResources(array_keys($modules), 'css', 'cacheUrl') as $url) {
$this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header');
$styles[] = $url;
}
}
//This allows angular apps to be loaded as snippets in tabs
$this->assign("snippet", $snippet);
if($snippet) {
$this->assign("route", $_REQUEST['route']);
$this->assign("scripts", $scripts);
$this->assign("styles", $styles);
}
}
}
{if $snippet}
<script type="text/javascript">
location.hash = '{$route}';
</script>
{foreach from=$styles item=styleURL}
<link rel="stylesheet" type="text/css" href="{$styleURL}" />
{/foreach}
{foreach from=$scripts item=scriptURL}
<script type="text/javascript" src="{$scriptURL}"></script>
{/foreach}
{/if}
{literal}
<div ng-app="crmApp">
<div ng-view></div>
</div>
{/literal}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment