all files / dotcode/ dotcode.js

93.14% Statements 163/175
89.11% Branches 90/101
86.21% Functions 25/29
92.9% Lines 157/169
18 statements, 7 functions, 29 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  1× 1× 1× 1× 1× 1×   1× 1× 1× 1×       1× 1× 1× 1× 1×   1×   1× 1× 1× 1× 1× 45×   1×         1×     1× 45× 2× 2× 2×             1× 139× 139× 12×         139×   1× 45× 45× 45× 45× 45× 45× 45× 45×   1×       1× 275×   1× 45×                               1× 45×   1× 178× 178×   1× 89×     89×     89× 89× 89× 89× 89× 89× 89×       89×   1× 89× 89×   89×   1× 26× 26×   1× 45× 45× 45× 45×   1× 18× 2×       2× 1× 1× 1×     18× 1×           18×     18× 18× 18×   1×     1× 1×   1× 63× 63× 63× 63× 63× 20× 20× 20× 9× 9× 9× 9×   11× 11× 10×       43×   54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54×   54× 54× 17× 17× 17× 17×     37× 37×   54× 54× 4× 4×     1× 45× 45× 45× 45× 45× 45×   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;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (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", "../barcode/enum/enum", "../barcode/primitives/displaytext", "../barcode/primitives/margin", "../barcode/rendering/renderer", "../barcode/utility/barcode-util", "./dotcode-util", "./gs1-dotcode", "../barcode/integrations/webmcp-adapter"], function (require, exports, ej2_base_1, ej2_base_2, enum_1, displaytext_1, margin_1, renderer_1, barcode_util_1, dotcode_util_1, gs1_dotcode_1, webmcp_adapter_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var DOTCODE_ARIA_TEXT_LIMIT = 100;
    var DotCodeGenerator = (function (_super) {
        __extends(DotCodeGenerator, _super);
        function DotCodeGenerator(options, element) {
            return _super.call(this, options, element) || this;
        }
        DotCodeGenerator.prototype.getWebMcpTools = function (toolNames) {
            var args = { toolNames: toolNames };
            this.notify('getWebMcpTools', args);
            return args.tools || [];
        };
        DotCodeGenerator.prototype.registerWebMcpTools = function (prefix, tools, exposedTo) {
            this.notify('registerWebMcpTools', { prefix: prefix, tools: tools, exposedTo: exposedTo });
        };
        DotCodeGenerator.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
                });
            }
        };
        DotCodeGenerator.prototype.requiredModules = function () {
            var modules = [];
            if (this.enableWebMcp) {
                modules.push({
                    member: 'WebMcpBarcode',
                    args: [this]
                });
            }
            return modules;
        };
        DotCodeGenerator.prototype.destroy = function () {
            this.notify('destroy', {});
            this.removeRootElement();
            this.element.classList.remove('e-dotcode');
            this.element.removeAttribute('role');
            this.element.removeAttribute('aria-label');
            this.barcodeRenderer = null;
            this.symbolInfo = null;
            _super.prototype.destroy.call(this);
        };
        DotCodeGenerator.prototype.getPersistData = function () {
            var keyEntity = ['loaded'];
            return this.addOnPersist(keyEntity);
        };
        DotCodeGenerator.prototype.getModuleName = function () {
            return 'DotCodeGenerator';
        };
        DotCodeGenerator.prototype.initializePrivateVariables = function () {
            this.defaultLocale = {
                ariaLabel: 'DotCode barcode',
                EmptyValue: 'The value is empty.',
                InvalidColumns: 'The number of columns must be 0 (auto) or at least 5, and enough to hold the value.',
                CapacityExceeded: 'The value is too long for the DotCode symbol with the current settings.',
                InsufficientSize: 'The width and height are too small to render the DotCode symbol after margins and text.',
                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.'
            };
        };
        DotCodeGenerator.prototype.setCulture = function () {
            this.localeObj = new ej2_base_1.L10n(this.getModuleName(), this.defaultLocale, this.locale);
        };
        DotCodeGenerator.prototype.getElementSize = function (real) {
            var value = real.toString();
            return (value.indexOf('px') > 0 || value.indexOf('%') > 0) ? value : value + 'px';
        };
        DotCodeGenerator.prototype.initialize = function () {
            Iif (!this.width) {
                this.width = '100%';
            }
            Iif (!this.height) {
                this.height = '100%';
            }
            this.element.style.display = 'block';
            this.element.style.width = this.getElementSize(this.width);
            this.element.style.height = this.getElementSize(this.height);
            this.barcodeRenderer = new renderer_1.BarcodeRenderer(this.element.id, this.mode === 'SVG');
            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);
        };
        DotCodeGenerator.prototype.removeRootElement = function () {
            Eif (this.barcodeCanvas && this.barcodeCanvas.parentNode) {
                this.barcodeCanvas.parentNode.removeChild(this.barcodeCanvas);
            }
            this.barcodeCanvas = null;
        };
        DotCodeGenerator.prototype.triggerEvent = function (eventName, message) {
            var arg = { message: message };
            this.trigger(enum_1.BarcodeEvent["" + eventName], arg);
        };
        DotCodeGenerator.prototype.preRender = function () {
            this.element.classList.add('e-dotcode');
            this.initializePrivateVariables();
            this.setCulture();
            this.initialize();
        };
        DotCodeGenerator.prototype.onPropertyChanged = function (newProp, oldProp) {
            if (newProp.enableWebMcp !== undefined) {
                Iif (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;
                }
            }
            if (newProp.webMcpSettings && this.webMcpBarcodeModule) {
                this.webMcpBarcodeModule.registerTools({
                    prefix: this.webMcpSettings.name || this.element.id,
                    tools: this.webMcpSettings.tools,
                    exposedTo: this.webMcpSettings.exposedTo
                });
            }
            Iif (newProp.locale !== undefined) {
                this.setCulture();
            }
            this.removeRootElement();
            this.initialize();
            this.renderElements();
        };
        DotCodeGenerator.prototype.exportImage = function (fileName, exportType) {
            barcode_util_1.exportAsImage(exportType, fileName, this.element, false, this);
        };
        DotCodeGenerator.prototype.exportAsBase64Image = function (barcodeExportType) {
            return barcode_util_1.exportAsImage(barcodeExportType, '', this.element, true, this);
        };
        DotCodeGenerator.prototype.renderElements = function () {
            var dotcode = new dotcode_util_1.DotCode();
            this.value = (this.value === undefined || this.value === null) ? '' : this.value;
            var gs1DotCode = this.enableGS1 ? new gs1_dotcode_1.Gs1DotCode() : undefined;
            var originalText = this.displayText.text;
            if (gs1DotCode) {
                gs1DotCode.locale = this.localeObj;
                var gs1Message = gs1DotCode.validateGs1(this.value);
                if (gs1Message !== undefined) {
                    this.triggerEvent(enum_1.BarcodeEvent.invalid, gs1Message);
                    this.removeRootElement();
                    this.initialize();
                    return;
                }
                dotcode.value = gs1DotCode.buildGs1Data();
                if (!originalText) {
                    this.displayText.text = gs1DotCode.getHumanReadableText();
                }
            }
            else {
                dotcode.value = this.value;
            }
            dotcode.width = Number(this.barcodeCanvas.getAttribute('width'));
            dotcode.height = Number(this.barcodeCanvas.getAttribute('height'));
            dotcode.isSvgMode = this.mode === 'SVG';
            dotcode.margin = this.margin;
            dotcode.displayText = this.displayText;
            dotcode.foreColor = this.foreColor;
            dotcode.columns = this.columns;
            dotcode.isGs1 = this.enableGS1 === true;
            var result = dotcode.build();
            this.element.setAttribute('role', 'img');
            var label = this.localeObj.getConstant('ariaLabel');
            var text = String(this.displayText.text || this.value);
            var spoken = text.length > DOTCODE_ARIA_TEXT_LIMIT ?
                text.substring(0, DOTCODE_ARIA_TEXT_LIMIT) + '\u2026' : text;
            this.element.setAttribute('aria-label', spoken ? label + ': ' + spoken : label);
            if (!result.success) {
                this.symbolInfo = null;
                this.triggerEvent(enum_1.BarcodeEvent.invalid, this.localeObj.getConstant(result.error));
                this.removeRootElement();
                this.initialize();
            }
            else {
                this.symbolInfo = { rows: result.rows, columns: result.columns, mask: result.mask };
                dotcode.draw(this.barcodeCanvas);
            }
            this.displayText.text = originalText;
            if (this.mode === 'Canvas') {
                this.barcodeCanvas.style.transform = 'scale(' + (2 / 3) + ')';
                this.barcodeCanvas.style.transformOrigin = '0 0';
            }
        };
        DotCodeGenerator.prototype.render = function () {
            this.notify('initial-load', {});
            this.trigger('load');
            this.notify('initial-end', {});
            this.renderElements();
            this.initWebMcp();
            this.renderComplete();
        };
        __decorate([
            ej2_base_1.Property('SVG')
        ], DotCodeGenerator.prototype, "mode", void 0);
        __decorate([
            ej2_base_1.Property(undefined)
        ], DotCodeGenerator.prototype, "value", void 0);
        __decorate([
            ej2_base_1.Property(false)
        ], DotCodeGenerator.prototype, "enableGS1", void 0);
        __decorate([
            ej2_base_1.Property(0)
        ], DotCodeGenerator.prototype, "columns", void 0);
        __decorate([
            ej2_base_1.Property('100%')
        ], DotCodeGenerator.prototype, "height", void 0);
        __decorate([
            ej2_base_1.Property('100%')
        ], DotCodeGenerator.prototype, "width", void 0);
        __decorate([
            ej2_base_2.Complex({}, displaytext_1.DisplayText)
        ], DotCodeGenerator.prototype, "displayText", void 0);
        __decorate([
            ej2_base_2.Complex({}, margin_1.Margin)
        ], DotCodeGenerator.prototype, "margin", void 0);
        __decorate([
            ej2_base_1.Property('white')
        ], DotCodeGenerator.prototype, "backgroundColor", void 0);
        __decorate([
            ej2_base_1.Property('black')
        ], DotCodeGenerator.prototype, "foreColor", void 0);
        __decorate([
            ej2_base_2.Event()
        ], DotCodeGenerator.prototype, "invalid", void 0);
        __decorate([
            ej2_base_1.Property(false)
        ], DotCodeGenerator.prototype, "enableWebMcp", void 0);
        __decorate([
            ej2_base_2.Complex({}, webmcp_adapter_1.WebMcpBarcodeSettings)
        ], DotCodeGenerator.prototype, "webMcpSettings", void 0);
        __decorate([
            ej2_base_2.Event()
        ], DotCodeGenerator.prototype, "beforeWebMcpToolExecute", void 0);
        return DotCodeGenerator;
    }(ej2_base_1.Component));
    exports.DotCodeGenerator = DotCodeGenerator;
});