Skip to content

Commit

Permalink
Afform - Allow picking icon for tab, add CrmUiIconPicker widget
Browse files Browse the repository at this point in the history
Since this is needed in several places, this adds a general purpose crmUiIconPicker widget to crmUi.
It also switches Afforms's hook_civicrm_tabset to use the API, which ensures virtual forms get picked up.
Adds the new icon for CiviGrant as an example.
  • Loading branch information
colemanw committed Jun 30, 2022
1 parent c898b8c commit 9df0dea
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 28 deletions.
15 changes: 15 additions & 0 deletions ang/crmUi.js
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,21 @@
};
})

// Adds an icon picker widget
// Example: `<input crm-ui-icon-picker ng-model="model.icon">`
.directive('crmUiIconPicker', function($timeout) {
return {
restrict: 'A',
controller: function($element) {
CRM.loadScript(CRM.config.resourceBase + 'js/jquery/jquery.crmIconPicker.js').then(function() {
$timeout(function() {
$element.crmIconPicker();
});
});
}
};
})

.run(function($rootScope, $location) {
/// Example: <button ng-click="goto('home')">Go home!</button>
$rootScope.goto = function(path) {
Expand Down
1 change: 1 addition & 0 deletions ext/afform/admin/ang/afGuiEditor/afGuiEditor.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
editor.afform.is_dashlet = false;
editor.afform.title += ' ' + ts('(copy)');
}
editor.afform.icon = editor.afform.icon || 'fa-list-alt';
$scope.canvasTab = 'layout';
$scope.layoutHtml = '';
$scope.entities = {};
Expand Down
3 changes: 3 additions & 0 deletions ext/afform/admin/ang/afGuiEditor/config-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
<option value="block">{{:: ts('As Block') }}</option>
<option value="tab">{{:: ts('As Tab') }}</option>
</select>
<div class="form-group" ng-show="editor.afform.contact_summary === 'tab'">
<input required ng-model="editor.afform.icon" crm-ui-icon-picker class="form-control">
</div>
</div>
<p class="help-block">{{:: ts('Placement can be configured using the Contact Layout Editor.') }}</p>
</div>
Expand Down
4 changes: 4 additions & 0 deletions ext/afform/core/Civi/Api4/Afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ public static function getFields($checkPermissions = TRUE) {
'tab' => ts('Contact Summary Tab'),
],
],
[
'name' => 'icon',
'description' => 'Icon shown in the contact summary tab',
],
[
'name' => 'server_route',
],
Expand Down
36 changes: 18 additions & 18 deletions ext/afform/core/afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,25 @@ function afform_civicrm_tabset($tabsetName, &$tabs, $context) {
if ($tabsetName !== 'civicrm/contact/view') {
return;
}
$scanner = \Civi::service('afform_scanner');
$afforms = Civi\Api4\Afform::get(FALSE)
->addWhere('contact_summary', '=', 'tab')
->addSelect('name', 'title', 'icon', 'module_name', 'directive_name')
->execute();
$weight = 111;
foreach ($scanner->getMetas() as $afform) {
if (!empty($afform['contact_summary']) && $afform['contact_summary'] === 'tab') {
$module = _afform_angular_module_name($afform['name']);
$tabs[] = [
'id' => $afform['name'],
'title' => $afform['title'],
'weight' => $weight++,
'icon' => 'crm-i fa-list-alt',
'is_active' => TRUE,
'template' => 'afform/contactSummary/AfformTab.tpl',
'module' => $module,
'directive' => _afform_angular_module_name($afform['name'], 'dash'),
];
// If this is the real contact summary page (and not a callback from ContactLayoutEditor), load module.
if (empty($context['caller'])) {
Civi::service('angularjs.loader')->addModules($module);
}
foreach ($afforms as $afform) {
$tabs[] = [
'id' => $afform['name'],
'title' => $afform['title'],
'weight' => $weight++,
'icon' => 'crm-i ' . ($afform['icon'] ?: 'fa-list-alt'),
'is_active' => TRUE,
'template' => 'afform/contactSummary/AfformTab.tpl',
'module' => $afform['module_name'],
'directive' => $afform['directive_name'],
];
// If this is the real contact summary page (and not a callback from ContactLayoutEditor), load module.
if (empty($context['caller'])) {
Civi::service('angularjs.loader')->addModules($afform['module_name']);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions ext/civigrant/ang/afsearchGrants.aff.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"type": "search",
"title": "Grants",
"contact_summary": "tab",
"icon": "fa-money",
"server_route": "",
"permission": "access CiviGrant"
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@
};
};

function initWidgets() {
CRM.loadScript(CRM.config.resourceBase + 'js/jquery/jquery.crmIconPicker.js').then(function() {
$('.crm-search-admin-field-icon > input.crm-icon-picker[ng-model]', $element).crmIconPicker();
});
}

this.$onInit = function() {
$element.on('hidden.bs.dropdown', function() {
$timeout(function() {
Expand All @@ -51,7 +45,6 @@
}
ctrl.iconFields = _.transform(allFields, getIconFields, []);
ctrl.iconFieldMap = _.indexBy(ctrl.iconFields, 'id');
$timeout(initWidgets);
};

this.onSelectField = function(clause) {
Expand All @@ -72,7 +65,6 @@
searchMeta.pickIcon().then(function(icon) {
if (icon) {
ctrl.item.icons.push({icon: icon, side: 'left', if: []});
$timeout(initWidgets);
}
});
}
Expand All @@ -85,7 +77,6 @@
item.icon = icon;
delete item.field;
item.if = item.if || [];
$timeout(initWidgets);
}
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</div>
</div>
<div class="form-group crm-search-admin-field-icon" ng-if="icon.icon">
<input required ng-model="icon.icon" class="form-control crm-icon-picker">
<input required ng-model="icon.icon" crm-ui-icon-picker class="form-control crm-icon-picker">
</div>
<select class="form-control" ng-model="icon.side" title="{{:: ts('Show icon on left or right side of the field') }}">
<option value="left">{{:: ts('Align left') }}</option>
Expand Down

0 comments on commit 9df0dea

Please sign in to comment.