|
| 1 | +window.sfBlazor = window.sfBlazor || {}; |
| 2 | +window.sfBlazor.Barcode = (function () { |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 6 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 7 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 8 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 9 | + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } |
| 10 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 11 | + }); |
| 12 | +}; |
| 13 | +var __generator = (undefined && undefined.__generator) || function (thisArg, body) { |
| 14 | + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; |
| 15 | + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; |
| 16 | + function verb(n) { return function (v) { return step([n, v]); }; } |
| 17 | + function step(op) { |
| 18 | + if (f) throw new TypeError("Generator is already executing."); |
| 19 | + while (_) try { |
| 20 | + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; |
| 21 | + if (y = 0, t) op = [op[0] & 2, t.value]; |
| 22 | + switch (op[0]) { |
| 23 | + case 0: case 1: t = op; break; |
| 24 | + case 4: _.label++; return { value: op[1], done: false }; |
| 25 | + case 5: _.label++; y = op[1]; op = [0]; continue; |
| 26 | + case 7: op = _.ops.pop(); _.trys.pop(); continue; |
| 27 | + default: |
| 28 | + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } |
| 29 | + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } |
| 30 | + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } |
| 31 | + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } |
| 32 | + if (t[2]) _.ops.pop(); |
| 33 | + _.trys.pop(); continue; |
| 34 | + } |
| 35 | + op = body.call(thisArg, _); |
| 36 | + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } |
| 37 | + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; |
| 38 | + } |
| 39 | +}; |
| 40 | +/** |
| 41 | + * Barcode used to calculate the barcode, text size and export, print the give barcode - Blazor scripts |
| 42 | + */ |
| 43 | +var barcode = { |
| 44 | + getBarcodeSize: function (element) { |
| 45 | + var bounds = element.getBoundingClientRect(); |
| 46 | + var size = {}; |
| 47 | + var width = 'Width'; |
| 48 | + var height = 'Height'; |
| 49 | + size[width] = bounds.width; |
| 50 | + size[height] = bounds.height; |
| 51 | + return size; |
| 52 | + }, |
| 53 | + createHtmlElement: function (elementType, attribute) { |
| 54 | + var element = sf.base.createElement(elementType); |
| 55 | + if (attribute) { |
| 56 | + this.setAttribute(element, attribute); |
| 57 | + } |
| 58 | + return element; |
| 59 | + }, |
| 60 | + setAttribute: function (element, attributes) { |
| 61 | + var keys = Object.keys(attributes); |
| 62 | + for (var i = 0; i < keys.length; i++) { |
| 63 | + element.setAttribute(keys[i], attributes[keys[i]]); |
| 64 | + } |
| 65 | + }, |
| 66 | + createMeasureElements: function () { |
| 67 | + var measureElement = 'barcodeMeasureElement'; |
| 68 | + if (!window[measureElement]) { |
| 69 | + var divElement = this.createHtmlElement('div', { |
| 70 | + id: 'barcodeMeasureElement', class: 'barcodeMeasureElement', |
| 71 | + style: 'visibility:hidden ; height: 0px ; width: 0px; overflow: hidden;' |
| 72 | + }); |
| 73 | + var text = this.createHtmlElement('span', { 'style': 'display:inline-block ; line-height: normal' }); |
| 74 | + divElement.appendChild(text); |
| 75 | + var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); |
| 76 | + svg.setAttribute('xlink', 'http://www.w3.org/1999/xlink'); |
| 77 | + divElement.appendChild(svg); |
| 78 | + var tSpan = document.createElementNS('http://www.w3.org/2000/svg', 'text'); |
| 79 | + tSpan.setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:space', 'preserve'); |
| 80 | + svg.appendChild(tSpan); |
| 81 | + window[measureElement] = divElement; |
| 82 | + window[measureElement].usageCount = 1; |
| 83 | + document.body.appendChild(divElement); |
| 84 | + } |
| 85 | + else { |
| 86 | + window[measureElement].usageCount += 1; |
| 87 | + } |
| 88 | + }, |
| 89 | + measureText: function (value, size, fontStyle) { |
| 90 | + var measureElement = 'barcodeMeasureElement'; |
| 91 | + window[measureElement].style.visibility = 'visible'; |
| 92 | + var svg = window[measureElement].children[1]; |
| 93 | + var text = this.getChildNode(svg)[0]; |
| 94 | + text.textContent = value; |
| 95 | + text.setAttribute('style', 'font-size:' + size + 'px; font-family:' + fontStyle + ';'); |
| 96 | + var bounds = text.getBBox(); |
| 97 | + var bBox = {}; |
| 98 | + var width = 'Width'; |
| 99 | + var height = 'Height'; |
| 100 | + bBox[width] = bounds.width; |
| 101 | + bBox[height] = bounds.height; |
| 102 | + window[measureElement].style.visibility = 'hidden'; |
| 103 | + return bBox; |
| 104 | + }, |
| 105 | + getChildNode: function (node) { |
| 106 | + var child; |
| 107 | + var collection = []; |
| 108 | + if (sf.base.Browser.info.name === 'msie' || sf.base.Browser.info.name === 'edge') { |
| 109 | + for (var i = 0; i < node.childNodes.length; i++) { |
| 110 | + child = node.childNodes[i]; |
| 111 | + if (child.nodeType === 1) { |
| 112 | + collection.push(child); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + else { |
| 117 | + collection = node.children; |
| 118 | + } |
| 119 | + return collection; |
| 120 | + }, |
| 121 | + checkOverlapTextPosition: function (value, stringSize, fontStyle, barcodeStartX, barcodeWidth, textStartX, marginRight, options) { |
| 122 | + var textSize = 'stringSize'; |
| 123 | + var width = 'Width'; |
| 124 | + options = options || {}; |
| 125 | + options[textSize] = stringSize; |
| 126 | + var size = this.measureText(value, stringSize, fontStyle); |
| 127 | + var endValue = barcodeStartX + barcodeWidth; |
| 128 | + if ((endValue - (textStartX + size[width]) <= marginRight) && stringSize > 2) { |
| 129 | + options[textSize] -= .2; |
| 130 | + this.checkOverlapTextPosition(value, options[textSize], fontStyle, barcodeStartX, barcodeWidth, textStartX, marginRight, options); |
| 131 | + } |
| 132 | + return options[textSize]; |
| 133 | + }, |
| 134 | + triggerDownload: function (type, fileName, url) { |
| 135 | + var anchorElement = document.createElement('a'); |
| 136 | + anchorElement.download = fileName + '.' + type.toLocaleLowerCase(); |
| 137 | + anchorElement.href = url; |
| 138 | + anchorElement.click(); |
| 139 | + }, |
| 140 | + exportAsImage: function (exportType, fileName, element, isReturnBase64) { |
| 141 | + return __awaiter(this, void 0, void 0, function () { |
| 142 | + var returnValue; |
| 143 | + return __generator(this, function (_a) { |
| 144 | + switch (_a.label) { |
| 145 | + case 0: return [4 /*yield*/, this.imageExport(exportType, fileName, element, isReturnBase64)]; |
| 146 | + case 1: |
| 147 | + returnValue = _a.sent(); |
| 148 | + if (returnValue instanceof Promise) { |
| 149 | + returnValue.then(function (data) { |
| 150 | + return data; |
| 151 | + }); |
| 152 | + return [2 /*return*/, returnValue]; |
| 153 | + } |
| 154 | + else { |
| 155 | + return [2 /*return*/, returnValue]; |
| 156 | + } |
| 157 | + return [2 /*return*/]; |
| 158 | + } |
| 159 | + }); |
| 160 | + }); |
| 161 | + }, |
| 162 | + imageExport: function (type, fileName, element, isReturnBase64) { |
| 163 | + return __awaiter(this, void 0, void 0, function () { |
| 164 | + var instance, promise; |
| 165 | + return __generator(this, function (_a) { |
| 166 | + instance = this; |
| 167 | + promise = new Promise(function (resolve, reject) { |
| 168 | + var svgData = '<svg xmlns=' + 'http://www.w3.org/2000/svg' + ' xmlns:xlink=' + 'http://www.w3.org/1999/xlink' + '>' |
| 169 | + + element.children[0].outerHTML + '</svg>'; |
| 170 | + var serializer = 'XMLSerializer'; |
| 171 | + var url = window.URL.createObjectURL(new Blob(type === 'SVG' ? [svgData] : [new window[serializer]().serializeToString(element)], { type: 'image/svg+xml' })); |
| 172 | + if (type === 'SVG') { |
| 173 | + instance.triggerDownload(type, fileName, url); |
| 174 | + resolve(null); |
| 175 | + } |
| 176 | + else { |
| 177 | + var canvasElement_1 = document.createElement('canvas'); |
| 178 | + canvasElement_1.height = element.clientHeight; |
| 179 | + canvasElement_1.width = element.clientWidth; |
| 180 | + var context_1 = canvasElement_1.getContext('2d'); |
| 181 | + var image_1 = new Image(); |
| 182 | + image_1.onload = function () { |
| 183 | + context_1.drawImage(image_1, 0, 0); |
| 184 | + window.URL.revokeObjectURL(url); |
| 185 | + if (!isReturnBase64) { |
| 186 | + instance.triggerDownload(type, fileName, canvasElement_1.toDataURL('image/png').replace('image/png', 'image/octet-stream')); |
| 187 | + resolve(null); |
| 188 | + } |
| 189 | + else { |
| 190 | + var base64String = (type === 'JPEG') ? canvasElement_1.toDataURL('image/jpeg') : |
| 191 | + (type === 'PNG') ? canvasElement_1.toDataURL('image/png') : ''; |
| 192 | + resolve(base64String); |
| 193 | + } |
| 194 | + }; |
| 195 | + image_1.src = url; |
| 196 | + } |
| 197 | + }); |
| 198 | + return [2 /*return*/, promise]; |
| 199 | + }); |
| 200 | + }); |
| 201 | + } |
| 202 | +}; |
| 203 | + |
| 204 | +return barcode; |
| 205 | + |
| 206 | +}()); |
0 commit comments