Skip to content

Commit

Permalink
Two new apps, counter and calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
cadar committed Apr 20, 2009
1 parent 3687666 commit a9cceae
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 10 deletions.
35 changes: 35 additions & 0 deletions examples/web_calc.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(define-module web_calc
(export all))

(include-file "lfeweb/wf.lfe")

(defun main () (make-template file '"./wwwroot/template.html"))

(defun title () '"Web calculator")

(defun body ()
(list
(make-textbox id 'number1 text '"")
(make-br)
(make-textbox id 'number2 text '"")
(make-br)
(make-button id 'add text '"+" postback 'add_button)
(make-button text '"c" postback 'clear_button)
(make-panel id 'res1)
(make-hr)
(make-link url '"viewsource?module=web_calc" text '"source")))

(defun event
(('add_button)
(let* ((s1 (: wf q 'number1))
(s2 (: wf q 'number2))
(n1 (list_to_integer (: lists flatten s1)))
(n2 (list_to_integer (: lists flatten s2)))
(sum1 (+ n1 n2)))
(: wf update 'res1 (integer_to_list sum1)))
'ok)
(('clear_button)
(: wf wire '"obj('number1').value=''; obj('number2').value='';") 'ok)
((all) (: wf flash (: wf f '"no match, ~p~n" (list all))) 'ok))


28 changes: 28 additions & 0 deletions examples/web_counter.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(define-module web_counter
(export all))

(include-file "lfeweb/wf.lfe")

(defun main () (make-template file '"./wwwroot/template.html"))

(defun title () '"Counter")

(defun body ()
(list
(make-panel id 'counter_and_hvalue body (list '"0"
(make-hidden id 'value1 text '"0")))
(make-br)
(make-button text '"++" postback (tuple 'inc1 1))
(make-button text '"--" postback (tuple 'inc1 -1))
(make-hr)
(make-link url '"viewsource?module=web_counter" text '"source")))

(defun event
(((tuple 'inc1 d)) (let ((next_value (integer_to_list (+ d (list_to_integer (hd (: wf q 'value1)))))))
(: wf update 'counter_and_hvalue
(make-panel body (list next_value
(make-hidden id 'value1 text next_value)))))
'ok)
((all) (: wf flash (: wf f '"no match, ~p~n" (list all)))
'ok))

3 changes: 2 additions & 1 deletion examples/web_index2.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
(make-br)


(make-panel class '"abstract" body '"Abstract: Lfeweb is using Lfe (Lisp Flavoured Erlang) to add lisp syntax to Nitrogen. Lfe, created by Roberg Virding, is as a lisp syntax front-end to the Erlang compiler. Nitrogen is a web framework created by Rusty Klophaus that put javascript, comet and jquery functionality in the background, making it easier to work on an even higher abstraction level than before. This page hope to demonstrate how much of Nitrogen is working using Lfe – and hopefully – how addictive the lisp syntax is.")

(make-panel class '"abstract" body '"Abstract: Lfeweb is using Lfe (Lisp Flavoured Erlang) to add lisp syntax to Nitrogen. Lfe, created by Roberg Virding, is as a lisp syntax front-end to the Erlang compiler. Nitrogen is a web framework created by Rusty Klophaus that put javascript, comet and jquery functionality in the background, making it possible to work on an even higher abstraction level than before. This page try to demonstrate how much of Nitrogen is working using Lfe – and hopefully – how addictive the lisp syntax is.")
(make-br)
(make-br)

Expand Down
27 changes: 27 additions & 0 deletions examples/web_piki.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(define-module web_piki
(export all))

(include-file "lfeweb/wf.lfe")

(defun main () (make-template file '"./wwwroot/template.html"))

(defun title () '"Piki")

(defun body ()
(list

(make-p body '"Text")
(make-textarea id 'area1 text '"non")))

(defun event
(('checkbox_clicked) (: wf flash (list '"Clicked, " (: wf q 'check1))) 'ok)
(('box1) (: wf flash (list '"box1 is " (: wf q 'box1))) 'ok)
(('save) (: wf flash (list (: wf q 'inbox1)
'" Saved "
(: wf q 'area1))) 'ok)
(('clear) (: wf wire '"obj('area1').value=''") 'ok)
((all) (: wf flash (: wf f '"no match, ~p~n" (list all))) 'ok))

(defun inplace_textbox_event (tag value)
(: io format '"~s~n" (list value))
value)
7 changes: 3 additions & 4 deletions examples/web_sort.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
(make-button id 'sendbutton text '"Send" postback 'chat)
(make-hr)
(make-link url '"viewsource?module=web_sort" text '"source")))
(pid (: wf comet (lambda () (listenformessages))))
)
(pid (: wf comet (lambda () (listen_for_messages)))))
(: io format '"comet pid is ~p, registered database is ~p~n" (list pid (whereis 'database)))
(! 'database (tuple 'join pid))
(: wf render body)))
Expand All @@ -36,7 +35,7 @@
((all) (: wf flash (: wf f '"no match, ~p~n" (list all))) 'ok))


(defun listenformessages ()
(defun listen_for_messages ()
(receive
((tuple 'message username message)
(let ((term (list
Expand All @@ -49,7 +48,7 @@
(: wf wire '"obj('chathistory').scrollTop = obj('chathistory').scrollHeight;")
(: io format '"incoming: ~p~p~n" (list username message))
(: wf comet_flush))))
(listenformessages))
(listen_for_messages))


(defun thedatabase (users)
Expand Down
12 changes: 8 additions & 4 deletions include.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ vpath %.lfe ./lfeweb/examples
vpath %.beam ./ebin


LSRCS=web_blog.lfe web_viewsource.lfe web_vote.lfe web_sort.lfe web_link.lfe web_chat.lfe $(AND_FILE)
LSRCS=web_blog.lfe web_viewsource.lfe web_vote.lfe \
web_sort.lfe web_link.lfe web_chat.lfe web_piki.lfe \
web_counter.lfe web_calc.lfe $(AND_FILE)
LOBJS=$(LSRCS:.lfe=.beam)

ERL_LOAD='code:load_file(lfe_comp).'
Expand All @@ -28,13 +30,15 @@ FLY_BEAM=$(notdir $(CHK_SOURCES:.lfe=.beam))
BEAM=$(notdir $(CHK_SOURCES:_flymake.lfe=.beam))
MODULE=$(notdir $(CHK_SOURCES:_flymake.lfe=))

# prerequisite 1. Only one screen, 2. run "screen","screen -t server1","sh start.sh"
# Install mozrepl for page reload, http://wiki.github.com/bard/mozrepl

check-syntax:
erl -noshell -pa ${HOME}/elib/lfe/ebin -eval $(ERL_LOAD) -eval $(ERL_COMP) -extra $(CHK_SOURCES)
# If flymake-mode is not working, comment lines below.
mv ebin/$(FLY_BEAM) ebin/$(BEAM) > compile.out 2> compile.err
# prerequisite 1. Only one screen, 2. run "screen","screen -t server1","sh start.sh"
@screen -p server1 -X stuff $''code:purge($(MODULE)),code:load_file($(MODULE)).' >> compile.out 2>> compile.err
# Install mozrepl for page reload, http://wiki.github.com/bard/mozrepl
@screen -p server1 -X stuff $''code:purge($(MODULE)),code:load_file($(MODULE)).' \
>> compile.out 2>> compile.err
@echo BrowserReload\(\)\; repl.quit\(\) | nc localhost 4242 >> compile.out 2>> compile.err


Expand Down
18 changes: 17 additions & 1 deletion wf.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@
(defrecord upload
(module 'element_upload) id actions (show_if 'true) (class '"") (style '"") tag
(show_button 'true) (button_text '"Upload"))

(defrecord validate (module 'element_validate)
id actions (show_if 'true) (class '"") (style '"")
(on 'submit) (success_text '" ") 'validators 'attacg_to)
(defrecord validation_error (module 'element_validation_error)
id actions (show_if 'true) (class '"") (style '"")
(text '""))
(defrecord alert (module 'element_alert) id actions (show_if 'true) (class '"") (style '"") (text '""))
(defrecord flash (module 'element_flash) id actions (show_if 'true) (class '"") (style '""))
(defrecord script (module 'action_script) id actions (show_if 'true) (class '"") (style '"") script)
Expand All @@ -51,3 +56,14 @@



;; -define(VALIDATOR_BASE(Module), module=Module, text="Failed.").
;; -record(validatorbase, {?VALIDATOR_BASE(undefined)}).
;; -record(is_required, {?VALIDATOR_BASE(validator_is_required)}).
(defrecord is_required (module 'validator_is_required) (text '"Failed."))
;; -record(is_email, {?VALIDATOR_BASE(validator_is_email)}).
;; -record(is_integer, {?VALIDATOR_BASE(validator_is_integer)}).
;; -record(min_length, {?VALIDATOR_BASE(validator_min_length), length}).
;; -record(max_length, {?VALIDATOR_BASE(validator_max_length), length}).
;; -record(confirm_password, {?VALIDATOR_BASE(validator_confirm_password), password}).
;; -record(custom, {?VALIDATOR_BASE(validator_custom), function, tag }).
;; -record(js_custom, {?VALIDATOR_BASE(validator_js_custom), function, args="{}" }).

0 comments on commit a9cceae

Please sign in to comment.