Skip to content

Commit a422e0d

Browse files
margopolopetermuessigakudev
authored
docs: introduce js to readme (#134)
* Add JS/TS switches to Step 1-10 * docs: add js parts to readme * Fix visualization of JS/TS block switches * docs: add missing js parts to readme * Adapt links with current language; make dev server support URL params This ensures that after navigating to another page the current language is still active * Let browsersync popup not cover the language switch * Update README.md * Update 01/README * Update 02/README * Update 16/README and 17/README --------- Co-authored-by: Peter Muessig <peter.muessig@sap.com> Co-authored-by: akudev <andreas.kunz@sap.com>
1 parent 4b1d83d commit a422e0d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1839
-425
lines changed

README.md

Lines changed: 45 additions & 52 deletions
Large diffs are not rendered by default.

README_AUTHORS.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,15 @@ To immediately preview the markdown document you are writing *including* the two
2424
npm i
2525
```
2626

27-
in the root folder of this project once for the setup, then to actually run the server, run
27+
in the root folder of this project once for the setup.
2828

29-
```sh
30-
npm start
31-
```
32-
33-
Then, open http://localhost:1337/README.md in your browser.
34-
35-
Alternatively, and even easier, run
29+
Then, to actually start the server, run
3630

3731
```sh
38-
npm run watch
32+
npm start
3933
```
4034

41-
And the browser will automatically open (on port 3000) and automatically reload on every saved change.
35+
A browser window will automatically open (on port 3000 or the next free port) and automatically reload on every saved change.
4236

4337

4438
## Writing *one* document which covers both JavaScript and TypeScript without duplication

assets/css/custom.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ body.page-language-ts .language-switch-button.lang-ts {
114114
border-color: var(--active-border);
115115
}
116116

117+
li > section.ts-only, li > section.js-only {
118+
display: inline;
119+
}
120+
117121
.language-switch-button:hover {
118122
opacity: 0.8;
119123
background-color: rgba(20, 20, 20, 0.8);
@@ -134,3 +138,8 @@ body.page-language-ts .language-switch-button.lang-ts {
134138
border-color: var(--active-border);
135139
}
136140
}
141+
142+
#__bs_notify__ {
143+
background-color: rgba(27, 32, 50, 0.3) !important;
144+
top: 40px !important;
145+
}

assets/js/custom.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ function replaceDetailSections() {
164164
if (child.tagName.toLocaleUpperCase() === "SUMMARY") {
165165
return;
166166
}
167-
sectionTag.appendChild(child);
167+
// only add the child if it has content
168+
if (child.innerHTML.trim()) {
169+
sectionTag.appendChild(child);
170+
}
168171
});
169172

170173
// remove the detail tag
@@ -205,6 +208,27 @@ function replaceFileExtensions(lang) {
205208
}
206209
}
207210

211+
function updatePrevNextLinks(lang) {
212+
const links = document.querySelectorAll('a[href$="README.md"], a[href$="/"], a[href$="README.md?lang=js"], a[href$="/?lang=js"]');
213+
links.forEach(link => {
214+
// Only handle relative links (not starting with http://, https://, or //)
215+
if (/^(https?:)?\/\//.test(link.getAttribute("href"))) {
216+
return;
217+
}
218+
219+
let url = new URL(link.href, window.location.origin);
220+
221+
// Remove any existing lang param
222+
url.searchParams.delete("lang");
223+
224+
if (lang === "js") {
225+
url.searchParams.set("lang", "js");
226+
}
227+
228+
link.href = url.pathname + url.search + url.hash;
229+
});
230+
}
231+
208232
// dynamic overall language switching
209233

210234
function addLanguageSwitchButtons() {
@@ -233,6 +257,7 @@ function switchLanguage(newLang) {
233257
replaceFileExtensions(lang);
234258
resetCodeCoupleButtons();
235259
updateAllCodeCouples(lang);
260+
updatePrevNextLinks(lang);
236261
}
237262

238263
function updateAllCodeCouples(globalLang) {
@@ -252,5 +277,6 @@ document.addEventListener("DOMContentLoaded", (event) => {
252277
replaceFileExtensions(lang);
253278
addLanguageSwitchButtons();
254279
updateAllCodeCouples(lang);
280+
updatePrevNextLinks(lang);
255281
});
256282
});

steps/01/README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Step 1: Hello World!
22

3-
As you know OpenUI5 is all about HTML5. Let's get started with building a first "Hello World" with only HTML. In addition we'll initialize the UI5 Tooling, so we can benefit from it from the beginning.
3+
As you know, OpenUI5 is all about HTML5. Let's get started with building a first "Hello World" with only HTML. In addition we'll initialize the UI5 Tooling, so we can benefit from it from the beginning.
44

55
&nbsp;
66

@@ -15,12 +15,23 @@ As you know OpenUI5 is all about HTML5. Let's get started with building a first
1515

1616
You can access the live preview by clicking on this link: [🔗 Live Preview of Step 1](https://sap-samples.github.io/ui5-typescript-walkthrough/build/01/index.html).
1717

18-
Download solution for step 1 in [📥 TypeScript](https://sap-samples.github.io/ui5-typescript-walkthrough/ui5-typescript-walkthrough-step-01.zip) or [📥 JavaScript](https://sap-samples.github.io/ui5-typescript-walkthrough/ui5-typescript-walkthrough-step-01-js.zip).
19-
2018
***
2119

2220
### Coding
2321

22+
<details class="ts-only">
23+
24+
You can download the solution for this step here: [📥 Download step 1](https://sap-samples.github.io/ui5-typescript-walkthrough/ui5-typescript-walkthrough-step-01.zip).
25+
26+
</details>
27+
28+
<details class="js-only">
29+
30+
You can download the solution for this step here: [📥 Download step 1](https://sap-samples.github.io/ui5-typescript-walkthrough/ui5-typescript-walkthrough-step-01-js.zip).
31+
32+
</details>
33+
***
34+
2435
### webapp \(New\)
2536

2637
We create a folder on our local machine which will contain all the sources of the app we're going to build. We'll refer to this folder as the “app root directory".
@@ -154,7 +165,7 @@ This will open a new browser window hosting your newly created `index.html`.
154165

155166
### Conventions
156167

157-
- The `index.html` file is located in the `webapp` folder if it is used productively.
168+
- The `index.html` file is located in the `webapp` folder.
158169

159170
&nbsp;
160171

steps/02/README.md

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,23 @@ Before we can do something with OpenUI5, we need to load and initialize it. This
1515

1616
You can access the live preview by clicking on this link: [🔗 Live Preview of Step 2](https://sap-samples.github.io/ui5-typescript-walkthrough/build/02/index-cdn.html).
1717

18-
Download solution for step 2 in [📥 TypeScript](https://sap-samples.github.io/ui5-typescript-walkthrough/ui5-typescript-walkthrough-step-02.zip) or [📥 JavaScript](https://sap-samples.github.io/ui5-typescript-walkthrough/ui5-typescript-walkthrough-step-02-js.zip).
18+
***
19+
### Coding
20+
21+
<details class="ts-only">
22+
23+
You can download the solution for this step here: [📥 Download step 2](https://sap-samples.github.io/ui5-typescript-walkthrough/ui5-typescript-walkthrough-step-02.zip).
24+
25+
</details>
26+
27+
<details class="js-only">
1928

29+
You can download the solution for this step here: [📥 Download step 2](https://sap-samples.github.io/ui5-typescript-walkthrough/ui5-typescript-walkthrough-step-02-js.zip).
30+
31+
</details>
2032
***
2133

22-
### Tooling
34+
### UI5 Tooling
2335

2436
First, let's set up our UI5 Tooling to use the OpenUI5 framework for our project. We also need to add the necessary OpenUI5 libraries as dependencies to the project's UI5 Tooling configuration.
2537

@@ -37,26 +49,28 @@ ui5 add sap.ui.core themelib_sap_horizon
3749

3850
The `ui5 add` command adds specific libraries as dependency to the projects UI5 Tooling configuration. In this case, we'e adding the `sap.ui.core` library, which provides core functionality of the OpenUI5 framework. This library is essential for bootstrapping OpenUI5. Additionally, we're adding the `themelib_sap_horizon` library which provides the visual styles for the Horizon theme. We'll use this theme with our application.
3951

40-
***
4152

42-
### package.json
53+
<details class="ts-only">
54+
55+
### TypeScript Setup
4356

4457
To work with TypeScript, we must install it in our project. To do this, we execute the following command in the terminal:
4558

59+
4660
```sh
4761
npm install typescript --save-dev
4862
```
4963

5064
By running this command, npm will download the TypeScript package from the npm registry and install it in our project's "node_modules" directory. It will also add an entry for TypeScript in the "devDependencies" section of our package.json file, so that other developers working on the project can easily install the same version of TypeScript.
5165

52-
***
5366

5467
### tsconfig.json \(New\)
5568

5669
As a next step, we need to create the file `tsconfig.json` in the app root directory to indicate that this folder is the root of a TypeScript project. This file specifies various compiler options and project settings that affect how TypeScript code is compiled into JavaScript.
5770

5871
We specify the compiler options as follow:
5972

73+
6074
```json
6175
{
6276
"compilerOptions": {
@@ -101,18 +115,16 @@ Let's go through the compiler options specified in the file:
101115

102116
***
103117

104-
### Coding
118+
</details>
105119

106-
### webapp/index.ts \(New\)
120+
### webapp/index.?s \(New\)
107121

108-
Now let's move on to the UI work. We create a new `index.ts` script in the webapp folder. In this script, we add a native `alert()` method with the message "UI5 is ready".
122+
Now let's move on to the UI work. We create a new `index.?s` script in the webapp folder. In this script, we add a native `alert()` method with the message "UI5 is ready".
109123

110-
```ts
124+
```js
111125
alert("UI5 is ready");
112126
```
113127

114-
***
115-
116128
### webapp/index.html
117129

118130
Next, we'll integrate the script we just created into the `index.html` page to signal when the OpenUI5 framework has finished loading. This process involves first incorporating the OpenUI5 framework into our HTML page by adding a script tag specifically for loading OpenUI5.
@@ -157,22 +169,30 @@ We initialize the core modules with the following configuration options:
157169
</html>
158170
```
159171

160-
> 📝 **Note:**
172+
> 📝 **Note:**<br>
161173
> The namespace is a unique identifier for your application file. It helps prevent naming conflicts with other modules or libraries.
162174
163175
***
164176

165-
### Tooling
166-
167-
### package.json
177+
### UI5 Tooling
168178

169179
Let's enhance our tooling setup once again by installing some custom middleware for the ui5-server. This will help us handle our development project more efficiently.
170180

171181
We open a terminal and navigate to the root folder of our app. Then, we execute the following command:
172182

183+
<details class="ts-only">
184+
173185
```sh
174186
npm install ui5-middleware-livereload ui5-middleware-serveframework ui5-tooling-transpile --save-dev
175187
```
188+
</details>
189+
190+
<details class="js-only">
191+
192+
```sh
193+
npm install ui5-middleware-livereload ui5-middleware-serveframework --save-dev
194+
```
195+
</details>
176196

177197
When you run the command, npm will download the specified packages from the npm registry and store them in a folder called `node_modules` within your project directory. The `--save-dev` flag instructs npm to save these packages as development dependencies in the `devDependencies` section of the `package.json` file. Development dependencies are packages that are only needed during development and not in production. By separating them from production dependencies, we can keep our project clean and ensure that only the required packages are included when deploying the application.
178198

@@ -182,21 +202,28 @@ Let's break down what each package does:
182202

183203
- `ui5-middleware-serveframework` is another middleware plugin for the UI5 Tooling that provides a web server to serve your OpenUI5 project during development. It allows you to easily serve the necessary OpenUI5 libraries and resources required by your application from your development environment.
184204

205+
<details class="ts-only">
206+
185207
- `ui5-tooling-transpile` is a plugin for the UI5 Tooling that transpiles modern JavaScript (ES6+) and TypeScript into a compatible version for OpenUI5. OpenUI5 is based on older versions of JavaScript, so this plugin allows you to take advantage of the latest language features and syntax while ensuring that your code remains compatible with OpenUI5.
186208

187-
***
209+
</details>
188210

189211
### ui5.yaml
190212

191213
Next, we have to configure the tooling extension we installed from npm to our UI5 Tooling setup, so we can use them in our project. To hook a custom task into a certain build phase of a project, it needs to reference another task that will get executed before or after it. The same applies for a custom middleware:
214+
<details class="ts-only">
192215

193216
- For the `ui5-tooling-transpile-task` we specify that this should happen after the`replaceVersion` task.
194217

218+
</details>
219+
195220
- All our custom middleware extensions will be called after the `compression` middleware.
196221

197222
> 📌 **Important:** <br>
198223
> Middleware configurations are applied in the order in which they are defined.
199224
225+
<details class="ts-only">
226+
200227
```yaml
201228
framework:
202229
name: OpenUI5
@@ -219,7 +246,31 @@ server:
219246
```
220247
Now you can benefit from live reload on changes, built framework resources at development time, and make use of TypeScript in OpenUI5.
221248
222-
> 📝 **Note:** <br>
249+
</details>
250+
251+
<details class="js-only">
252+
253+
```yaml
254+
framework:
255+
name: OpenUI5
256+
version: "1.132.1"
257+
libraries:
258+
- name: sap.ui.core
259+
- name: themelib_sap_horizon
260+
builder:
261+
server:
262+
customMiddleware:
263+
- name: ui5-middleware-serveframework
264+
afterMiddleware: compression
265+
- name: ui5-middleware-livereload
266+
afterMiddleware: compression
267+
```
268+
Now you can benefit from live reload on changes and built framework resources at development time.
269+
270+
</details>
271+
272+
<br>
273+
> 📝 **Note:**<br>
223274
> During its initial run, the `ui5-middleware-serveframework` middleware will build the framework, which can take a while. In all following steps, the build will not happen again and the framework is served from the built resources.
224275

225276
&nbsp;

0 commit comments

Comments
 (0)