Communication in both directions (Text control)

Code regrouping
This commit is contained in:
YNH Webdev
2013-08-28 12:52:09 +02:00
parent b72e6871e8
commit b7ab840d71
8 changed files with 255 additions and 70 deletions

View File

@@ -23,11 +23,24 @@ feature {NONE}
click_event := agent donothing
end
feature
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
--UGLY HACK MUST BE REMOVED
set_state (new_state: JSON_OBJECT)
do
if attached {JSON_STRING} new_state.item (create {JSON_STRING}.make_json ("text")) as new_text then
text := new_text.unescaped_string_32
end
end
donothing (p: WSF_PAGE_CONTROL)
state: JSON_OBJECT
do
create Result.make
Result.put (create {JSON_STRING}.make_json (text), create {JSON_STRING}.make_json ("text"))
end
feature --EVENT HANDLING
donothing (p: WSF_PAGE_CONTROL) --UGLY HACK MUST BE REMOVED
do
end
@@ -43,17 +56,13 @@ feature
end
end
feature
render: STRING
do
Result := "<button data-name=%"" + control_name + "%" data-type=%"WSF_BUTTON_CONTROL%">" + text + "</button>"
end
state: JSON_OBJECT
do
create Result.make
Result.put (create {JSON_STRING}.make_json (text), create {JSON_STRING}.make_json ("text"))
end
set_text (t: STRING)
do
text := t

View File

@@ -11,18 +11,16 @@ feature
control_name: STRING
feature {WSF_PAGE_CONTROL, WSF_CONTROL}
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
handle_callback (cname: STRING; event: STRING; page: WSF_PAGE_CONTROL)
deferred
end
render: STRING
deferred
end
state: JSON_OBJECT
deferred
load_state (new_states: JSON_OBJECT)
local
new_state: detachable JSON_VALUE
do
new_state := new_states.item (create {JSON_STRING}.make_json (control_name))
if attached {JSON_OBJECT} new_state as new_state_obj then
set_state (new_state_obj)
end
end
read_state (states: JSON_OBJECT)
@@ -30,4 +28,24 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL}
states.put (state, create {JSON_STRING}.make_json (control_name))
end
set_state (new_state: JSON_OBJECT)
deferred
end
state: JSON_OBJECT
deferred
end
feature --EVENT HANDLING
handle_callback (cname: STRING; event: STRING; page: WSF_PAGE_CONTROL)
deferred
end
feature
render: STRING
deferred
end
end

View File

@@ -11,7 +11,8 @@ inherit
WSF_CONTROL
redefine
read_state
read_state,
load_state
end
create
@@ -27,32 +28,28 @@ feature {NONE}
controls := create {LINKED_LIST [WSF_CONTROL]}.make;
end
feature
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
add_control (c: WSF_CONTROL)
load_state (new_states: JSON_OBJECT)
do
controls.put_front (c)
end
handle_callback (event: STRING; cname: STRING; page: WSF_PAGE_CONTROL)
do
if equal (cname, control_name) then
else
across
controls as c
loop
c.item.handle_callback (event, cname, page)
end
end
end
render: STRING
do
Result := ""
across
controls as c
loop
Result := Result + c.item.render
c.item.load_state (new_states)
end
end
set_state (new_state: JSON_OBJECT)
do
end
read_state (states: JSON_OBJECT)
do
states.put (state, create {JSON_STRING}.make_json (control_name))
across
controls as c
loop
c.item.read_state (states)
end
end
@@ -68,13 +65,35 @@ feature
end
end
read_state (states: JSON_OBJECT)
feature --EVENT HANDLING
handle_callback (event: STRING; cname: STRING; page: WSF_PAGE_CONTROL)
do
states.put (state, create {JSON_STRING}.make_json (control_name))
if equal (cname, control_name) then
else
across
controls as c
loop
c.item.handle_callback (event, cname, page)
end
end
end
feature
render: STRING
do
Result := ""
across
controls as c
loop
c.item.read_state(states)
Result := c.item.render + Result
end
end
add_control (c: WSF_CONTROL)
do
controls.put_front (c)
end
end

View File

@@ -40,16 +40,23 @@ feature
local
event: detachable STRING
control_name: detachable STRING
states: JSON_OBJECT
states: detachable STRING
new_states: JSON_OBJECT
json_parser: JSON_PARSER
do
control_name := get_parameter ("control_name")
event := get_parameter ("event")
if attached event and attached control_name and attached control then
states := get_parameter ("states")
if attached event and attached control_name and attached control and attached states then
create json_parser.make_parser (states)
if attached {JSON_OBJECT} json_parser.parse_json as sp then
control.load_state (sp)
end
control.handle_callback (control_name, event, Current)
create states.make
control.read_state (states)
create new_states.make
control.read_state (new_states)
response.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "application/json"]>>)
response.put_string (states.representation)
response.put_string (new_states.representation)
else
process
render
@@ -64,18 +71,17 @@ feature
do
create states.make
control.read_state (states)
data := "<html><head>"
data.append ("</head><body>")
data.append (control.render)
data.append ("<script type=%"text/javascript%">var states=")
data.append ("<script type=%"text/javascript%">window.states=")
data.append (states.representation)
data.append (";</script>")
data.append ("<script src=%"//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js%"></script>")
data.append ("<script src=%"/widget.js%"></script>")
data.append ("</body></html>")
create page.make
page.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "text/html"]>>)
page.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "text/html"]>>)
page.set_body (data)
response.send (page)
end

View File

@@ -0,0 +1,79 @@
note
description: "Summary description for {WSF_TEXT_CONTROL}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
WSF_TEXT_CONTROL
inherit
WSF_CONTROL
create
make
feature {NONE}
make (n: STRING; v: STRING)
do
control_name := n
text := v
change_event := agent donothing
end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
set_state (new_state: JSON_OBJECT)
do
if attached {JSON_STRING} new_state.item (create {JSON_STRING}.make_json ("text")) as new_text then
text := new_text.unescaped_string_32
end
end
state: JSON_OBJECT
do
create Result.make
Result.put (create {JSON_STRING}.make_json (text), create {JSON_STRING}.make_json ("text"))
end
feature --EVENT HANDLING
donothing (p: WSF_PAGE_CONTROL) --UGLY HACK MUST BE REMOVED
do
end
set_change_event (e: PROCEDURE [ANY, TUPLE [WSF_PAGE_CONTROL]])
do
change_event := e
end
handle_callback (cname: STRING; event: STRING; page: WSF_PAGE_CONTROL)
do
if Current.control_name.is_equal (cname) and attached change_event then
if event.is_equal ("change") then
change_event.call ([page])
end
end
end
feature
render: STRING
do
Result := "<input type=%"text%" data-name=%"" + control_name + "%" data-type=%"WSF_TEXT_CONTROL%" value=%"" + text + "%" />"
end
set_text (t: STRING)
do
text := t
end
feature
text: STRING
change_event: PROCEDURE [ANY, TUPLE [WSF_PAGE_CONTROL]]
end