Skip to content

Commit 8309f11

Browse files
committed
1.0.77 misc
1 parent 2ce56d4 commit 8309f11

23 files changed

Lines changed: 976 additions & 551 deletions

extras/examples/build.fan

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Build : BuildPod
2828
"vcs.name": "Mercurial",
2929
"vcs.uri": "http://hg.fantom.org/fan-1.0/"]
3030
depends = ["sys 1.0"]
31-
resDirs = [`index.fog`, `concurrent/`, `email/`, `fwt/`, `java/`, `js/`, `sys/`, `util/`, `web/`]
31+
resDirs = [`index.fog`, `concurrent/`, `email/`, `fwt/`, `java/`, `sys/`, `util/`, `web/`, `webfwt/`]
3232
}
3333

3434
@Target { help = "Verify all examples compile" }

extras/examples/index.fog

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@
2222
[`fwt/table.fan`, "Table model and visualization options"],
2323
"email",
2424
[`email/sending.fan`, "Sending email via SMTP"],
25-
"js",
26-
[`js/demo.fan`, "Run web server to demo Fantom to JavaScript in browser"],
2725
"java",
2826
[`java/hello.fan`, "Hello world with Java FFI"],
2927
[`java/swing.fan`, "Swing application using Java FFI"],
3028
[`java/buildjardist.fan`, "Build fansh into a single JAR for deployment"],
29+
"js",
30+
[`js/hello.fan`, "Hello world JS appliaction"],
31+
[`js/env.fan`, "Shows how to configure the JS boot Env"],
32+
[`js/dom.fan`, "Basic examples of using dom APIs"],
3133
"util",
3234
[`util/main.fan`, "Working with util::AbstractMain"],
3335
"web",
3436
[`web/client.fan`, "Working with web::WebClient"],
3537
[`web/hello.fan`, "Hello world web server application"],
3638
[`web/demo.fan`, "Illustrates some basic web APIs"],
39+
"webfwt",
40+
[`webfwt/demo.fan`, "Run web server to demo Web FWT in browser"],
3741
]
3842

