Skip to content

Commit

Permalink
various updates
Browse files Browse the repository at this point in the history
  • Loading branch information
stevedonovan committed Sep 30, 2011
1 parent 534b119 commit 7ec2a06
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 41 deletions.
98 changes: 93 additions & 5 deletions examples/timer.lua
Expand Up @@ -4,26 +4,98 @@
local orbiter = require 'orbiter'
local html = require 'orbiter.html'
local jq = require 'orbiter.libs.jquery'
local form = require 'orbiter.form'

local timer = orbiter.new(html)

-- to use jq.timer(), must call this first
-- to use jq.timer(), must call this first
jq.use_timer()

local h2,h3,div,p = html.tags 'h2,h3,div,p'
local button_ = html.tags 'button'
local k = 0

local style = [[
#modalbox {
position: absolute;
left: 200;
top: 400;
width: 400;
background-color: #EEEEFF;
border: 2px solid #000099;
}
#buttonrow {
width: 100%;
background-color: #EEFFFF;
}
]]

local script = [[
function jq_close_form(div,id) {
$('#'+div).remove()
}
function jq_submit_form(div,id) {
var form = $("form#"+id)
$.post(form.attr("action"), form.serialize());
jq_close_form(div,id);
}
]]

obj = {
name = 'johnny',
age = 12
}

local f = form.new {
obj = obj, buttons = {},
action = "/form/submit",
"name","name",form.non_blank,
"age","age",form.range(1,120)
}

function timer:submit(web)
f:prepare(web)
print('ding',obj.name,obj.age)
return jq.alert("howzit dammit!")
end

timer:dispatch_post(timer.submit,'/form/submit')

function show_modal(f,web)
f:prepare(web)
return jq 'body' : append (
div { id = 'modalbox';
f:show();
html.table {id = 'buttonrow'; {
button_{"OK",onclick="jq_submit_form('modalbox','form1')"},
button_{"Cancel",onclick="jq_close_form('modalbox','form1')"},
}}
})
end

function button (label,callback)
local id = jq.data_to_id {click = callback}
return button_{label,
id = id; class='click-button';
onclick="jq_call_server('"..id.."','click-button','three')";
}
end

function timer:index(web)
return html {
title = 'Testing Timers';
inline_style = style;
inline_script = script;
h2 'Setting a timer',
jq.button('Click me!',function()
return jq "#content" : find 'p'
return jq "#content" : find 'p'
: eq(0) : html 'no more strings!'
: _end()
: eq(1) : html 'ditto!'
end),
jq.button("Start timer",function()
jq.button("Start timer",function()
return jq.timer(1000,function()
k = k + 1
if k == 6 then
Expand All @@ -33,20 +105,36 @@ function timer:index(web)
end
end)
end),
jq.button("append!",function()
return show_modal(f,web)
end),
button("hello",function()
return jq.alert("bonzo!")
end),
html.script(jq.timeout(500,function()
return jq 'h2' : css ('color','red') : after (
div { id = "content";
h3 'Lua Syntax for JQuery expressions',
p '(strings are irritating)',
h3 'Can be used with htmlification',
p '(avoiding more strings)'
}
}
)
end)),
end)),
}
end

timer:dispatch_get(timer.index,'/')

timer:run(...)

