-
Notifications
You must be signed in to change notification settings - Fork 7
/
annotation_behavior.rb
81 lines (74 loc) · 2.59 KB
/
annotation_behavior.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# customize behavior for IiifSearch
module ESSI
module BlacklightIiifSearch
module AnnotationBehavior
##
# Create a URL for the annotation
# use a Hyrax-y URL syntax:
# protocol://host:port/concern/model_type/work_id/manifest/canvas/file_set_id/annotation/index
# @return [String]
def annotation_id
"#{base_url}/manifest/canvas/#{file_set_id}/annotation/#{hl_index}"
end
##
# Create a URL for the canvas that the annotation refers to
# match the Hyrax default canvas URL syntax:
# protocol://host:port/concern/model_type/work_id/manifest/canvas/file_set_id
# @return [String]
def canvas_uri_for_annotation
"#{base_url}/manifest/canvas/#{file_set_id}#{coordinates}"
end
private
##
# return a string like "#xywh=100,100,250,20"
# corresponding to coordinates of query term on image
# local implementation expected, value returned below is just a placeholder
# @return [String]
def coordinates
return default_coords if query.blank?
coords_json = fetch_and_parse_coords
return default_coords unless coords_json && coords_json['coords']
query_terms = query.split(' ').map(&:downcase)
matches = coords_json['coords'].select do |k, _v|
k.downcase =~ /#{query_terms.join('|')}/
end
return default_coords if matches.blank?
coords_array = matches.values.flatten(1)[hl_index]
return default_coords unless coords_array #FIXME: check default vs default_coords
"#xywh=#{coords_array.join(',')}"
end
##
# return the JSON word-coordinates file contents
# @return [JSON]
def fetch_and_parse_coords
coords = document['word_boundary_tsi']
return nil if coords.blank?
begin
JSON.parse(coords).with_indifferent_access
rescue JSON::ParserError
nil
end
end
##
# a default set of coordinates
# @return [String]
def default_coords
'#xywh=0,0,0,0'
end
##
# the base URL for the Newspaper object
# use polymorphic_url, since we deal with multiple object types
# @return [String]
def base_url
host = controller.request.base_url
controller.polymorphic_url(parent_document, host: host, locale: nil)
end
##
# return the first file set id
# @return [String]
def file_set_id
return document['id']
end
end
end
end