From 36368aff0bded4c4fd58695d0273a3f3542bcd35 Mon Sep 17 00:00:00 2001 From: YNH Webdev Date: Fri, 13 Sep 2013 00:20:27 +0200 Subject: [PATCH] Add Grid Widget --- examples/widgetapp/application.e | 11 +++ examples/widgetapp/demo_data.e | 45 +++++++++ examples/widgetapp/demo_datasource.e | 38 ++++++++ examples/widgetapp/grid_page.e | 34 +++++++ .../wsf_html/webcontrol/grid/wsf_datasource.e | 76 +++++++++++++++ .../wsf_html/webcontrol/grid/wsf_entity.e | 16 ++++ .../webcontrol/grid/wsf_grid_column.e | 38 ++++++++ .../webcontrol/grid/wsf_grid_control.e | 96 +++++++++++++++++++ library/server/wsf_html/wsf_html-safe.ecf | 1 + 9 files changed, 355 insertions(+) create mode 100644 examples/widgetapp/demo_data.e create mode 100644 examples/widgetapp/demo_datasource.e create mode 100644 examples/widgetapp/grid_page.e create mode 100644 library/server/wsf_html/webcontrol/grid/wsf_datasource.e create mode 100644 library/server/wsf_html/webcontrol/grid/wsf_entity.e create mode 100644 library/server/wsf_html/webcontrol/grid/wsf_grid_column.e create mode 100644 library/server/wsf_html/webcontrol/grid/wsf_grid_control.e diff --git a/examples/widgetapp/application.e b/examples/widgetapp/application.e index 9163a630..6528583a 100644 --- a/examples/widgetapp/application.e +++ b/examples/widgetapp/application.e @@ -33,6 +33,7 @@ feature {NONE} -- Initialization do -- router.map (create {WSF_URI_MAPPING}.make ("/hello", create {WSF_AGENT_URI_HANDLER}.make (agent execute_hello))) map_agent_uri ("/", agent execute_hello, Void) + map_agent_uri ("/grid", agent grid_demo, Void) map_agent_uri ("/widget.js", agent load_js, Void) end @@ -55,6 +56,16 @@ feature -- Execution page.execute end + grid_demo (req: WSF_REQUEST; res: WSF_RESPONSE) + local + page: GRID_PAGE + do + -- To send a response we need to setup, the status code and + -- the response headers. + create page.make (req, res) + page.execute + end + load_js (req: WSF_REQUEST; res: WSF_RESPONSE) local f: WSF_FILE_RESPONSE diff --git a/examples/widgetapp/demo_data.e b/examples/widgetapp/demo_data.e new file mode 100644 index 00000000..c0e484f7 --- /dev/null +++ b/examples/widgetapp/demo_data.e @@ -0,0 +1,45 @@ +note + description: "Summary description for {DEMO_DATA}." + author: "" + date: "$Date$" + revision: "$Revision$" + +class + DEMO_DATA + +inherit + + WSF_ENTITY + +create + make + +feature {NONE} + + make (a_id: INTEGER; a_name, a_description: STRING) + do + id := a_id + name := a_name + description := a_description + end + +feature + + id: INTEGER + + name: STRING + + description: STRING + + get (field: STRING): detachable ANY + do + if field.is_equal ("id") then + Result := id + elseif field.is_equal ("name") then + Result := name + elseif field.is_equal ("description") then + Result := description + end + end + +end diff --git a/examples/widgetapp/demo_datasource.e b/examples/widgetapp/demo_datasource.e new file mode 100644 index 00000000..50cbb30c --- /dev/null +++ b/examples/widgetapp/demo_datasource.e @@ -0,0 +1,38 @@ +note + description: "Summary description for {DEMO_DATASOURCE}." + author: "" + date: "$Date$" + revision: "$Revision$" + +class + DEMO_DATASOURCE + +inherit + + WSF_DATASOURCE [DEMO_DATA] + +create + make_demo + +feature + + make_demo + do + page := 1 + page_size := 10 + end + + data: ITERABLE [DEMO_DATA] + local + list: LINKED_LIST [DEMO_DATA] + do + create list.make + across + ((page - 1) * page_size) |..| (page * page_size - 1) as c + loop + list.extend (create {DEMO_DATA}.make(c.item,"Name"+c.item.out,"desc "+c.item.out)) + end + Result := list + end + +end diff --git a/examples/widgetapp/grid_page.e b/examples/widgetapp/grid_page.e new file mode 100644 index 00000000..98ef14ed --- /dev/null +++ b/examples/widgetapp/grid_page.e @@ -0,0 +1,34 @@ +note + description: "Summary description for {GRID_PAGE}." + author: "" + date: "$Date$" + revision: "$Revision$" + +class + GRID_PAGE + +inherit + + WSF_PAGE_CONTROL + +create + make + +feature + + initialize_controls + local + ds: DEMO_DATASOURCE + do + create ds.make_demo + create grid.make_grid ("mygrid", <>, ds) + control := grid + end + + process + do + end + + grid: WSF_GRID_CONTROL [DEMO_DATA] + +end diff --git a/library/server/wsf_html/webcontrol/grid/wsf_datasource.e b/library/server/wsf_html/webcontrol/grid/wsf_datasource.e new file mode 100644 index 00000000..5c58035e --- /dev/null +++ b/library/server/wsf_html/webcontrol/grid/wsf_datasource.e @@ -0,0 +1,76 @@ +note + description: "Summary description for {WSF_DATASOURCE}." + author: "" + date: "$Date$" + revision: "$Revision$" + +deferred class + WSF_DATASOURCE [G -> WSF_ENTITY] + +feature --State + + 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_NUMBER}.make_integer(page), create {JSON_STRING}.make_json("page")) + Result.put (create {JSON_NUMBER}.make_integer(page_size), create {JSON_STRING}.make_json("page_size")) + if attached sort_column as a_sort_column then + Result.put (create {JSON_STRING}.make_json(a_sort_column), create {JSON_STRING}.make_json("sort_column")) + else + Result.put (create {JSON_NULL}, create {JSON_STRING}.make_json("sort_column")) + end + Result.put (create {JSON_BOOLEAN}.make_boolean(sort_direction), create {JSON_STRING}.make_json("sort_direction")) + end + + set_state (new_state: JSON_OBJECT) + do + if attached {JSON_NUMBER} new_state.item (create {JSON_STRING}.make_json ("page")) as new_page then + page := new_page.integer_type + end + if attached {JSON_NUMBER} new_state.item (create {JSON_STRING}.make_json ("page_size")) as new_page_size then + page_size := new_page_size.integer_type + end + if attached {JSON_STRING} new_state.item (create {JSON_STRING}.make_json ("sort_column")) as new_sort_column then + sort_column := new_sort_column.unescaped_string_32 + elseif attached {JSON_NULL} new_state.item (create {JSON_STRING}.make_json ("sort_column")) as new_sort_column then + sort_column := VOID + end + if attached {JSON_BOOLEAN} new_state.item (create {JSON_STRING}.make_json ("sort_direction")) as new_sort_direction then + sort_direction := new_sort_direction.item + end + end +feature + + set_page (a_page: like page) + do + page := a_page + end + + set_page_size (a_page_size: like page_size) + do + page_size := a_page_size + end + + set_sort_column (a_sort_column: like sort_column) + do + sort_column := a_sort_column + end + + set_sort_direction (a_sort_direction: like sort_direction) + do + sort_direction := a_sort_direction + end + + page: INTEGER + + page_size: INTEGER + + sort_column: detachable STRING + + sort_direction: BOOLEAN + + data: ITERABLE [G] + deferred + end +end diff --git a/library/server/wsf_html/webcontrol/grid/wsf_entity.e b/library/server/wsf_html/webcontrol/grid/wsf_entity.e new file mode 100644 index 00000000..5f80caf3 --- /dev/null +++ b/library/server/wsf_html/webcontrol/grid/wsf_entity.e @@ -0,0 +1,16 @@ +note + description: "Summary description for {WSF_ENTITY}." + author: "" + date: "$Date$" + revision: "$Revision$" + +deferred class + WSF_ENTITY + +feature + + get (field: STRING): detachable ANY + deferred + end + +end diff --git a/library/server/wsf_html/webcontrol/grid/wsf_grid_column.e b/library/server/wsf_html/webcontrol/grid/wsf_grid_column.e new file mode 100644 index 00000000..4f33c3e9 --- /dev/null +++ b/library/server/wsf_html/webcontrol/grid/wsf_grid_column.e @@ -0,0 +1,38 @@ +note + description: "Summary description for {WSF_GRID_COLUMN}." + author: "" + date: "$Date$" + revision: "$Revision$" + +class + WSF_GRID_COLUMN +create + make_column +feature {NONE} + + make_column(a_header,a_field:STRING) + do + header:=a_header + field_name:=a_field + sorting_name:=a_field + end +feature + + header: STRING + + sortable: BOOLEAN + + sorting_name: STRING + + field_name: STRING + + render_column (e: WSF_ENTITY): STRING + do + if attached e.get (field_name) as data then + Result := data.out + else + Result := "[VOID]" + end + end + +end diff --git a/library/server/wsf_html/webcontrol/grid/wsf_grid_control.e b/library/server/wsf_html/webcontrol/grid/wsf_grid_control.e new file mode 100644 index 00000000..253cbaca --- /dev/null +++ b/library/server/wsf_html/webcontrol/grid/wsf_grid_control.e @@ -0,0 +1,96 @@ +note + description: "Summary description for {WSF_GRID_CONTROL}." + author: "" + date: "$Date$" + revision: "$Revision$" + +class + WSF_GRID_CONTROL [G -> WSF_ENTITY] + +inherit + + WSF_CONTROL + +create + make_grid + +feature {NONE} + + make_grid (n: STRING; a_columns: ITERABLE [WSF_GRID_COLUMN]; a_datasource: WSF_DATASOURCE [G]) + do + make (n, "div") + columns := a_columns + datasource := a_datasource + end + +feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT + + update + do + end + + set_state (new_state: JSON_OBJECT) + -- Restore html from json + do + if attached {JSON_OBJECT} new_state.item (create {JSON_STRING}.make_json ("datasource")) as datasource_state then + datasource.set_state (datasource_state) + 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 (datasource.state, create {JSON_STRING}.make_json ("datasource")) + end + +feature --EVENT HANDLING + + handle_callback (cname: STRING; event: STRING) + do + end + +feature -- Implementation + + render_header: STRING + do + Result := "" + across + columns as c + loop + Result.append (render_tag_with_tagname ("th", c.item.header, "", "")) + end + Result := render_tag_with_tagname ("thead", render_tag_with_tagname ("tr", Result, "", ""), "", "") + end + + render_body: STRING + local + row: STRING + do + Result := "" + across + datasource.data as entity + loop + row := "" + across + columns as c + loop + row.append (render_tag_with_tagname ("td", c.item.render_column (entity.item), "", "")) + end + Result.append (render_tag_with_tagname ("tr", row, "", "")) + end + Result :=render_tag_with_tagname ("tbody", Result, "", "") + end + + render: STRING + do + Result := render_tag (render_tag_with_tagname ("table", render_header + render_body, "", "table table-striped"), "") + end + +feature + + columns: ITERABLE [WSF_GRID_COLUMN] + + datasource: WSF_DATASOURCE [G] + +end diff --git a/library/server/wsf_html/wsf_html-safe.ecf b/library/server/wsf_html/wsf_html-safe.ecf index a432e76f..79ff241e 100644 --- a/library/server/wsf_html/wsf_html-safe.ecf +++ b/library/server/wsf_html/wsf_html-safe.ecf @@ -22,6 +22,7 @@ +