extras/examples/js/dom.fan

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#! /usr/bin/env fan
2+
//
3+
// Copyright (c) 2021, Brian Frank and Andy Frank
4+
// Licensed under the Academic Free License version 3.0
5+
//
6+
// History:
7+
// 29 Jun 21 Andy Frank Creation
8+
//
9+
10+
using dom
11+
using util
12+
using web
13+
using webmod
14+
using wisp
15+
16+
*************************************************************************
17+
** JsDomMain
18+
*************************************************************************
19+
20+
class JsDomMain : AbstractMain
21+
{
22+
@Opt { help = "http port" }
23+
Int port := 8080
24+
25+
override Int run()
26+
{
27+
wisp := WispService
28+
{
29+
it.httpPort = this.port
30+
it.root = JsDomMod()
31+
}
32+
return runServices([wisp])
33+
}
34+
}
35+
36+
*************************************************************************
37+
** JsDomMod
38+
*************************************************************************
39+
40+
const class JsDomMod : WebMod
41+
{
42+
new make()
43+
{
44+
pods := ["sys","dom"].map |n| { Pod.find(n) }
45+
files := File[,]
46+
.addAll(FilePack.toAppJsFiles(pods))
47+
.add(compileScriptJs)
48+
this.jsPack = FilePack(files)
49+
}
50+
51+
override Void onGet()
52+
{
53+
switch (req.modRel.path.first)
54+
{
55+
case null: onIndex
56+
case "dom.js": jsPack.onGet
57+
}
58+
}
59+
60+
Void onIndex()
61+
{
62+
res.headers["Content-Type"] = "text/html; charset=utf-8"
63+
out := res.out
64+
out.docType5
65+
out.html
66+
out.head
67+
.title.w("Dom Basics").titleEnd
68+
.includeJs(`/dom.js`)
69+
.style.w(
70+
"body {
71+
padding: 0.25em 2em;
72+
font: 16px -apple-system, BlinkMacSystemFont, sans-serif;
73+
}").styleEnd
74+
out.headEnd
75+
out.body
76+
.h1.w("Dom Basics").h1End
77+
78+
// Win
79+
out.hr
80+
.h2.w("Win").h2End
81+
.p
82+
.button("value='Alert' onclick='fan.domExample.JsDom.winAlert()'")
83+
.button("value='Uri' onclick='fan.domExample.JsDom.winUri()'")
84+
.button("value='Viewport' onclick='fan.domExample.JsDom.winViewport()'")
85+
.pEnd
86+
87+
// Elem
88+
out.hr
89+
.h2.w("Elem").h2End
90+
.p
91+
.button("value='Add Item' onclick='fan.domExample.JsDom.elemAddItem()'")
92+
.button("value='Clear Items' onclick='fan.domExample.JsDom.elemClearItems()'")
93+
.pEnd
94+
.div("id='elem-list'").divEnd
95+
96+
out.bodyEnd
97+
out.htmlEnd
98+
}
99+
100+
**
101+
** Normally your @Js code would exist in a pod, but since
102+
** this is a Fantom script, we need to compile on the fly
103+
** in order to get JS output to add to FilePack.
104+
**
105+
private File compileScriptJs()
106+
{
107+
src := Env.cur.homeDir + `examples/js/dom.fan`
108+
js := Env.cur.compileScriptToJs(src, ["podName":"domExample"])
109+
temp := Env.cur.tempDir + `dom.js`
110+
temp.out.print(js).sync.close
111+
return temp
112+
}
113+
114+
private const FilePack jsPack
115+
}
116+
117+
*************************************************************************
118+
** JsDom
119+
*************************************************************************
120+
121+
@Js class JsDom
122+
{
123+
static Void winAlert()
124+
{
125+
Win.cur.alert("Hello world, from JsDom.winAlert!")
126+
}
127+
128+
static Void winUri()
129+
{
130+
Win.cur.alert("uri: ${Win.cur.uri}")
131+
}
132+
133+
static Void winViewport()
134+
{
135+
Win.cur.alert("viewport: ${Win.cur.viewport}")
136+
}
137+
138+
static Void elemAddItem()
139+
{
140+
list := Win.cur.doc.elemById("elem-list")
141+
list.add(Elem {
142+
it.text = "This is item #${list.children.size}"
143+
})
144+
}
145+
146+
static Void elemClearItems()
147+
{
148+
list := Win.cur.doc.elemById("elem-list")
149+
list.removeAll
150+
}
151+
}

