|
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 | | -** JsHelloMain |
18 | | -************************************************************************* |
19 | | - |
20 | | -class JsHelloMain : 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 = JsHelloMod() |
31 | | - } |
32 | | - return runServices([wisp]) |
33 | | - } |
34 | | -} |
35 | | - |
36 | | -************************************************************************* |
37 | | -** JsHelloMod |
38 | | -************************************************************************* |
39 | | - |
40 | | -const class JsHelloMod : WebMod |
41 | | -{ |
42 | | - new make() |
43 | | - { |
44 | | - pods := ["sys"].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 "hello.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("Hello World, from Fantom JS!").titleEnd |
68 | | - .initJs(["main":"hello::JsHello"]) |
69 | | - .includeJs(`/hello.js`) |
70 | | - .style.w( |
71 | | - "body { |
72 | | - padding: 0.25em 2em; |
73 | | - font: 16px -apple-system, BlinkMacSystemFont, sans-serif; |
74 | | - }").styleEnd |
75 | | - out.headEnd |
76 | | - out.body |
77 | | - .h1.w("Hello World, from Fantom JS!").h1End |
78 | | - .p.w("Check your JavaScript console for <code>echo()</code> output").pEnd |
79 | | - out.bodyEnd |
80 | | - out.htmlEnd |
81 | | - } |
82 | | - |
83 | | - ** |
84 | | - ** Normally your @Js code would exist in a pod, but since |
85 | | - ** this is a Fantom script, we need to compile on the fly |
86 | | - ** in order to get JS output to add to FilePack. |
87 | | - ** |
88 | | - private File compileScriptJs() |
89 | | - { |
90 | | - src := Env.cur.homeDir + `examples/js/hello.fan` |
91 | | - js := Env.cur.compileScriptToJs(src, ["podName":"hello"]) |
92 | | - temp := Env.cur.tempDir + `dom.js` |
93 | | - temp.out.print(js).sync.close |
94 | | - return temp |
95 | | - } |
96 | | - |
97 | | - private const FilePack jsPack |
98 | | -} |
99 | | - |
100 | | -************************************************************************* |
101 | | -** JsHello |
102 | | -************************************************************************* |
103 | | - |
104 | | -@Js class JsHello |
105 | | -{ |
106 | | - static Void main() |
107 | | - { |
108 | | - echo("Hello there! This is from JsHello!") |
109 | | - } |
110 | | -} |
| 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 | +** JsHelloMain |
| 18 | +************************************************************************* |
| 19 | + |
| 20 | +class JsHelloMain : 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 = JsHelloMod() |
| 31 | + } |
| 32 | + return runServices([wisp]) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +************************************************************************* |
| 37 | +** JsHelloMod |
| 38 | +************************************************************************* |
| 39 | + |
| 40 | +const class JsHelloMod : WebMod |
| 41 | +{ |
| 42 | + new make() |
| 43 | + { |
| 44 | + pods := ["sys"].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 "hello.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("Hello World, from Fantom JS!").titleEnd |
| 68 | + .initJs(["main":"hello::JsHello"]) |
| 69 | + .includeJs(`/hello.js`) |
| 70 | + .style.w( |
| 71 | + "body { |
| 72 | + padding: 0.25em 2em; |
| 73 | + font: 16px -apple-system, BlinkMacSystemFont, sans-serif; |
| 74 | + }").styleEnd |
| 75 | + out.headEnd |
| 76 | + out.body |
| 77 | + .h1.w("Hello World, from Fantom JS!").h1End |
| 78 | + .p.w("Check your JavaScript console for <code>echo()</code> output").pEnd |
| 79 | + out.bodyEnd |
| 80 | + out.htmlEnd |
| 81 | + } |
| 82 | + |
| 83 | + ** |
| 84 | + ** Normally your @Js code would exist in a pod, but since |
| 85 | + ** this is a Fantom script, we need to compile on the fly |
| 86 | + ** in order to get JS output to add to FilePack. |
| 87 | + ** |
| 88 | + private File compileScriptJs() |
| 89 | + { |
| 90 | + src := Env.cur.homeDir + `examples/js/hello.fan` |
| 91 | + js := Env.cur.compileScriptToJs(src, ["podName":"hello"]) |
| 92 | + temp := Env.cur.tempDir + `dom.js` |
| 93 | + temp.out.print(js).sync.close |
| 94 | + return temp |
| 95 | + } |
| 96 | + |
| 97 | + private const FilePack jsPack |
| 98 | +} |
| 99 | + |
| 100 | +************************************************************************* |
| 101 | +** JsHello |
| 102 | +************************************************************************* |
| 103 | + |
| 104 | +@Js class JsHello |
| 105 | +{ |
| 106 | + static Void main() |
| 107 | + { |
| 108 | + echo("Hello there! This is from JsHello!") |
| 109 | + } |
| 110 | +} |
0 commit comments