Merge branch 'widget' of github.com:souvarin/EWF into widget

This commit is contained in:
YNH Webdev
2013-09-24 15:20:43 +02:00
48 changed files with 204 additions and 123 deletions

View File

@@ -0,0 +1,66 @@
note
description: "Summary description for {WSF_BASIC_CONTROL}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
WSF_BASIC_CONTROL
inherit
WSF_STATELESS_CONTROL
redefine
attributes
end
create
make_control, make_with_body
feature {NONE} -- Initialization
make_control (t: STRING)
-- Initialize
do
make_with_body (t, "", "")
end
make_with_body (t, attr, b: STRING)
-- Initialize with specific attributes and body
do
make (t)
attributes := attr
body := b
end
feature -- Rendering
render: STRING
-- HTML representation of this control
do
Result := render_tag (body, attributes)
end
feature -- Change
set_attributes (a: STRING)
-- Set the attributes string of this control
do
attributes := a
end
set_body (b: STRING)
-- Set the body of this control
do
body := b
end
feature -- Access
attributes: STRING
-- Attributes of this control
body: STRING
-- Body of this control
end

View File

@@ -0,0 +1,88 @@
note
description: "Summary description for {WSF_BUTTON_CONTROL}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
WSF_BUTTON_CONTROL
inherit
WSF_CONTROL
create
make_button
feature {NONE} -- Initialization
make_button (n: STRING; t: STRING)
-- Initialize with specified control name and text
do
make_control (n, "button")
add_class ("btn")
add_class ("btn-default")
text := t
end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
set_state (new_state: JSON_OBJECT)
-- Restore text from json
do
if attached {JSON_STRING} new_state.item ("text") as new_text then
text := new_text.unescaped_string_32
end
end
state: JSON_OBJECT
-- Return state which contains the current text and if there is an event handle attached
do
create Result.make
Result.put (create {JSON_STRING}.make_json (text), "text")
Result.put (create {JSON_BOOLEAN}.make_boolean (attached click_event), "callback_click")
end
feature --Event handling
set_click_event (e: attached like click_event)
-- Set button click event handle
do
click_event := e
end
handle_callback (cname: STRING; event: STRING; event_parameter: detachable STRING)
do
if Current.control_name.same_string (cname) and attached click_event as cevent then
cevent.call (Void)
end
end
feature -- Rendering
render: STRING
-- HTML representation of this control
do
Result := render_tag (text, "")
end
feature -- Change
set_text (t: STRING)
-- Set text of that button
do
if not t.same_string (text) then
text := t
state_changes.replace (create {JSON_STRING}.make_json (text), "text")
end
end
feature -- Properties
text: STRING
-- The text currently displayed on this button
click_event: detachable PROCEDURE [ANY, TUPLE]
-- Event that is executed when button is clicked
end

View File

@@ -0,0 +1,159 @@
note
description: "Summary description for {WSF_CONTROL}."
author: ""
date: "$Date$"
revision: "$Revision$"
deferred class
WSF_CONTROL
inherit
WSF_STATELESS_CONTROL
redefine
render_tag
end
feature
control_name: STRING
feature {NONE} -- Initialization
make_control (n, a_tag_name: STRING)
-- Initialize with specified control name and tag
require
not n.is_empty
not a_tag_name.is_empty
do
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; title: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")
modal.put (create {JSON_STRING}.make_json(title), "title")
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)
-- Select state stored with `control_name` as key
do
if attached {JSON_OBJECT} new_states.item ("state") as new_state_obj then
set_state (new_state_obj)
end
end
set_state (new_state: JSON_OBJECT)
-- Before we process the callback. We restore the state of control.
deferred
end
full_state: JSON_OBJECT
-- Return state of object
do
create Result.make
Result.put (state, "state")
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
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
-- 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
feature -- Rendering
render_tag (body, attrs: STRING): STRING
-- Render this control with the specified body and attributes
do
Result := render_tag_with_generator_name (generator, body, attrs)
end
render_tag_with_generator_name (a_generator, body, attrs: STRING): STRING
-- Render this control with the specified generator name, body and attributes
local
css_classes_string: STRING
l_attributes: STRING
do
css_classes_string := ""
across
css_classes as c
loop
css_classes_string := css_classes_string + " " + c.item
end
l_attributes := "id=%"" + control_name + "%" data-name=%"" + control_name + "%" data-type=%"" + a_generator + "%" " + attrs
if isolate then
l_attributes.append (" data-isolation=%"1%"")
end
Result := render_tag_with_tagname (tag_name, body, l_attributes, css_classes_string)
end
feature -- EVENT HANDLING
handle_callback (cname: STRING; event: STRING; event_parameter: detachable STRING)
-- Method called if any callback received. In this method you can route the callback to the event handler
deferred
end
feature -- Change
set_isolation (p: BOOLEAN)
do
isolate := true
end
feature -- Properties
isolate: BOOLEAN
actions: JSON_ARRAY
end

