Skip to content

Commit

Permalink
Merge pull request #607 from sul-dlss/exhibit-specific-labels
Browse files Browse the repository at this point in the history
Settable and Sortable Navigation Links
  • Loading branch information
cbeer committed Apr 6, 2014
2 parents 6f6c248 + a7589e7 commit c974a13
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 34 deletions.
26 changes: 26 additions & 0 deletions app/assets/javascripts/spotlight/appearance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Spotlight.onLoad(function(){
$('#nested-navigation').nestable({maxDepth: 1});
updateWeightsAndRelationships($('#nested-navigation'));
addRestoreDefaultBehavior();
});

function addRestoreDefaultBehavior(){
$("[data-behavior='restore-default']").each(function(){
var hidden = $("[data-default-value]", $(this));
var value = $($("[data-in-place-edit-target]", $(this)).data('in-place-edit-target'), $(this));
var button = $("[data-restore-default]", $(this));
hidden.on('blur', function(){
if( $(this).val() == $(this).data('default-value') ) {
button.addClass('hidden');
} else {
button.removeClass('hidden');
}
});
button.on('click', function(e){
e.preventDefault();
hidden.val(hidden.data('default-value'));
value.text(hidden.data('default-value'));
button.hide();
});
});
}
2 changes: 1 addition & 1 deletion app/controllers/spotlight/about_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def attach_breadcrumbs
if action_name == 'edit'
add_breadcrumb t(:'spotlight.pages.index.about_pages.header'), exhibit_about_pages_path(@exhibit)
else
add_breadcrumb t(:'spotlight.about_pages.nav_link'), [@exhibit, @exhibit.main_about_page]
add_breadcrumb (@exhibit.main_navigations.about.label_or_default), [@exhibit, @exhibit.main_about_page]
end

unless @page == @exhibit.main_about_page
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/spotlight/appearances_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def load_and_authorize_appearance
def appearance_params
params.require(:appearance).permit(:default_per_page, :thumbnail_size,
document_index_view_types: @appearance.view_type_options,
sort_fields: @appearance.allowed_params)
sort_fields: @appearance.allowed_params,
main_navigations: [:id, :label, :weight])
end

end
2 changes: 1 addition & 1 deletion app/controllers/spotlight/browse_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def history_session

def attach_breadcrumbs
add_breadcrumb t(:'spotlight.exhibits.breadcrumb', title: @exhibit.title), @exhibit
add_breadcrumb t(:'spotlight.browse.nav_link'), exhibit_browse_index_path(@exhibit)
add_breadcrumb (@exhibit.main_navigations.browse.label_or_default), exhibit_browse_index_path(@exhibit)
end

def _prefixes
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/spotlight/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def show
end

if current_browse_category
add_breadcrumb t(:'spotlight.browse.nav_link'), exhibit_browse_index_path(current_browse_category.exhibit)
add_breadcrumb current_browse_category.exhibit.main_navigations.browse.label_or_default, exhibit_browse_index_path(current_browse_category.exhibit)
add_breadcrumb current_browse_category.title, exhibit_browse_path(current_browse_category.exhibit, current_browse_category)
elsif current_page_context
add_breadcrumb current_page_context.title, [current_page_context.exhibit, current_page_context] if current_page_context.title.present? and !current_page_context.is_a?(Spotlight::HomePage)
Expand Down
20 changes: 16 additions & 4 deletions app/forms/spotlight/appearance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Spotlight
class Appearance
extend ActiveModel::Naming
include ActiveModel::Conversion

def initialize(configuration)
@configuration = configuration
end
Expand All @@ -11,6 +11,8 @@ def initialize(configuration)
delegate :persisted?, :exhibit, :exhibit_id, :default_per_page, :thumbnail_size,
:default_blacklight_config, to: :configuration

delegate :main_navigations, to: :exhibit

##
# This enables us to have a group of checkboxes that is backed by the array
# stored in Spotlight::BlacklightConfiguration#document_index_view_types
Expand Down Expand Up @@ -40,9 +42,8 @@ def allowed_params
end

def update(params)
params[:document_index_view_types] = keep_selected_values(params[:document_index_view_types])
params[:sort_fields] = enable_sort_fields(keep_selected_values(params[:sort_fields]))
configuration.update(params)
configuration.exhibit.update(main_navigations_attributes: exhibit_params(params)) if exhibit_params(params)
configuration.update(configuration_params(params))
end

