-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bhCurrencySelect): currency selection radios
This commit introduces the bhCurrencySelect radio component to select currencies in forms. The basic signature looks like this: ```html <!-- simple usage --> <bh-currency-select currency-id="ParentCtrl.model.currencyId" validation-trigger="ParentForm.$submitted" > </bh-currency-select> <!-- complex usage: disable currencies based on ids found in the disable-ids array and register an ngChange event. --> <bh-currency-select currency-id="ParentCtrl.model.currencyId" on-change="ParentCtrl.currencyChangeEvent()" disable-ids="ParentCtrl.disabledIds" validation-trigger="ParentForm.$submitted" > </bh-currency-select> ``` See the source code for implementation details. The list of supported bindings follows: 1. [currency-id] - the model value for the underlying `<input>`s. This is two-way bound to the parent controller. 2. [validation-trigger] (_optional_) - a boolean that can be passed in to show validation messages will only show if this boolean is true. It is useful to bind `ParentForm.$submitted` value to this attribute. 3. [on-change] (_optional_) - a callback bound the `ng-change` event on the `<input>`s. 4. [disable-ids] (_optional_) - an array of currency ids to be disabled as required. An implementation of the most complex case is given in the cash module. Partially addresses #106.
- Loading branch information
Showing
5 changed files
with
159 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
angular.module('bhima.components') | ||
.component('bhCurrencySelect', { | ||
controller : bhCurrencySelect, | ||
templateUrl : 'partials/templates/bhCurrencySelect.tmpl.html', | ||
bindings : { | ||
validationTrigger : '<', | ||
currencyId : '=', | ||
disableIds : '<', | ||
onChange : '&' | ||
} | ||
}); | ||
|
||
bhCurrencySelect.$inject = [ '$scope', 'CurrencyService' ]; | ||
|
||
/** | ||
* Currency Selection Component | ||
* | ||
* This is a radio button currency selection component for choosing currencies | ||
* in a form. If a list of currencies are passed in, these are used instead of | ||
* the application's currencies. | ||
* | ||
* @module components/bhCurrencySelect | ||
* | ||
* @example | ||
* <!-- simple usage --> | ||
* <bh-currency-select | ||
* currency-id="ParentCtrl.model.currencyId" | ||
* validation-trigger="ParentForm.$submitted" | ||
* > | ||
* </bh-currency-select> | ||
* | ||
* <!-- | ||
* complex usage: filter the currencies and register an onChange event | ||
* --> | ||
* <bh-currency-select | ||
* currency-id="ParentCtrl.model.currencyId" | ||
* on-change="ParentCtrl.currencyChangeEvent()" | ||
* disable-ids="ParentCtrl.disabledIds" | ||
* validation-trigger="ParentForm.$submitted" | ||
* > | ||
* </bh-currency-select> | ||
* | ||
* BINDINGS | ||
* - [currency-id] | ||
* The model value for the underlying `<input>`s. This | ||
* is two-way bound to the parent controller. | ||
* | ||
* - [validation-trigger] | ||
* a boolean that can be passed in | ||
* to show validation messages will only show if this boolean is true. It | ||
* is useful to bind `ParentForm.$submitted` value to this attribute. | ||
* | ||
* - [on-change] | ||
* a callback bound the `ng-change` event on the `<input>`s. | ||
* | ||
* - [disable-ids] | ||
* an array of currency ids to be disabled as required. | ||
*/ | ||
function bhCurrencySelect($scope, Currencies) { | ||
var $ctrl = this; | ||
|
||
// bind the currency service to the view | ||
$ctrl.service = Currencies; | ||
|
||
// default currencies to an empty list | ||
$ctrl.currencies = []; | ||
|
||
// default to noop() if an onChange() method was not passed in | ||
$ctrl.onChange = $ctrl.onChange || angular.noop(); | ||
|
||
// load all the available currencies | ||
Currencies.read() | ||
.then(function (currencies) { | ||
|
||
// cache a label for faster view rendering | ||
currencies.forEach(function (currency) { | ||
currency.label = Currencies.format(currency.id); | ||
}); | ||
|
||
$ctrl.currencies = currencies; | ||
}); | ||
|
||
// watch the disabledIds array for changes, and disable the ids in the the | ||
// view based on which ids are present in it | ||
$scope.$watch(function () { | ||
return $ctrl.disableIds; | ||
}, function (array) { | ||
|
||
// ensure the array exists and has values | ||
if (!array || !array.length) { return; } | ||
|
||
// loop through the currencies, disabling the currencies with ids in the | ||
// disabledIds array. | ||
$ctrl.currencies.forEach(function (currency) { | ||
currency.disabled = array.indexOf(currency.id) > -1; | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<ng-form name="CurrencyForm"> | ||
<div class="radio" ng-class="{ 'has-error' : $ctrl.validationTrigger && CurrencyForm.$invalid }" data-bh-currency-select> | ||
<p class="control-label"> | ||
<strong>{{ "COLUMNS.CURRENCY" | translate }}</strong> | ||
</p> | ||
|
||
<!-- repeat each currency in the application --> | ||
<label ng-repeat="currency in $ctrl.currencies track by currency.id" class="radio-inline" ng-class="{ 'disabled' : currency.disabled }"> | ||
<input | ||
name="currency" | ||
type="radio" | ||
ng-model="$ctrl.currencyId" | ||
ng-value="currency.id" | ||
ng-change="$ctrl.onChange" | ||
data-currency-option="{{ currency.id }}" | ||
ng-disabled="currency.disabled" | ||
required> | ||
{{ currency.label }} | ||
</label> | ||
|
||
<!-- radio button validation messages --> | ||
<div class="help-block" ng-messages="CurrencyForm.currency.$error" ng-show="$ctrl.validationTrigger"> | ||
<div ng-messages-include="partials/templates/messages.tmpl.html"></div> | ||
</div> | ||
</div> | ||
</ng-form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters