forked from goodeggs/angular-barcode-listener
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-barcode-listener.js
More file actions
131 lines (119 loc) · 5.1 KB
/
angular-barcode-listener.js
File metadata and controls
131 lines (119 loc) · 5.1 KB
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
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
var barcodeScanListener;
barcodeScanListener = require('barcode-scan-listener');
/*
Listen for scan with specified product prefix.
@param [Function] onScan - callback to call when scan is successful. Passes the scanned string.
@param [String] prefix - character prefix that appears before the scanned string (e.g. 'P%', 'C%')
*/
module.exports = angular.module('barcodeListener', []).directive('barcodeListener', function() {
return {
restrict: 'EA',
scope: {
onScan: '=',
prefix: '@',
length: '@',
scanDuration: '@?'
},
link: function(scope, element, attrs) {
var removeScanListener, scanDuration;
scanDuration = +scope.scanDuration || 50;
removeScanListener = barcodeScanListener.onScan({
barcodePrefix: scope.prefix,
barcodeLength: +scope.length || void 0,
scanDuration: scanDuration
}, scope.onScan);
return element.on('$destroy', removeScanListener);
}
};
});
},{"barcode-scan-listener":2}],2:[function(require,module,exports){
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
/* eslint-env browser */
exports.default = {
/**
* Listen for scan with specified characteristics
* @param {String} scanCharacteristics.barcodePrefix
* @param {Number} [scanCharacteristics.barcodeLength] - if provided, the listener will
* wait for this many characters to be read before calling the handler
* @param {Number} [scanCharacteristics.scanDuration]
* @param {Function} scanHandler - called with the results of the scan
* @return {Function} remove this listener
*/
onScan: function onScan() {
var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var barcodePrefix = _ref.barcodePrefix;
var barcodeLength = _ref.barcodeLength;
var scanDuration = _ref.scanDuration;
var scanHandler = arguments[1];
if (typeof barcodePrefix !== 'string') {
throw new TypeError('barcodePrefix must be a string');
}
if (barcodeLength && typeof barcodeLength !== 'number') {
throw new TypeError('barcodeLength must be a number');
}
if (scanDuration && typeof scanDuration !== 'number') {
throw new TypeError('scanDuration must be a number');
}
if (typeof scanHandler !== 'function') {
throw new TypeError('scanHandler must be a function');
}
/**
* SwipeTrack calls this function, if defined, whenever a barcode is scanned
* within the SwipeTrack browser. See "SwipeTrack Browser JavaScript Functions" section of
* SwipeTrack API: http://swipetrack.net/support/faq/pdf/SwipeTrack%20API%20(v5.0.0).pdf
*/
if (typeof window.onScanAppBarCodeData !== 'function') {
window.onScanAppBarCodeData = function (barcode) {
window.onScanAppBarCodeData.scanHandlers.forEach(function (handler) {
return handler(barcode);
});
return true;
};
window.onScanAppBarCodeData.scanHandlers = [];
}
var swipeTrackHandler = function swipeTrackHandler(barcode) {
if (barcode.match('^' + barcodePrefix) !== null) scanHandler(barcode.slice(barcodePrefix.length));
};
window.onScanAppBarCodeData.scanHandlers.push(swipeTrackHandler);
scanDuration = scanDuration || 50;
var isScanning = false;
var codeBuffer = '';
var scannedPrefix = '';
var finishScan = function finishScan() {
if (codeBuffer) {
if (!barcodeLength) scanHandler(codeBuffer);else if (codeBuffer.length >= barcodeLength) scanHandler(codeBuffer.substr(0, barcodeLength));
}
scannedPrefix = '';
codeBuffer = '';
isScanning = false;
};
var keypressHandler = function keypressHandler(e) {
var char = String.fromCharCode(e.which);
var charIndex = barcodePrefix.indexOf(char);
var expectedPrefix = barcodePrefix.slice(0, charIndex);
if (!isScanning) {
isScanning = true;
setTimeout(finishScan, scanDuration);
}
if (scannedPrefix === barcodePrefix && /[^\s]/.test(char)) {
codeBuffer += char;
} else if (scannedPrefix === expectedPrefix && char === barcodePrefix.charAt(charIndex)) {
scannedPrefix += char;
}
};
var removeListener = function removeListener() {
document.removeEventListener('keypress', keypressHandler);
var swipeTrackHandlerIndex = window.onScanAppBarCodeData.scanHandlers.indexOf(swipeTrackHandler);
if (swipeTrackHandlerIndex >= 0) window.onScanAppBarCodeData.scanHandlers.splice(swipeTrackHandlerIndex, 1);
};
document.addEventListener('keypress', keypressHandler);
return removeListener;
}
};
module.exports = exports['default'];
},{}]},{},[1])