def view_type_options
Expand Down Expand Up @@ -73,6 +74,17 @@ def enable_sort_fields(checked_fields)
end
end

def configuration_params(params)
p = params.except(:main_navigations)
p[:document_index_view_types] = keep_selected_values(p[:document_index_view_types])
p[:sort_fields] = enable_sort_fields(keep_selected_values(p[:sort_fields]))
p
end

def exhibit_params(params)
params[:main_navigations].try(:values)
end

##
# A group of checkboxes on a form returns values like this:
# {"list"=>["0", "1"], "gallery"=>["0", "1"], "map"=>["0"]}
Expand Down
14 changes: 14 additions & 0 deletions app/models/spotlight/exhibit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Spotlight::Exhibit < ActiveRecord::Base
has_many :custom_fields, dependent: :delete_all
has_many :contacts, dependent: :delete_all # These are the contacts who appear in the sidebar
has_many :contact_emails, dependent: :delete_all # These are the contacts who get "Contact us" emails
has_many :main_navigations, dependent: :delete_all
has_many :solr_document_sidecars, dependent: :delete_all
has_many :roles, dependent: :delete_all
has_many :attachments, dependent: :destroy
Expand All @@ -29,6 +30,7 @@ class Spotlight::Exhibit < ActiveRecord::Base
accepts_nested_attributes_for :about_pages
accepts_nested_attributes_for :feature_pages
accepts_nested_attributes_for :home_page, update_only: true
accepts_nested_attributes_for :main_navigations
accepts_nested_attributes_for :contacts
accepts_nested_attributes_for :contact_emails, reject_if: proc {|attr| attr['email'].blank?}
accepts_nested_attributes_for :roles, allow_destroy: true, reject_if: proc {|attr| attr['user_key'].blank?}
Expand All @@ -42,6 +44,7 @@ class Spotlight::Exhibit < ActiveRecord::Base
before_create :build_home_page
after_create :initialize_config
after_create :initialize_browse
after_create :initialize_main_navigation
before_save :sanitize_description

validate :title, presence: true
Expand Down Expand Up @@ -91,7 +94,18 @@ def initialize_browse
long_description: "All items in this exhibit"
end

def initialize_main_navigation
self.default_main_navigations.each_with_index do |nav_type, weight|
self.main_navigations.create nav_type: nav_type, weight: weight
end
end

def sanitize_description
self.description = HTML::FullSanitizer.new.sanitize(description) if description_changed?
end

def default_main_navigations
[:curated_features, :browse, :about]
end

end
20 changes: 20 additions & 0 deletions app/models/spotlight/main_navigation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Spotlight
class MainNavigation < ActiveRecord::Base
belongs_to :exhibit
default_scope -> { order("weight ASC") }
scope :browse, -> { where(nav_type: "browse").take }
scope :about, -> { where(nav_type: "about").take }

def label_or_default
if label.present?
label
else
default_label
end
end

