Skip to content

Instantly share code, notes, and snippets.

@strawherotk
Created July 20, 2017 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strawherotk/ee3f056567e1998aa89c22d0a4e350bb to your computer and use it in GitHub Desktop.
Save strawherotk/ee3f056567e1998aa89c22d0a4e350bb to your computer and use it in GitHub Desktop.
BBraun Code snippets
/*global history */
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/core/routing/History"
], function(Controller, History) {
"use strict";
return Controller.extend("com.bbraun.app.safetymaintenance.bbraunProcessSteps.controller.BaseController", {
/**
* Customize Show Change Log button to be on the right side
*/
onInit: function() {
try {
// Customize show change log
var sChangeLog = this.getResourceBundle().getText("Showchangelog");
this.getView().byId("ChangeLogMarker").addEventDelegate({
"onAfterRendering": function() {
var oMarker = $("#" + this.getView().byId("ChangeLogMarker").sId);
oMarker[0].childNodes[0].innerText = sChangeLog;
}
}, this);
} catch (exc) {
jQuery.sap.log.info(exc);
}
},
/*
* Trigger when click on 'show change log'
* Open Popover to display "Created" & "last modified" User and Time.
* @param oEvent
*/
onShowChangeLogPress: function(oEvent) {
if (!this._oPopover) {
this._oPopover = sap.ui.xmlfragment("com.bbraun.app.safetymaintenance.bbraunProcessSteps.fragment.ShowChangeLogPopover",
this);
this.getView().addDependent(this._oPopover);
}
this._oPopover.openBy(oEvent.getSource());
},
/**
* Convenience method for accessing the router in every controller of the application.
* @public
* @returns {sap.ui.core.routing.Router} the router for this component
*/
getRouter: function() {
return this.getOwnerComponent().getRouter();
},
/**
* Convenience method for getting the view model by name in every controller of the application.
* @public
* @param {string} sName the model name
* @returns {sap.ui.model.Model} the model instance
*/
getModel: function(sName) {
return this.getView().getModel(sName);
},
/**
* Convenience method for setting the view model in every controller of the application.
* @public
* @param {sap.ui.model.Model} oModel the model instance
* @param {string} sName the model name
* @returns {sap.ui.mvc.View} the view instance
*/
setModel: function(oModel, sName) {
return this.getView().setModel(oModel, sName);
},
/**
* Convenience method for getting the resource bundle.
* @public
* @returns {sap.ui.model.resource.ResourceModel} the resourceModel of the component
*/
getResourceBundle: function() {
return this.getOwnerComponent().getModel("i18n").getResourceBundle();
},
/**
* Event handler for navigating back.
* It checks if there is a history entry. If yes, history.go(-1) will happen.
* If not, it will replace the current entry of the browser history with the master route.
* @public
*/
onNavBack: function() {
var sPreviousHash = History.getInstance().getPreviousHash();
if (sPreviousHash !== undefined) {
// The history contains a previous entry
history.go(-1);
} else {
// Otherwise we go backwards with a forward history
var bReplace = true;
this.getRouter().navTo("master", {}, bReplace);
}
},
/**
* Therapy is updated by XS JS server, not ODATA, so the link to server is returen by this function.
* @returns {String} URL of SXJS server
*/
_getUpdateURL: function(sObject) {
// If sObject is not defined, return default Odata service url
if (!sObject) {
return "/app_safety_app/i1/xs/odata/process/PROC_ANALYSIS_SERVICES.xsodata/";
}
if (sObject.localeCompare("processStep") === 0) {
return "/app_safety_app/i1/xs/xsjs/process/XS_MAINTAIN_PROC_STEP.xsjs";
}
return "/app_safety_app/i1/xs/odata/process/PROC_ANALYSIS_SERVICES.xsodata/";
},
/**
* Get user logged into fiori lauchpad
* @returns {String} user Id of logged in user
*/
_getUserId: function() {
if (sap.ushell) {
return sap.ushell.Container.getUser().getId();
} else {
sap.m.MessageBox.error(
"Cannot get user Id from Fiori launchpad Component. User 'testUser' will be used for any data modification.", {
title: "Get User Error"
});
return "testUser";
}
},
/**
* Shows a {@link sap.m.MessageBox}.
* The user can try to refresh the metadata.
* @param {string} sTitle the MessageBox title, {string} sMessageHeader and {string} sDetails a technical error to be displayed on request
* @private
*/
_showErrorMessage: function(sErrorDetails) {
var sTitle = this.getResourceBundle().getText("errorTitle"),
sText = this.getResourceBundle().getText("errorText");
this.getOwnerComponent()._oErrorHandler._showErrorMsg(sTitle, sText, sErrorDetails);
},
/**
* Clear all data in all inputs field in the form, which in the content of oDialog.
* @param {sap.m.Dialog} oDialog
*/
_clearForm: function(oForm) {
var oForms = oForm.getContent();
for (var i = 1; i < oForms.length; i++) {
if (oForms[i].getMetadata().getElementName().includes("Input")) {
// set value
oForms[i].setValue("");
oForms[i].setValueState("None");
}
}
},
/**
* Return Binding path of item to be selected in master List
* @param {String} sObjectId - Id of Object
* @returns {String} Binding path of item to be selected in master List
*/
_createObjectPath: function(sObjectId) {
var oKeys = {
IP_LANGU: "E",
IP_COUNTRY: "01",
IP_PIC_ID: 0,
PROC_STEP_ID: sObjectId
};
var sObjectPath = this.getModel().createKey("/ProcessSteps", oKeys);
return sObjectPath;
},
/**
* return true if user's input in user's inputs meet validation requirement.
* @constructor
* @returns {Boolean} return true if user's inputs meet validation requirement.
*/
_validateFormInputsAreOK: function(oForm) {
var aInputs = this._getFormInputs(oForm);
// var oNameInput = this.getView().byId("ipDesc");
// aInputs.push(oNameInput);
var bValidateOK = true;
for (var i = 0; i < aInputs.length; i++) {
if (this._blankValidation(aInputs[i]) === "Error") {
bValidateOK = false;
}
}
return bValidateOK;
},
/**
* check if the Input is blank, highlight blank Input
* @param {sap.m.Input} oInput - Input control
* @returns {sap.ui.core.ValueState} error state of the input control
*/
_blankValidation: function(oInput) {
// var oInput = oEvent.getSource();
if (oInput.getValue() === "") {
oInput.setValueState(sap.ui.core.ValueState.Error); // if the enterDatafield is empty after change, it will go red
oInput.setPlaceholder(this.getResourceBundle().getText("pleaseEnterData"));
return sap.ui.core.ValueState.Error;
} else {
oInput.setValueState(sap.ui.core.ValueState.None); // if the field is not empty after change, the value state (if any) is removed
return sap.ui.core.ValueState.None;
}
},
/**
* Return as a array containing all input fields of oForm
* @param {sap.ui.layout.form.SimpleForm} oForm
* @returns {array} all input fields of oForm as a array
*/
_getFormInputs: function(oForm) {
var aInputs = [];
var oForms = oForm.getContent();
for (var i = 1; i < oForms.length; i++) {
if (oForms[i].getMetadata().getElementName().includes("Input")) {
aInputs.push(oForms[i]);
}
}
return aInputs;
},
/**
* Returns the registered element for the given id, if any.
* the principle to get control by id follows SAP Id defination principle,
* The element need to be to registered to the controller which call this function.
* The registration can be done by passing the 'this' controller when the element is created.
* Element with id generated with the View is not registered to the controller,
* so this function will not work.
* @private
* @param {string} sId
* @returns {sap.core.control} The control that identified by sId
*/
_byId: function(sId) {
return sap.ui.getCore().byId(sId);
}
});
});
@darkreddditt
Copy link

wanna start project that will pay contact me Darkreditt@outlook.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment