forked from PonyLux/angular-bar-code-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-bar-code-scanner.js
138 lines (127 loc) · 4.96 KB
/
angular-bar-code-scanner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* @ngdoc overview
* @name angular-bar-code-scanner
* @description
* This module allow the detection of bar code scan.
*/
angular.module('angular-bar-code-scanner', []);
/**
* @ngdoc object
* @name angular-bar-code-scanner.BarCodeScannerConfigProvider
* @description
* This provider allow to configure the bar code scan.
*
*/
angular.module('angular-bar-code-scanner').provider('BarCodeScannerConfig', function() {
'use strict';
var broadcastEventName = 'bar-code-scan-event',
numberOfCharOfBarCode = 10;
/**
* @ngdoc function
* @name setBroadcastEventName
* @methodOf angular-bar-code-scanner.BarCodeScannerConfigProvider
*
* @description
* This method allow to set the event name that will be broadcast when a scan is detected.
* Default event name is 'bar-code-scan-event'.
*
* @param {string} eventName The event name to set
*/
this.setBroadcastEventName = function(eventName) {
broadcastEventName = eventName;
};
/**
* @ngdoc function
* @name setNumberOfCharOfBarCode
* @methodOf angular-bar-code-scanner.BarCodeScannerConfigProvider
*
* @description
* This method allow to set the limit until we consider that a scan is trigger.
* Default limit is 10.
*
* @param {number} charLengthLimit The number of min char entry for detect a scan
*/
this.setNumberOfCharOfBarCode = function(charLengthLimit) {
numberOfCharOfBarCode = charLengthLimit;
};
this.$get = function() {
var barCodeScannerConfig = {};
barCodeScannerConfig.getNumberOfCharOfBarCode = function() {
return numberOfCharOfBarCode;
};
barCodeScannerConfig.getBroadcastEventName = function() {
return broadcastEventName;
};
return barCodeScannerConfig;
};
});
/**
* @ngdoc directive
* @name angular-bar-code-scanner.directive:barCodeScanner
* @restrict A
* @scope
*
* @description
* By using this directive, you will be able to detect the scan of bar code.
* You can set this directive on input on anything else (body, div, ...).
* When a scan is detected, an event 'bar-code-scan-event' (or else if redefine @see config part) is broadcast on $rootscope.
* The broadcast contain { barCodeValue : number }.
*
* Note: it seems to be ugly <u>but</u> we need to set a timeout of 0.5 sec. The scan work like a keyboard.
* It send numeric char every x ms.
*
* @example
* <pre>
* <!-- in template -->
* <body data-ng-bar-code-scanner>
* ....
* </body>
*
* // in controller
* $scope.$on('bar-code-scan-event',function(event, parameters){
* console.info('Scan detected, bar code is : ' , parameters.barCodeValue);
* }
* </pre>
*
*
*/
angular.module('angular-bar-code-scanner').directive('barCodeScanner', ['$rootScope', '$timeout', 'BarCodeScannerConfig',
function ($rootScope, $timeout, BarCodeScannerConfig) {
"use strict";
return {
restrict: 'A',
link: function($scope, element) {
// Ensure the element is fully load until we add the "keypress" listener
element.ready(function(){
var pressed = false;
var chars = [];
var charLimit = BarCodeScannerConfig.getNumberOfCharOfBarCode();
var eventName = BarCodeScannerConfig.getBroadcastEventName();
element.bind("keypress", function (event) {
// if a number is pressed we add it to the chars array
chars.push(String.fromCharCode(event.which));
// Pressed is initially set to false so we enter - this variable is here to stop us setting a
// timeout everytime a key is pressed. It is easy to see here that this timeout is set to give
// us 1 second before it resets everything back to normal. If the keypresses have not matched
// the checks in the readBarcodeScanner function below then this is not a barcode
if (pressed === false) {
// we set a timeout function that expires after 1 sec, once it does it clears out a list
// of characters
$timeout(function(){
// check we have a long length e.g. it is a barcode
if (chars.length >= charLimit) {
// join the chars array to make a string of the barcode scanned
var barcode = chars.join("");
$rootScope.$broadcast(eventName,{barCodeValue:barcode});
}
chars = [];
pressed = false;
}, 500);
}
// set press to true so we do not reenter the timeout function above
pressed = true;
});
});
}
};
}]);