service-utilities
Version:
Utility Package for FIORI UI5
213 lines (198 loc) • 7.22 kB
JavaScript
/**
* @module BaseController
* @description Base Controller for FIORI UI5 Projects
* @author jpanti
* @version 1.0.0
* @created 2025-08-01
* @lastModified 2025-08-01
* @license ISC
*/
sap.ui.define(
[
"sap/ui/core/mvc/Controller",
"./RouterExpansion",
"./FactoryModelResource",
"./MessageExpansion",
"./FragmentController",
],
(
Controller,
RouterExpansion,
FactoryModelResource,
MessageExpansion,
FragmentController
) => {
"use strict";
return Controller.extend("service.BaseController", {
// ======================================================================================================
// PRIVATE IMPORTANT VARIABLES
_oModelResource: undefined,
_oRouter: undefined,
getModelResource() {
return this._oModelResource;
},
getRouter() {
return this._oRouter;
},
// ======================================================================================================
// ======================================================================================================
// OVERRIDABLES
oModelData: {},
sFallbackRouteName: "",
oFallbackRouteDetails: {},
sRouteName: "",
async onRouteValidator(hashSplit) {
return true;
},
aFragmentControls: [],
// ======================================================================================================
// ======================================================================================================
// UTILITIES
getController() {
return this;
},
getFullname() {
return this.getMetadata().getName();
},
getControllerName() {
return this.getFullname().split(".").pop();
},
getNamespace() {
return this.getFullname().split(".").slice(0, -1).join(".");
},
// ======================================================================================================
// ======================================================================================================
// MODEL RESOURCE
_oBaseModel: {
initialized: false,
navigating: true,
},
getBaseModelResource() {
return {
...this._oBaseModel,
...this.oModelData,
};
},
onInitModelResource() {
this._oModelResource = new FactoryModelResource(
this.getController(),
"modelData",
this.getBaseModelResource()
);
this.getModelResource().setProperties({
initialized: false,
navigating: true,
});
MessageExpansion.logger("Model Resource Initialized...");
},
// ======================================================================================================
// ======================================================================================================
// ROUTER-BASED METHODS
onInitRouter() {
this._oRouter = new RouterExpansion(this.getController());
// View Mounting
this.getRouter()
.getEventRadio()
.listen(this.getMountChannel(), () => {
this.getModelResource().set("navigating", true);
this.onMounted();
this.getModelResource().set("navigating", false);
});
this.getRouter().attachRouteMatched(() =>
this.getRouter().getEventRadio().transmit(this.getMountChannel())
);
// View Unmounting
this.getRouter()
.getEventRadio()
.listen(this.getUnmountChannel(), () => this.onUnmounted());
this.getRouter().attachBeforeRouteMatched(() =>
this.getRouter().getEventRadio().transmit(this.getUnmountChannel())
);
MessageExpansion.logger("Router Initialized...");
},
onRouteTo(sRouteName, oDetails) {
this.getModelResource().set("navigating", true);
this.getRouter().goTo(sRouteName, oDetails);
},
onRouteBack(
sRouteName = this.sFallbackRouteName,
oDetails = this.oFallbackRouteDetails
) {
this.getModelResource().set("navigating", true);
this.getRouter().backTo(sRouteName, oDetails);
},
getMountChannel() {
return `mount-${this.sRouteName}`;
},
getUnmountChannel() {
return `unmount-${this.sRouteName}`;
},
// ======================================================================================================
// ======================================================================================================
// LIFE CYCLE METHODS
// Initialization (One-time) ------------------------
onInit() {
// dependencies
this.onInitModelResource();
this.onInitRouter();
this._onInit();
},
async _onInit() {
// Pre-Initialization (Before Routing Validations and Dependency Initializations)
await this.onStart();
await FragmentController.onStart(
this.getNamespace(),
this.getController(),
this.aFragmentControls
);
MessageExpansion.logger("Start Initialization Done...");
const bValidated = await this.onRouteValidate();
MessageExpansion.logger(
`Router Validation ${bValidated ? "Success" : "Failed"}...`
);
if (!bValidated) return;
// Post-Initialization (After Routing Validations and Dependency Initializations)
await this.onAwake();
await FragmentController.onAwake();
MessageExpansion.logger("Awake Initialization Done...");
await this.onMounted(); // first render update
this.getModelResource().set("initialized", true);
MessageExpansion.logger("Initialization Completed...");
},
async onStart() {},
async onAwake() {},
async onRouteValidate() {
var hash = window.location.hash;
const hashSplit = hash.split("/");
const bValidated = await this.onRouteValidator(hashSplit);
if (!bValidated) this.getRouter().goTo("");
return bValidated;
},
// --------------------------------------------------
// Rendering (every before and after view refresh) --
onBeforeRendering() {
this.onPreUpdate();
FragmentController.onPreUpdate();
},
onAfterRendering() {
this.onPostUpdate();
FragmentController.onPostUpdate();
},
async onPreUpdate() {},
async onPostUpdate() {},
// --------------------------------------------------
// Routing (One-time per view route) ----------------
async onMounted() {},
async onUnmounted() {},
// --------------------------------------------------
// Cleaning (One-time) ------------------------------
onExit() {
FragmentController.onDestroy();
this.onDestroy();
},
onDestroy() {},
// --------------------------------------------------
// ======================================================================================================
});
}
);