Skip to content

Commit

Permalink
CRM-20600 - Angular deps - Allow declaration in PHP code
Browse files Browse the repository at this point in the history
If third-parties are allowed to alter the HTML content, then they may
introduce new dependencies.  This means that they'll need to delcare those
dependencies.

Tangentially, this will also make it easier to construct more optimized
base-pages (e.g.  "make a base-page with module X plus all its
dependencies").
  • Loading branch information
totten committed Jun 17, 2017
1 parent fe15812 commit 5438399
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 15 deletions.
8 changes: 7 additions & 1 deletion CRM/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,12 @@ public static function alterDisplayName(&$displayName, $contactId, $dao) {
* EXPERIMENTAL: This hook allows one to register additional Angular modules
*
* @param array $angularModules
* List of modules.
* List of modules. Each module defines:
* - ext: string, the CiviCRM extension which hosts the files.
* - js: array, list of JS files or globs.
* - css: array, list of CSS files or globs.
* - partials: array, list of base-dirs containing HTML.
* - requires: array, list of required Angular modules.
* @return null
* the return value is ignored
*
Expand All @@ -2090,6 +2095,7 @@ public static function alterDisplayName(&$displayName, $contactId, $dao) {
* 'js' => array('js/part1.js', 'js/part2.js'),
* 'css' => array('css/myAngularModule.css'),
* 'partials' => array('partials/myBigAngularModule'),
* 'requires' => array('otherModuleA', 'otherModuleB'),
* );
* }
* @endcode
Expand Down
1 change: 1 addition & 0 deletions Civi/Angular/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ public function getResources($moduleNames, $resType, $refType) {
break;

case 'settings':
case 'requires':
if (!empty($module[$resType])) {
$result[$moduleName] = $module[$resType];
}
Expand Down
2 changes: 2 additions & 0 deletions Civi/Angular/Page/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ public function registerResources() {
'resourceUrls' => \CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(),
'angular' => array(
'modules' => array_merge(array('ngRoute'), $moduleNames),
'requires' => $page->angular->getResources($moduleNames, 'requires', 'requires'),
'cacheCode' => $page->res->getCacheCode(),
'bundleUrl' => \Civi::service('asset_builder')->getUrl('angular-modules.json', $assetParams),
),
));
});

$this->res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, $this->region, FALSE);
$this->res->addScriptFile('civicrm', 'js/crm.angular.js', 101, $this->region, FALSE);

$headOffset = 0;
$config = \CRM_Core_Config::singleton();
Expand Down
4 changes: 4 additions & 0 deletions js/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,10 @@ if (!CRM.vars) CRM.vars = {};
return format.replace(/1.*234.*56/, result);
};

CRM.angRequires = function(name) {
return CRM.angular.requires[name] || [];
};

CRM.console = function(method, title, msg) {
if (window.console) {
method = $.isFunction(console[method]) ? method : 'log';
Expand Down
7 changes: 7 additions & 0 deletions js/crm.angular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(function (angular, $, _) {
// DEPRECATED: A variant of angular.module() which uses a dependency list provided by the server.
// REMOVE circa v4.7.22.
angular.crmDepends = function crmDepends(name) {
return angular.module(name, CRM.angRequires(name));
};
})(angular, CRM.$, CRM._);
21 changes: 20 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
var cv = require('civicrm-cv')({mode: 'sync'});
var _CV = cv('vars:show');
var cmd =
'CRM_Core_BAO_ConfigSetting::enableComponent("CiviCase");' +
'global $civicrm_root;' +
'$f = CRM_Utils_File::addTrailingSlash($civicrm_root)."tmp/karma.cv.js";' +
'mkdir(dirname($f), 0777, TRUE);' +
'$a=Civi::service("angular");' +
'$data = "var CRM = CRM || {}; CRM.angular =";' +
'$data .= json_encode(array(' +
' "modules" => array_keys($a->getModules()),' +
' "requires" => $a->getResources(array_keys($a->getModules()), "requires","requires"),' +
'));' +
'$data .= ";";' +
'file_put_contents($f, $data);' +
'return $f;';
var angularTempFile = cv(['php:eval', '-U', _CV.ADMIN_USER, cmd]);

module.exports = function(config) {
config.set({
autoWatch: true,
Expand All @@ -15,14 +33,15 @@ module.exports = function(config) {
'packages/jquery/plugins/jquery.timeentry.js',
'js/Common.js',
'bower_components/angular/angular.js',
'js/crm.angular.js',
angularTempFile,
'bower_components/angular-file-upload/angular-file-upload.js',
'bower_components/angular-jquery-dialog-service/dialog-service.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-ui-sortable/sortable.js',
'bower_components/angular-ui-utils/ui-utils.js',
'bower_components/angular-unsavedChanges/dist/unsavedChanges.js',
'tests/karma/modules.js',
'js/crm.ajax.js',
'ang/*.js',
'ang/**/*.js',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"devDependencies": {
"bower": "^1.3.1",
"civicrm-cv": "^0.1.2",
"karma": "^0.12.16",
"karma-ng-html2js-preprocessor": "^0.1.2",
"karma-phantomjs-launcher": "^0.1.4",
Expand Down
13 changes: 0 additions & 13 deletions tests/karma/modules.js

This file was deleted.

0 comments on commit 5438399

Please sign in to comment.