View File

@@ -0,0 +1,51 @@
note
description: "Summary description for {WSF_FORM_CONTROL}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
WSF_FORM_CONTROL
inherit
WSF_MULTI_CONTROL [WSF_CONTROL]
WSF_VALIDATABLE
create
make_form_control
feature {NONE} -- Initialization
make_form_control (n: STRING)
-- Initialize
do
make_multi_control (n)
tag_name := "form"
end
feature -- Validation
validate
-- Perform form validation
do
is_valid := True
across
controls as c
until
is_valid = False
loop
if attached {WSF_VALIDATABLE} c.item as elem then
elem.validate
if not elem.is_valid then
is_valid := False
end
end
end
end
is_valid: BOOLEAN
-- Tells whether the last validation was valid
end

View File

@@ -0,0 +1,184 @@
note
description: "Summary description for {WSF_FORM_ELEMENT_CONTROL}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
WSF_FORM_ELEMENT_CONTROL [G]
inherit
WSF_CONTROL
redefine
read_state_changes,
load_state,
full_state
end
WSF_VALIDATABLE
create
make_form_element, make_form_element_with_validators
feature {NONE} -- Initialization
make_form_element (a_label: STRING; c: WSF_VALUE_CONTROL [G])
-- Initialize form element control with a specific label and value control
do
make_form_element_with_validators (a_label, c, create {ARRAYED_LIST [WSF_VALIDATOR [G]]}.make (0))
end
make_form_element_with_validators (a_label: STRING; c: WSF_VALUE_CONTROL [G]; v: LIST [WSF_VALIDATOR [G]])
-- Initialize form element control with a specific label, value control and list of validators
do
make_control (c.control_name + "_container", "div")
add_class ("form-group")
if attached {WSF_INPUT_CONTROL} c or attached {WSF_TEXTAREA_CONTROL} c then
c.add_class ("form-control")
end
if attached {WSF_HTML_CONTROL} c then
c.add_class ("form-control-static")
end
value_control := c
validators := v
label := a_label
error := ""
end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
load_state (new_states: JSON_OBJECT)
-- Pass new_states to subcontrols
do
Precursor (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)
-- Set new state
do
value_control.set_state (new_state)
end
full_state: JSON_OBJECT
local
controls_state: JSON_OBJECT
do
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)
-- Read states_changes in subcontrols
do
Precursor (states)
value_control.read_state_changes (states)
end
state: JSON_OBJECT
-- Read state
local
validator_description: JSON_ARRAY
do
create Result.make
create validator_description.make_array
across
validators as v
loop
validator_description.add (v.item.state)
end
Result.put (create {JSON_STRING}.make_json (value_control.control_name), "value_control")
Result.put (validator_description, "validators")
end
feature -- Event handling
handle_callback (cname: STRING; event: STRING; event_parameter: detachable STRING)
-- Pass callback to subcontrols
do
if cname.same_string (control_name) then
if event.same_string ("validate") then
validate
end
else
value_control.handle_callback (cname, event, event_parameter)
end
end
feature -- Implementation
render: STRING
-- HTML Respresentation of this form element control
local
body: STRING
do
body := ""
if not label.is_empty then
body.append ("<label class=%"col-lg-2 control-label%" for=%"" + value_control.control_name + "%">" + label + "</label>")
end
body.append ("<div class=%"col-lg-10%">")
body.append (value_control.render)
body.append ("</div>")
Result := render_tag (body, "")
end
feature -- Validation
add_validator (v: WSF_VALIDATOR [G])
-- Add an additional validator that will check the input of the value control of this form element control on validation
do
validators.extend (v)
end
set_error (e: STRING)
-- Set the error message that will be displayed upon failure of client side validation
do
error := e
state_changes.replace (create {JSON_STRING}.make_json (e), "error")
end
validate
-- Perform validation
local
current_value: G
do
current_value := value_control.value
is_valid := True
across
validators as c
until
not is_valid
loop
if not c.item.is_valid (current_value) then
is_valid := False
set_error (c.item.error)
end
end
if is_valid then
set_error ("")
end
end
is_valid: BOOLEAN
-- Tells whether the last validation was successful or not
feature -- Properties
value_control: WSF_VALUE_CONTROL [G]
-- The value control associated with this form element control
validators: LIST [WSF_VALIDATOR [G]]
-- The validators which check the input when validaton is performed
label: STRING
-- The label of this form element control
error: STRING
-- The error message that is displayed when client side validation fails
end

View File

@@ -0,0 +1,74 @@
note
description: "Summary description for {WSF_html_CONTROL}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
WSF_HTML_CONTROL
inherit
WSF_VALUE_CONTROL [STRING]
create
make_html
feature {NONE} -- Initialization
make_html (n, t, v: STRING)
-- Initialize
do
make_control (n, t)
html := v
end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
set_state (new_state: JSON_OBJECT)
-- Restore html from json
do
if attached {JSON_STRING} new_state.item ("html") as new_html then
html := new_html.unescaped_string_32
end
end
state: JSON_OBJECT
-- Return state which contains the current html and if there is an event handle attached
do
create Result.make
Result.put (create {JSON_STRING}.make_json (html), "html")
end
feature --Event handling
handle_callback (cname: STRING; event: STRING; event_parameter: detachable STRING)
do
end
feature -- Implementation
render: STRING
-- HTML representation of this html control
do
Result := render_tag (html, "")
end
set_html (t: STRING)
do
if not t.same_string (html) then
html := t
state_changes.replace (create {JSON_STRING}.make_json (html), "html")
end
end
value: STRING
do
Result := html
end
feature -- Properties
html: STRING
end

View File

@@ -0,0 +1,143 @@
note
description: "Summary description for {WSF_MULTI_CONTROL}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
WSF_MULTI_CONTROL [G -> WSF_STATELESS_CONTROL]
inherit
WSF_CONTROL
redefine
full_state,
read_state_changes,
load_state
end
create
make_multi_control, make_with_tag_name
feature {NONE} -- Initialization
make_multi_control (n: STRING)
-- Initialize with specified control name and default tag "div"
do
make_with_tag_name (n, "div")
end
make_with_tag_name (n, t: STRING)
-- Initialize with specified control name and tag
do
make_control (n, t)
controls := create {ARRAYED_LIST [G]}.make (5);
end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
load_state (new_states: JSON_OBJECT)
-- Pass new_states to subcontrols
do
Precursor (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
set_state (new_state: JSON_OBJECT)
-- Before we process the callback. We restore the state of control.
do
end
full_state: JSON_OBJECT
-- Read states in subcontrols
local
controls_state: JSON_OBJECT
do
Result := Precursor
create controls_state.make
across
controls as c
loop
if attached {WSF_CONTROL} c.item as cont then
controls_state.put (cont.full_state, cont.control_name)
end
end
Result.put (controls_state, "controls")
end
read_state_changes (states: JSON_OBJECT)
-- Read states_changes in subcontrols
do
Precursor (states)
across
controls as c
loop
if attached {WSF_CONTROL} c.item as cont then
cont.read_state_changes (states)
end
end
end
state: JSON_OBJECT
--Read state
do
create Result.make
end
feature -- Event handling
handle_callback (cname: STRING; event: STRING; event_parameter: detachable STRING)
-- Pass callback to subcontrols
do
if equal (cname, control_name) then
else
across
controls as c
loop
if attached {WSF_CONTROL} c.item as cont then
cont.handle_callback (cname, event, event_parameter)
end
end
end
end
feature -- Rendering
render: STRING
-- HTML representation of this multi control
do
Result := ""
across
controls as c
loop
Result := c.item.render + Result
end
Result := render_tag (Result, "")
end
feature -- Change
add_control (c: detachable G)
-- Add a control to this multi control
do
if attached c as d then
controls.put_front (d)
end
end
feature -- Properties
controls: ARRAYED_LIST [G]
-- List of current controls in this multi control
end

View File

@@ -0,0 +1,189 @@
note
description: "Summary description for {WSF_PAGE_CONTROL}."
author: ""
date: "$Date$"
revision: "$Revision$"
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
end
feature -- Access
request: WSF_REQUEST
-- The http request
response: WSF_RESPONSE
-- The http response
feature -- Specific implementation
initialize_controls
-- Initalize all the controls, all the event handles must be set in this function.
deferred
ensure
attached control
end
process
-- Function called on page load (not on callback)
deferred
end
feature -- Implementation
execute
-- Entry Point: If request is a callback, restore control states and execute handle then return new state json.
-- If request is not a callback. Run process and render the html page
local
event: detachable STRING
event_parameter: detachable STRING
event_control_name: detachable STRING
states: STRING
states_changes: JSON_OBJECT
json_parser: JSON_PARSER
do
event_control_name := get_parameter ("control_name")
event := get_parameter ("event")
event_parameter := get_parameter ("event_parameter")
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
set_state (sp)
end
handle_callback (event_control_name, event, event_parameter)
create states_changes.make
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_page
end
end
render_page
-- Render and send the HTML Page
local
page: WSF_PAGE_RESPONSE
do
create page.make
page.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "text/html; charset=ISO-8859-1"]>>)
page.set_body (render)
response.send (page)
end
render: STRING
local
ajax: BOOLEAN
do
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
-- Read query parameter as string
local
value: detachable WSF_VALUE
do
Result := VOID
value := request.query_parameter (key)
if attached value and then value.is_string then
Result := value.as_string.value
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
-- The root control of this page
end

