Skip to content

Commit

Permalink
CRM-16253 - Angular - Declare settings in $angularModules. Add settin…
Browse files Browse the repository at this point in the history
…g for callback URL.
  • Loading branch information
totten committed Apr 10, 2015
1 parent 7128aff commit 1da632e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
22 changes: 20 additions & 2 deletions Civi/Angular/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Manager {
* - partials: array(string $relativeFilePath)
* A list of partial-HTML folders (relative to the extension).
* This will be mapped to "~/moduleName" by crmResource.
* - settings: array(string $key => mixed $value)
* List of settings to preload.
*/
protected $modules = NULL;

Expand All @@ -50,9 +52,12 @@ public function __construct($res) {
* - partials: array(string $relativeFilePath)
* A list of partial-HTML folders (relative to the extension).
* This will be mapped to "~/moduleName" by crmResource.
* - settings: array(string $key => mixed $value)
* List of settings to preload.
*/
public function getModules() {
if ($this->modules === NULL) {
$config = \CRM_Core_Config::singleton();

$angularModules = array();
$angularModules['angularFileUpload'] = array(
Expand All @@ -68,6 +73,9 @@ public function getModules() {
'js' => array('ang/crmAttachment.js'),
'css' => array('ang/crmAttachment.css'),
'partials' => array('ang/crmAttachment'),
'settings' => array(
'token' => \CRM_Core_Page_AJAX_Attachment::createToken(),
),
);
$angularModules['crmAutosave'] = array(
'ext' => 'civicrm',
Expand All @@ -87,6 +95,10 @@ public function getModules() {
'ext' => 'civicrm',
'js' => array('ang/crmUi.js'),
'partials' => array('ang/crmUi'),
'settings' => array(
'browseUrl' => $config->userFrameworkResourceURL . 'packages/kcfinder/browse.php',
'uploadUrl' => $config->userFrameworkResourceURL . 'packages/kcfinder/upload.php',
),
);
$angularModules['crmUtil'] = array(
'ext' => 'civicrm',
Expand Down Expand Up @@ -264,9 +276,9 @@ public function getStrings($name) {
* @param string|array $moduleNames
* List of module names.
* @param string $resType
* Type of resource ('js', 'css').
* Type of resource ('js', 'css', 'settings').
* @param string $refType
* Type of reference to the resource ('cacheUrl', 'rawUrl', 'path').
* Type of reference to the resource ('cacheUrl', 'rawUrl', 'path', 'settings').
* @return array
* List of URLs or paths.
* @throws \CRM_Core_Exception
Expand All @@ -291,6 +303,12 @@ public function getResources($moduleNames, $resType, $refType) {
$result[] = $this->res->getUrl($module['ext'], $file, TRUE);
break;

case 'settings':
if (!empty($module[$resType])) {
$result[$moduleName] = $module[$resType];
}
break;

default:
throw new \CRM_Core_Exception("Unrecognized resource format");
}
Expand Down
7 changes: 2 additions & 5 deletions Civi/Angular/Page/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,13 @@ public function registerResources() {

$this->res->addSettingsFactory(function () use (&$modules, $page) {
// TODO optimization; client-side caching
return array(
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(),
),
'crmAttachment' => array(
'token' => \CRM_Core_Page_AJAX_Attachment::createToken(),
),
);
));
});

$this->res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE);
Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/Civi/Angular/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function testGetModules() {
'js' => 0,
'css' => 0,
'partials' => 0,
'settings' => 0,
);

foreach ($modules as $module) {
Expand All @@ -91,11 +92,18 @@ public function testGetModules() {
$counts['partials']++;
}
}
if (isset($module['settings'])) {
$this->assertTrue(is_array($module['settings']));
foreach ($module['settings'] as $name => $value) {
$counts['settings']++;
}
}
}

$this->assertTrue($counts['js'] > 0, 'Expect to find at least one JS file');
$this->assertTrue($counts['css'] > 0, 'Expect to find at least one CSS file');
$this->assertTrue($counts['partials'] > 0, 'Expect to find at least one partial HTML file');
$this->assertTrue($counts['settings'] > 1, 'Expect to find at least one setting');
}

/**
Expand Down

0 comments on commit 1da632e

Please sign in to comment.