|
| 1 | +sap.ui.define(["sap/ui/core/Control", "sap/ui/core/Fragment", "sap/ui/model/json/JSONModel"], (Control, Fragment, JSONModel) => { |
| 2 | + "use strict"; |
| 3 | + |
| 4 | + return Control.extend("z2ui5.cc.DebugTool", { |
| 5 | + |
| 6 | + //printer XML |
| 7 | + prettifyXml: function (sourceXml) { |
| 8 | + const xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml'); |
| 9 | + var sParse = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 10 | + <xsl:strip-space elements="*" /> |
| 11 | + <xsl:template match="para[content-style][not(text())]"> |
| 12 | + <xsl:value-of select="normalize-space(.)" /> |
| 13 | + </xsl:template> |
| 14 | + <xsl:template match="node()|@*"> |
| 15 | + <xsl:copy> |
| 16 | + <xsl:apply-templates select="node()|@*" /> |
| 17 | + </xsl:copy> |
| 18 | + </xsl:template> |
| 19 | + <xsl:output indent="yes" /> |
| 20 | + </xsl:stylesheet>`; |
| 21 | + sParse = sParse.replace(/>/g, unescape("%3E")).replace(/</g, unescape("%3C")); |
| 22 | + const xsltDoc = new DOMParser().parseFromString(sParse, 'application/xml'); |
| 23 | + |
| 24 | + const xsltProcessor = new XSLTProcessor(); |
| 25 | + xsltProcessor.importStylesheet(xsltDoc); |
| 26 | + const resultDoc = xsltProcessor.transformToDocument(xmlDoc); |
| 27 | + const resultXml = new XMLSerializer().serializeToString(resultDoc); |
| 28 | + return resultXml.replace(/>/g, ">").replace(/</g, "<"); |
| 29 | + }, onItemSelect: function (oEvent) { |
| 30 | + const selItem = oEvent.getSource().getSelectedKey(); |
| 31 | + const oView = z2ui5?.oView; |
| 32 | + const oResponse = z2ui5?.oResponse; |
| 33 | + const displayEditor = this.displayEditor.bind(this); |
| 34 | + |
| 35 | + switch (selItem) { |
| 36 | + case 'CONFIG': |
| 37 | + displayEditor(oEvent, JSON.stringify(z2ui5.oConfig, null, 3), 'json'); |
| 38 | + break; |
| 39 | + case 'MODEL': |
| 40 | + displayEditor(oEvent, JSON.stringify(oView?.getModel()?.getData(), null, 3), 'json'); |
| 41 | + break; |
| 42 | + case 'VIEW': |
| 43 | + const viewContent = oView?.mProperties?.viewContent || z2ui5.responseData.S_FRONT.PARAMS.S_VIEW.XML; |
| 44 | + displayEditor(oEvent, this.prettifyXml(viewContent), 'xml', this.prettifyXml(oView?._xContent.outerHTML)); |
| 45 | + break; |
| 46 | + case 'PLAIN': |
| 47 | + displayEditor(oEvent, JSON.stringify(z2ui5.responseData, null, 3), 'json'); |
| 48 | + break; |
| 49 | + case 'REQUEST': |
| 50 | + displayEditor(oEvent, JSON.stringify(z2ui5.oBody, null, 3), 'json'); |
| 51 | + break; |
| 52 | + case 'POPUP': |
| 53 | + displayEditor(oEvent, this.prettifyXml(oResponse?.PARAMS?.S_POPUP?.XML), 'xml'); |
| 54 | + break; |
| 55 | + case 'POPUP_MODEL': |
| 56 | + displayEditor(oEvent, JSON.stringify(z2ui5.oViewPopup.getModel().getData(), null, 3), 'json'); |
| 57 | + break; |
| 58 | + case 'POPOVER': |
| 59 | + displayEditor(oEvent, this.prettifyXml(oResponse?.PARAMS?.S_POPOVER?.XML), 'xml'); |
| 60 | + break; |
| 61 | + case 'POPOVER_MODEL': |
| 62 | + displayEditor(oEvent, JSON.stringify(z2ui5?.oViewPopover?.getModel()?.getData(), null, 3), 'json'); |
| 63 | + break; |
| 64 | + case 'NEST1': |
| 65 | + displayEditor(oEvent, this.prettifyXml(z2ui5?.oViewNest?.mProperties?.viewContent), 'xml', this.prettifyXml(z2ui5?.oViewNest?._xContent.outerHTML)); |
| 66 | + break; |
| 67 | + case 'NEST1_MODEL': |
| 68 | + displayEditor(oEvent, JSON.stringify(z2ui5?.oViewNest?.getModel()?.getData(), null, 3), 'json'); |
| 69 | + break; |
| 70 | + case 'NEST2': |
| 71 | + displayEditor(oEvent, this.prettifyXml(z2ui5?.oViewNest2?.mProperties?.viewContent), 'xml', this.prettifyXml(z2ui5?.oViewNest2?._xContent.outerHTML)); |
| 72 | + break; |
| 73 | + case 'NEST2_MODEL': |
| 74 | + displayEditor(oEvent, JSON.stringify(z2ui5?.oViewNest2?.getModel()?.getData(), null, 3), 'json'); |
| 75 | + break; |
| 76 | + case 'SOURCE': |
| 77 | + const parent = oEvent.getSource().getParent(); |
| 78 | + const contentControl = parent.getContent()[2].getItems()[0]; |
| 79 | + const url = `${window.location.origin}/sap/bc/adt/oo/classes/${z2ui5.responseData.S_FRONT.APP}/source/main`; |
| 80 | + const content = atob('PGlmcmFtZSBpZD0idGVzdCIgc3JjPSInICsgdXJsICsgJyIgaGVpZ2h0PSI4MDBweCIgd2lkdGg9IjEyMDBweCIgLz4=').replace("' + url + '", url); |
| 81 | + contentControl.setProperty("content", content); |
| 82 | + const modelData = oEvent.getSource().getModel().oData; |
| 83 | + modelData.editor_visible = false; |
| 84 | + modelData.source_visible = true; |
| 85 | + oEvent.getSource().getModel().refresh(); |
| 86 | + break; |
| 87 | + } |
| 88 | + }, |
| 89 | + |
| 90 | + displayEditor: function (oEvent, content, type, xcontent = "") { |
| 91 | + const modelData = oEvent.getSource().getModel().oData; |
| 92 | + modelData.editor_visible = true; |
| 93 | + modelData.source_visible = false; |
| 94 | + modelData.isTemplating = content.includes("xmlns:template"); |
| 95 | + modelData.value = content; |
| 96 | + modelData.previousValue = content; |
| 97 | + modelData.xContent = xcontent; |
| 98 | + modelData.type = type; |
| 99 | + oEvent.getSource().getModel().refresh(); |
| 100 | + }, |
| 101 | + |
| 102 | + onTemplatingPress: function (oEvent) { |
| 103 | + const modelData = oEvent.getSource().getModel().oData; |
| 104 | + modelData.value = oEvent.getSource().getPressed() ? modelData.xContent : modelData.previousValue; |
| 105 | + oEvent.getSource().getModel().refresh(); |
| 106 | + }, |
| 107 | + |
| 108 | + onClose: function () { |
| 109 | + this.close(); |
| 110 | + }, |
| 111 | + |
| 112 | + async show() { |
| 113 | + if (!this.oDialog) { |
| 114 | + this.oDialog = await Fragment.load({ |
| 115 | + name: "z2ui5.cc.DebugTool", |
| 116 | + controller: this, |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + const value = JSON.stringify(z2ui5.responseData, null, 3); |
| 121 | + const oData = { |
| 122 | + type: 'json', |
| 123 | + source_visible: false, |
| 124 | + editor_visible: true, |
| 125 | + value: value, |
| 126 | + xContent: '', |
| 127 | + previousValue: value, |
| 128 | + isTemplating: false, |
| 129 | + templatingSource: false, |
| 130 | + activeNest1: z2ui5?.oViewNest?.mProperties?.viewContent !== undefined, |
| 131 | + activeNest2: z2ui5?.oViewNest2?.mProperties?.viewContent !== undefined, |
| 132 | + activePopup: z2ui5?.oResponse?.PARAMS?.S_POPUP?.XML !== undefined, |
| 133 | + activePopover: z2ui5?.oResponse?.PARAMS?.S_POPOVER?.XML !== undefined, |
| 134 | + }; |
| 135 | + const oModel = new JSONModel(oData); |
| 136 | + |
| 137 | + this.oDialog.addStyleClass('dbg-ltr'); |
| 138 | + this.oDialog.setModel(oModel); |
| 139 | + this.oDialog.open(); |
| 140 | + }, |
| 141 | + |
| 142 | + async close(){ |
| 143 | + if (this.oDialog){ |
| 144 | + this.oDialog.close(); |
| 145 | + this.oDialog.destroy(); |
| 146 | + this.oDialog = null; |
| 147 | + } |
| 148 | + }, |
| 149 | + |
| 150 | + async toggle(){ |
| 151 | + if (this.oDialog){ |
| 152 | + this.close() |
| 153 | + } else { |
| 154 | + this.show() |
| 155 | + } |
| 156 | + }, |
| 157 | + |
| 158 | + renderer(){ |
| 159 | + } |
| 160 | + }); |
| 161 | +}); |
0 commit comments