Google news example

This commit is contained in:
YNH Webdev
2013-09-14 17:46:57 +02:00
parent 95f823e7a1
commit 26ec7d94c6
13 changed files with 333 additions and 15 deletions

View File

@@ -21,6 +21,7 @@ feature {NONE}
id := a_id
name := a_name
description := a_description
image := "http://placehold.it/20x20&text=" + id.out
end
feature
@@ -31,6 +32,8 @@ feature
description: STRING
image: STRING
get (field: STRING): detachable ANY
do
if field.is_equal ("id") then
@@ -39,6 +42,8 @@ feature
Result := name
elseif field.is_equal ("description") then
Result := description
elseif field.is_equal ("image") then
Result := image
end
end

View File

@@ -28,12 +28,12 @@ feature
do
create list.make
across
((page - 1) * page_size) |..| (page * page_size - 1).min(131) as c
((page - 1) * page_size + 1) |..| (page * page_size).min (131) as c
loop
list.extend (create {DEMO_DATA}.make (c.item, "Name" + c.item.out, "desc " + c.item.out))
end
Result := list
row_count:=132
row_count := 132
end
end

View File

@@ -0,0 +1,72 @@
note
description: "Summary description for {GOOGLE_AUTOCOMPLETION}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
GOOGLE_AUTOCOMPLETION
inherit
WSF_AUTOCOMPLETION
create
make
feature {NONE} -- Initialization
make ()
do
template := "{{=value}}";
end
feature -- Implementation
autocompletion (input: STRING): JSON_ARRAY
local
o: JSON_OBJECT
l_result: INTEGER
l_curl_string: CURL_STRING
json_parser: JSON_PARSER
query_str: STRING
do
query_str := input
query_str.replace_substring_all (" ", "+")
curl_handle := curl_easy.init
create Result.make_array
if curl_handle /= default_pointer then
create l_curl_string.make_empty
curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_url, "http://suggestqueries.google.com/complete/search?client=firefox&q=" + query_str)
curl_easy.set_write_function (curl_handle)
curl_easy.setopt_integer (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_writedata, l_curl_string.object_id)
l_result := curl_easy.perform (curl_handle)
-- Always cleanup
curl_easy.cleanup (curl_handle)
create json_parser.make_parser (l_curl_string.out)
if attached {JSON_ARRAY} json_parser.parse_json as data and then attached {JSON_ARRAY} data.i_th (2) as list then
across
1 |..| list.count as c
loop
if attached {JSON_STRING} list.i_th (c.item) as row then
create o.make
o.put (create {JSON_STRING}.make_json (row.unescaped_string_32), "value")
Result.add (o)
end
end
end
end
end
feature {NONE} -- Implementation
curl_easy: CURL_EASY_EXTERNALS
once
create Result
end
curl_handle: POINTER;
-- cURL handle
end

View File

@@ -0,0 +1,51 @@
note
description: "Summary description for {GOOGLE_NEWS}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
GOOGLE_NEWS
inherit
WSF_ENTITY
create
make_from_json
feature {NONE}
make_from_json (json: JSON_OBJECT)
do
if attached {JSON_STRING} json.item (create {JSON_STRING}.make_json ("title")) as a_title then
title := a_title.unescaped_string_32
end
if attached {JSON_STRING} json.item (create {JSON_STRING}.make_json ("content")) as a_content then
content := a_content.unescaped_string_32
end
if attached {JSON_OBJECT} json.item (create {JSON_STRING}.make_json ("image")) as img and then attached {JSON_STRING} img.item (create {JSON_STRING}.make_json ("url")) as a_image then
image := a_image.item
end
end
feature
title: detachable STRING
content: detachable STRING
image: detachable STRING
get (field: STRING): detachable ANY
do
if field.is_equal ("title") then
Result := title
elseif field.is_equal ("content") then
Result := content
elseif field.is_equal ("image") then
Result := image
end
end
end

View File

@@ -0,0 +1,107 @@
note
description: "Summary description for {GOOGLE_NEWS_DATASOURCE}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
GOOGLE_NEWS_DATASOURCE
inherit
WSF_PAGABLE_DATASOURCE [GOOGLE_NEWS]
redefine
state,
set_state
end
create
make_news
feature --States
state: JSON_OBJECT
-- Return state which contains the current html and if there is an event handle attached
do
Result := Precursor
Result.put (create {JSON_STRING}.make_json (query), create {JSON_STRING}.make_json ("query"))
end
set_state (new_state: JSON_OBJECT)
do
Precursor (new_state)
if attached {JSON_STRING} new_state.item (create {JSON_STRING}.make_json ("query")) as new_query then
query := new_query.item
end
end
feature
make_news
do
page := 1
page_size := 8
query := "eiffel"
end
data: ITERABLE [GOOGLE_NEWS]
local
list: LINKED_LIST [GOOGLE_NEWS]
l_result: INTEGER
l_curl_string: CURL_STRING
json_parser: JSON_PARSER
query_str: STRING
do
curl_handle := curl_easy.init
create list.make
row_count := 0
if curl_handle /= default_pointer then
create l_curl_string.make_empty
query_str := query.out
query_str.replace_substring_all (" ", "+")
curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_url, "https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=" + query_str + "&rsz=" + page_size.out + "&start=" + (page_size * (page - 1)).out)
curl_easy.set_write_function (curl_handle)
curl_easy.setopt_integer (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_writedata, l_curl_string.object_id)
l_result := curl_easy.perform (curl_handle)
-- Always cleanup
curl_easy.cleanup (curl_handle)
create json_parser.make_parser (l_curl_string.out)
if attached {JSON_OBJECT} json_parser.parse_json as sp then
if attached {JSON_OBJECT} sp.item (create {JSON_STRING}.make_json ("responseData")) as responsedata and then attached {JSON_ARRAY} responsedata.item (create {JSON_STRING}.make_json ("results")) as results then
if attached {JSON_OBJECT} responsedata.item (create {JSON_STRING}.make_json ("cursor")) as cursor and then attached {JSON_STRING} cursor.item (create {JSON_STRING}.make_json ("estimatedResultCount")) as count then
row_count := count.item.to_integer.min (64)
end
across
1 |..| results.count as c
loop
if attached {JSON_OBJECT} results.i_th (c.item) as j then
list.extend (create {GOOGLE_NEWS}.make_from_json (j))
end
end
end
end
end
Result := list
end
feature
set_query (q: STRING)
do
query := q
end
query: STRING
feature {NONE} -- Implementation
curl_easy: CURL_EASY_EXTERNALS
once
create Result
end
curl_handle: POINTER;
-- cURL handle
end

View File

@@ -18,17 +18,34 @@ feature
initialize_controls
local
ds: DEMO_DATASOURCE
container: WSF_MULTI_CONTROL [WSF_STATELESS_CONTROL]
do
create ds.make_demo
create grid.make_grid ("mygrid", <<create {WSF_GRID_COLUMN}.make_column ("#", "id"), create {WSF_GRID_COLUMN}.make_column ("Name", "name"), create {WSF_GRID_COLUMN}.make_column ("Description", "description")>>, ds)
control := grid
create container.make_multi_control ("container")
create datasource.make_news
create search_query.make_autocomplete ("query", create {GOOGLE_AUTOCOMPLETION}.make)
search_query.add_class ("form-control")
search_query.set_change_event (agent change_query)
container.add_control (search_query)
create grid.make_grid ("mygrid", <<create {WSF_GRID_COLUMN}.make_column ("Title", "title"), create {WSF_GRID_COLUMN}.make_column ("Content", "content"), create {WSF_GRID_IMAGE_COLUMN}.make_image_column ("Image", "image")>>, datasource)
container.add_control (grid)
control := container
end
change_query
do
datasource.set_query (search_query.value)
datasource.set_page (1)
datasource.update
end
process
do
end
grid: WSF_GRID_CONTROL [DEMO_DATA]
grid: WSF_GRID_CONTROL [GOOGLE_NEWS]
search_query: WSF_AUTOCOMPLETE_CONTROL
datasource: GOOGLE_NEWS_DATASOURCE
end

