UNPKG

inline-print

Version:

Inline Print için kullanılır.

598 lines (492 loc) 24.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireWildcard(require("react")); var _PrintInlineComponent = _interopRequireDefault(require("./PrintInlineComponent")); var _CircularProgress = _interopRequireDefault(require("@material-ui/core/CircularProgress")); var _Select = _interopRequireDefault(require("@material-ui/core/Select")); var _MenuItem = _interopRequireDefault(require("@material-ui/core/MenuItem")); var _Grid = _interopRequireDefault(require("@material-ui/core/Grid")); var _reactIntl = require("react-intl"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var axios = require("axios"); var JSPM = window.JSPM; var PrintInline = /*#__PURE__*/function (_React$Component) { _inherits(PrintInline, _React$Component); var _super = _createSuper(PrintInline); function PrintInline(_props) { var _this2; _classCallCheck(this, PrintInline); _this2 = _super.call(this, _props); _defineProperty(_assertThisInitialized(_this2), "jspmWsStatusChanged", function (status) { _this2.setState({ JSPM_WS_Status: status }); }); _defineProperty(_assertThisInitialized(_this2), "printersInfoChanged", function (newPrintersInfo) { _this2.setState({ printersInfo: newPrintersInfo }); }); _defineProperty(_assertThisInitialized(_this2), "clearArray", function () { var options = _toConsumableArray(_this2.state.PrintDestinationContainer); for (var i = 0; i < options.length; i++) { if (options[i][0] > 7) { options.splice(options.indexOf(options[i])); } } _this2.setState({ PrintDestinationContainer: options }); }); _defineProperty(_assertThisInitialized(_this2), "pushPrinters", function (printers) { _this2.clearArray(); var count = 8; var options = _toConsumableArray(_this2.state.PrintDestinationContainer); printers.map(function (value, index) { options.push([count, "printer", value, value]); count++; }); _this2.setState({ PrintDestinationContainer: _toConsumableArray(options) }); }); _defineProperty(_assertThisInitialized(_this2), "getSelectedPrinter", function () { var item = localStorage.getItem("selectedPrintDestination"); if (item == null) return "PDF"; return item; }); _defineProperty(_assertThisInitialized(_this2), "getPrinters", function (outerfunction) { JSPM.JSPrintManager.stop(); JSPM.JSPrintManager.start(); JSPM.JSPrintManager.WS.onStatusChanged = function () { if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Open) { JSPM.JSPrintManager.getPrinters().then(function (detectedPrinters) { outerfunction(detectedPrinters); }); } }; }); _defineProperty(_assertThisInitialized(_this2), "setSelectedPrinter", function (printerName) { localStorage.setItem("selectedPrintDestination", printerName); _this2.setState({ PrinterName: printerName }); }); _defineProperty(_assertThisInitialized(_this2), "onHandlePrinterChange", function (vals) { _this2.setSelectedPrinter(vals[3]); }); _defineProperty(_assertThisInitialized(_this2), "onHandlePrint", function (vals) { //eğer printPrerequestFunction verilmiş ise bu fonksiyonu çağırır if (_this2.props.printPrerequestFunction) { _this2.props.printPrerequestFunction(_this2.state.ReportKey).then(function (res) { if (res) { var params = _this2.getParamsForPrint(vals); _this2.getFileAndPrint(params, _this2.filePrint); } }); } else { var params = _this2.getParamsForPrint(vals); _this2.getFileAndPrint(params, _this2.filePrint); } }); _defineProperty(_assertThisInitialized(_this2), "uniqueFileName", function (format) { if (_this2.state.FastDefaultPrinter && _this2.state.FastDefaultPrinter.raporKod) { return _this2.state.FastDefaultPrinter.raporName.replace(" ", "_") + "_" + _this2.filenamePostfix("_") + "." + format; } return _this2.state.ReportName.replace(" ", "_") + "_" + _this2.filenamePostfix("_") + "." + format; }); _defineProperty(_assertThisInitialized(_this2), "filenamePostfix", function () { var separator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ""; var newDate = new Date(); var date = newDate.getDate(); var month = newDate.getMonth() + 1; var year = newDate.getFullYear(); var hours = newDate.getHours(); var minutes = newDate.getMinutes(); return "".concat(year).concat(separator).concat(month < 10 ? "0".concat(month) : "".concat(month)).concat(separator).concat(date).concat(separator).concat(hours < 10 ? "0".concat(hours) : "".concat(hours)).concat(separator).concat(minutes < 10 ? "0".concat(minutes) : "".concat(minutes)); }); _defineProperty(_assertThisInitialized(_this2), "onHandleDownload", function (vals) { //eğer downloadPrerequestFunction verilmiş ise bu fonksiyonu çağırır if (_this2.props.downloadPrerequestFunction) { _this2.props.downloadPrerequestFunction(_this2.state.ReportKey).then(function (res) { if (res) { var params = _this2.getParamsForDownload(vals); _this2.getFileAndDownload(params); } }); } else { var params = _this2.getParamsForDownload(vals); _this2.getFileAndDownload(params); } }); _defineProperty(_assertThisInitialized(_this2), "getParamsForPrint", function (vals) { var kontrol = _this2.state.FastDefaultPrinter && _this2.state.FastDefaultPrinter.raporKod !== null; return { format: "pdf", reportName: kontrol ? _this2.state.FastDefaultPrinter.raporName : _this2.state.ReportName, reportKod: kontrol ? _this2.state.FastDefaultPrinter.raporKod : _this2.state.ReportKod, //reportKey: kontrol ? this.state.FastDefaultPrinter.raporKey : this.state.ReportKey, parameters: _this2.state.Parameters, kullaniciKod: _this2.state.UserName, kullaniciAd: _this2.state.UserId }; }); _defineProperty(_assertThisInitialized(_this2), "getParamsForDownload", function (vals) { var kontrol = _this2.state.FastDefaultPrinter && _this2.state.FastDefaultPrinter.raporKod !== null; return { format: vals[3], reportName: kontrol ? _this2.state.FastDefaultPrinter.raporName : _this2.state.ReportName, reportKod: kontrol ? _this2.state.FastDefaultPrinter.raporKod : _this2.state.ReportKod, parameters: _this2.state.Parameters, kullaniciKod: _this2.state.UserId, kullaniciAd: _this2.state.UserName }; }); _defineProperty(_assertThisInitialized(_this2), "prepareFormData", function (params) { var bodyFormData = new FormData(); bodyFormData.set("format", params.format); bodyFormData.set("reportName", params.reportName); bodyFormData.set("reportKod", params.reportKod); bodyFormData.set("kullaniciAd", params.kullaniciAd); bodyFormData.set("kullaniciKod", params.kullaniciKod); params.parameters.map(function (value, index) { bodyFormData.append("parameters[" + index + "][parameterName]", String(value.ParameterName)); bodyFormData.append("parameters[" + index + "][parameterValue]", String(value.ParameterValue)); }); return bodyFormData; }); _defineProperty(_assertThisInitialized(_this2), "getReports", function () { axios({ method: "get", url: _this2.state.ReportDataUrl + "?modul=" + _this2.state.Modul, headers: { Authorization: "Bearer " + localStorage.getItem("access_token") } }).then(function (response) { if (_this2.state.FastDefaultPrinter && !_this2.state.FastDefaultPrinter.raporKod) { _this2.setState({ loadingFile: false, Reports: response.data, ReportKod: response.data[0]["raporKod"], ReportName: response.data[0]["raporAd"], ReportKey: response.data[0]["raporKey"] }); } else { var temp = _this2.state.FastDefaultPrinter ? _this2.state.FastDefaultPrinter.raporKod : 0; var rapor = response.data.find(function (item) { return item.raporKod === temp; }); if (rapor) { _this2.setState({ loadingFile: false, Reports: response.data, ReportKod: rapor.raporKod, ReportName: rapor.raporName, ReportKey: rapor.raporKey }); } else { _this2.setState({ loadingFile: false, Reports: response.data, ReportKod: response.data[0]["raporKod"], ReportName: response.data[0]["raporAd"], ReportKey: response.data[0]["raporKey"] }); } } }, function (error) { _this2.setState({ loadingFile: false }); }); }); _defineProperty(_assertThisInitialized(_this2), "getFileAndDownload", function (params) { var bodyFormData = _this2.prepareFormData(params); _this2.setState({ loadingFile: true }); axios({ method: "post", url: _this2.state.ReportUrl, data: bodyFormData, headers: { "Content-Type": "multipart/form-data", Authorization: "Bearer " + localStorage.getItem("access_token") }, responseType: "blob" }).then(function (response) { var filename = _this2.uniqueFileName(params.format); var downloadUrl = window.URL.createObjectURL(new Blob([response.data])); var link = document.createElement("a"); link.href = downloadUrl; link.setAttribute("download", filename); //any other extension document.body.appendChild(link); link.click(); link.remove(); _this2.setState({ loadingFile: false }); //eğer downloadSuccessFuction verilmiş ise bu fonksiyonu çağırır if (_this2.props.downloadSuccessFuction) { if (_this2.state.FastDefaultPrinter && !_this2.state.FastDefaultPrinter.raporKod) { _this2.props.downloadSuccessFuction(_this2.state.ReportKey); } else { _this2.props.downloadSuccessFuction(_this2.state.FastDefaultPrinter.raporKey); } } }, function (error) { if (_this2.props.downloadFailFunction) { if (_this2.state.FastDefaultPrinter && !_this2.state.FastDefaultPrinter.raporKod) { _this2.props.downloadFailFunction(_this2.state.ReportKey); } else { _this2.props.downloadFailFunction(_this2.state.FastDefaultPrinter.raporKey); } } _this2.setState({ loadingFile: false }); }); }); _defineProperty(_assertThisInitialized(_this2), "createPrintJobPdf", function () { var cpj = new JSPM.ClientPrintJob(); cpj.clientPrinter = new JSPM.InstalledPrinter(_this2.state.PrinterName, false, _this2.state.PrinterTrayName, _this2.state.PrinterPaperName); if (_this2.state.File) { var myPdfFile; myPdfFile = new JSPM.PrintFilePDF(_this2.state.File, JSPM.FileSourceType.BLOB, _this2.uniqueFileName("pdf"), 1); cpj.files.push(myPdfFile); } return cpj; }); _defineProperty(_assertThisInitialized(_this2), "doPrinting", function () { var cpj = _this2.createPrintJobPdf(); if (cpj) { cpj.sendToClient(); _this2.setState({ loadingFile: false }); } }); _defineProperty(_assertThisInitialized(_this2), "getFileAndPrint", function (params) { var bodyFormData = _this2.prepareFormData(params); var self = _assertThisInitialized(_this2); _this2.setState({ loadingFile: true }); axios({ method: "post", url: _this2.state.ReportUrl, data: bodyFormData, headers: { "Content-Type": "multipart/form-data", Authorization: "Bearer " + localStorage.getItem("access_token") }, responseType: "blob" }).then(function (response) { self.setState({ File: new Blob([response.data]) }); self.doPrinting(); //eğer printSuccessFuction verilmiş ise bu fonksiyonu çağırır if (_this2.props.printSuccessFuction) { if (_this2.state.FastDefaultPrinter && !_this2.state.FastDefaultPrinter.raporKod) { props.printSuccessFuction(_this2.state.ReportKey); } else { props.printSuccessFuction(_this2.state.FastDefaultPrinter.raporKey); } } }, function (error) { _this2.setState({ loadingFile: false }); }); }); _defineProperty(_assertThisInitialized(_this2), "onHandlePrinterRefresh", function (event, setLoadingExternal) { var _this = _assertThisInitialized(_this2); _this2.getPrinters(function (detectedPrinters) { _this.setState({ Printers: detectedPrinters }); _this.pushPrinters(detectedPrinters); localStorage.setItem("printers", detectedPrinters); localStorage.setItem("selectedPrintDestination", "PDF"); setLoadingExternal(); }); }); _defineProperty(_assertThisInitialized(_this2), "getIndexFromData", function (value, arr, prop) { if (arr == "undefined") return; for (var i = 0; i < arr.length; i++) { if (arr[i][prop] == value) { return arr[i]; } } }); _defineProperty(_assertThisInitialized(_this2), "onHandleChangeReport", function (event) { var rapor = _this2.getIndexFromData(event.target.value, _this2.state.Reports, "raporKod"); _this2.setState({ ReportName: rapor["raporAd"], ReportKod: rapor["raporKod"], ReportKey: rapor["raporKey"] }); }); _this2.state = { ReportName: "", ReportKod: "0", ReportKey: "", Modul: _props.Modul, UserName: localStorage.getItem("user_name"), UserId: localStorage.getItem("user_id"), Parameters: _props.Parameters, // ReportUrl: "http://k8s.usta.com.tr:31080/raporservis/api/data/print", // ReportDataUrl: "http://k8s.usta.com.tr:31080/raporservis/api/data/getrapormodul", ReportUrl: "/raporservis/api/data/print", ReportDataUrl: "/raporservis/api/data/getrapormodul", Reports: [], Printers: [], PrintDestinationContainer: [[1, "file", /*#__PURE__*/_react["default"].createElement("i", { className: "fas fa-file-excel" }), "XLS"], [2, "file", /*#__PURE__*/_react["default"].createElement("i", { className: "fas fa-file-csv" }), "DOC"], [3, "file", /*#__PURE__*/_react["default"].createElement("i", { className: "fas fa-file-word" }), "DOCX"], [4, "file", /*#__PURE__*/_react["default"].createElement("i", { className: "far fa-file" }), "RTF"], [5, "file", /*#__PURE__*/_react["default"].createElement("i", { className: "fas fa-file-pdf" }), "PDF"], [6, "file", /*#__PURE__*/_react["default"].createElement("i", { className: "far fa-image" }), "IMAGE"], [7, "none", /*#__PURE__*/_react["default"].createElement("hr", null), "HR"]], File: null, PrinterName: null, PrinterTrayName: "", PrinterPaperName: "", loadingFile: false, JSPM_WS_Status: null, printersInfo: null, FastDefaultPrinter: { raporKod: null, raporName: "", raporKey: "" } }; _this2.OS = ""; _this2.locale = _props.locale ? _props.locale : "tr"; return _this2; } _createClass(PrintInline, [{ key: "componentDidMount", value: function componentDidMount() { var _this = this; if (_this.props.FastDefaultPrinter) { _this.setState({ FastDefaultPrinter: { raporKod: _this.props.FastDefaultPrinter.raporKod, raporName: _this.props.FastDefaultPrinter.raporName, raporKey: _this.props.FastDefaultPrinter.raporKey } }); } var printers = localStorage.getItem("printers"); this.setSelectedPrinter(localStorage.getItem("selectedPrintDestination")); if (printers == null) { this.getPrinters(function (detectedPrinters) { _this.setState({ Printers: detectedPrinters }); _this.pushPrinters(detectedPrinters); localStorage.setItem("printers", detectedPrinters); }); } else { this.pushPrinters(printers.split(",")); } var os = function () { var ua = navigator.userAgent.toLowerCase(); return { isWindows: /(windows|win32|win64)/.test(ua), isLinux: /(linux)/.test(ua), isIntelMac: /(intel mac)/.test(ua), isRPi: /(Linux arm)/.test(ua) }; }(); if (os.isWindows) { this.OS = "win"; } else if (os.isLinux) { this.OS = "linux"; } else if (os.isIntelMac) { this.OS = "mac"; } else if (os.isRPi) { this.OS = "rpi"; } else { this.OS = "other"; } this.getReports(); } }, { key: "render", value: function render() { return /*#__PURE__*/_react["default"].createElement(_react.Fragment, null, /*#__PURE__*/_react["default"].createElement("div", { style: { display: "inline-block", marginRight: 10, height: "15px" } }, this.state.loadingFile ? /*#__PURE__*/_react["default"].createElement(_CircularProgress["default"], null) : ""), this.state.FastDefaultPrinter && !this.state.FastDefaultPrinter.raporKod && /*#__PURE__*/_react["default"].createElement("div", { style: { display: "inline-block", marginRight: 10, marginTop: 10 } }, /*#__PURE__*/_react["default"].createElement(_Select["default"], { id: "rapor-select-" + String(this.state.Modul), onChange: this.onHandleChangeReport, value: String(this.state.ReportKod), style: { height: "40px" } }, this.state.Reports.map(function (rapor) { return /*#__PURE__*/_react["default"].createElement(_MenuItem["default"], { value: String(rapor["raporKod"]), key: rapor["raporModulKod"] }, rapor["raporAd"]); }))), /*#__PURE__*/_react["default"].createElement("div", { style: { display: "inline-block", marginRight: 10 } }, /*#__PURE__*/_react["default"].createElement(_PrintInlineComponent["default"], { status: this.state.JSPM_WS_Status, reportName: this.state.ReportName, options: _toConsumableArray(this.state.PrintDestinationContainer), onHandlePrintChange: this.onHandlePrinterChange, onHandlePrint: this.onHandlePrint, onHandleDownload: this.onHandleDownload, selected: this.getSelectedPrinter(), onHandlePrinterRefresh: this.onHandlePrinterRefresh, locale: this.locale }))); } }]); return PrintInline; }(_react["default"].Component); var _default = PrintInline; exports["default"] = _default;