def default_label
I18n.t(:"spotlight.main_navigation.#{nav_type}")
end
end
end
3 changes: 3 additions & 0 deletions app/views/shared/_about_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% if current_exhibit.main_about_page %>
<li class="<%= "active" if on_about_page? %>"><%= link_to navigation.label_or_default, [spotlight, current_exhibit, current_exhibit.main_about_page] %></li>
<% end %>
3 changes: 3 additions & 0 deletions app/views/shared/_browse_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% if current_exhibit.has_browse_categories? %>
<li class="<%= "active" if on_browse_page? %>"><%= link_to navigation.label_or_default, spotlight.exhibit_browse_index_path(current_exhibit) %></li>
<% end %>
15 changes: 15 additions & 0 deletions app/views/shared/_curated_features_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% published_top_level_feature_pages = current_exhibit.feature_pages.published.at_top_level -%>
<% if published_top_level_feature_pages.present? %>
<% if published_top_level_feature_pages.many? %>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><%= navigation.label_or_default %> <b class="caret"></b></a>
<ul class="dropdown-menu">
<% published_top_level_feature_pages.each do |page| %>
<li><%= link_to page.title, [spotlight, page.exhibit, page] %></li>
<% end %>
</ul>
</li>
<% else %>
<li class="<%= "active" if current_page?(url_for([spotlight, published_top_level_feature_pages.first.exhibit, published_top_level_feature_pages.first])) %>"><%= link_to published_top_level_feature_pages.first.title, [spotlight, published_top_level_feature_pages.first.exhibit, published_top_level_feature_pages.first] %></li>
<% end %>
<% end %>
26 changes: 3 additions & 23 deletions app/views/shared/_exhibit_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,11 @@
<div class="container">
<ul class="nav navbar-nav">
<% if current_exhibit -%>
<li class="<%= "active" if current_page?(current_exhibit) %>"><%= link_to t(:'spotlight.curation.nav.home'), current_exhibit %></li>
<% published_top_level_feature_pages = current_exhibit.feature_pages.published.at_top_level -%>
<% if published_top_level_feature_pages.present? %>
<% if published_top_level_feature_pages.many? %>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><%= t(:'spotlight.feature_pages.nav_link') %> <b class="caret"></b></a>
<ul class="dropdown-menu">
<% published_top_level_feature_pages.each do |page| %>
<li>
<%= link_to page.title, [spotlight, page.exhibit, page] %>
</li>
<% end %>
</ul>
</li>
<% else %>
<li class="<%= "active" if current_page?(url_for([spotlight, published_top_level_feature_pages.first.exhibit, published_top_level_feature_pages.first])) %>"><%= link_to published_top_level_feature_pages.first.title, [spotlight, published_top_level_feature_pages.first.exhibit, published_top_level_feature_pages.first] %></li>
<li class="<%= "active" if current_page?(current_exhibit) %>"><%= link_to t(:'spotlight.curation.nav.home'), current_exhibit %></li>
<% current_exhibit.main_navigations.each do |navigation| %>
<%= render partial: "shared/#{navigation.nav_type}_navbar", locals: {navigation: navigation} %>
<% end %>
<% end %>
<% if current_exhibit.has_browse_categories? %>
<li class="<%= "active" if on_browse_page? %>"><%= link_to t(:'spotlight.browse.nav_link'), spotlight.exhibit_browse_index_path(current_exhibit) %></li>
<% end %>
<% if current_exhibit.main_about_page %>
<li class="<%= "active" if on_about_page? %>"><%= link_to t(:'spotlight.about_pages.nav_link'), [spotlight, current_exhibit, current_exhibit.main_about_page] %></li>
<% end %>
<% end %>
</ul>