View File

@@ -0,0 +1,90 @@
note
description: "Summary description for {WSF_STATELESS_CONTROL}."
author: ""
date: "$Date$"
revision: "$Revision$"
deferred class
WSF_STATELESS_CONTROL
feature {NONE} -- Initialization
make (a_tag_name: STRING)
-- Initialize with specified tag
require
not a_tag_name.is_empty
do
tag_name := a_tag_name
create css_classes.make (0)
end
feature -- Access
tag_name: STRING
-- The tag name
css_classes: ARRAYED_LIST [STRING]
-- List of classes (appear in the "class" attribute)
attributes: detachable STRING
-- Attributes string
feature -- Change
add_class (c: STRING)
-- Add a css class to this control
do
css_classes.force (c)
end
feature -- Rendering
render_tag (body, attrs: STRING): STRING
-- Generate HTML of this control with the specified body and attributes
local
css_classes_string: STRING
do
create css_classes_string.make_empty
across
css_classes as c
loop
css_classes_string.append (" " + c.item)
end
Result := render_tag_with_tagname (tag_name, body, attrs, css_classes_string)
end
render_tag_with_tagname (tag, body, attrs, css_classes_string: STRING): STRING
-- Generate HTML of the specified tag with specified body, attributes and css classes
local
l_attributes: STRING
do
create l_attributes.make_from_string (attrs)
if not css_classes_string.is_empty then
l_attributes.append (" class=%"")
l_attributes.append (css_classes_string)
l_attributes.append_character ('%"')
end
Result := "<" + tag + " " + l_attributes
if body.is_empty and not tag.same_string ("textarea") and not tag.same_string ("span") and not tag.same_string ("button") and not tag.same_string ("ul") then
Result.append (" />")
else
Result.append (" >" + body + "</" + tag + ">")
end
end
render_tag_with_body (body: STRING): STRING
-- Generate HTML of this control with the specified body
do
if attached attributes as attrs then
Result := render_tag (body, attrs)
else
Result := render_tag (body, "")
end
end
render: STRING
-- Return html representation of control
deferred
end
end

View File

@@ -0,0 +1,21 @@
note
description: "Summary description for {WSF_VALUE_CONTROL}."
author: ""
date: "$Date$"
revision: "$Revision$"
deferred class
WSF_VALUE_CONTROL [G]
inherit
WSF_CONTROL
feature -- Access
value: G
-- The current value of this control
deferred
end
end