Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Box for typing messages made into a widget.

  • Loading branch information...
commit 29be2af230384d7a01f60683ea76bab565360dce 1 parent d9af958
@akoprow akoprow authored
View
50 src/msg.opa
@@ -5,8 +5,6 @@
package mlstate.twopenny
-import widgets.{core,button}
-
type Msg.t = private(string)
type Msg.segment =
@@ -15,10 +13,7 @@ type Msg.segment =
/ { url : Uri.uri }
/ { user : User.ref }
-Msg =
- MAXIMUM_MESSAGE_LENGTH = 140
- REMAINING_MESSAGE_LENGTH = 20
-{{
+Msg = {{
create(s : string) : Msg.t =
@wrap(s)
@@ -48,8 +43,7 @@ Msg =
content = List.map(render_segment, parse(msg))
<div class="msg">{content}</>
- submit_msg(user : User.ref, msgbox : dom) = _ ->
- content = Dom.get_value(msgbox)
+ submit_msg(user : User.ref)(content : string) =
msg = create(content)
do Data.new_message(user, msg)
index =
@@ -61,44 +55,4 @@ Msg =
do exec([#msgs +<- <>{render(msg)}</>])
void
- @private apply_css(css_style, dom) =
- WStyler.add(WStyler.make_style(css_style), dom)
-
- @client update_msg_counter(msgbox : dom, counter_id : string) = _ ->
- remaining = Dom.get_value(msgbox)
- |> String.length(_)
- |> MAXIMUM_MESSAGE_LENGTH - _
- counter_css =
- if remaining < REMAINING_MESSAGE_LENGTH then
- css { color: red }
- else
- []
- counter_xhtml = <span>{remaining}</>
- |> apply_css(counter_css, _)
- do exec([#{counter_id} <- counter_xhtml])
- void
-
- msgbox(id : string, user : User.ref) : xhtml =
- counter_id = "{id}_counter"
- accept = WButton.html(WButton.default_config, uniq(),
- [({click}, submit_msg(user, #{id}))], <>Tweet!</>)
- inputbox =
- /* WARNING! If we change the function call below
- into a closure then we have to be careful with
- slicing, as at the moment this whole function ends
- up on the server. But there must be a better
- way of doing it than the repetition below... */
- <textarea id=#{id}
- onready={update_msg_counter(#{id}, counter_id)}
- onkeypress={update_msg_counter(#{id}, counter_id)}
- onchange={update_msg_counter(#{id}, counter_id)} />
- counter =
- <div id=#{counter_id}></>;
- <>
- {inputbox}
- {counter}
- {accept}
- <div id=#msgs />
- </>
-
}}
View
60 src/msgbox.opa
@@ -0,0 +1,60 @@
+/*
+ * Twopenny. (C) MLstate - 2011
+ * @author Adam Koprowski
+**/
+
+package mlstate.twopenny
+
+import widgets.{core,button}
+
+WMsgBox =
+ MAXIMUM_MESSAGE_LENGTH = 140
+ REMAINING_MESSAGE_LENGTH = 20
+
+ get_input_text_id(id) = id
+ get_counter_id(id) = "{id}_counter"
+ get_submit_button_id(id) = "{id}_submit_button"
+
+ apply_css(css_style, dom) =
+ WStyler.add(WStyler.make_style(css_style), dom)
+
+ get_content(id) =
+ Dom.get_value(#{get_input_text_id(id)})
+{{
+
+ @client update_msg(id : string) = _ ->
+ remaining = get_content(id)
+ |> String.length(_)
+ |> MAXIMUM_MESSAGE_LENGTH - _
+ counter_css =
+ if remaining < REMAINING_MESSAGE_LENGTH then
+ css { color: red }
+ else
+ []
+ counter_xhtml = <span>{remaining}</>
+ |> apply_css(counter_css, _)
+ do exec([#{get_counter_id(id)} <- counter_xhtml])
+ void
+
+ html(id : string, submit : string -> void) : xhtml =
+ text_id = get_input_text_id(id)
+ inputbox =
+ /* WARNING! If we change the function call below
+ into a closure then we have to be careful with
+ slicing, as at the moment this whole function ends
+ up on the server. But there must be a better
+ way of doing it than the repetition below... */
+ <textarea id=#{text_id} onready={update_msg(id)}
+ onkeypress={update_msg(id)} onchange={update_msg(id)} />
+ counter =
+ <div id=#{get_counter_id(id)}></>
+ submit_msg(_) = submit(get_content(id))
+ accept = WButton.html(WButton.default_config, get_submit_button_id(id),
+ [({click}, submit_msg)], <>Tweet!</>)
+ <>
+ {inputbox}
+ {counter}
+ {accept}
+ </>
+
+}}
View
2  src/pages.opa
@@ -23,7 +23,7 @@ Pages = {{
content =
<>
<h1>User: {@unwrap(user)}</>
- {Msg.msgbox("msgbox", user)}
+ {WMsgBox.html("msgbox", Msg.submit_msg(user))}
</>
html("Twopenny :: {user}", content)
View
1  src/twopenny.opack
@@ -1,6 +1,7 @@
main.opa
pages.opa
msg.opa
+msgbox.opa
data.opa
user.opa
label.opa
Please sign in to comment.
Something went wrong with that request. Please try again.