Skip to content

Commit

Permalink
Merge pull request #1249 from sul-dlss/no-namespace
Browse files Browse the repository at this point in the history
Stop namespacing components
  • Loading branch information
thatbudakguy authored Aug 30, 2024
2 parents 358a73f + d8742b6 commit 9747846
Show file tree
Hide file tree
Showing 42 changed files with 327 additions and 363 deletions.
File renamed without changes.
18 changes: 18 additions & 0 deletions app/components/alert_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class AlertComponent < ViewComponent::Base
attr_reader :title, :body, :type, :icon

def initialize(body:, type:, title: nil)
super
@title = title
@body = body
@type = type
@icon = match_icon
end

def match_icon
icon_mapping = { 'info' => 'bi-info-circle-fill', 'warning' => 'bi-exclamation-triangle-fill',
'danger' => 'bi-exclamation-triangle-fill', 'sucess' => 'bi-check-circle-fill',
'note' => 'bi-exclamation-circle-fill' }
icon_mapping[type]
end
end
20 changes: 20 additions & 0 deletions app/components/also_available_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

# Display also available as links in sidebar
class AlsoAvailableComponent < ViewComponent::Base
attr_reader :document

def initialize(document:)
@document = document
super
end

# a list of all the available other urls for the document
def also_available_links
{ SearchWorks: document.searchworks_url }.compact_blank
end

def render?
also_available_links.present?
end
end
7 changes: 7 additions & 0 deletions app/components/code_snippet_link_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Display link that triggers modal with code snippet content
class CodeSnippetLinkComponent < ViewComponent::Base
# Do not display the link to code snippets if the document is restricted
def key
'code_snippet_link'
end
end
130 changes: 130 additions & 0 deletions app/components/code_snippet_modal_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# frozen_string_literal: true

class CodeSnippetModalComponent < ViewComponent::Base
def initialize(document)
@document = document
@raster_data_type = raster_data?
@vector_data_type = vector_data?
super
end

def code_block(content)
sanitize "<pre><code>#{h content}</code></pre>"
end

def render_python
substitute_values(retrieve_text('Python'))
end

def render_r
substitute_values(retrieve_text('R'))
end

def render_leaflet
substitute_values(retrieve_text('Leaflet'))
end

# Incorporate
def substitute_values(content)
sub_content = content

# replace WXS_ID
sub_content = sub_content.gsub('<<WXS_ID>>',
substitute_id_value(@document.wxs_identifier, '[Insert Web Services ID]'))

# If layer id is required
sub_content = sub_content.gsub('<<LAYER_ID>>',
substitute_id_value(layer_id, '[Insert Layer ID]'))

# replace WMS reference
sub_content = sub_content.gsub('<<GEOSERVER_WMS>>',
substitute_webservice_value(@document.references.wms, '[Insert WMS endpoint]'))

# replace WFS reference
sub_content = sub_content.gsub('<<GEOSERVER_WFS>>',
substitute_webservice_value(@document.references.wfs, '[Insert WFS endpoint]'))

# Replace Bounding box information
if @document.geom_field.blank? || @document.geometry.geom.blank?
# If geometry information is not available, subsitute the coordinates with placeholder text
sub_content = sub_content.gsub('<<MIN_X>>', '[Insert bounding box min X value]')
sub_content = sub_content.gsub('<<MIN_Y>>', '[Insert bounding box min Y value]')
sub_content = sub_content.gsub('<<MAX_X>>', '[Insert bounding box max X value]')
sub_content = sub_content.gsub('<<MAX_Y>>', '[Insert bounding box max Y value]')
else
geometry = @document.geometry.geom
bbox_array = geometry.split('ENVELOPE(')[1].split(')')[0].split(',').map(&:strip)

if bbox_array.length == 4
min_x = bbox_array[0]
max_x = bbox_array[1]
min_y = bbox_array[2]
max_y = bbox_array[3]

sub_content = sub_content.gsub('<<MIN_X>>', min_x)
sub_content = sub_content.gsub('<<MIN_Y>>', min_y)
sub_content = sub_content.gsub('<<MAX_X>>', max_x)
sub_content = sub_content.gsub('<<MAX_Y>>', max_y)
end
end
sub_content
end

