Skip to content

Commit

Permalink
fix(bhTransactionTypes): default to empty array
Browse files Browse the repository at this point in the history
The bhTransactionTypes will now default to an empty array if the
transactions types are not provided to it.  This makes the transaction
types binding truly optional.  A number of small issues have also been
cleaned up: addition of `ngForm` to make sure the `ngModelOptions` is
respected, code updated to standards.
  • Loading branch information
jniles committed Sep 16, 2017
1 parent ec832f0 commit ddf9bc8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
18 changes: 9 additions & 9 deletions client/src/js/components/bhTransactionTypeSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ angular.module('bhima.components')
.component('bhTransactionTypeSelect', {
templateUrl : 'modules/templates/bhTransactionTypeSelect.tmpl.html',
controller : transactionTypeSelectController,
bindings : {
bindings : {
onChange : '&',
transactionTypeIds : '<?',
label : '@?',
onChange : '&',
required : '<?',
validationTrigger : '<',
label : '@?',
required : '<?',
validationTrigger : '<?',
},
});

transactionTypeSelectController.$inject = [
'TransactionTypeService', 'NotifyService', '$translate'
'TransactionTypeService', 'NotifyService', '$translate',
];

/**
Expand All @@ -23,19 +23,19 @@ function transactionTypeSelectController(TransactionTypes, Notify, $translate) {
var $ctrl = this;

$ctrl.$onInit = function onInit() {
//label to display
// label to display
$ctrl.label = $ctrl.label || 'FORM.LABELS.TRANSACTION_TYPE';

// fired when a transaction type has been selected or removed from the list
$ctrl.onChange = $ctrl.onChange || angular.noop;

// init the model
$ctrl.selectedTransactionTypes = $ctrl.transactionTypeIds;
$ctrl.selectedTransactionTypes = $ctrl.transactionTypeIds || [];

// load all Transaction types
TransactionTypes.read()
.then(function (tts) {
tts.forEach(function(item){
tts.forEach(function (item) {
item.plainText = $translate.instant(item.text);
});
$ctrl.transactionTypes = tts;
Expand Down
14 changes: 7 additions & 7 deletions client/src/modules/templates/bhTransactionTypeSelect.tmpl.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<div name="TransactionTypeForm" bh-transaction-type-select ng-model-options="{ updateOn: 'default' }">
<div
<div ng-form="TransactionTypeForm" bh-transaction-type-select ng-model-options="{ updateOn: 'default' }">
<div
class="form-group"
ng-class="{ 'has-error' : $ctrl.validationTrigger && TransactionTypeForm.transactionTypes.$invalid }">
<label class="control-label" translate>
{{ $ctrl.label }}
</label>

<ui-select
<ui-select
multiple
name="transactionTypes"
name="transactionTypes"
ng-model="$ctrl.selectedTransactionTypes"
on-select="$ctrl.handleChange($ctrl.selectedTransactionTypes)"
on-remove="$ctrl.handleChange($ctrl.selectedTransactionTypes)"
close-on-select="false"
ng-required="$ctrl.required">
<ui-select-match placeholder="{{ 'FORM.SELECT.TRANSACTION_TYPE' | translate }}">
<span>{{$item.plainText}}</span>
<span>{{ $item.plainText }}</span>
</ui-select-match>
<ui-select-choices ui-select-focus-patch repeat="type.id as type in ($ctrl.transactionTypes | filter:{plainText : $select.search} | orderBy:'plainText')">
<ui-select-choices ui-select-focus-patch repeat="type.id as type in ($ctrl.transactionTypes | filter: { plainText : $select.search } | orderBy: 'plainText')">
<span ng-bind-html="type.plainText | highlight:$select.search"></span>
</ui-select-choices>
</ui-select>
Expand All @@ -26,4 +26,4 @@
<div ng-messages-include="modules/templates/messages.tmpl.html"></div>
</div>
</div>
</div>
</div>
3 changes: 1 addition & 2 deletions client/src/modules/vouchers/modals/search.modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@

<bh-transaction-type-select
on-change="$ctrl.onTransactionTypesChange(transactionTypes)"
transaction-type-ids="$ctrl.searchQueries.type_ids"
required="false">
transaction-type-ids="$ctrl.searchQueries.type_ids">
</bh-transaction-type-select>
</div>
</uib-tab>
Expand Down
26 changes: 12 additions & 14 deletions client/src/modules/vouchers/modals/search.modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ angular.module('bhima.controllers')
.controller('VoucherRegistrySearchModalController', VoucherRegistrySearchModalController);

VoucherRegistrySearchModalController.$inject = [
'$uibModalInstance', 'filters', 'NotifyService', 'moment',
'bhConstants', 'PeriodService', 'Store', 'util', 'TransactionTypeService', '$translate'
'$uibModalInstance', 'filters', 'NotifyService', 'moment', 'PeriodService', 'Store', 'util',
];

/**
Expand All @@ -14,28 +13,28 @@ VoucherRegistrySearchModalController.$inject = [
* returning it as a JSON object to the parent controller. The data can be
* preset by passing in a filters object using filtersProvider().
*/
function VoucherRegistrySearchModalController(ModalInstance, filters, Notify, moment,
bhConstants, Periods, Store, util, TransactionTypes, $translate) {
function VoucherRegistrySearchModalController(ModalInstance, filters, Notify, moment, Periods, Store, util) {
var vm = this;
var changes = new Store({identifier : 'key'});
var changes = new Store({ identifier : 'key' });
var searchQueryOptions = [
'reference', 'description', 'user_id', 'type_ids',
];

vm.filters = filters;
vm.searchQueries = {};
vm.defaultQueries = {};

var searchQueryOptions = [
'reference', 'description', 'user_id', 'type_ids',
];

// assign already defined custom filters to searchQueries object
vm.searchQueries = util.maskObjectFromKeys(filters, searchQueryOptions);

if(filters.limit){
if (filters.limit) {
vm.defaultQueries.limit = filters.limit;
}

vm.onTransactionTypesChange = function onTransactionTypesChange (transactionTypes){
vm.onTransactionTypesChange = function onTransactionTypesChange(transactionTypes) {
vm.searchQueries.type_ids = transactionTypes;
}
};

// custom filter user_id - assign the value to the params object
vm.onSelectUser = function onSelectUser(user) {
Expand Down Expand Up @@ -68,9 +67,8 @@ bhConstants, Periods, Store, util, TransactionTypes, $translate) {

// submit the filter object to the parent controller.
vm.submit = function submit(form) {

// delete type_ids if there is no transaction type sent
if(vm.searchQueries.type_ids && vm.searchQueries.type_ids.length === 0){
if (vm.searchQueries.type_ids && vm.searchQueries.type_ids.length === 0) {
vm.clear('type_ids');
}

Expand All @@ -85,5 +83,5 @@ bhConstants, Periods, Store, util, TransactionTypes, $translate) {

// return values to the voucher controller
return ModalInstance.close(loggedChanges);
}
};
}

0 comments on commit ddf9bc8

Please sign in to comment.