Google news example
This commit is contained in:
@@ -21,6 +21,7 @@ feature {NONE}
|
|||||||
id := a_id
|
id := a_id
|
||||||
name := a_name
|
name := a_name
|
||||||
description := a_description
|
description := a_description
|
||||||
|
image := "http://placehold.it/20x20&text=" + id.out
|
||||||
end
|
end
|
||||||
|
|
||||||
feature
|
feature
|
||||||
@@ -31,6 +32,8 @@ feature
|
|||||||
|
|
||||||
description: STRING
|
description: STRING
|
||||||
|
|
||||||
|
image: STRING
|
||||||
|
|
||||||
get (field: STRING): detachable ANY
|
get (field: STRING): detachable ANY
|
||||||
do
|
do
|
||||||
if field.is_equal ("id") then
|
if field.is_equal ("id") then
|
||||||
@@ -39,6 +42,8 @@ feature
|
|||||||
Result := name
|
Result := name
|
||||||
elseif field.is_equal ("description") then
|
elseif field.is_equal ("description") then
|
||||||
Result := description
|
Result := description
|
||||||
|
elseif field.is_equal ("image") then
|
||||||
|
Result := image
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ feature
|
|||||||
do
|
do
|
||||||
create list.make
|
create list.make
|
||||||
across
|
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
|
loop
|
||||||
list.extend (create {DEMO_DATA}.make (c.item, "Name" + c.item.out, "desc " + c.item.out))
|
list.extend (create {DEMO_DATA}.make (c.item, "Name" + c.item.out, "desc " + c.item.out))
|
||||||
end
|
end
|
||||||
|
|||||||
72
examples/widgetapp/google_autocompletion.e
Normal file
72
examples/widgetapp/google_autocompletion.e
Normal 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
|
||||||
51
examples/widgetapp/google_news.e
Normal file
51
examples/widgetapp/google_news.e
Normal 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
|
||||||
107
examples/widgetapp/google_news_datasource.e
Normal file
107
examples/widgetapp/google_news_datasource.e
Normal 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
|
||||||
@@ -18,17 +18,34 @@ feature
|
|||||||
|
|
||||||
initialize_controls
|
initialize_controls
|
||||||
local
|
local
|
||||||
ds: DEMO_DATASOURCE
|
container: WSF_MULTI_CONTROL [WSF_STATELESS_CONTROL]
|
||||||
do
|
do
|
||||||
create ds.make_demo
|
create container.make_multi_control ("container")
|
||||||
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)
|
create datasource.make_news
|
||||||
control := grid
|
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
|
end
|
||||||
|
|
||||||
process
|
process
|
||||||
do
|
do
|
||||||
end
|
end
|
||||||
|
|
||||||
grid: WSF_GRID_CONTROL [DEMO_DATA]
|
grid: WSF_GRID_CONTROL [GOOGLE_NEWS]
|
||||||
|
|
||||||
|
search_query: WSF_AUTOCOMPLETE_CONTROL
|
||||||
|
|
||||||
|
datasource: GOOGLE_NEWS_DATASOURCE
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -131,7 +131,6 @@ class WSF_TEXTAREA_CONTROL extends WSF_INPUT_CONTROL
|
|||||||
|
|
||||||
class WSF_AUTOCOMPLETE_CONTROL extends WSF_INPUT_CONTROL
|
class WSF_AUTOCOMPLETE_CONTROL extends WSF_INPUT_CONTROL
|
||||||
attach_events: () ->
|
attach_events: () ->
|
||||||
super
|
|
||||||
self = @
|
self = @
|
||||||
@$el.typeahead({
|
@$el.typeahead({
|
||||||
name: @control_name
|
name: @control_name
|
||||||
@@ -148,6 +147,8 @@ class WSF_AUTOCOMPLETE_CONTROL extends WSF_INPUT_CONTROL
|
|||||||
filter: (parsedResponse) ->
|
filter: (parsedResponse) ->
|
||||||
parsedResponse[self.control_name]['suggestions']
|
parsedResponse[self.control_name]['suggestions']
|
||||||
})
|
})
|
||||||
|
@$el.on 'typeahead:closed',()->
|
||||||
|
self.change()
|
||||||
|
|
||||||
class WSF_CHECKBOX_CONTROL extends WSF_CONTROL
|
class WSF_CHECKBOX_CONTROL extends WSF_CONTROL
|
||||||
attach_events: ()->
|
attach_events: ()->
|
||||||
|
|||||||
@@ -263,9 +263,8 @@
|
|||||||
|
|
||||||
WSF_AUTOCOMPLETE_CONTROL.prototype.attach_events = function() {
|
WSF_AUTOCOMPLETE_CONTROL.prototype.attach_events = function() {
|
||||||
var self;
|
var self;
|
||||||
WSF_AUTOCOMPLETE_CONTROL.__super__.attach_events.apply(this, arguments);
|
|
||||||
self = this;
|
self = this;
|
||||||
return this.$el.typeahead({
|
this.$el.typeahead({
|
||||||
name: this.control_name,
|
name: this.control_name,
|
||||||
template: window.states[this.control_name]['template'],
|
template: window.states[this.control_name]['template'],
|
||||||
engine: Mini,
|
engine: Mini,
|
||||||
@@ -284,6 +283,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return this.$el.on('typeahead:closed', function() {
|
||||||
|
return self.change();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return WSF_AUTOCOMPLETE_CONTROL;
|
return WSF_AUTOCOMPLETE_CONTROL;
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
|
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
|
||||||
</option>
|
</option>
|
||||||
<library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
|
<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="http" location="..\..\library\network\protocol\http\http-safe.ecf"/>
|
||||||
<library name="wsf" location="..\..\library\server\wsf\wsf-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"/>
|
<library name="wsf_html" location="..\..\library\server\wsf_html\wsf_html-safe.ecf"/>
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -12,9 +12,29 @@ inherit
|
|||||||
WSF_DATASOURCE [G]
|
WSF_DATASOURCE [G]
|
||||||
redefine
|
redefine
|
||||||
state,
|
state,
|
||||||
set_state
|
set_state,
|
||||||
|
update
|
||||||
end
|
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
|
feature --States
|
||||||
|
|
||||||
state: JSON_OBJECT
|
state: JSON_OBJECT
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ feature {NONE}
|
|||||||
make_control (n, "ul")
|
make_control (n, "ul")
|
||||||
add_class ("pagination")
|
add_class ("pagination")
|
||||||
datasource := ds
|
datasource := ds
|
||||||
|
datasource.set_on_update_page_agent (agent update)
|
||||||
end
|
end
|
||||||
|
|
||||||
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
||||||
@@ -49,16 +50,19 @@ feature --EVENT HANDLING
|
|||||||
datasource.set_page (i)
|
datasource.set_page (i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
state_changes.replace (create {JSON_STRING}.make_json (render), create {JSON_STRING}.make_json ("_html"))
|
|
||||||
datasource.update
|
datasource.update
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
feature
|
feature
|
||||||
|
|
||||||
|
update
|
||||||
|
do
|
||||||
|
state_changes.replace (create {JSON_STRING}.make_json (render), create {JSON_STRING}.make_json ("_html"))
|
||||||
|
end
|
||||||
|
|
||||||
render: STRING
|
render: STRING
|
||||||
local
|
local
|
||||||
page_count: INTEGER
|
|
||||||
paging_start: INTEGER
|
paging_start: INTEGER
|
||||||
paging_end: INTEGER
|
paging_end: INTEGER
|
||||||
cssclass: STRING
|
cssclass: STRING
|
||||||
|
|||||||
Reference in New Issue
Block a user