Commit 0c73e8d9 authored by colemanw's avatar colemanw
Browse files

Merge branch 'partials' into 'master'

Use dynamic snippet for afform module layout.

See merge request extensions/afform!16
parents 76d03a4b d66af448
......@@ -170,18 +170,33 @@ function afform_civicrm_angularModules(&$angularModules) {
'js' => ['assetBuilder://afform.js?name=' . urlencode($name)],
'requires' => $meta['requires'],
'basePages' => [],
'partialsCallback' => '_afform_get_partials',
'_afform' => $name,
'exports' => [
_afform_angular_module_name($name, 'dash') => 'AE',
],
];
// FIXME: The HTML layout template is embedded in the JS asset.
// This works at runtime for basic usage, but it bypasses
// the normal workflow for templates (e.g. translation).
// We should update core so that 'partials' can be specified more dynamically.
}
}
/**
* Construct a list of partials for a given afform/angular module.
*
* @param string $moduleName
* The module name.
* @param array $module
* The module definition.
* @return array
* Array(string $filename => string $html).
*/
function _afform_get_partials($moduleName, $module) {
/** @var CRM_Afform_AfformScanner $scanner */
$scanner = Civi::service('afform_scanner');
return [
"~/$moduleName/$moduleName.aff.html" => $scanner->getLayout($module['_afform']),
];
}
/**
* Scan the list of Angular modules and inject automatic-requirements.
*
......@@ -301,7 +316,7 @@ function _afform_reverse_deps_find($formName, $html, $revMap) {
*/
function afform_civicrm_alterAngular($angular) {
$fieldMetadata = \Civi\Angular\ChangeSet::create('fieldMetadata')
->alterHtml(';^~afform/;', function($doc, $path) {
->alterHtml(';\\.aff\\.html$;', function($doc, $path) {
$entities = _afform_getMetadata($doc);
foreach (pq('af-field', $doc) as $afField) {
......@@ -407,34 +422,19 @@ function afform_civicrm_buildAsset($asset, $params, &$mimeType, &$content) {
/** @var \CRM_Afform_AfformScanner $scanner */
$scanner = Civi::service('afform_scanner');
$meta = $scanner->getMeta($name);
$moduleName = _afform_angular_module_name($name, 'camel');
$smarty = CRM_Core_Smarty::singleton();
$smarty->assign('afform', [
'camel' => _afform_angular_module_name($name, 'camel'),
'camel' => $moduleName,
'meta' => $meta,
'metaJson' => json_encode($meta),
'layout' => _afform_html_filter($name, $scanner->getLayout($name)),
'templateUrl' => "~/$moduleName/$moduleName.aff.html",
]);
$mimeType = 'text/javascript';
$content = $smarty->fetch('afform/AfformAngularModule.tpl');
}
/**
* Apply any filters to an HTML partial.
*
* @param string $formName
* @param string $html
* Original HTML.
* @return string
* Modified HTML.
*/
function _afform_html_filter($formName, $html) {
$fileName = '~afform/' . _afform_angular_module_name($formName, 'camel');
$htmls = [$fileName => $html];
$htmls = \Civi\Angular\ChangeSet::applyResourceFilters(Civi::service('angular')->getChangeSets(), 'partials', $htmls);
return $htmls[$fileName];
}
/**
* Implements hook_civicrm_alterMenu().
*/
......
......@@ -18,7 +18,7 @@
<version>0.3</version>
<develStage>alpha</develStage>
<compatibility>
<ver>5.18</ver>
<ver>5.22</ver>
</compatibility>
<requires>
<ext version="~4.5">org.civicrm.api4</ext>
......
......@@ -10,8 +10,8 @@
angular.module('{/literal}{$afform.camel}{literal}', CRM.angRequires('{/literal}{$afform.camel}{literal}'));
angular.module('{/literal}{$afform.camel}{literal}').directive('{/literal}{$afform.camel}{literal}', function(afCoreDirective) {
return afCoreDirective({/literal}{$afform.camel|json},{$afform.metaJson}{literal}, {
template: {/literal}{$afform.layout|json}{literal}
templateUrl: {/literal}{$afform.templateUrl|json}{literal}
});
});
})(angular, CRM.$, CRM._);
{/literal}
\ No newline at end of file
{/literal}
......@@ -20,10 +20,24 @@ class FilterTest extends \PHPUnit\Framework\TestCase implements HeadlessInterfac
return \Civi\Test::headless()->installMe(__DIR__)->apply();
}
/**
* Apply any filters to an HTML partial.
*
* @param string $fileName
* @param string $html
* Original HTML.
* @return string
* Modified HTML.
*/
private function htmlFilter($fileName, $html) {
$htmls = \Civi\Angular\ChangeSet::applyResourceFilters(\Civi::service('angular')->getChangeSets(), 'partials', [$fileName => $html]);
return $htmls[$fileName];
}
public function testDefnInjection() {
$inputHtml = sprintf(self::PERSON_TPL,
'<div af-fieldset="person"><af-field name="first_name" /></div>');
$filteredHtml = _afform_html_filter('~afform/MyForm.html', $inputHtml);
$filteredHtml = $this->htmlFilter('~/afform/MyForm.aff.html', $inputHtml);
$converter = new \CRM_Afform_ArrayHtml(TRUE);
$parsed = $converter->convertHtmlToArray($filteredHtml);
......@@ -35,7 +49,7 @@ class FilterTest extends \PHPUnit\Framework\TestCase implements HeadlessInterfac
public function testDefnInjectionNested() {
$inputHtml = sprintf(self::PERSON_TPL,
'<span><div af-fieldset="person"><foo><af-field name="first_name" /></foo></div></span>');
$filteredHtml = _afform_html_filter('~afform/MyForm.html', $inputHtml);
$filteredHtml = $this->htmlFilter('~/afform/MyForm.aff.html', $inputHtml);
$converter = new \CRM_Afform_ArrayHtml(TRUE);
$parsed = $converter->convertHtmlToArray($filteredHtml);
......@@ -47,7 +61,7 @@ class FilterTest extends \PHPUnit\Framework\TestCase implements HeadlessInterfac
public function testDefnOverrideTitle() {
$inputHtml = sprintf(self::PERSON_TPL,
'<div af-fieldset="person"><af-field name="first_name" defn="{title: \'Given name\'}" /></div>');
$filteredHtml = _afform_html_filter('~afform/MyForm.html', $inputHtml);
$filteredHtml = $this->htmlFilter('~/afform/MyForm.aff.html', $inputHtml);
$converter = new \CRM_Afform_ArrayHtml(TRUE);
$parsed = $converter->convertHtmlToArray($filteredHtml);
......
......@@ -18,7 +18,7 @@
<version>0.3</version>
<develStage>alpha</develStage>
<compatibility>
<ver>5.18</ver>
<ver>5.22</ver>
</compatibility>
<comments>This is a new, undeveloped module</comments>
<requires>
......
......@@ -18,7 +18,7 @@
<version>0.3</version>
<develStage>alpha</develStage>
<compatibility>
<ver>5.18</ver>
<ver>5.22</ver>
</compatibility>
<requires>
<ext>org.civicrm.afform</ext>
......
......@@ -18,7 +18,7 @@
<version>0.3</version>
<develStage>alpha</develStage>
<compatibility>
<ver>5.18</ver>
<ver>5.22</ver>
</compatibility>
<requires>
<ext>org.civicrm.afform</ext>
......
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