Skip to content

Instantly share code, notes, and snippets.

@Hologos
Created October 7, 2020 08:46
Show Gist options
  • Save Hologos/56071bc83fc48d4cc60b1cfcfff0204a to your computer and use it in GitHub Desktop.
Save Hologos/56071bc83fc48d4cc60b1cfcfff0204a to your computer and use it in GitHub Desktop.
(function() {
let _shadowRoot;
let _id;
let _password;
let tmpl = document.createElement("template");
tmpl.innerHTML = `
<style>
</style>
<div id="ui5_content" name="ui5_content">
<slot name="content"></slot>
</div>
<script id="oView" name="oView" type="sapui5/xmlview">
<mvc:View
controllerName="myView.Template"
xmlns:l="sap.ui.layout"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m">
<l:VerticalLayout
class="sapUiContentPadding"
width="100%">
<l:content>
<Input
id="passwordInput"
type="Password"
placeholder="Enter password ..." liveChange="onButtonPress"/>
</l:content>
</l:VerticalLayout>
</mvc:View>
</script>
`;
class InputPassword extends HTMLElement {
constructor() {
super();
_shadowRoot = this.attachShadow({
mode: "open"
});
_shadowRoot.appendChild(tmpl.content.cloneNode(true));
_id = createGuid();
_shadowRoot.querySelector("#oView").id = _id + "_oView";
this._export_settings = {};
this._export_settings.password = "";
this.addEventListener("click", event => {
console.log('click');
});
}
connectedCallback() {
try {
if (window.commonApp) {
let outlineContainer = commonApp.getShell().findElements(true, ele => ele.hasStyleClass && ele.hasStyleClass("sapAppBuildingOutline"))[0]; // sId: "__container0"
if (outlineContainer && outlineContainer.getReactProps) {
let parseReactState = state => {
let components = {};
let globalState = state.globalState;
let instances = globalState.instances;
let app = instances.app["[{\"app\":\"MAIN_APPLICATION\"}]"];
let names = app.names;
for (let key in names) {
let name = names[key];
let obj = JSON.parse(key).pop();
let type = Object.keys(obj)[0];
let id = obj[type];
components[id] = {
type: type,
name: name
};
}
for (let componentId in components) {
let component = components[componentId];
}
let metadata = JSON.stringify({
components: components,
vars: app.globalVars
});
if (metadata != this.metadata) {
this.metadata = metadata;
this.dispatchEvent(new CustomEvent("propertiesChanged", {
detail: {
properties: {
metadata: metadata
}
}
}));
}
};
let subscribeReactStore = store => {
this._subscription = store.subscribe({
effect: state => {
parseReactState(state);
return {
result: 1
};
}
});
};
let props = outlineContainer.getReactProps();
if (props) {
subscribeReactStore(props.store);
} else {
let oldRenderReactComponent = outlineContainer.renderReactComponent;
outlineContainer.renderReactComponent = e => {
let props = outlineContainer.getReactProps();
subscribeReactStore(props.store);
oldRenderReactComponent.call(outlineContainer, e);
}
}
}
}
} catch (e) {}
}
disconnectedCallback() {
if (this._subscription) {
this._subscription();
this._subscription = null;
}
}
onCustomWidgetBeforeUpdate(changedProperties) {
if ("designMode" in changedProperties) {
this._designMode = changedProperties["designMode"];
}
}
onCustomWidgetAfterUpdate(changedProperties) {
loadthis(this);
}
_firePropertiesChanged() {
this.password = "";
this.dispatchEvent(new CustomEvent("propertiesChanged", {
detail: {
properties: {
password: this.password
}
}
}));
}
// SETTINGS
get password() {
return this._export_settings.password;
}
set password(value) {
value = _password;
this._export_settings.password = value;
}
static get observedAttributes() {
return [
"password"
];
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue != newValue) {
this[name] = newValue;
}
}
}
customElements.define("com-fd-djaja-sap-sac-inputpassword", InputPassword);
// UTILS
function loadthis(that) {
var that_ = that;
let content = document.createElement('div');
content.slot = "content";
that_.appendChild(content);
sap.ui.getCore().attachInit(function() {
"use strict";
//### Controller ###
sap.ui.define([
"jquery.sap.global",
"sap/ui/core/mvc/Controller"
], function(jQuery, Controller) {
"use strict";
return Controller.extend("myView.Template", {
onButtonPress: function(oEvent) {
_password = oView.byId("passwordInput").getValue();
that._firePropertiesChanged();
console.log(_password);
this.settings = {};
this.settings.password = "";
that.dispatchEvent(new CustomEvent("onStart", {
detail: {
settings: this.settings
}
}));
}
});
});
//### THE APP: place the XMLView somewhere into DOM ###
var oView = sap.ui.xmlview({
viewContent: jQuery(_shadowRoot.getElementById(_id + "_oView")).html(),
});
oView.placeAt(content);
if (that_._designMode) {
oView.byId("passwordInput").setEnabled(false);
}
});
}
function createGuid() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, c => {
let r = Math.random() * 16 | 0,
v = c === "x" ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment