# **Chapter 28: Onsen SPA, SJS, and KotlinGates**
> **Prerequisites:** Chapter 27 (MobileGate project + generate),
> Chapter 13 (jsServer block ideas on the Kotlin side)
> **Try it:** Same `mobileapp/` project as Chapter 27 — not a website Gate.
> After `php -f start.php -- --gate index`, preview Onsen nav in
> `mobileapp/www/index.html`, then run the APK in Android Studio for
> `callKotlinGateEvent` / C++ demos.
---
## **28.1 Onsen as the In-App SPA**
Generated `www/index.html` is a **single-page** WebView file. Screens from
`addPage` are **not** separate HTTP routes — use framework Onsen components
under `uikitdef/onsen/` (especially `OnsenPage.php`).
```xml
<!-- apps/fronts/index_main.front — first OnsenPage becomes start page -->
<ons-page id="home" runat="server" path="comppath/uikitdef/onsen/OnsenPage.php">
…
<ons-button onclick="onsen.loadPage('page2.html')">Go to Page 2</ons-button>
</ons-page>
```
```xml
<!-- apps/fronts/page2.front -->
<ons-page id="page2" runat="server" path="comppath/uikitdef/onsen/OnsenPage.php">
<ons-back-button>Back</ons-back-button>
…
</ons-page>
```
Generate emits `<ons-navigator id="onsennavi" page="home.html">` plus
`<template id="home.html">` / `<template id="page2.html">`. JS:
`onsen.loadPage('page2.html')` → `bringPageTop`.
**Auto-link:** putting `OnsenPage` in a front calls `addFileLink` for Onsen
CSS/JS; `MobileGate` folds JS into `sjmm.js`. Do **not** add a manual
`<script src="onsenui.min.js">`. Keep `addDistLib(.../jslib/onsen)` so CSS/fonts
copy into `www/sphpmob/resm`. Match page `id` with SJS hooks
`comp_{id}_init|show|hide|destroy`.
Any SPA library can work; this demo uses the built-in Onsen stack for a
minimum navigable example.
## **28.2 SJS Files — Client JS Next to Fronts**
Place `apps/sjs/{same-as-front}.sjs` next to fronts (e.g. `index_main.sjs`,
`page2.sjs`). Prefer this over scattering `addHeaderJS*` in Gate PHP.
Processor: **`Sphp\Tools\Psjs`**. It extracts `function name(paras){ … }`,
binds `comp_{id}_{event}` onto Front components, and emits client JS into the
page bundle.
| Pattern | When | What happens |
|---------|------|----------------|
| `comp_{id}_{event}` | client | Body injected; Onsen `init`/`show`/… calls it |
| `ofjs_{id}_{domEvent}` | client | Auto `$('#id').on(...)` + body |
| `jq_ready` / `jq_*` | client | Ready / shared jQuery hooks |
| `onjs_{id}_{event}` | **Server AJAX** | Not usable inside the APK |
| `jevt_*` | **Server AJAX** | Not usable inside the APK |
**Mobile runtime:** generate bakes **client** SJS (`comp_*`, `ofjs_*`, `jq_*`)
into `www/` / `sjmm.js`. There is **no PHP server** in the APK, so
`onjs_*` / `jevt_*` / `processSJSEvent` do **not** run on device — use
KotlinGates / `callKotlinGateEvent` instead.
```javascript
// apps/sjs/index_main.sjs — matches OnsenPage id="home"
function comp_home_init(eventer){ }
function comp_home_show(eventer){ }
function comp_home_hide(eventer){ }
function comp_home_destroy(eventer){ }
```
## **28.3 KotlinGate — Device-Side Gates**
Kotlin classes under `apps/kotlin_apps/` are the phone twin of a PHP Gate:
lifecycle (`onstart`, `page_new`), `page_event_*` routing, and JSServer blocks
(same idea as Chapter 13 jsServer). The WebView calls:
```javascript
callKotlinGate('index');
callKotlinGateEvent('index', 'nativeAdd', '', { a: 7, b: 5 });
```
```kotlin
// apps/kotlin_apps/IndexGate.kt (excerpt, tested)
class IndexGate : KotlinGate() {
suspend fun page_event_nativeAdd(evtp: String) {
val sum = NativeLib.addInts(dataOptInt("a", 0), dataOptInt("b", 0))
JSServer.addJSONHTMLBlock("spnNative", "C++ addInts = $sum")
JSServer.callJsFunction("onNativeResult",
JSONObject().put("result", sum))
JSServer.flush()
}
}
```
Generate registers classes into `app/src/main/assets/regapp.json` (e.g.
`"index": "com.sartajphp.gate.IndexGate"`).
## **28.4 Optional C++ / JNI**
`MobileGate::addCppDir($folder)` copies into `app/src/main/cpp/{basename}`.
Wire CMake with `addAndroidSetting("android", …)`:
```php
$this->addCppDir(__DIR__ . "/cpp/nativedemo");
$this->addAndroidSetting("android", '
ndkVersion = "26.1.10909125"
externalNativeBuild {
cmake {
path = file("src/main/cpp/nativedemo/CMakeLists.txt")
version = "3.22.1"
}
}');
```
| Source | Role |
|--------|------|
| `apps/cpp/nativedemo/*.cpp` | JNI entry points |
| `apps/cpp/nativedemo/CMakeLists.txt` | Shared library |
| `apps/kotlin_apps/NativeLib.kt` | `System.loadLibrary` + `external fun` |
| KotlinGate `page_event_*` | Calls C++ and flushes UI |
Also use `addJavaDir` / `addAndroidLibFile` for Java or `.jar`/`.aar` under
`app/libs`.
## **28.5 Request Flow Inside the App**
```
WebView UI (Onsen Fronts + SJS)
→ callKotlinGateEvent → KotlinGate (device) → optional JNI/C++
→ sendAjax / callNativeGate → remote PHP Gate (your site / SphpServer)
```
No Cordova/Electron layer — generate once with
`php -f start.php -- --gate index`, then build the APK.
## **28.6 Verify Locally**
1. `cd mobileapp && php -f start.php -- --gate index` completes
2. `www/index.html` has `ons-navigator` and templates `home.html` / `page2.html`
3. `sjmm.js` includes Onsen + `window.onsen.loadPage`
4. `app/src/main/assets` synced; `regapp.json` lists IndexGate
5. Optional: C++ under `app/src/main/cpp/nativedemo/` + gradle `externalNativeBuild`
6. Android Studio Sync → Run — home buttons exercise Kotlin / C++
This docs website cannot ship an APK runner; use Chapters 27–28 as the
tutorial and `mobileapp/` on your machine as the lab.
## **28.7 Next Steps**
- Chapter 27 for project layout, generate, CSP, and MobileGate APIs
- Chapter 13 for jsServer block types mirrored by Kotlin `JSServer`
- Chapter 22 for remote NativeGate WebSockets from the WebView
- Chapter 25/26 if a separate React/Vue client talks to the same remote Gates
---
**Summary:** Onsen pages + SJS own the in-app SPA. KotlinGates handle device
events (`callKotlinGateEvent`); optional C++ sits behind JNI. Remote PHP Gates
remain available over AJAX/WebSocket. Generate locally — the public site links
to these chapters, not a live mobile example.