all files / barcode/ barcode.js

95.55% Statements 236/247
90.85% Branches 149/164
96.67% Functions 29/30
95.44% Lines 230/241
5 statements, 2 functions, 13 branches Ignored     
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414  1× 1× 1× 1× 1× 1×   1× 31× 31× 31×       1× 99× 99× 99× 99×   1×   1× 1× 1× 1× 321×   1× 53×     53×   1× 52× 11×     41×   52× 18×   52× 124× 124×                         1× 1×           1×   18× 18× 18×   13× 13× 13×   6× 6×   18×     52×   1× 339× 1×   339× 1×   339× 339× 339× 339× 339× 339×       339×   1× 2×   1× 7× 7×   1× 373× 373× 373×   3× 3×   79× 79×   13× 13×   13× 13×   7× 7×   7× 7×   65× 65×   22× 22×   34× 34×           62× 62×     26× 26×   4× 4×   5× 5×   8× 8×   3× 3×   2× 2×   6× 6×   2× 2×   12× 12×   373× 28× 28×   373× 373×     373×   2× 2×   371× 371× 371× 371× 371× 371× 371× 371× 371× 371× 22×   371× 32× 32× 32×   371×     2× 2×   369× 24× 24× 24×   369× 62× 62×           369× 369× 320× 2×   318× 2×     316×       49×   369× 28× 28×     1× 11×   1× 11× 11× 11× 11× 11×     1×       1× 709× 709× 675×     34×   709×   1× 321× 321× 321× 321× 321× 321× 321× 272× 272×   272× 272×     1× 321×                                       1× 321×   1× 321× 321× 321× 321× 321× 321×   1× 1976×   1× 5× 5× 5×   1× 2×   1× 321× 42× 42× 42×             1× 845× 845× 137×         845×   1× 286× 286× 286× 286× 285×     1×     1×     1×     1×     1×     1×     1×     1×     1×     1×     1×     1×     1×     1×     1×     1×     1×     1×   1×    
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
/* istanbul ignore next */ 
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    Iif (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) Eif (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "./enum/enum", "./integrations/webmcp-adapter", "./rendering/renderer", "./rendering/canvas-renderer", "./one-dimension/code128B", "./one-dimension/code128C", "./primitives/displaytext", "./primitives/margin", "./one-dimension/code39", "./one-dimension/codabar", "./one-dimension/code128A", "./one-dimension/code128", "./one-dimension/gs1-code128", "./one-dimension/itf14", "./one-dimension/gs1-databar", "./one-dimension/gs1-databar-expanded", "./one-dimension/ean8", "./one-dimension/ean13", "./one-dimension/upcE", "./one-dimension/upcA", "./one-dimension/code11", "./one-dimension/code93", "./one-dimension/code93Extension", "./one-dimension/code32", "./one-dimension/code39Extension", "./utility/barcode-util"], function (require, exports, ej2_base_1, ej2_base_2, enum_1, webmcp_adapter_1, renderer_1, canvas_renderer_1, code128B_1, code128C_1, displaytext_1, margin_1, code39_1, codabar_1, code128A_1, code128_1, gs1_code128_1, itf14_1, gs1_databar_1, gs1_databar_expanded_1, ean8_1, ean13_1, upcE_1, upcA_1, code11_1, code93_1, code93Extension_1, code32_1, code39Extension_1, barcode_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var BarcodeGenerator = (function (_super) {
        __extends(BarcodeGenerator, _super);
        function BarcodeGenerator(options, element) {
            return _super.call(this, options, element) || this;
        }
        BarcodeGenerator.prototype.triggerEvent = function (eventName, message) {
            var arg = {
                message: message
            };
            this.trigger(enum_1.BarcodeEvent["" + eventName], arg);
        };
        BarcodeGenerator.prototype.onPropertyChanged = function (newProp, oldProp) {
            if (this.mode === 'Canvas' && newProp.mode !== 'Canvas') {
                this.refreshCanvasBarcode();
            }
            else {
                this.barcodeRenderer = barcode_util_1.removeChildElements(newProp, this.barcodeCanvas, this.mode, this.element.id);
            }
            if (newProp.width) {
                this.barcodeCanvas.setAttribute('width', String(newProp.width));
            }
            for (var _i = 0, _a = Object.keys(newProp); _i < _a.length; _i++) {
                var prop = _a[_i];
                switch (prop) {
                    case 'enableWebMcp':
                        if (newProp.enableWebMcp && !this.webMcpBarcodeModule) {
                            this.webMcpBarcodeModule = new webmcp_adapter_1.WebMcpBarcode(this);
                            this.initWebMcp();
                        }
                        else if (!newProp.enableWebMcp && this.webMcpBarcodeModule) {
                            this.webMcpBarcodeModule.unregisterTools();
                            this.webMcpBarcodeModule.destroy();
                            this.webMcpBarcodeModule = null;
                        }
                        break;
                    case 'webMcpSettings':
                        Eif (this.webMcpBarcodeModule) {
                            this.webMcpBarcodeModule.registerTools({
                                prefix: this.webMcpSettings.name || this.element.id,
                                tools: this.webMcpSettings.tools,
                                exposedTo: this.webMcpSettings.exposedTo
                            });
                        }
                        break;
                    case 'width':
                        this.element.style.width = this.getElementSize(this.width);
                        this.barcodeCanvas.setAttribute('width', String(this.element.offsetWidth));
                        break;
                    case 'height':
                        this.element.style.height = this.getElementSize(this.height);
                        this.barcodeCanvas.setAttribute('height', String(this.element.offsetHeight));
                        break;
                    case 'backgroundColor':
                        this.barcodeCanvas.style.background = newProp.backgroundColor;
                        break;
                    case 'mode':
                        this.initialize();
                }
            }
            this.renderElements();
        };
        BarcodeGenerator.prototype.initialize = function () {
            if (!this.height) {
                this.height = '100px';
            }
            if (!this.width) {
                this.width = '100%';
            }
            this.element.style.display = 'block';
            this.element.style.height = this.getElementSize(this.height);
            this.element.style.width = this.getElementSize(this.width);
            var height = this.mode === 'SVG' ? this.element.offsetHeight : this.element.offsetHeight * 1.5;
            var width = this.mode === 'SVG' ? this.element.offsetWidth : this.element.offsetWidth * 1.5;
            this.barcodeCanvas = this.barcodeRenderer.renderRootElement({
                id: this.element.id + 'content',
                height: height, width: width
            }, this.backgroundColor, width, height);
            this.element.appendChild(this.barcodeCanvas);
        };
        BarcodeGenerator.prototype.exportImage = function (filename, exportType) {
            barcode_util_1.exportAsImage(exportType, filename, this.element, false, this);
        };
        BarcodeGenerator.prototype.exportAsBase64Image = function (exportType) {
            var returnValue = barcode_util_1.exportAsImage(exportType, '', this.element, true, this);
            return returnValue;
        };
        BarcodeGenerator.prototype.renderElements = function () {
            var barCode;
            this.type = this.type || 'Code128';
            switch (this.type) {
                case 'Code39Extension':
                    barCode = new code39Extension_1.Code39Extension;
                    break;
                case 'Code39':
                    barCode = new code39_1.Code39();
                    break;
                case 'Codabar':
                    barCode = new codabar_1.CodaBar();
                    break;
                case 'Code128A':
                    barCode = new code128A_1.Code128A();
                    break;
                case 'Code128B':
                    barCode = new code128B_1.Code128B();
                    break;
                case 'Code128C':
                    barCode = new code128C_1.Code128C();
                    break;
                case 'Code128':
                    barCode = new code128_1.Code128();
                    break;
                case 'GS1Code128':
                    barCode = new gs1_code128_1.GS1Code128();
                    break;
                case 'ITF14':
                    barCode = new itf14_1.ITF14();
                    break;
                case 'GS1DataBarOmnidirectional':
                case 'GS1DataBarTruncated':
                case 'GS1DataBarStacked':
                case 'GS1DataBarStackedOmnidirectional':
                case 'GS1DataBarLimited':
                    barCode = new gs1_databar_1.GS1DataBar();
                    break;
                case 'GS1DataBarExpanded':
                case 'GS1DataBarExpandedStacked':
                    barCode = new gs1_databar_expanded_1.GS1DataBarExpanded();
                    break;
                case 'Ean8':
                    barCode = new ean8_1.Ean8();
                    break;
                case 'Ean13':
                    barCode = new ean13_1.Ean13();
                    break;
                case 'UpcA':
                    barCode = new upcA_1.UpcA();
                    break;
                case 'UpcE':
                    barCode = new upcE_1.UpcE();
                    break;
                case 'Code11':
                    barCode = new code11_1.Code11();
                    break;
                case 'Code93':
                    barCode = new code93_1.Code93();
                    break;
                case 'Code93Extension':
                    barCode = new code93Extension_1.Code93Extension();
                    break;
                case 'Code32':
                    barCode = new code32_1.Code32();
                    break;
            }
            if (this.mode === 'Canvas') {
                this.barcodeCanvas.getContext('2d').setTransform(1, 0, 0, 1, 0, 0);
                this.barcodeCanvas.getContext('2d').scale(1.5, 1.5);
            }
            barCode.width = this.barcodeCanvas.getAttribute('width');
            Iif ((this.type === 'Ean8' || this.type === 'Ean13' || this.type === 'UpcA') && this.displayText.text.length > 0) {
                this.triggerEvent(enum_1.BarcodeEvent.invalid, 'Invalid Display Text');
            }
            if (this.type === 'ITF14'
                && (!this.bearerBarWidth || this.bearerBarWidth < 1 || this.bearerBarWidth > 10)) {
                this.triggerEvent(enum_1.BarcodeEvent.invalid, this.localeObj.getConstant('InvalidBearerBarWidth'));
                return;
            }
            this.value = this.value || '';
            barCode.value = this.value;
            barCode.margin = this.margin;
            barCode.type = this.type;
            barCode.height = this.barcodeCanvas.getAttribute('height');
            barCode.foreColor = this.foreColor;
            barCode.isSvgMode = this.mode === 'SVG' ? true : false;
            barCode.displayText = this.displayText;
            barCode.enableCheckSum = this.enableCheckSum;
            if (this.type === 'GS1Code128') {
                barCode.locale = this.localeObj;
            }
            if (this.type === 'ITF14') {
                barCode.locale = this.localeObj;
                barCode.bearerBar = this.bearerBar;
                barCode.bearerBarWidth = this.bearerBarWidth;
            }
            if (this.type === 'GS1DataBarExpandedStacked'
                && (!this.segmentsPerRow || this.segmentsPerRow < 2 || this.segmentsPerRow > 20
                    || this.segmentsPerRow % 2 !== 0)) {
                this.triggerEvent(enum_1.BarcodeEvent.invalid, this.localeObj.getConstant('DataBarInvalidSegmentsPerRow'));
                return;
            }
            if (barCode instanceof gs1_databar_expanded_1.GS1DataBarExpanded) {
                barCode.locale = this.localeObj;
                barCode.databarVariant = this.type === 'GS1DataBarExpanded' ? 'Expanded' : 'ExpandedStacked';
                barCode.segmentsPerRow = this.segmentsPerRow;
            }
            if (barCode instanceof gs1_databar_1.GS1DataBar) {
                barCode.locale = this.localeObj;
                barCode.databarVariant =
                    this.type === 'GS1DataBarOmnidirectional' ? 'Omnidirectional' :
                        this.type === 'GS1DataBarTruncated' ? 'Truncated' :
                            this.type === 'GS1DataBarStacked' ? 'Stacked' :
                                this.type === 'GS1DataBarStackedOmnidirectional' ? 'StackedOmnidirectional' : 'Limited';
            }
            var validateMessage = barCode.validateInput(this.value);
            if (validateMessage === undefined) {
                if (this.type === 'Code39Extension') {
                    barCode.drawCode39(this.barcodeCanvas);
                }
                else if (this.type === 'Code93Extension') {
                    barCode.drawCode93(this.barcodeCanvas);
                }
                else {
                    barCode.draw(this.barcodeCanvas);
                }
            }
            else {
                this.triggerEvent(enum_1.BarcodeEvent.invalid, validateMessage);
            }
            if (this.mode === 'Canvas') {
                this.barcodeCanvas.style.transform = 'scale(' + (2 / 3) + ')';
                this.barcodeCanvas.style.transformOrigin = '0 0';
            }
        };
        BarcodeGenerator.prototype.refreshCanvasBarcode = function () {
            this.clearCanvas(this);
        };
        BarcodeGenerator.prototype.clearCanvas = function (view) {
            var width = view.element.offsetWidth;
            var height = view.element.offsetHeight;
            Eif (view.mode !== 'SVG') {
                var ctx = canvas_renderer_1.BarcodeCanvasRenderer.getContext(this.barcodeCanvas);
                ctx.clearRect(0, 0, width, height);
            }
        };
        BarcodeGenerator.prototype.getPersistData = function () {
            var keyEntity = ['loaded'];
            return this.addOnPersist(keyEntity);
        };
        BarcodeGenerator.prototype.getElementSize = function (real, rulerSize) {
            var value;
            if (real.toString().indexOf('px') > 0 || real.toString().indexOf('%') > 0) {
                value = real.toString();
            }
            else {
                value = real.toString() + 'px';
            }
            return value;
        };
        BarcodeGenerator.prototype.preRender = function () {
            this.element.classList.add('e-barcode');
            this.barcodeRenderer = new renderer_1.BarcodeRenderer(this.element.id, this.mode === 'SVG');
            this.initialize();
            this.initializePrivateVariables();
            this.setCulture();
            var measureElement = document.getElementsByClassName('barcodeMeasureElement');
            if (measureElement.length > 0) {
                for (var i = measureElement.length - 1; i >= 0; i--) {
                    measureElement[parseInt(i.toString(), 10)].parentNode.removeChild(measureElement[parseInt(i.toString(), 10)]);
                }
                var element = 'barcodeMeasureElement';
                window["" + element] = null;
            }
        };
        BarcodeGenerator.prototype.initializePrivateVariables = function () {
            this.defaultLocale = {
                GS1InvalidFormat: 'The value must be GS1 data, in bracketed AI notation such as (01)09506000134376(10)ABC123, or raw.',
                GS1UnsupportedAI: 'The Application Identifier is not supported.',
                GS1InvalidLength: 'The value of an Application Identifier has an invalid length.',
                GS1InvalidCharacter: 'The value of an Application Identifier contains characters outside the GS1 character set.',
                GS1InvalidDate: 'The date of an Application Identifier is not a valid YYMMDD date.',
                GS1InvalidCheckDigit: 'The check digit of the GTIN or SSCC is not valid.',
                GS1DuplicateAI: 'An Application Identifier appears more than once.',
                GS1MissingRequiredAI: 'The value requires a GTIN (01) for the Application Identifiers used.',
                GS1DataTooLong: 'GS1-128 data must not exceed 48 characters.',
                ITF14InvalidValue: 'ITF-14 requires 13 or 14 digits.',
                ITF14InvalidCheckDigit: 'The ITF-14 check digit is not valid.',
                InvalidBearerBarWidth: 'The bearer bar width must be a number from 1 to 10.',
                DataBarInvalidGtin: 'GS1 DataBar requires a 13- or 14-digit GTIN, or (01) followed by 14 digits.',
                DataBarLimitedInvalidGtin: 'GS1 DataBar Limited requires a GTIN starting with 0 or 1.',
                DataBarDataTooLong: 'GS1 DataBar Expanded data must not exceed 74 digits or 41 letters.',
                DataBarInvalidSegmentsPerRow: 'segmentsPerRow must be an even number from 2 to 20.',
                InsufficientSize: 'The width and height are too small to render the barcode after margins and text.'
            };
        };
        BarcodeGenerator.prototype.setCulture = function () {
            this.localeObj = new ej2_base_1.L10n(this.getModuleName(), this.defaultLocale, this.locale);
        };
        BarcodeGenerator.prototype.render = function () {
            this.notify('initial-load', {});
            this.trigger('load');
            this.notify('initial-end', {});
            this.renderElements();
            this.initWebMcp();
            this.renderComplete();
        };
        BarcodeGenerator.prototype.getModuleName = function () {
            return 'barcode';
        };
        BarcodeGenerator.prototype.getWebMcpTools = function (toolNames) {
            var args = { toolNames: toolNames };
            this.notify('getWebMcpTools', args);
            return args.tools || [];
        };
        BarcodeGenerator.prototype.registerWebMcpTools = function (prefix, tools, exposedTo) {
            this.notify('registerWebMcpTools', { prefix: prefix, tools: tools, exposedTo: exposedTo });
        };
        BarcodeGenerator.prototype.initWebMcp = function () {
            if (this.webMcpBarcodeModule) {
                this.webMcpBarcodeModule.init(this);
                var settings = this.webMcpSettings || {};
                this.webMcpBarcodeModule.registerTools({
                    prefix: settings.name || this.element.id,
                    tools: settings.tools,
                    exposedTo: settings.exposedTo
                });
            }
        };
        BarcodeGenerator.prototype.requiredModules = function () {
            var modules = [];
            if (this.enableWebMcp) {
                modules.push({
                    member: 'WebMcpBarcode',
                    args: [this]
                });
            }
            return modules;
        };
        BarcodeGenerator.prototype.destroy = function () {
            this.notify('destroy', {});
            _super.prototype.destroy.call(this);
            var content = document.getElementById(this.element.id + 'content');
            if (content) {
                this.element.removeChild(content);
            }
        };
        __decorate([
            ej2_base_1.Property('100%')
        ], BarcodeGenerator.prototype, "width", void 0);
        __decorate([
            ej2_base_1.Property('100px')
        ], BarcodeGenerator.prototype, "height", void 0);
        __decorate([
            ej2_base_1.Property('SVG')
        ], BarcodeGenerator.prototype, "mode", void 0);
        __decorate([
            ej2_base_1.Property('Code128')
        ], BarcodeGenerator.prototype, "type", void 0);
        __decorate([
            ej2_base_1.Property(undefined)
        ], BarcodeGenerator.prototype, "value", void 0);
        __decorate([
            ej2_base_1.Property(true)
        ], BarcodeGenerator.prototype, "enableCheckSum", void 0);
        __decorate([
            ej2_base_2.Complex({}, displaytext_1.DisplayText)
        ], BarcodeGenerator.prototype, "displayText", void 0);
        __decorate([
            ej2_base_2.Complex({}, margin_1.Margin)
        ], BarcodeGenerator.prototype, "margin", void 0);
        __decorate([
            ej2_base_1.Property('white')
        ], BarcodeGenerator.prototype, "backgroundColor", void 0);
        __decorate([
            ej2_base_1.Property('black')
        ], BarcodeGenerator.prototype, "foreColor", void 0);
        __decorate([
            ej2_base_1.Property('Frame')
        ], BarcodeGenerator.prototype, "bearerBar", void 0);
        __decorate([
            ej2_base_1.Property(5)
        ], BarcodeGenerator.prototype, "bearerBarWidth", void 0);
        __decorate([
            ej2_base_1.Property(4)
        ], BarcodeGenerator.prototype, "segmentsPerRow", void 0);
        __decorate([
            ej2_base_2.Event()
        ], BarcodeGenerator.prototype, "invalid", void 0);
        __decorate([
            ej2_base_1.Property(false)
        ], BarcodeGenerator.prototype, "enableWebMcp", void 0);
        __decorate([
            ej2_base_2.Complex({}, webmcp_adapter_1.WebMcpBarcodeSettings)
        ], BarcodeGenerator.prototype, "webMcpSettings", void 0);
        __decorate([
            ej2_base_2.Event()
        ], BarcodeGenerator.prototype, "beforeWebMcpToolExecute", void 0);
        return BarcodeGenerator;
    }(ej2_base_1.Component));
    exports.BarcodeGenerator = BarcodeGenerator;
});