11/* Copyright 2018 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
2- Copyright 2018 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
2+ Copyright 2018-2019 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
33 License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). */
44odoo . define ( 'pos_qr_scan' , function ( require ) {
55 var exports = { } ;
@@ -9,6 +9,7 @@ odoo.define('pos_qr_scan', function(require){
99 var gui = require ( 'point_of_sale.gui' ) ;
1010 var PopupWidget = require ( 'point_of_sale.popups' ) ;
1111 var screens = require ( 'point_of_sale.screens' ) ;
12+ Quagga = window . Quagga ;
1213
1314 var QrButton = screens . ActionButtonWidget . extend ( {
1415 template : 'QrButton' ,
@@ -32,17 +33,76 @@ odoo.define('pos_qr_scan', function(require){
3233 var self = this ;
3334 this . gUM = false ;
3435 this . _super ( options ) ;
35- this . generate_qr_scanner ( ) ;
36+ this . prepare_video_element ( ) ;
37+ var config = this . pos . config ;
38+ if ( config . use_only_qr_scan ) {
39+ this . generate_qr_scanner ( ) ;
40+ } else if ( config . use_only_barcode_scan ) {
41+ this . generate_barcode_scanner ( ) ;
42+ } else {
43+ this . generate_qr_scanner ( ) ;
44+ this . generate_barcode_scanner ( ) ;
45+ }
46+ this . read_callback = options . read_callback || this . read ;
47+ } ,
48+ init : function ( parent , args ) {
49+ this . _super ( parent , args ) ;
50+ } ,
51+ generate_barcode_scanner : function ( ) {
52+ var self = this ;
53+ Quagga . init ( {
54+ inputStream : {
55+ name : "Live" ,
56+ type : "LiveStream" ,
57+ target : document . querySelector ( '#preview' ) // Or '#yourElement' (optional)
58+ } ,
59+ decoder : {
60+ readers : [
61+ "code_128_reader" ,
62+ "ean_reader" ,
63+ "ean_8_reader" ,
64+ "code_39_reader" ,
65+ "code_39_vin_reader" ,
66+ "codabar_reader" ,
67+ "upc_reader" ,
68+ "upc_e_reader" ,
69+ "i2of5_reader" ,
70+ "code_93_reader" ,
71+ ]
72+ }
73+ } , function ( err ) {
74+ if ( err ) {
75+ console . log ( err ) ;
76+ return
77+ }
78+ console . log ( "Initialization finished. Ready to start" ) ;
79+ Quagga . start ( ) ;
80+ } ) ;
81+
82+ Quagga . onDetected ( function ( result ) {
83+ var code = result . codeResult . code ;
84+
85+ if ( this . lastResult !== code ) {
86+ this . lastResult = code ;
87+ var $node = null ,
88+ canvas = Quagga . canvas . dom . image ;
89+ self . read_callback ( code , 'barcode' ) ;
90+ }
91+ } ) ;
3692 } ,
3793 click_cancel : function ( ) {
38- this . stop_camera ( ) ;
3994 this . _super ( arguments ) ;
95+ this . stop_camera ( ) ;
4096 } ,
4197 stop_camera : function ( camera ) {
4298 this . cam_is_on = false ;
4399 if ( this . stream ) {
44100 this . stream . getTracks ( ) [ 0 ] . stop ( ) ;
45101 }
102+ if ( ! this . pos . config . use_only_qr_scan ) {
103+ Quagga . stop ( ) ;
104+ Quagga . offDetected ( ) ;
105+ }
46106 } ,
47107 add_button : function ( content ) {
48108 var button = document . createElement ( 'div' ) ;
@@ -64,16 +124,16 @@ odoo.define('pos_qr_scan', function(require){
64124 return cam . deviceId === id ;
65125 } ) ;
66126 } ,
67- generate_qr_scanner : function ( ) {
68- var options = false ;
69- var self = this ;
127+ prepare_video_element : function ( ) {
70128 this . video_element = document . getElementById ( "preview" ) ;
71129 $ ( this . video_element ) . on ( 'click' , function ( ) {
72130 self . click_cancel ( ) ;
73131 } ) ;
132+ var options = false ;
133+ var self = this ;
74134 if ( navigator . mediaDevices && navigator . mediaDevices . enumerateDevices ) {
75135 this . capture_timeout = 700 ;
76- try {
136+ try {
77137 navigator . mediaDevices . enumerateDevices ( ) . then ( function ( devices ) {
78138 self . video_devices = _ . filter ( devices , function ( d ) {
79139 return d . kind === 'videoinput' ;
@@ -96,31 +156,35 @@ odoo.define('pos_qr_scan', function(require){
96156 }
97157 self . start_webcam ( options ) ;
98158 } ) ;
99- }
100- catch ( e ) {
159+ } catch ( e ) {
101160 alert ( e ) ;
102161 }
103- }
104- else {
162+ } else {
105163 console . log ( "no navigator.mediaDevices.enumerateDevices" ) ;
106- this . start_webcam ( options ) ;
107164 }
108165 } ,
166+ generate_qr_scanner : function ( ) {
167+ this . cam_is_on = true ;
168+ setTimeout ( function ( ) {
169+ self . captureToCanvas ( ) ;
170+ } , this . capture_timeout ) ;
171+ } ,
109172
110- read : function ( result ) {
173+ read : function ( result , method ) {
174+ method = method || 'qr' ;
111175 // Trigger event on scanning and close camera window
112176 if ( this . pos . debug ) {
113- console . log ( 'QR scanned', result ) ;
177+ console . log ( method . toUpperCase ( ) + ' scanned', result ) ;
114178 }
115- core . bus . trigger ( 'qr_scanned' , result ) ;
116- posmodel . gui . popup_instances . qr_scan . click_cancel ( ) ;
179+ this . click_cancel ( ) ;
180+ core . bus . trigger ( method + '_scanned' , result ) ;
117181 } ,
118182
119183 start_webcam : function ( options ) {
120184 var self = this ;
121185 this . initCanvas ( 800 , 600 ) ;
122186 qrcode . callback = function ( value ) {
123- self . read ( value ) ;
187+ self . read_callback ( value , 'qr' ) ;
124188 }
125189 if ( navigator . mediaDevices . getUserMedia ) {
126190 navigator . mediaDevices . getUserMedia ( { video : options , audio : false } ) .
@@ -141,11 +205,6 @@ odoo.define('pos_qr_scan', function(require){
141205 webkit = true ;
142206 navigator . webkitGetUserMedia ( { video :options , audio : false } , success , error ) ;
143207 }
144-
145- this . cam_is_on = true ;
146- setTimeout ( function ( ) {
147- self . captureToCanvas ( ) ;
148- } , this . capture_timeout ) ;
149208 } ,
150209
151210 success : function ( stream ) {
0 commit comments