Introduce actions

First working modal
This commit is contained in:
YNH Webdev
2013-09-23 00:15:43 +02:00
parent 7dd726ca42
commit ca633d3524
8 changed files with 230 additions and 50 deletions

View File

@@ -30,10 +30,35 @@ feature {NONE} -- Initialization
make (a_tag_name)
control_name := n
create state_changes.make
create actions.make_array
ensure
attached state_changes
end
feature -- Actions
start_modal(url:STRING)
--Start a modal window containg an other or the same page
local
modal:JSON_OBJECT
do
create modal.make
modal.put (create {JSON_STRING}.make_json("start_modal"), "type")
modal.put (create {JSON_STRING}.make_json(url), "url")
actions.add (modal)
end
show_alert(mesage:STRING)
--Start a modal window containg an other or the same page
local
modal:JSON_OBJECT
do
create modal.make
modal.put (create {JSON_STRING}.make_json("show_alert"), "type")
modal.put (create {JSON_STRING}.make_json(mesage), "message")
actions.add (modal)
end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
load_state (new_states: JSON_OBJECT)
@@ -62,6 +87,18 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
if state_changes.count > 0 then
states.put (state_changes, control_name)
end
if actions.count > 0 then
if not attached states.item ("actions") then
states.put (create {JSON_ARRAY}.make_array,"actions")
end
if attached {JSON_ARRAY}states.item ("actions") as action_list then
across
actions.array_representation as action
loop
action_list.add (action.item)
end
end
end
end
state: JSON_OBJECT
@@ -117,4 +154,5 @@ feature -- Properties
isolate: BOOLEAN
actions: JSON_ARRAY
end

View File

@@ -7,11 +7,22 @@ note
deferred class
WSF_PAGE_CONTROL
inherit
WSF_CONTROL
rename
make as make_wsf_control
redefine
full_state,
read_state_changes
end
feature {NONE} -- Initialization
make (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Initialize
do
make_control (req.request_time_stamp.out, "body")
request := req
response := res
initialize_controls
@@ -60,68 +71,66 @@ feature -- Implementation
request.read_input_data_into (states)
create json_parser.make_parser (states)
if attached {JSON_OBJECT} json_parser.parse_json as sp then
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
set_state (sp)
end
control.handle_callback (event_control_name, event, event_parameter)
handle_callback (event_control_name, event, event_parameter)
create states_changes.make
control.read_state_changes (states_changes)
read_state_changes (states_changes)
response.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "application/json; charset=ISO-8859-1"]>>)
response.put_string (states_changes.representation)
else
process
render
render_page
end
end
render
render_page
-- Render and send the HTML Page
local
data: STRING
page: WSF_PAGE_RESPONSE
states: JSON_OBJECT
do
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-name=%"" + control_name + "%" data-type=%"WSF_PAGE_CONTROL%">")
data.append (control.render)
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"]>>)
page.set_body (data)
page.set_body (render)
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")
Result.put (create {JSON_STRING}.make_json (request.path_info), "url")
Result.put (create {JSON_STRING}.make_json (request.query_string), "url_params")
end
full_state: JSON_OBJECT
render: STRING
local
controls_state: JSON_OBJECT
ajax: BOOLEAN
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")
ajax := attached get_parameter ("ajax")
create Result.make_empty
if not ajax then
Result.append ("<html><head>")
Result.append ("<link href=%"/bootstrap.min.css%" rel=%"stylesheet%">")
Result.append ("<link href=%"/widget.css%" rel=%"stylesheet%">")
Result.append ("</head><body data-name=%"" + control_name + "%" data-type=%"WSF_PAGE_CONTROL%">")
Result.append (control.render)
Result.append ("<script src=%"/jquery.min.js%"></script>")
Result.append ("<script src=%"/typeahead.min.js%"></script>")
Result.append ("<script src=%"/bootstrap.min.js%"></script>")
Result.append ("<script src=%"/widget.js%"></script>")
Result.append ("<script type=%"text/javascript%">$(function() {var page= new WSF_PAGE_CONTROL(")
Result.append (full_state.representation)
Result.append (");page.attach_events();});</script>")
Result.append ("</body></html>")
else
Result.append ("<div data-name=%"" + control_name + "%" data-type=%"WSF_PAGE_CONTROL%">")
Result.append (control.render)
Result.append ("<script type=%"text/javascript%">$(function() {var page= new WSF_PAGE_CONTROL(")
Result.append (full_state.representation)
Result.append (");page.attach_events();});</script>")
Result.append ("</div>")
end
end
read_state_changes (states: JSON_OBJECT)
-- Add a new entry in the `states_changes` JSON object with the `control_name` as key and the `state` as value
do
Precursor (states)
control.read_state_changes (states)
end
get_parameter (key: STRING): detachable STRING
@@ -136,6 +145,42 @@ feature -- Implementation
end
end
feature -- EVENT HANDLING
handle_callback (cname: STRING; event: STRING; event_parameter: detachable STRING)
-- Forward callback to control
do
control.handle_callback (cname, event, event_parameter)
end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
state: JSON_OBJECT
do
create Result.make
Result.put (create {JSON_STRING}.make_json (control_name), "id")
Result.put (create {JSON_STRING}.make_json (request.path_info), "url")
Result.put (create {JSON_STRING}.make_json (request.query_string), "url_params")
end
set_state (sp: JSON_OBJECT)
do
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
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
feature {NONE} -- Root control
control: WSF_CONTROL