extras/examples/js/env.fan

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#! /usr/bin/env fan
2+
//
3+
// Copyright (c) 2021, Brian Frank and Andy Frank
4+
// Licensed under the Academic Free License version 3.0
5+
//
6+
// History:
7+
// 29 Jun 21 Andy Frank Creation
8+
//
9+
10+
using dom
11+
using util
12+
using web
13+
using webmod
14+
using wisp
15+
16+
*************************************************************************
17+
** JsEnvMain
18+
*************************************************************************
19+
20+
class JsEnvMain : AbstractMain
21+
{
22+
@Opt { help = "http port" }
23+
Int port := 8080
24+
25+
override Int run()
26+
{
27+
wisp := WispService
28+
{
29+
it.httpPort = this.port
30+
it.root = JsEnvMod()
31+
}
32+
return runServices([wisp])
33+
}
34+
}
35+
36+
*************************************************************************
37+
** JsEnvMod
38+
*************************************************************************
39+
40+
const class JsEnvMod : WebMod
41+
{
42+
new make()
43+
{
44+
// app runtime js
45+
pods := ["sys", "dom"].map |n| { Pod.find(n) }
46+
app := File[,]
47+
.addAll(FilePack.toAppJsFiles(pods))
48+
.add(compileScriptJs)
49+
50+
// additional locales to include (en-US is already included by sys.js)
51+
loc := ["es", "fr", "de"].map |n| {
52+
FilePack.toLocaleJsFile(Locale(n))
53+
}
54+
55+
this.appPack = FilePack(app)
56+
this.locPack = FilePack(loc)
57+
}
58+
59+
override Void onGet()
60+
{
61+
switch (req.modRel.path.first)
62+
{
63+
case null: onIndex
64+
case "app.js": appPack.onGet
65+
case "loc.js": locPack.onGet
66+
}
67+
}
68+
69+
Void onIndex()
70+
{
71+
//
72+
// Modify or comment out to change booted env:
73+
// * if no tz is specified; query browser for current tz
74+
// * locale must be served in locPack; see make ctor
75+
//
76+
env := [
77+
"main": "env::JsEnv",
78+
"timezone": "Los_Angeles",
79+
"locale": "fr"
80+
]
81+
82+
res.headers["Content-Type"] = "text/html; charset=utf-8"
83+
out := res.out
84+
out.docType5
85+
out.html
86+
out.head
87+
.title.w("JS Env").titleEnd
88+
.initJs(env)
89+
.includeJs(`/app.js`)
90+
.includeJs(`/loc.js`)
91+
.style.w(
92+
"body {
93+
padding: 0.25em 2em;
94+
font: 16px -apple-system, BlinkMacSystemFont, sans-serif;
95+
}
96+
hr {
97+
border: none;
98+
background: #d9d9d9;
99+
height: 1px;
100+
margin: 1em 0;
101+
}
102+
pre {
103+
background: #f2f6f9;
104+
padding: 1em;
105+
border-radius: 5px;
106+
}
107+
table {
108+
border-collapse: collapse;
109+
border: 1px solid #d9d9d9;
110+
}
111+
td { padding: 0.5em 1em; }
112+
td + td { border-left: 1px solid #d9d9d9; }
113+
").styleEnd
114+
out.headEnd
115+
out.body
116+
.h1.w("JS Env").h1End
117+
.hr
118+
.p.w("Env.vars (edit in JsEnvMod.onIndex):").pEnd
119+
.pre("id='env-vars'").preEnd
120+
.hr
121+
.table
122+
.tr.td.w("TimeZone.cur").tdEnd .td("id='tz-cur'").tdEnd.trEnd
123+
.tr.td.w("DateTime.now").tdEnd .td("id='dt-now'").tdEnd.trEnd
124+
.tr.td.w("DateTime.boot").tdEnd .td("id='dt-boot'").tdEnd.trEnd
125+
.tr.td.w("Locale.cur").tdEnd .td("id='loc-cur'").tdEnd.trEnd
126+
.tr.td.w("Locale Month").tdEnd .td("id='loc-mon'").tdEnd.trEnd
127+
.tr.td.w("Locale Weekday").tdEnd.td("id='loc-wd'").tdEnd.trEnd
128+
.tableEnd
129+
out.bodyEnd
130+
out.htmlEnd
131+
}
132+
133+
**
134+
** Normally your @Js code would exist in a pod, but since
135+
** this is a Fantom script, we need to compile on the fly
136+
** in order to get JS output to add to FilePack.
137+
**
138+
private File compileScriptJs()
139+
{
140+
src := Env.cur.homeDir + `examples/js/env.fan`
141+
js := Env.cur.compileScriptToJs(src, ["podName":"env"])
142+
temp := Env.cur.tempDir + `env.js`
143+
temp.out.print(js).sync.close
144+
return temp
145+
}
146+
147+
private const FilePack appPack
148+
private const FilePack locPack
149+
}
150+
151+
*************************************************************************
152+
** JsEnv
153+
*************************************************************************
154+
155+
@Js class JsEnv
156+
{
157+
static Void main()
158+
{
159+
doc := Win.cur.doc
160+
doc.elemById("env-vars").text = Env.cur.vars.toStr
161+
162+
// timezone
163+
doc.elemById("tz-cur").text = TimeZone.cur.toStr
164+
doc.elemById("dt-now").text = DateTime.now.toLocale
165+
doc.elemById("dt-boot").text = DateTime.boot.toLocale
166+
167+
// locale
168+
doc.elemById("loc-cur").text = Locale.cur.toStr
169+
doc.elemById("loc-mon").text = Date.today.month.localeFull
170+
doc.elemById("loc-wd").text = Date.today.weekday.localeFull
171+
172+
Win.cur.setInterval(1sec) {
173+
doc.elemById("dt-now").text = DateTime.now.toLocale
174+
}
175+
}
176+
}

0 commit comments

Comments
 (0)