diff --git a/README.md b/README.md index bfe12e9f..e5205aef 100644 --- a/README.md +++ b/README.md @@ -2,64 +2,11 @@ # SAP CodeJam - UI5 -This repository contains the material for SAP CodeJam events on UI5. +This branch of the repository contains [instructions](/instructions.md) on how to improve the performance of the bookshop application that can be found in the [main branch](https://github.com/SAP-samples/ui5-exercises-codejam/tree/main). -Please check the [prerequisites](/prerequisites.md) before the event an make sure you meet them. +Furthermore, this branch was used for the corresponding session at Devtoberfest 2022: [Improving the Performance of UI5 Applications](https://groups.community.sap.com/t5/devtoberfest/improving-the-performance-of-ui5-applications/ec-p/9012#M42) -## Overview - -The material in this repository introduces you to the core principles of UI5, an enterprise-ready web development framework used to build apps that follow the Fiori design guidelines. This repository is a step-by-step guide explaining how to build a frontend web application using UI5. The finished app is a bookshop app, where users can browse and order books. The app sits on top of the well-known [bookshop](https://github.com/SAP-samples/cloud-cap-samples/tree/main/bookshop) backend application built with the Node.js flavour of the [SAP Cloud Application Programming Model (CAP)](https://cap.cloud.sap/docs/). - -![The finished app](/finished-app.png) - -The finished UI5 bookshop app already exists in the [bookshop/app/finished-webapp](/bookshop/app/finished-webapp/) directory, but we want to rebuild it from scratch step by step. You can compare the finished app with your version in case you have issues along the way. - -After reading all chapters and following the instructions, you will be able to build your own UI5 applications leveraging the official [SAPUI5 API Reference](https://sapui5.hana.ondemand.com/#/api). - -## Previous Knowledge - -The material in this repository aims to be beginner friendly. If you have never built a (UI5) web app before, you will still be able to follow along. No prior knowledge is required, although it certainly helps to have experience in (web) development. - -The material includes additional explanations in collapsable sections (see example below), whenever a concept is used that web developers are probably already familiar with, but beginners might not be. You can decide for yourself whether you want to read or skip them. - -See this example: - -
-What is SAPUI5? 💬 - -
- -> SAPUI5 is an HTML5 framework for creating cross-platform, enterprise-grade web applications in an efficient way. -> -> See this [blog post](https://blogs.sap.com/2021/08/23/what-is-sapui5/) for more information. - -
- -## Material Organization - -The material consists of a series of chapters. The chapters build on top of each other and are meant to be completed in the given order. Each of the [chapters](/chapters/) has its own 'readme' file with explanations, instructions, code samples and screen shots. - -## Chapters - -0. [Preparing the Development Environment](/chapters/chapter00) -1. [Scaffolding the App - Our First View](/chapters/chapter01) -1. [Creating and Consuming Our First Model](/chapters/chapter02) -1. [Creating and Extending Our First Controller](/chapters/chapter03) -1. [Adding an 'Order' Feature to Our Bookshop](/chapters/chapter04) -1. [Adding a 'Search' Feature to Our Bookshop](/chapters/chapter05) -1. [Adding Custom Formatting](/chapters/chapter06) -1. [Adding i18n Features](/chapters/chapter07) -1. [Adding Custom CSS](/chapters/chapter08) -1. [Deploying Our App](/chapters/chapter09) (Optional) -1. [Further Improvements and Learning Material](/chapters/chapter10) - -## SAPUI5 vs. OpenUI5 - -You will often read about either SAPUI5 or OpenUI5 when working with the framework. The main difference between the two is the license. Whereas SAPUI5 requires a license and is integrated into a lot of SAP products, OpenUI5 is open source and generally available under an Apache 2.0 license. SAPUI5 includes more libraries than OpenUI5, but the latter still contains all central functionality and most commonly used control libraries are identical in both deliveries. - -The material in this repository would work with both deliveries, but uses OpenUI5. For the sake of simplicity and to indicate that the material would work with SAPUI5, too, the material simply refers to the framework as 'UI5'. - -You can find more information about this in the [SAPUI5 Documentation](https://sapui5.hana.ondemand.com/#/topic/5982a9734748474aa8d4af9c3d8f31c0). +![thumbnail](thumbnail.png) ## Support diff --git a/bookshop/app/performance-webapp/package.json b/bookshop/app/performance-webapp/package.json new file mode 100644 index 00000000..8d849872 --- /dev/null +++ b/bookshop/app/performance-webapp/package.json @@ -0,0 +1,16 @@ +{ + "name": "performance-webapp", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start:dist": "serve dist" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "@ui5/cli": "^2.14.13", + "serve": "^14.0.1" + } +} diff --git a/bookshop/app/performance-webapp/ui5.yaml b/bookshop/app/performance-webapp/ui5.yaml new file mode 100644 index 00000000..100e5797 --- /dev/null +++ b/bookshop/app/performance-webapp/ui5.yaml @@ -0,0 +1,11 @@ +specVersion: '2.6' +metadata: + name: performance-webapp +type: application +framework: + name: OpenUI5 + version: "1.107.1" + libraries: + - name: sap.m + - name: sap.ui.core + - name: themelib_sap_horizon diff --git a/bookshop/app/performance-webapp/webapp/Component.js b/bookshop/app/performance-webapp/webapp/Component.js new file mode 100644 index 00000000..466523dc --- /dev/null +++ b/bookshop/app/performance-webapp/webapp/Component.js @@ -0,0 +1,27 @@ +sap.ui.define([ + "sap/ui/core/UIComponent", + "sap/ui/model/json/JSONModel", + "sap/ui/core/date/Gregorian", + "sap/ui/core/ComponentSupport" +], function (UIComponent, JSONModel) { + "use strict" + return UIComponent.extend( + "sap.codejam.Component", { + metadata : { + "interfaces": [ + "sap.ui.core.IAsyncContentCreation" + ], + manifest: "json" + }, + init : function () { + this.setModel(new JSONModel({"@odata.context":"$metadata#Books(genre())","value":[{"createdAt":"2022-10-18T12:25:17.907Z","modifiedAt":"2022-10-18T12:25:17.907Z","ID":201,"title":"Wuthering Heights","descr":"Wuthering Heights, Emily Brontë's only novel, was published in 1847 under the pseudonym \"Ellis Bell\". It was written between October 1845 and June 1846. Wuthering Heights and Anne Brontë's Agnes Grey were accepted by publisher Thomas Newby before the success of their sister Charlotte's novel Jane Eyre. After Emily's death, Charlotte edited the manuscript of Wuthering Heights and arranged for the edited version to be published as a posthumous second edition in 1850.","author":"Emily Brontë","genre_ID":11,"stock":12,"price":11.11,"currency_code":"GBP","image@odata.mediaContentType":"image/png","genre":{"name":"Drama","descr":null,"ID":11,"parent_ID":10}},{"createdAt":"2022-10-18T12:25:17.907Z","modifiedAt":"2022-10-18T12:25:17.907Z","ID":207,"title":"Jane Eyre","descr":"Jane Eyre /ɛər/ (originally published as Jane Eyre: An Autobiography) is a novel by English writer Charlotte Brontë, published under the pen name \"Currer Bell\", on 16 October 1847, by Smith, Elder & Co. of London. The first American edition was published the following year by Harper & Brothers of New York. Primarily a bildungsroman, Jane Eyre follows the experiences of its eponymous heroine, including her growth to adulthood and her love for Mr. Rochester, the brooding master of Thornfield Hall. The novel revolutionised prose fiction in that the focus on Jane's moral and spiritual development is told through an intimate, first-person narrative, where actions and events are coloured by a psychological intensity. The book contains elements of social criticism, with a strong sense of Christian morality at its core and is considered by many to be ahead of its time because of Jane's individualistic character and how the novel approaches the topics of class, sexuality, religion and feminism.","author":"Charlotte Brontë","genre_ID":11,"stock":11,"price":12.34,"currency_code":"GBP","image@odata.mediaContentType":"image/png","genre":{"name":"Drama","descr":null,"ID":11,"parent_ID":10}},{"createdAt":"2022-10-18T12:25:17.907Z","modifiedAt":"2022-10-18T12:25:17.907Z","ID":251,"title":"The Raven","descr":"\"The Raven\" is a narrative poem by American writer Edgar Allan Poe. First published in January 1845, the poem is often noted for its musicality, stylized language, and supernatural atmosphere. It tells of a talking raven's mysterious visit to a distraught lover, tracing the man's slow fall into madness. The lover, often identified as being a student, is lamenting the loss of his love, Lenore. Sitting on a bust of Pallas, the raven seems to further distress the protagonist with its constant repetition of the word \"Nevermore\". The poem makes use of folk, mythological, religious, and classical references.","author":"Edgar Allen Poe","genre_ID":16,"stock":333,"price":13.13,"currency_code":"USD","image@odata.mediaContentType":"image/png","genre":{"name":"Mystery","descr":null,"ID":16,"parent_ID":10}},{"createdAt":"2022-10-18T12:25:17.907Z","modifiedAt":"2022-10-18T12:25:17.907Z","ID":252,"title":"Eleonora","descr":"\"Eleonora\" is a short story by Edgar Allan Poe, first published in 1842 in Philadelphia in the literary annual The Gift. It is often regarded as somewhat autobiographical and has a relatively \"happy\" ending.","author":"Edgar Allen Poe","genre_ID":16,"stock":555,"price":14,"currency_code":"USD","image@odata.mediaContentType":"image/png","genre":{"name":"Mystery","descr":null,"ID":16,"parent_ID":10}},{"createdAt":"2022-10-18T12:25:17.907Z","modifiedAt":"2022-10-18T12:25:17.907Z","ID":271,"title":"Catweazle","descr":"Catweazle is a British fantasy television series, starring Geoffrey Bayldon in the title role, and created by Richard Carpenter for London Weekend Television. The first series, produced and directed by Quentin Lawrence, was screened in the UK on ITV in 1970. The second series, directed by David Reid and David Lane, was shown in 1971. Each series had thirteen episodes, most but not all written by Carpenter, who also published two books based on the scripts.","author":"Richard Carpenter","genre_ID":13,"stock":22,"price":150,"currency_code":"JPY","image@odata.mediaContentType":"image/png","genre":{"name":"Fantasy","descr":null,"ID":13,"parent_ID":10}}]})) + UIComponent.prototype.init.apply( + this, + arguments + ) + }, + onAfterRendering: function() { + document.body.classList.remove("loading"); + } + }) + }) \ No newline at end of file diff --git a/bookshop/app/performance-webapp/webapp/boot.js b/bookshop/app/performance-webapp/webapp/boot.js new file mode 100644 index 00000000..5ac13bee --- /dev/null +++ b/bookshop/app/performance-webapp/webapp/boot.js @@ -0,0 +1,19 @@ +// configuration object +window["sap-ui-config"] = { + "compatVersion": "edge", + "async": true, + "language": "en", + "resourceroots": { + "sap.codejam": "./" + }, + "onInit": "module:sap/ui/core/ComponentSupport", + "theme": (function () { + // determine the proper theme for UI5 from current color scheme + try { + return window.matchMedia("(prefers-color-scheme: dark)").matches ? "sap_horizon_dark" : "sap_horizon"; + } catch (ex) { + console.warn("window.matchMedia not supported - keep default theme"); + return "sap_horizon"; + } + })() +}; \ No newline at end of file diff --git a/bookshop/app/performance-webapp/webapp/controller/App.controller.js b/bookshop/app/performance-webapp/webapp/controller/App.controller.js new file mode 100644 index 00000000..8177166e --- /dev/null +++ b/bookshop/app/performance-webapp/webapp/controller/App.controller.js @@ -0,0 +1,83 @@ +sap.ui.define([ + "sap/ui/core/mvc/Controller", + "sap/ui/model/json/JSONModel", + "sap/ui/model/Filter", + "sap/ui/model/FilterOperator" +], function (Controller, JSONModel, Filter, FilterOperator) { + "use strict"; + return Controller.extend("sap.codejam.controller.App", { + onInit: function () { + this.getView().setModel(new JSONModel({ + itemSelected: false, + selectedQuantity: 1 + }), "userSelection") + }, + onSelect: function (oEvent) { + let oModel = this.getView().getModel("userSelection") + let selectedModelPath = oEvent.getSource().getBindingContext().sPath + let selectedModelData = oEvent.getSource().getModel().getProperty(selectedModelPath) + oModel.setProperty("/selectedItemPath", selectedModelPath) + oModel.setProperty("/selectedItemData", selectedModelData) + + oModel.setProperty("/selectedQuantity", 1) + this.getView().byId("orderStatus").setText("") + oModel.setProperty("/itemSelected", true) + }, + onSubmitOrder: function () { + let oView = this.getView() + let userSelectionData = oView.getModel("userSelection").getData() + + let reqSettings = { + "url": "/browse/submitOrder", + "method": "POST", + "timeout": 0, + "headers": { + "Content-Type": "application/json" + }, + "data": JSON.stringify({ + "book": userSelectionData.selectedItemData.ID, + "quantity": userSelectionData.selectedQuantity + }), + } + + let i18nModel = oView.getModel("i18n") + jQuery.ajax(reqSettings) + .done(function (response) { + console.log(response) + oView.byId("orderStatus") + .setText( + `${i18nModel.getProperty("orderSuccessful")} + (${userSelectionData.selectedItemData.title}, + ${userSelectionData.selectedQuantity} + ${i18nModel.getProperty("pieces")})` + ) + oView.byId("orderStatus").setState("Success") + + let userSelectedPath = oView.getModel("userSelection").getProperty("/selectedItemPath") + oView.getModel().setProperty(userSelectedPath + "/stock", response.stock) + oView.getModel("userSelection").setProperty("/selectedItemData/stock", response.stock) + }) + .fail(function(response) { + console.log(response) + oView.byId("orderStatus").setText(`${i18nModel.getProperty("Error")}`) + oView.byId("orderStatus").setState("Error") + }) + }, + onSearch: function (oEvent) { + var aFilter = []; + var sQuery = oEvent.getParameter("newValue"); + if (sQuery) { + aFilter.push(new Filter("title", FilterOperator.Contains, sQuery)); + } + var oList = this.byId("booksTable"); + var oBinding = oList.getBinding("items"); + oBinding.filter(aFilter); + + let oModel = this.getView().getModel("userSelection") + oModel.setProperty("/selectedItemPath", {}) + oModel.setProperty("/selectedItemData", {}) + this.getView().byId("orderStatus").setText("") + oModel.setProperty("/itemSelected", false) + } + }); +}); \ No newline at end of file diff --git a/bookshop/app/performance-webapp/webapp/css/style.css b/bookshop/app/performance-webapp/webapp/css/style.css new file mode 100644 index 00000000..c2c9cf2f --- /dev/null +++ b/bookshop/app/performance-webapp/webapp/css/style.css @@ -0,0 +1,31 @@ +.orderControls { + gap: 20px; +} + +html, body { + width: 100%; + height: 100%; +} + +body.loading { + background-size: 20rem !important; + background-position: center; + background-repeat: no-repeat; + overflow: hidden; +} +body.loading > * { + visibility: hidden; +} + +@media (prefers-color-scheme: light) { + body.loading { + background-color: white; + background-image: url("data:image/svg+xml;utf8,"); + } +} +@media (prefers-color-scheme: dark) { + body.loading { + background-color: black; + background-image: url("data:image/svg+xml;utf8,"); + } +} \ No newline at end of file diff --git a/bookshop/app/performance-webapp/webapp/i18n/i18n_de.properties b/bookshop/app/performance-webapp/webapp/i18n/i18n_de.properties new file mode 100644 index 00000000..32c068ee --- /dev/null +++ b/bookshop/app/performance-webapp/webapp/i18n/i18n_de.properties @@ -0,0 +1,10 @@ + Bookshop=Buchhandlung + Book=Buch + Author=Autor + Genre=Genre + Price=Preis + Stock=Verfügbarkeit + Order=Bestellen + orderSuccessful=Bestellung erfolgreich + pieces=Stk. + Error=Fehler \ No newline at end of file diff --git a/bookshop/app/performance-webapp/webapp/i18n/i18n_en.properties b/bookshop/app/performance-webapp/webapp/i18n/i18n_en.properties new file mode 100644 index 00000000..0999f6ca --- /dev/null +++ b/bookshop/app/performance-webapp/webapp/i18n/i18n_en.properties @@ -0,0 +1,10 @@ + Bookshop=Bookshop + Book=Book + Author=Author + Genre=Genre + Price=Price + Stock=Stock + Order=Order + orderSuccessful=Order successful + pieces=pcs. + Error=Error \ No newline at end of file diff --git a/bookshop/app/performance-webapp/webapp/index.html b/bookshop/app/performance-webapp/webapp/index.html new file mode 100644 index 00000000..a326b90b --- /dev/null +++ b/bookshop/app/performance-webapp/webapp/index.html @@ -0,0 +1,19 @@ + + + + + + + + + + +
+
+ + + \ No newline at end of file diff --git a/bookshop/app/performance-webapp/webapp/manifest.json b/bookshop/app/performance-webapp/webapp/manifest.json new file mode 100644 index 00000000..b3e9812d --- /dev/null +++ b/bookshop/app/performance-webapp/webapp/manifest.json @@ -0,0 +1,42 @@ +{ + "sap.app": { + "id": "sap.codejam", + "type": "application", + "title": "CodeJam Bookshop", + "applicationVersion": { + "version": "1.0.0" + }, + "dataSources": { + "capBooks": { + "uri": "/browse/Books?$expand=genre", + "type": "JSON" + } + } + }, + "sap.ui5": { + "rootView": { + "viewName": "sap.codejam.view.App", + "type": "XML", + "id": "app" + }, + "dependencies": { + "minUI5Version": "1.107.1", + "libs": { + "sap.ui.core": {}, + "sap.m": {} + } + }, + "models": { + "models": { + "i18n": { + "type": "sap.ui.model.resource.ResourceModel", + "settings": { + "bundleName": "sap.codejam.i18n.i18n", + "supportedLocales": ["en", "de"], + "fallbackLocale": "en" + } + } + } + } + } +} \ No newline at end of file diff --git a/bookshop/app/performance-webapp/webapp/view/App.view.xml b/bookshop/app/performance-webapp/webapp/view/App.view.xml new file mode 100644 index 00000000..510ee9fc --- /dev/null +++ b/bookshop/app/performance-webapp/webapp/view/App.view.xml @@ -0,0 +1,100 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +