Skip to content

Commit

Permalink
Adds in new search,list svg icons and blacklight_icons helper
Browse files Browse the repository at this point in the history
This commit adds in several things:

 - Blacklight::Icons - a svg icon reader class
 - Blacklight::IconHelperBehavior - a helper that calls and wraps in a span,
   also caches the svg lookups
 - svg icons for search, and list. Both optimized for size reduction.

Link to sketch document used to create svgs: https://drive.google.com/open?id=0BzWuWHFTTIPES0tXUnkwQXZNaFk
  • Loading branch information
mejackreed committed Nov 7, 2016
1 parent ab3dbf9 commit 770a566
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/assets/images/blacklight/list.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/assets/images/blacklight/search.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/assets/stylesheets/blacklight/_blacklight_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
@import "blacklight/search_history";
@import "blacklight/modal";
@import "blacklight/twitter_typeahead";
@import "blacklight/icons";
42 changes: 42 additions & 0 deletions app/assets/stylesheets/blacklight/_icons.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.blacklight-icons {
display: inline-block;
height: $font-size-root;
vertical-align: text-top;
width: $font-size-root;
}

.btn-primary {
.blacklight-icons g {
stroke: $btn-primary-color;
}
}

.btn-secondary {
.blacklight-icons g {
stroke: $btn-secondary-color;
}
}

.btn-info {
.blacklight-icons g {
stroke: $btn-info-color;
}
}

.btn-success {
.blacklight-icons g {
stroke: $btn-success-color;
}
}

.btn-warning {
.blacklight-icons g {
stroke: $btn-warning-color;
}
}

.btn-danger {
.blacklight-icons g {
stroke: $btn-danger-color;
}
}
1 change: 1 addition & 0 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Blacklight::BlacklightHelperBehavior
include UrlHelperBehavior
include HashAsHiddenFieldsHelperBehavior
include LayoutHelperBehavior
include IconHelperBehavior

##
# Get the name of this application, from either:
Expand Down
3 changes: 2 additions & 1 deletion app/helpers/blacklight/catalog_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def thumbnail_url document
# @param [String] view
# @return [String]
def render_view_type_group_icon view
content_tag :span, '', class: "glyphicon #{blacklight_config.view[view].icon_class || default_view_type_group_icon_classes(view)}"
blacklight_icon(view)
end

##
Expand All @@ -253,6 +253,7 @@ def render_view_type_group_icon view
# @param [String] view
# @return [String]
def default_view_type_group_icon_classes view
Deprecation.warn(Blacklight::CatalogHelperBehavior, 'This method has been deprecated, use blacklight_icons helper instead')
"glyphicon-#{view.to_s.parameterize} view-icon-#{view.to_s.parameterize}"
end

Expand Down
16 changes: 16 additions & 0 deletions app/helpers/blacklight/icon_helper_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
##
# Module to help generate icon helpers for SVG images
module Blacklight::IconHelperBehavior
##
# Returns the raw SVG (String) for a Blacklight Icon located in
# app/assets/images/blacklight/*.svg. Caches them so we don't have to look up
# the svg everytime.
# @param [String, Symbol] icon_name
# @return [String]
def blacklight_icon(icon_name, options = {})
Rails.cache.fetch([:blacklight_icons, icon_name, options]) do
icon = Blacklight::Icon.new(icon_name, options)
content_tag(:span, icon.svg.html_safe, icon.options)
end
end
end
51 changes: 51 additions & 0 deletions app/models/blacklight/icon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Blacklight
class Icon
attr_reader :icon_name
##
# @param [String, Symbol] icon_name
# @param [Hash] options
# @param [String] classes additional classes separated by a string
def initialize(icon_name, classes: '')
@icon_name = icon_name
@classes = classes
end

##
# Returns the raw source, but you could extend this to add additional attributes
# @return [String]
def svg
file_source
end

##
# @return [Hash]
def options
{
class: classes
}
end

##
# @return [String]
def path
"blacklight/#{icon_name}.svg"
end

##
# @return [String]
def file_source
raise Blacklight::Exceptions::IconNotFound, "Could not find #{path}" if file.blank?
file.source.force_encoding('UTF-8')
end

private

def file
Rails.application.assets.find_asset(path)
end

def classes
" blacklight-icons #{@classes} ".strip
end
end
end
2 changes: 1 addition & 1 deletion app/views/catalog/_search_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<span class="input-group-btn">
<button type="submit" class="btn btn-primary search-btn" id="search">
<span class="submit-search-text"><%= t('blacklight.search.form.submit') %></span>
<span class="glyphicon glyphicon-search"></span>
<%= blacklight_icon :search %>
</button>
</span>
</div>
Expand Down
3 changes: 3 additions & 0 deletions lib/blacklight/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ class ExpiredSessionToken < StandardError
end

class ECONNREFUSED < ::Errno::ECONNREFUSED; end

class IconNotFound < StandardError
end
end
end
8 changes: 8 additions & 0 deletions spec/helpers/blacklight/icon_helper_behavior_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
RSpec.describe Blacklight::IconHelperBehavior do
describe '#blacklight_icon' do
it 'wraps the svg in a span with classes' do
expect(helper.blacklight_icon(:search))
.to have_css 'span.blacklight-icons svg'
end
end
end
34 changes: 34 additions & 0 deletions spec/models/blacklight/icon_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
RSpec.describe Blacklight::Icon do
subject { described_class.new(:search, classes: 'awesome') }
describe '#svg' do
it 'returns a string' do
expect(subject.svg).to be_an String
end
it 'returns raw svg' do
expect(Capybara.string(subject.svg))
.to have_css 'svg title', text: 'Search'
end
end
describe '#options' do
it 'applies options classes and default class' do
expect(subject.options[:class]).to eq 'blacklight-icons awesome'
end
end
describe '#path' do
it 'prepends blacklight and sufixes .svg' do
expect(subject.path).to eq 'blacklight/search.svg'
end
end
describe 'file_source' do
context 'file is not available' do
subject { described_class.new(:yolo) }
it { expect { subject.file_source }
.to raise_error(Blacklight::Exceptions::IconNotFound) }
end
context 'file is available' do
it 'returns the filesource' do
expect(subject.file_source).to include '<svg'
end
end
end
end
4 changes: 4 additions & 0 deletions spec/views/catalog/_view_type_group.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
describe "catalog/_view_type_group" do
let(:blacklight_config) { Blacklight::Configuration.new }
let(:response) { instance_double(Blacklight::Solr::Response, empty?: false) }
let(:icon_instance) { instance_double(Blacklight::Icon) }

before do
allow(view).to receive(:view_label) do |view|
view.to_s
end
allow(Blacklight::Icon).to receive(:new).and_return icon_instance
allow(icon_instance).to receive(:svg).and_return '<svg></svg>'
allow(icon_instance).to receive(:options).and_return({})
allow(view).to receive_messages(how_sort_and_per_page?: true, blacklight_config: blacklight_config)
controller.request.path_parameters[:action] = 'index'
assign(:response, response)
Expand Down

0 comments on commit 770a566

Please sign in to comment.