<div class="navbar-right col-sm-4 col-md-4">
Expand Down
2 changes: 1 addition & 1 deletion app/views/spotlight/about_pages/_sidebar.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div id="sidebar" class="col-md-3" role="complementary">
<h4><%= t :'.nav_link' %></h4>
<h4><%= current_exhibit.main_navigations.about.label_or_default %></h4>
<ul class="nav sidenav">
<% current_exhibit.about_pages.published.each do |page| %>
<li class="<%= 'active' if current_page? [page.exhibit, page] %>"><%= link_to_unless_current page.title, [page.exhibit, page] %></li>
Expand Down
33 changes: 33 additions & 0 deletions app/views/spotlight/appearances/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,39 @@
<div id="content" class="col-md-9">
<%= administration_page_title %>
<%= bootstrap_form_for @appearance, url: spotlight.exhibit_appearance_path(@exhibit), style: :horizontal, left: 'col-md-3', right: 'col-sm-5' do |f| %>
<%= field_set_tag t(:'.main_navigation.menu') do %>
<p><%= t(:'.main_navigation.help') %></p>
<div class="panel-group dd main_navigation_admin col-sm-7" id="nested-navigation">
<ol class="dd-list">
<%= f.fields_for :main_navigations do |navigation| %>
<% @appearance.main_navigations.each do |nav| %>
<%= navigation.fields_for nav.id.to_s, nav do |label| %>
<li class="dd-item dd3-item" data-id="<%= nav.id %>">
<div class="dd3-content panel panel-default">
<div class="dd-handle dd3-handle"><%= t :drag %></div>
<div class="panel-heading page" data-behavior="restore-default">
<%= label.hidden_field :id %>
<div class="row">
<div class="col-sm-8">
<h3 class="panel-title" data-in-place-edit-target=".edit-in-place">
<a href="#edit-in-place" class="field-label edit-in-place"><%= nav.label_or_default %></a>
<%= label.hidden_field :label, data: {:"default-value" => nav.default_label} %>
</h3>
</div>
<div class="col-sm-4">
<%= button_tag t(:'.restore_default'), data: {:"restore-default" => true}, class: "btn restore-default btn-default btn-sm pull-right #{'hidden' if nav.label.blank? || nav.label == nav.default_label}" %>
</div>
</div>
<%= label.hidden_field :weight, data: {property: "weight"} %>
</div>
</div>
</li>
<% end %>
<% end %>
<% end %>
</ol>
</div>
<% end %>
<%= field_set_tag t(:'.search_results') do %>
<%= f.form_group :document_index_view_types, label: {text: t(:'.document_index_view_types')} do %>
Expand Down
11 changes: 10 additions & 1 deletion config/locales/spotlight.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,13 @@ en:
header: Appearance
document_index_view_types: Result page types
default_per_page: Default results per page
main_navigation:
menu: Main Navigation Menu
help: Click a menu item to change its display label. Drag and drop a menu item to change their order in the main navigation menu.
restore_default: "Restore default"
search_results: Search Results
sort_fields: Sort fields
thumbnail_size: Thumbnail size
sort_fields: Sort fields
thumbnail:
small: Small
medium: Medium
Expand Down Expand Up @@ -233,6 +238,10 @@ en:
confirmation_sent: Confirmation sent.
not_validated: Not validated.
resend: Resend confirmation?
main_navigation:
about: "About"
browse: "Browse"
curated_features: "Curated Features"
pages:
order_pages:
pages_header: Defined Pages
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20140403180324_create_spotlight_main_navigations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateSpotlightMainNavigations < ActiveRecord::Migration
def change
create_table :spotlight_main_navigations do |t|
t.string :label
t.integer :weight, default: 20
t.string :nav_type
t.references :exhibit
t.timestamps
end
add_index :spotlight_main_navigations, :exhibit_id
end
end
42 changes: 42 additions & 0 deletions spec/features/main_navigation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'

describe "Main navigation labels are settable" do
let!(:exhibit) { FactoryGirl.create(:exhibit) }
let!(:about) { FactoryGirl.create(:about_page, exhibit: exhibit, published: true) }
before do
about_nav = exhibit.main_navigations.about
about_nav.label = "New About Label"
about_nav.save
browse_nav = exhibit.main_navigations.browse
browse_nav.label = "New Browse Label"
browse_nav.save
search = exhibit.searches.first
search.on_landing_page = true
search.save
exhibit.reload
end

it "should have the configured about and browse navigation labels" do
visit spotlight.exhibit_path(exhibit)
expect(page).to have_css(".navbar-nav li", text: "New About Label")
expect(page).to have_css(".navbar-nav li", text: "New Browse Label")
end
it "should have the configured about page label in the sidebar" do
visit spotlight.exhibit_about_page_path(exhibit, about)
expect(page).to have_css("#sidebar h4", text: "New About Label")
end
it "should have the configured about page label visible in the breadcrumb" do
visit spotlight.exhibit_about_page_path(exhibit, about)
expect(page).to have_css(".breadcrumb li", text: "New About Label")
end
it "should have the configured browse page label visible in the breadcrumb of the browse index page" do
visit spotlight.exhibit_browse_index_path(exhibit, exhibit.searches.first)
expect(page).to have_content("New Browse Label")
expect(page).to have_css(".breadcrumb li", text: "New Browse Label")
end
it "should have the configured browse page label visible in the breadcrumb of the browse show page" do
visit spotlight.exhibit_browse_path(exhibit, exhibit.searches.first)
expect(page).to have_content("New Browse Label")
expect(page).to have_css(".breadcrumb li", text: "New Browse Label")
end
end
6 changes: 6 additions & 0 deletions spec/models/spotlight/exhibit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
expect(subject.searches.published).to be_empty
expect(subject.searches.first.query_params).to be_empty
end

it "should have main navigations" do
expect(subject.main_navigations).to have(3).main_navigations
expect(subject.main_navigations.map(&:label).compact).to be_blank
expect(subject.main_navigations.map(&:weight)).to eq [0, 1, 2]
end
end

describe "contacts" do
Expand Down
Loading

0 comments on commit c974a13

Please sign in to comment.