Restructure callbacks

This commit is contained in:
YNH Webdev
2013-09-22 18:30:48 +02:00
parent f360e8a867
commit 3a9ede6e8c
10 changed files with 2023 additions and 722 deletions

View File

@@ -12,7 +12,7 @@ inherit
WSF_VALUE_CONTROL [LIST [STRING]]
undefine
load_state,
read_state,
full_state,
read_state_changes
end

View File

@@ -35,7 +35,7 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
load_state (new_states: JSON_OBJECT)
-- Select state stored with `control_name` as key
do
if attached {JSON_OBJECT} new_states.item (control_name) as new_state_obj then
if attached {JSON_OBJECT} new_states.item ("state") as new_state_obj then
set_state (new_state_obj)
end
end
@@ -45,10 +45,11 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
deferred
end
read_state (states: JSON_OBJECT)
-- Add a new entry in the `states` JSON object with the `control_name` as key and the `state` as value
full_state: JSON_OBJECT
-- Return state of object
do
states.put (state, control_name)
create Result.make
Result.put (state, "state")
end
read_state_changes (states: JSON_OBJECT)
@@ -62,6 +63,8 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
state: JSON_OBJECT
-- Returns the current state of the Control as JSON. This state will be transfered to the client.
deferred
ensure
controls_not_defined: not (attached Result.item ("controls"))
end
state_changes: JSON_OBJECT

View File

@@ -13,7 +13,7 @@ inherit
redefine
read_state_changes,
load_state,
read_state
full_state
end
WSF_VALIDATABLE
@@ -52,7 +52,9 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
-- Pass new_states to subcontrols
do
Precursor (new_states)
value_control.load_state (new_states)
if attached {JSON_OBJECT} new_states.item ("controls") as ct and then attached {JSON_OBJECT} ct.item (value_control.control_name) as value_state then
value_control.load_state (value_state)
end
end
set_state (new_state: JSON_OBJECT)
@@ -61,11 +63,14 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
value_control.set_state (new_state)
end
read_state (states: JSON_OBJECT)
-- Read states in subcontrols
full_state: JSON_OBJECT
local
controls_state: JSON_OBJECT
do
Precursor (states)
value_control.read_state (states)
Result := Precursor
create controls_state.make
controls_state.put (value_control.full_state, value_control.control_name)
Result.put (controls_state, "controls")
end
read_state_changes (states: JSON_OBJECT)

View File

@@ -11,7 +11,7 @@ inherit
WSF_CONTROL
redefine
read_state,
full_state,
read_state_changes,
load_state
end
@@ -31,7 +31,7 @@ feature {NONE} -- Initialization
-- Initialize with specified control name and tag
do
make_control (n, t)
controls := create {ARRAYED_LIST [G]}.make(5);
controls := create {ARRAYED_LIST [G]}.make (5);
end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
@@ -40,11 +40,15 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
-- Pass new_states to subcontrols
do
Precursor (new_states)
across
controls as c
loop
if attached {WSF_CONTROL} c.item as cont then
cont.load_state (new_states)
if attached {JSON_OBJECT} new_states.item ("controls") as ct then
across
controls as c
loop
if attached {WSF_CONTROL} c.item as cont then
if attached {JSON_OBJECT} ct.item (cont.control_name) as value_state then
cont.load_state (value_state)
end
end
end
end
end
@@ -54,17 +58,21 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
do
end
read_state (states: JSON_OBJECT)
full_state: JSON_OBJECT
-- Read states in subcontrols
local
controls_state: JSON_OBJECT
do
Precursor (states)
Result := Precursor
create controls_state.make
across
controls as c
loop
if attached {WSF_CONTROL} c.item as cont then
cont.read_state (states)
controls_state.put (cont.full_state, cont.control_name)
end
end
Result.put (controls_state, "controls")
end
read_state_changes (states: JSON_OBJECT)

View File

@@ -47,21 +47,24 @@ feature -- Implementation
local
event: detachable STRING
event_parameter: detachable STRING
control_name: detachable STRING
states: detachable STRING
event_control_name: detachable STRING
states: STRING
states_changes: JSON_OBJECT
json_parser: JSON_PARSER
do
control_name := get_parameter ("control_name")
event_control_name := get_parameter ("control_name")
event := get_parameter ("event")
event_parameter := get_parameter ("event_parameter")
states := get_parameter ("states")
if attached event and attached control_name and attached control and attached states then
if attached event and attached event_control_name and attached control then
create states.make_empty
request.read_input_data_into (states)
create json_parser.make_parser (states)
if attached {JSON_OBJECT} json_parser.parse_json as sp then
control.load_state (sp)
if attached {JSON_OBJECT} sp.item ("controls") as ct and then attached {JSON_OBJECT} ct.item (control.control_name) as value_state then
control.load_state (value_state)
end
end
control.handle_callback (control_name, event, event_parameter)
control.handle_callback (event_control_name, event, event_parameter)
create states_changes.make
control.read_state_changes (states_changes)
response.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "application/json; charset=ISO-8859-1"]>>)
@@ -82,16 +85,14 @@ feature -- Implementation
data := "<html><head>"
data.append ("<link href=%"/bootstrap.min.css%" rel=%"stylesheet%">")
data.append ("<link href=%"/widget.css%" rel=%"stylesheet%">")
data.append ("</head><body>")
data.append ("</head><body data-name=%"" + control_name + "%" data-type=%"WSF_PAGE_CONTROL%">")
data.append (control.render)
data.append ("<script type=%"text/javascript%">window.states=")
create states.make
control.read_state (states)
data.append (states.representation)
data.append (";</script>")
data.append ("<script src=%"/jquery.min.js%"></script>")
data.append ("<script src=%"/typeahead.min.js%"></script>")
data.append ("<script src=%"/widget.js%"></script>")
data.append ("<script type=%"text/javascript%">$(function() {var page= new WSF_PAGE_CONTROL(")
data.append (full_state.representation)
data.append (");page.attach_events();});</script>")
data.append ("</body></html>")
create page.make
page.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "text/html; charset=ISO-8859-1"]>>)
@@ -99,6 +100,28 @@ feature -- Implementation
response.send (page)
end
control_name: STRING
do
Result := request.request_time_stamp.out
end
state: JSON_OBJECT
do
create Result.make
Result.put (create {JSON_STRING}.make_json (control_name), "id")
end
full_state: JSON_OBJECT
local
controls_state: JSON_OBJECT
do
create Result.make
create controls_state.make
controls_state.put (control.full_state, control.control_name)
Result.put (controls_state, "controls")
Result.put (state, "state")
end
get_parameter (key: STRING): detachable STRING
-- Read query parameter as string
local