UNPKG

ui5_easy_use

Version:

CLI tool for SAP ui5 and SAPUI5 projects to initialize apps, generate pages, insert form and table components, manage routing, and automate i18n bindings

116 lines (96 loc) 4.15 kB
sap.ui.define([ "sap/ui/core/UIComponent", "sap/ui/model/json/JSONModel", "${ez5.appName}/model/models", "${ez5.appName}/${ez5.packgName}/i18n/Language", "${ez5.appName}/${ez5.packgName}/Helper/Env", "${ez5.appName}/${ez5.packgName}/initapp/LoadJson", "${ez5.appName}/${ez5.packgName}/auth/AuthService", "${ez5.appName}/${ez5.packgName}/auth/AccessControl", "${ez5.appName}/${ez5.packgName}/auth/UserModel", "${ez5.appName}/${ez5.packgName}/auth/PagesAccess" ], function (UIComponent, JSONModel, models, Language, Env, LoadJson, AuthService, AccessControl, UserModel, PagesAccess) { "use strict"; return UIComponent.extend("${ez5.appName}.Component", { metadata: { manifest: "json", interfaces: [ "sap.ui.core.IAsyncContentCreation" ] }, init() { UIComponent.prototype.init.apply(this, arguments); this.setModel(models.createDeviceModel(), "device"); this._initializeEnvironment(); this._initAsync(); }, async _initAsync() { const { modelsJson } = await this._loadBaseModels(); const isAuthenticated = await this._initializeAuth(); if (!isAuthenticated || !this._canOpenApplication()) { return; } this._applyAuthorizedNavigation(); await this._initializeLanguage(modelsJson); this.getRouter().initialize(); }, // Keep environment values in one place so generated apps can switch dev/qty/prd behavior safely. _initializeEnvironment: function () { const env = new Env(this); this.env = env.init(); this.setModel(new JSONModel(this.env), "env"); }, _loadBaseModels: async function () { const modelNames = ["navList", "rulesNavList", "localData"]; const loadJson = new LoadJson(this); const { modelsJson, modelsData } = await loadJson.getModel(modelNames); return { modelNames, modelsJson, modelsData }; }, _initializeAuth: async function () { this.userId = this.getUserIdFromUshell() || this.env.userInfo.userId; this.appId = this.env.appId; this.userModel = new UserModel(this); this.authService = new AuthService(this); this.accessControl = new AccessControl(this); this.pagesAccess = new PagesAccess(this); const success = await this.authService.login({ username: this.userId, password: this.appId }); if (!success) { return false; } this.setModel(new JSONModel(this.authService.getUser()), "userInfo"); return true; }, _canOpenApplication: function () { return this.accessControl.canAccess(["normal", "admin"]); }, _applyAuthorizedNavigation: function () { this.pagesAccess.setAuthorizedPages(); const filteredNavs = this.pagesAccess.filterNavigation(); const navListModel = this.getModel("navList"); if (navListModel) { navListModel.setProperty("/navigation", filteredNavs.navigation); } this.getRouter().attachRouteMatched(this.pagesAccess._onRouteMatched, this.pagesAccess); }, _initializeLanguage: async function (modelsJson) { this.language = new Language(this); await this.language.init(); this.language.replaceModelsJson(modelsJson); }, onChangeLanguage: function () { return this.language.onChangeLanguage(); }, getUserIdFromUshell: function () { const userInfoService = sap.ushell?.Container?.getService("UserInfo"); const userEmail = userInfoService ? userInfoService.getEmail() : ""; return userEmail?.includes("@") ? userEmail.split("@")[0] : userEmail; } }); });