forked from Third-Culture-Software/bhima
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(barcode): implement barcode component
This commit implements a barcode component to make working with barcodes a bit easier. It replaces the cash barcode modal's custom logic with generic logic to be tested. Key features: 1. If the input loses focus, the user is notified and a button appears to re-establish focus on the hidden input. 2. Simple API - only a single callback is provided for the success case. 3. Record agnostic. Closes Third-Culture-Software#1529.
- Loading branch information
Showing
8 changed files
with
157 additions
and
123 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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
{"BARCODE":{"SCAN":"Scan Invoice Barcode", | ||
"AWAITING_INPUT":"Waiting for Input", | ||
"AWAITING_HTTP":"Barcode Read! Looking up Invoice...", | ||
"READ_SUCCESS":"Found Invoice", | ||
"READ_ERROR":"Unreadable Barcode! Please enter the barcode manually."}} | ||
{ | ||
"BARCODE" : { | ||
"SCAN" : "Scan Document Barcode", | ||
"AWAITING_INPUT" : "Waiting for Input", | ||
"AWAITING_HTTP" : "Barcode Read! Looking up Record...", | ||
"READ_SUCCESS" : "Found Record", | ||
"READ_ERROR" : "Unreadable Barcode! Please enter the barcode manually.", | ||
"LOST_FOCUS" : "The barcode input has lost focus. Please click \"Read Barcode\" to ready the barcode component.", | ||
"RESET_BUTTON" : "Read Barcode" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
{"BARCODE":{"SCAN":"Scanner le Code à Barres", | ||
"AWAITING_INPUT":"En Attente d'Entrée", | ||
"AWAITING_HTTP":"Code à barres lu! Recherche de factures ....", | ||
"READ_SUCCESS":"Facture Trouvée", | ||
"READ_ERROR":"Code à barres illisible! Entrez le code à barres manuellement."}} | ||
{ | ||
"BARCODE" : { | ||
"SCAN" : "Scanner le Code à Barres", | ||
"AWAITING_INPUT" : "En Attente d'Entrée", | ||
"AWAITING_HTTP" : "Code à barres lu! Recherche l'enregistrement....", | ||
"READ_SUCCESS" : "Trouvée", | ||
"READ_ERROR" : "Code à barres illisible! Entrez le code à barres manuellement.", | ||
"LOST_FOCUS" : "L'entrée du code à barres a perdu le focus. Veuillez cliquer sur \"Lire le code à barres\" pour préparer le composant de code à barres.", | ||
"RESET_BUTTON" : "Lire le code à barres" | ||
} | ||
} |
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,69 @@ | ||
angular.module('bhima.components') | ||
.component('bhBarcodeScanner', { | ||
templateUrl : '/modules/templates/bhBarcodeScanner.html', | ||
controller : bhBarcodeScanner, | ||
bindings : { | ||
onScanCallback : '&', | ||
}, | ||
}); | ||
|
||
bhBarcodeScanner.$inject = [ | ||
'$timeout', '$window', 'BarcodeService', | ||
]; | ||
|
||
function bhBarcodeScanner($timeout, $window, Barcode) { | ||
const $ctrl = this; | ||
|
||
// steps in the search space | ||
const steps = { | ||
AWAIT_READ : 2, | ||
AWAIT_HTTP : 4, | ||
READ_SUCCESS : 8, | ||
READ_ERROR : 16, | ||
LOST_FOCUS : 32, | ||
}; | ||
|
||
$ctrl.$onInit = () => { | ||
angular.extend($ctrl, { steps }); | ||
$ctrl.currentStep = steps.AWAIT_READ; | ||
$ctrl.setFocusOnHiddenInput(); | ||
}; | ||
|
||
const setFocusOnHiddenInput = () => { | ||
// clear previous values | ||
delete $ctrl.barcode; | ||
|
||
// find and focus the input | ||
const input = $window.document.getElementById('hidden-barcode-input'); | ||
input.focus(); | ||
|
||
// set view state correctly | ||
$ctrl.isResetButtonVisible = false; | ||
$ctrl.currentStep = steps.AWAIT_READ; | ||
}; | ||
|
||
// wrap setFocusOnHiddenInput() in $timeout to trigger $digest | ||
$ctrl.setFocusOnHiddenInput = () => { | ||
$timeout(setFocusOnHiddenInput); | ||
}; | ||
|
||
$ctrl.triggerBarcodeRead = () => { | ||
$ctrl.currentStep = steps.AWAIT_HTTP; | ||
|
||
Barcode.search($ctrl.barcode) | ||
.then(record => { | ||
$ctrl.currentStep = steps.READ_SUCCESS; | ||
$ctrl.record = record; | ||
$ctrl.onScanCallback({ record }); | ||
}) | ||
.catch(() => { | ||
$ctrl.currentStep = steps.READ_ERROR; | ||
$ctrl.isResetButtonVisible = true; | ||
}); | ||
}; | ||
|
||
$ctrl.showResetButton = () => { | ||
$ctrl.isResetButtonVisible = true; | ||
$ctrl.currentStep = steps.LOST_FOCUS; | ||
}; | ||
} |
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
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 |
---|---|---|
@@ -1,49 +1,19 @@ | ||
<form name="BarcodeForm" novalidate> | ||
|
||
<div class="modal-header"> | ||
<ol class="headercrumb"> | ||
<li class="title" translate>BARCODE.SCAN</li> | ||
</ol> | ||
</div> | ||
|
||
<div class="modal-body text-center" style="min-height:300px;"> | ||
|
||
<!-- state: awaiting barcode input --> | ||
<h2 translate>BARCODE.SCAN</h2> | ||
|
||
<h1> | ||
<i class="fa fa-barcode fa-3x"></i> | ||
</h1> | ||
|
||
<p ng-class="{ 'animate-flicker' : BarcodeModalCtrl.flicker }"> | ||
<span class="text-muted" ng-if="BarcodeModalCtrl.step === BarcodeModalCtrl.INITIALIZED"> | ||
<span translate>BARCODE.AWAITING_INPUT</span> | ||
</span> | ||
|
||
<span class="text-primary" ng-if="BarcodeModalCtrl.step === BarcodeModalCtrl.LOADING"> | ||
<i class="fa fa-info-circle"></i> | ||
<span translate>BARCODE.AWAITING_HTTP</span> | ||
</span> | ||
|
||
<span class="text-success" ng-if="BarcodeModalCtrl.step === BarcodeModalCtrl.READ_SUCCESS"> | ||
<i class="fa fa-check-circle-o"></i> <span translate>BARCODE.READ_SUCCESS</span> {{ ::BarcodeModalCtrl.invoice.reference }} | ||
</span> | ||
|
||
<span class="text-danger" ng-if="BarcodeModalCtrl.step === BarcodeModalCtrl.READ_ERROR"> | ||
<i class="fa fa-danger-sign"></i> <span translate>BARCODE.READ_ERROR</span> | ||
</span> | ||
</p> | ||
|
||
<!-- hidden barcode input --> | ||
<!-- TODO(@jniles): use ng-model-options to delay the $digest() loop and validate that a full barcode was inputted --> | ||
<div style="width:0; overflow: hidden;"> | ||
<input ng-model="BarcodeModalCtrl.barcode" ng-change="BarcodeModalCtrl.triggerBarcodeRead()" bh-focus-on="true"> | ||
</div> | ||
<bh-barcode-scanner on-scan-callback="BarcodeModalCtrl.onScanCallback(record)"></bh-barcode-scanner> | ||
</div> | ||
|
||
<div class="modal-footer text-right"> | ||
<button type="button" class="btn btn-default" ng-click="BarcodeModalCtrl.dismiss()"> | ||
<span translate>FORM.BUTTONS.CANCEL</span> | ||
<button type="button" class="btn btn-default" ng-click="BarcodeModalCtrl.dismiss()" translate> | ||
FORM.BUTTONS.CANCEL | ||
</button> | ||
</div> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<div data-bh-barcode-scanner> | ||
|
||
<!-- giant barcode to emphasize what this component is for --> | ||
<h1 style="margin-bottom: 0;"><i class="fa fa-barcode fa-3x"></i></h1> | ||
|
||
<p ng-class="{ 'animate-flicker' : $ctrl.currentStep === $ctrl.steps.AWAIT_READ }"> | ||
<span class="text-primary" ng-if="$ctrl.currentStep === $ctrl.steps.LOST_FOCUS"> | ||
<span translate>BARCODE.LOST_FOCUS</span> | ||
</span> | ||
<span class="text-muted" ng-if="$ctrl.currentStep === $ctrl.steps.AWAIT_READ"> | ||
<span translate>BARCODE.AWAITING_INPUT</span> | ||
</span> | ||
|
||
<span class="text-primary" ng-if="$ctrl.currentStep === $ctrl.steps.AWAIT_HTTP"> | ||
<i class="fa fa-info-circle"></i> | ||
<span translate>BARCODE.AWAITING_HTTP</span> | ||
</span> | ||
|
||
<span class="text-success" ng-if="$ctrl.currentStep === $ctrl.steps.READ_SUCCESS"> | ||
<i class="fa fa-check-circle-o"></i> <span translate>BARCODE.READ_SUCCESS</span> {{ $ctrl.record.reference }} | ||
</span> | ||
|
||
<span class="text-danger" ng-if="$ctrl.currentStep === $ctrl.steps.READ_ERROR"> | ||
<i class="fa fa-danger-sign"></i> <span translate>BARCODE.READ_ERROR</span> | ||
</span> | ||
</p> | ||
|
||
<!-- hidden barcode input --> | ||
<div style="height:0; width:0; overflow: hidden;"> | ||
<input id="hidden-barcode-input" ng-model="$ctrl.barcode" ng-model-options="{ debounce : 150 }" ng-change="$ctrl.triggerBarcodeRead()" ng-blur="$ctrl.showResetButton()"> | ||
</div> | ||
|
||
<!-- reset button to put focus on hidden input --> | ||
<button class="btn btn-default" type="button" ng-show="$ctrl.isResetButtonVisible" ng-click="$ctrl.setFocusOnHiddenInput()" translate> | ||
BARCODE.RESET_BUTTON | ||
</button> | ||
</div> |