--[[
return jq 'body' : append (
div { id = 'block';
p 'new stuff after content',
button("remove me",function()
return jq("#block"):remove()
end)
}
)
]]
17 changes: 9 additions & 8 deletions orbiter/form.lua
Expand Up @@ -23,7 +23,7 @@ html.set_defaults {

function form.range(x1,x2)
return function(x)
if x < x1 or x > x2 then return false,'must be between %f and %f' % {x1,x2}
if x < x1 or x > x2 then return false,'must be between %f and %f' % {x1,x2}
else return x end
end
end
Expand All @@ -34,11 +34,11 @@ function form.match(pat,err)
end
end

form.non_blank = form.match('^%S+$', 'may not be blank')
form.non_blank = form.match('%S', 'may not be blank')

function form.irange(n1,n2)
return function(x)
if x < n1 or x > n2 then return false,'must be between %d and %d' % {n1,n2}
if x < n1 or x > n2 then return false,'must be between %d and %d' % {n1,n2}
elseif math.floor(x) ~= x then return false,'must be an integer'
else return x
end
Expand Down Expand Up @@ -125,6 +125,7 @@ local function generate_control(obj,var,constraint)
local vtype = type(value)
local cntrl
local converter = converters.string
--print(var,constraint,value,vtype)
if util.class_of(constraint,Constraint) then
print('value',value)
cntrl = constraint:control(var,value)
Expand Down Expand Up @@ -155,7 +156,7 @@ local function generate_control(obj,var,constraint)
return cntrl,converter,constraint
end

function form.new (t)
function form.new (t)
local f = { spec_of = {}, spec_table = t }
f.validate = form.validate
f.show = form.show
Expand Down Expand Up @@ -185,14 +186,14 @@ function form.create (self,web)
local cntrl,converter,constraint = generate_control(obj,var,constraint)
cntrl:set_attrib('id',id)
label = label_{['for']=id,title=label,label}
local spec = {label=label,cntrl=cntrl,converter=converter,constraint=constraint}
local spec = {label=label,cntrl=cntrl,converter=converter,constraint=constraint}
append(res,spec)
self.spec_of[var] = spec
end
self.spec_list = res
local contents
local ftype = spec.type or 'cols'
if ftype == 'cols' or ftype == 'rows' then -- wrap up as a table
if ftype == 'cols' or ftype == 'rows' then -- wrap up as a table
local tbl = {}
for i,item in ipairs(res) do
tbl[i] = {item.label,item.cntrl}
Expand All @@ -211,7 +212,7 @@ function form.create (self,web)
contents = fieldset_{legend_(spec.title),contents}
end
self.body = form_{
name = spec.name; action = spec.action, class = 'orbiter';
name = spec.name; id = spec.name; action = spec.action, class = 'orbiter';
method = spec.method or 'post';
contents,
}
Expand All @@ -237,7 +238,7 @@ function form.show(self)
end

function form.validate (self,values)
local ok = true
local ok = true
local res = {}
self.button = values[button_name]
for var,value in pairs(values) do
Expand Down
13 changes: 10 additions & 3 deletions orbiter/html.lua
Expand Up @@ -22,6 +22,7 @@ function _M.tostring(d)
end

_M.raw_tostring = doc.tostring
_M.escape = doc.xml_escape

function _M.literal(s)
return '\001'..s
Expand Down Expand Up @@ -82,6 +83,9 @@ function _M.document(t)
make_head(head,t,'scripts','script','text/javascript','src')
make_head(head,t,'inline_style','style','text/css')
make_head(head,t,'inline_script','script','text/javascript')
if t.refresh then
append(head,doc.elem('meta',{['http-equiv']='refresh',content=t.refresh}))
end
local data = t.body or t
local xmlns
if t.xml then xmlns = "http://www.w3.org/1999/xhtml" end
Expand All @@ -95,7 +99,9 @@ function _M.as_text(t)
end

--- the module is directly callable.
-- e.g. html { title = 'hello'; .... }
-- and is short for @{document}.
-- @usage html { title = 'hello'; .... }
-- @function html
setmetatable(_M,{
__call = function(o,t)
return _M.document(t)
Expand Down Expand Up @@ -197,8 +203,8 @@ local function table_arg(t)
local data = t.data or t
if t.map then
data = t.map(data)
end
-- assert (#data > 0)
end
-- assert (#data > 0)
return data
end

Expand Down Expand Up @@ -262,6 +268,7 @@ function _M.table(t)
end
res.border = t.border --???
copy_common(t,res)
res.width = t.width
local res = _table(res)
if t.styles then set_table_style(res,data,t.styles) end
return res
Expand Down
22 changes: 12 additions & 10 deletions orbiter/init.lua
Expand Up @@ -62,16 +62,16 @@ function _M.new(...)
local extensions = {...}
local obj
-- if passed a table which doesn't have register, then assume we're being called
-- from module()
-- from module()
-- use the module as the object, and manually add our methods to it.
if extensions[1] and not extensions[1]. register then
if extensions[1] and not extensions[1]. register then
obj = extensions[1]
local m = extensions[1]
for k,v in pairs(MT) do m[k] = v end
table.remove(extensions,1)
else
obj = setmetatable({},MT)
end
end
-- remember to strip off the starting @
local path = debug.getinfo(2, "S").source:sub(2):gsub('\\','/')
if path:find '/' then
Expand Down Expand Up @@ -116,7 +116,7 @@ local function which(prog)
end

function launch_browser (url,browser)
if browser == true then
if browser == true then
browser = nil -- autodetect!
end
if Windows then
Expand Down Expand Up @@ -311,7 +311,7 @@ local function send_headers (client,code, type, length)
end

-- process headers from a connection (borrowed from socket.http)
-- note that the variables have '-' replaced as '_' to make them WSAPI compatible
-- note that the variables have '-' replaced as '_' to make them WSAPI compatible
-- (e.g. HTTP_CONTENT_LENGTH)
local function receiveheaders(sock)
local line, name, value, err
Expand Down Expand Up @@ -408,7 +408,7 @@ function MT:run(...)
flags,args = _M.parse_flags(...)
local addr = flags['addr'] or 'localhost'
local port = flags['port'] or '8080'

local fake = flags['test']
local testing = flags['no_headers'] and fake
last_obj = self
Expand All @@ -424,7 +424,7 @@ function MT:run(...)
os.exit()
end

if fake then
if fake then
addr = fake==true and '/' or fake
end

Expand All @@ -436,7 +436,7 @@ function MT:run(...)
print ("Orbiter serving on "..URL)
if flags['launch'] then
launch_browser(URL,flags['launch'])
end
end
end
-- loop while waiting for a user agent request
while 1 do
Expand All @@ -450,11 +450,13 @@ function MT:run(...)
local method = request:match '^([A-Z]+)'
if not fake then
headers,err = receiveheaders(client)
--for k,v in pairs(headers) do print(k,v) end
headers.HTTP_REMOTE = client:getpeername()
end
if err then quit('header error: '..err) end
if method == 'POST' then
local size = tonumber(headers.HTTP_CONTENT_LENGTH)
vars = client:receive(size)
vars = client:receive(size)
if tracing then trace('post: '..vars) end
end
-- resolve requested file from user agent request
Expand All @@ -481,7 +483,7 @@ function MT:run(...)
status = false
end
else
print('exception: '..tostring(content))
print('exception: '..tostring(content))
end
if content then -- can naturally be nil for POST requests!
if status then
Expand Down

0 comments on commit 7ec2a06

Please sign in to comment.