View File

@@ -131,7 +131,6 @@ class WSF_TEXTAREA_CONTROL extends WSF_INPUT_CONTROL
class WSF_AUTOCOMPLETE_CONTROL extends WSF_INPUT_CONTROL
attach_events: () ->
super
self = @
@$el.typeahead({
name: @control_name
@@ -148,6 +147,8 @@ class WSF_AUTOCOMPLETE_CONTROL extends WSF_INPUT_CONTROL
filter: (parsedResponse) ->
parsedResponse[self.control_name]['suggestions']
})
@$el.on 'typeahead:closed',()->
self.change()
class WSF_CHECKBOX_CONTROL extends WSF_CONTROL
attach_events: ()->

View File

@@ -263,9 +263,8 @@
WSF_AUTOCOMPLETE_CONTROL.prototype.attach_events = function() {
var self;
WSF_AUTOCOMPLETE_CONTROL.__super__.attach_events.apply(this, arguments);
self = this;
return this.$el.typeahead({
this.$el.typeahead({
name: this.control_name,
template: window.states[this.control_name]['template'],
engine: Mini,
@@ -284,6 +283,9 @@
}
}
});
return this.$el.on('typeahead:closed', function() {
return self.change();
});
};
return WSF_AUTOCOMPLETE_CONTROL;

View File

@@ -10,6 +10,7 @@
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
</option>
<library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
<library name="curl" location="$ISE_LIBRARY\library\cURL\cURL-safe.ecf"/>
<library name="http" location="..\..\library\network\protocol\http\http-safe.ecf"/>
<library name="wsf" location="..\..\library\server\wsf\wsf-safe.ecf"/>
<library name="wsf_html" location="..\..\library\server\wsf_html\wsf_html-safe.ecf"/>

View File

@@ -16,8 +16,8 @@ feature -- Update event
update
do
if attached on_update_agent as a then
a.call([])
if attached on_update_agent as a then
a.call ([])
end
end

View File

@@ -0,0 +1,38 @@
note
description: "Summary description for {WSF_GRID_IMAGE_COLUMN}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
WSF_GRID_IMAGE_COLUMN
inherit
WSF_GRID_COLUMN
redefine
render_column
end
create
make_image_column
feature {NONE}
make_image_column (a_header, a_field: STRING)
do
make_column (a_header, a_field)
end
feature
render_column (e: WSF_ENTITY): STRING
do
if attached e.get (field_name) as data then
Result := "<img src=%"" + data.out + "%" />"
else
Result := "[VOID]"
end
end
end

View File

@@ -12,9 +12,29 @@ inherit
WSF_DATASOURCE [G]
redefine
state,
set_state
set_state,
update
end
feature -- Update event
set_on_update_page_agent (f: PROCEDURE [ANY, TUPLE []])
do
on_update_page_agent := f
end
update
do
if attached on_update_agent as a then
a.call ([])
end
if attached on_update_page_agent as a then
a.call ([])
end
end
on_update_page_agent: detachable PROCEDURE [ANY, TUPLE []]
feature --States
state: JSON_OBJECT

View File

@@ -21,6 +21,7 @@ feature {NONE}
make_control (n, "ul")
add_class ("pagination")
datasource := ds
datasource.set_on_update_page_agent (agent update)
end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
@@ -49,16 +50,19 @@ feature --EVENT HANDLING
datasource.set_page (i)
end
end
state_changes.replace (create {JSON_STRING}.make_json (render), create {JSON_STRING}.make_json ("_html"))
datasource.update
end
end
feature
update
do
state_changes.replace (create {JSON_STRING}.make_json (render), create {JSON_STRING}.make_json ("_html"))
end
render: STRING
local
page_count: INTEGER
paging_start: INTEGER
paging_end: INTEGER
cssclass: STRING