Uploaded image for project: 'ZK'
  1. ZK
  2. ZK-4337

barcodescanner can only instantiate reader once per page

    Details

    • Type: Bug
    • Status: Open
    • Priority: Normal
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      Steps to Reproduce

      run code locally:
      http://zkfiddle.org/sample/4m1op7/3-can-t-run-parallel-barcode-instances

      Current Result

      first reader will initialize the library and run scans
      2nd reader will not have listeners active from library

      Expected Result

      both reader should be active

      Debug Info

      https://github.com/zkoss/zkcml/blob/fe15b633304622d18eb529c6fbc94b26edd580d8/zkmax/src/archive/web/js/zkmax/barscanner/Barcodescanner.js#L165-L167

      this if statement will prevents components to register a reader for themselves if the library was already used in a different component.
      At the moment, quagga supports multiple callbacks on same reader, zxing doesn't.
      In both cases, only the first barcodereader is allowed to register listeners on the reader object.

      This means that if a page adds a reader (in a modal window, or in a template with multiple sources) then removes it, and adds another instance of reader,: only the first reader will actually receive JS library events

      Root Cause

      Abstraction layers initialize the library once per page, only let the first component actually register a listener

      Workaround

      <script><![CDATA[ 
      zk.afterLoad('zkmax.barscanner', function() {
      	var xBarcodescanner = {};
      	zk.override(zkmax.barscanner.Barcodescanner.prototype, xBarcodescanner ,{
      		_setReaders: function () {
      			var libs = this.$class._checkLibs(this),
      				wgt = this;
      			var wgtLibsName = [];
      			this.libs.forEach(function (lib) {
      				wgtLibsName.push(lib.libname);
      			});
      			for (var i = 0; i < libs.length; i++) {
      				wgt.$n('canvas').width = wgt.$n('canvas').width;
      				wgt.$n('canvas').height = wgt.$n('canvas').height;
      				var barReader = wgt.$class._readerConstruct(wgt, libs[i].constructor);
      				if (barReader['zinit']) {
      					barReader.zinit(wgt, wgt.$n('video'), wgt.$n('canvas'), wgt.$n());
      					barReader.zexecute(wgt);
      				}
      				wgt.readers.push(barReader);
      				wgt.libs.push(libs[i]);
      			}
      		}
      	});//zk.override
      });//zk.afterLoad
      ]]></script>
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              MDuchemin MDuchemin
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: