Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SearchKit - Optionally remember filter values when user revisits sear… #27737

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
SearchKit - Optionally remember filter values when user revisits sear…
…ch form
  • Loading branch information
colemanw committed Oct 6, 2023
commit e95d34fa463835f21788fb435075b10e8f672772
8 changes: 8 additions & 0 deletions ext/afform/admin/ang/afGuiEditor/afGuiSearch.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,12 @@
</div>
</div>
</fieldset>

<fieldset>
<legend>{{:: ts('Options') }}</legend>
<label>{{:: ts('Remember Filters') }} <input type="checkbox" ng-model="$ctrl.display.fieldset['store-values']"></label>
<p class="help-block">
{{:: ts('Filter fields will retain their value when the same user revisits the form.') }}
</p>
</fieldset>
</div>
3 changes: 3 additions & 0 deletions ext/afform/core/ang/af/afField.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
else if (urlArgs && (ctrl.fieldName in urlArgs)) {
setValue(urlArgs[ctrl.fieldName]);
}
else if (ctrl.afFieldset.getStoredValue(ctrl.fieldName) !== undefined) {
setValue(ctrl.afFieldset.getStoredValue(ctrl.fieldName));
}
// Set default value based on field defn
else if ('afform_default' in ctrl.defn) {
setValue(ctrl.defn.afform_default);
Expand Down
23 changes: 22 additions & 1 deletion ext/afform/core/ang/af/afFieldset.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
restrict: 'A',
require: ['afFieldset', '?^^afForm'],
bindToController: {
modelName: '@afFieldset'
modelName: '@afFieldset',
storeValues: '<'
},
link: function($scope, $el, $attr, ctrls) {
var self = ctrls[0];
Expand Down Expand Up @@ -40,6 +41,26 @@
this.getFormName = function() {
return ctrl.afFormCtrl ? ctrl.afFormCtrl.getFormMeta().name : $scope.meta.name;
};

// If `storeValue` setting is enabled, field values are cached in localStorage
function getCacheKey() {
return 'afform:' + ctrl.getFormName() + ctrl.getName();
}
this.getStoredValue = function(fieldName) {
if (!this.storeValues) {
return;
}
return CRM.cache.get(getCacheKey(), {})[fieldName];
};
this.$onInit = function() {
if (this.storeValues) {
$scope.$watch(ctrl.getFieldData, function(newVal, oldVal) {
if (typeof newVal === 'object' && typeof oldVal === 'object' && Object.keys(newVal).length) {
CRM.cache.set(getCacheKey(), newVal);
}
}, true);
}
};
}
};
});
Expand Down