def layer_id
# If the Solr document has a wxs_identifier field, handle two cases:
# if the string has 'druid:', return the portion after this prefix.
# Otherwise, return the whole string. The latter case is for other institutions' data.
if @document.wxs_identifier.present?
wxs_id = @document.wxs_identifier
return wxs_id.include?('druid:') ? wxs_id.split('druid:')[1] : wxs_id
end

nil
end

def substitute_id_value(value, placeholder_value)
value.presence || placeholder_value
end

def substitute_webservice_value(webservice, placeholder_value)
webservice.nil? || webservice.endpoint.nil? ? placeholder_value : webservice.endpoint
end

private

# Get the code sample text based on the data type and programming language
def retrieve_text(language)
file_name = code_sample_file_name(language)
return if file_name.empty?

File.read(Rails.root.join('config', 'code_samples', file_name).to_s).to_s
end

def vector_data?
return false unless @document.key?(Settings.FIELDS.RESOURCE_TYPE)

vector_types = ['polygon data', 'point data', 'line data', 'index maps',
'vector data']

# If the intersection of these arrays is not empty, then there is at least one vector type value
!(@document[Settings.FIELDS.RESOURCE_TYPE].map(&:downcase) &
vector_types).empty?
end

def raster_data?
return false unless @document.key?(Settings.FIELDS.RESOURCE_TYPE)

@document[Settings.FIELDS.RESOURCE_TYPE].map(&:downcase).include?('raster data')
end

def code_sample_file_name(language)
case language
when 'Python'
@vector_data_type ? 'vector_python.txt' : 'raster_python.txt'
when 'Leaflet'
@vector_data_type ? 'vector_leaflet.txt' : 'raster_leaflet.txt'
when 'R'
@vector_data_type ? 'vector_r.txt' : 'raster_r.txt'
end
end
end
15 changes: 15 additions & 0 deletions app/components/document/sidebar_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# Render the sidebar on the show view
module Document
class SidebarComponent < Geoblacklight::Document::SidebarComponent
def components
[
Geoblacklight::LoginLinkComponent.new(document: document),
Geoblacklight::StaticMapComponent.new(document: document),
DownloadLinksComponent.new(document: document),
AlsoAvailableComponent.new(document: document)
]
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= title %>
<div class="mb-2">
<%= render Earthworks::HeaderIconsComponent.new(document: @document) %>
<%= render HeaderIconsComponent.new(document: @document) %>
</div>
<%= content_tag @component,
id: @id,
Expand Down Expand Up @@ -43,9 +43,9 @@

<section class="col-lg-6">
<% if (@document.image? || @document.item_viewer.iiif) && !@document[Settings.FIELDS[:GEOREFERENCED]] %>
<%= render Earthworks::AlertComponent.new(type: 'info', body: t('earthworks.show.no_georeference_message')) %>
<%= render AlertComponent.new(type: 'info', body: t('earthworks.show.no_georeference_message')) %>
<% end %>
<%= render Geoblacklight::ViewerContainerComponent.new(document: @document) %>
<%= render Geoblacklight::AttributeTableComponent.new(document: @document) %>
<%= render Geoblacklight::IndexMapInspectComponent.new(document: @document) %>
</section>
</section>
4 changes: 4 additions & 0 deletions app/components/document_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class DocumentComponent < Geoblacklight::DocumentComponent
end
32 changes: 32 additions & 0 deletions app/components/download_links_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

# Display expandable file download links in sidebar
class DownloadLinksComponent < Geoblacklight::DownloadLinksComponent
def download_link_file(label, id, url)
link_to(
label,
url,
'contentUrl' => url,
:class => 'btn btn-primary',
:data => {
download: 'trigger',
download_type: 'direct',
download_id: id
}
)
end

# Generates the link markup for the IIIF JPEG download
# @return [String]
def download_link_iiif
link_to(
download_text('JPG'),
iiif_jpg_url,
'contentUrl' => iiif_jpg_url,
:class => 'btn btn-primary',
:data => {
download: 'trigger'
}
)
end
end
20 changes: 0 additions & 20 deletions app/components/earthworks/alert_component.rb

This file was deleted.

22 changes: 0 additions & 22 deletions app/components/earthworks/also_available_component.rb

This file was deleted.

9 changes: 0 additions & 9 deletions app/components/earthworks/code_snippet_link_component.rb

This file was deleted.

Loading

0 comments on commit 9747846

Please sign in to comment.