Skip to content

Commit e9773c3

Browse files
author
hartsantler
committed
js backend: HTMLElement->(args) is a helper for common HTML DOM calls: create text-nodes, append elements, set attributes.
1 parent 86bc4c9 commit e9773c3

File tree

3 files changed

+91
-67
lines changed

3 files changed

+91
-67
lines changed

examples/hello_fullstack.md

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ tornado.ioloop.IOLoop.instance().start()
126126
Client Side
127127
-----------
128128

129+
note: the syntax `HTMLElement->(args)` is a shortcut for common HTML DOM calls,
130+
it quickly lets you create text-nodes, append multiple elements, or set attributes.
131+
129132

130133
@myapp
131134
```rusthon
@@ -152,14 +155,13 @@ def on_message_ws(event):
152155
else:
153156
msg = JSON.parse(event.data)
154157

155-
156158
pre = document.getElementById('RESULTS')
157159
if isinstance(msg, list):
158160
for res in msg:
159161
s = JSON.stringify(res)
160-
pre.appendChild( document.createTextNode(s+'\n') )
162+
pre->(s+'\n')
161163
else:
162-
pre.appendChild( document.createTextNode(msg+'\n') )
164+
pre->(msg+'\n')
163165

164166
print msg
165167

@@ -183,70 +185,68 @@ def main():
183185
try: connect_ws()
184186
except: print 'could not connect to websocket'
185187

186-
with 𝔼 as "document.createElement(%s)":
187-
with 𝕋 as "document.createTextNode(%s)":
188-
189-
con = document.getElementById('FORM')
190-
h = 𝔼('h3')
191-
h.appendChild(𝕋('update database:'))
192-
con.appendChild(h)
193-
keys = ('first name', 'last name', 'age')
194-
fields = {}
195-
for key in keys:
196-
input = 𝔼('input')
197-
input.setAttribute('type', 'text')
198-
fields[key] = input
199-
con.appendChild(𝕋(key))
200-
con.appendChild(input)
201-
con.appendChild(𝔼('br'))
202-
203-
button = 𝔼('button')
204-
button.appendChild(𝕋('submit'))
205-
con.appendChild(button)
206-
@bind(button.onclick)
207-
def onclick():
208-
ob = {}
209-
for key in fields.keys():
210-
elt = fields[key]
211-
ob[key] = elt.value
212-
213-
jsondata = JSON.stringify(ob)
214-
ws.send(jsondata)
215-
216-
searchform = document.getElementById('SEARCH')
217-
if searchform is None:
218-
searchform = 𝔼('div')
219-
searchform.setAttribute('id', 'SEARCH')
220-
document.body.appendChild(searchform)
221-
h = 𝔼('h3')
222-
h.appendChild(𝕋('search database:'))
223-
searchform.appendChild( h )
224-
225-
search_fields = {}
226-
for key in keys:
227-
input = 𝔼('input')
228-
input.setAttribute('type', 'text')
229-
search_fields[key] = input
230-
searchform.appendChild(𝕋(key))
231-
searchform.appendChild(input)
232-
searchform.appendChild(𝔼('br'))
233-
234-
235-
sbutton = 𝔼('button')
236-
sbutton.appendChild(𝕋('search'))
237-
searchform.appendChild(sbutton)
238-
@bind(sbutton.onclick)
239-
def onsearch():
240-
s = []
241-
for key in search_fields.keys():
242-
elt = search_fields[key]
243-
## note ES6 syntax for a computed key name `[key]` ##
244-
o = {
245-
[key] : elt.value
246-
}
247-
s.append( o )
248-
ws.send( JSON.stringify(s) )
249-
188+
with 𝕖𝕝𝕥 as "document.createElement(%s)":
189+
190+
con = document.getElementById('FORM')
191+
h = 𝕖𝕝𝕥('h3')->('Update Database:')
192+
con->(h)
193+
194+
keys = ('first name', 'last name', 'age')
195+
fields = {}
196+
for key in keys:
197+
input = 𝕖𝕝𝕥('input')->(type='text')
198+
fields[key] = input
199+
con->(
200+
key,
201+
input,
202+
𝕖𝕝𝕥('br')
203+
)
204+
205+
button = 𝕖𝕝𝕥('button')
206+
button->('submit')
207+
con->(button)
208+
209+
@bind(button.onclick)
210+
def onclick():
211+
ob = {}
212+
for key in fields.keys():
213+
elt = fields[key]
214+
ob[key] = elt.value
215+
216+
jsondata = JSON.stringify(ob)
217+
ws.send(jsondata)
218+
219+
searchform = document.getElementById('SEARCH')
220+
if searchform is None:
221+
searchform = 𝕖𝕝𝕥('div')->(id="SEARCH")
222+
document.body.appendChild(searchform)
223+
searchform->( 𝕖𝕝𝕥('h3')->('search database:') )
224+
225+
search_fields = {}
226+
for key in keys:
227+
input = 𝕖𝕝𝕥('input')->(type='text')
228+
search_fields[key] = input
229+
searchform->(
230+
key,
231+
input,
232+
𝕖𝕝𝕥('br')
233+
)
234+
235+
236+
sbutton = 𝕖𝕝𝕥('button')->('search')
237+
searchform->(sbutton)
238+
239+
@bind(sbutton.onclick)
240+
def onsearch():
241+
s = []
242+
for key in search_fields.keys():
243+
elt = search_fields[key]
244+
## note ES6 syntax for a computed key name `[key]` ##
245+
o = {
246+
[key] : elt.value
247+
}
248+
s.append( o )
249+
ws.send( JSON.stringify(s) )
250250

251251

252252
```

src/runtime/builtins_core.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@
88
inline('WebWorkerError = function(msg) {this.message = msg || "";}; WebWorkerError.prototype = Object.create(Error.prototype);WebWorkerError.prototype.name = "WebWorkerError";')
99
inline('TypeError = function(msg) {this.message = msg || "";}; TypeError.prototype = Object.create(Error.prototype);TypeError.prototype.name = "TypeError";')
1010

11+
if HTMLElement is not undefined:
12+
@bind(HTMLElement.prototype.__right_arrow__)
13+
def __auto_dom__():
14+
for item in arguments:
15+
T = typeof(item)
16+
if instanceof(item, HTMLElement):
17+
this.appendChild( item )
18+
elif instanceof(item, Text): ## a text node create by `document.createTextNode`
19+
this.appendChild( item )
20+
elif T=='string':
21+
this.appendChild( document.createTextNode(item) )
22+
elif T=='function':
23+
raise RuntimeError('HTMLElement->(lambda function) is invalid')
24+
elif T=='object':
25+
for key in item.keys():
26+
this.setAttribute(key, item[key])
27+
else:
28+
raise RuntimeError('HTMLElement->(invalid type): '+ item)
29+
30+
return this
31+
1132
# the unicode decorator is used in the global namespace to define
1233
# a mapping from the function name to the unicode version of the name.
1334
# the user is able to then define their own unicode scripting language,

src/typedpython.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,9 @@ class typedpython:
835835
#this_name = a.split()[-1].split('=')[-1].split(':')[-1].split(',')[-1]
836836
#method_name = b.split()[0].split('(')[0]
837837
#c = c.replace('->'+method_name, '.__right_arrow__<<'+method_name)
838+
839+
c = c.replace('->(', '.__right_arrow__(')
840+
c = c.replace('->[', '.__right_arrow__[')
838841
c = c.replace('->', '.__right_arrow__.')
839842

840843

0 commit comments

Comments
 (0)