-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added full navigation definition in UserConfiguration (Group/Link) (#…
…2270) Added full navigation definition in UserConfiguration. This means that the navigation bar is totally customizable from the configuration. It even adds support for links created directly through configuration. With the combination of app tokens and links one could completely redefine the navigation bar.
- Loading branch information
Showing
19 changed files
with
582 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Wrapper around OodApp to override category and subcategory | ||
# This is to create artificial groups and subgroups for a custom navigation | ||
class AppRecategorizer < SimpleDelegator | ||
|
||
def initialize(ood_app, category: nil, subcategory: nil) | ||
super(ood_app) | ||
@inner_category = category | ||
@inner_subcategory = subcategory | ||
end | ||
|
||
def category | ||
inner_category | ||
end | ||
|
||
def subcategory | ||
inner_subcategory | ||
end | ||
|
||
private | ||
|
||
attr_reader :inner_category, :inner_subcategory | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Creates navigation items based on user configuration. | ||
# | ||
class NavBar | ||
|
||
STATIC_LINKS = { | ||
all_apps: "layouts/nav/all_apps", | ||
featured_apps: "layouts/nav/featured_apps", | ||
sessions: "layouts/nav/sessions", | ||
} | ||
|
||
def self.items(nav_config) | ||
nav_config.map do |nav_item| | ||
if nav_item.is_a?(String) | ||
item_from_token(nav_item) | ||
elsif nav_item.is_a?(Hash) | ||
if nav_item.fetch(:links, nil) | ||
extend_group(nav_menu(nav_item)) | ||
elsif nav_item.fetch(:url, nil) | ||
extend_link(nav_link(nav_item, nil, nil)) | ||
elsif nav_item.fetch(:apps, nil) | ||
matched_apps = nav_apps(nav_item, nil, nil) | ||
extend_link(matched_apps.first.links.first) if matched_apps.first && matched_apps.first.links.first | ||
elsif nav_item.fetch(:profile, nil) | ||
extend_link(nav_profile(nav_item, nil, nil)) | ||
end | ||
end | ||
end.flatten.compact | ||
end | ||
|
||
private | ||
|
||
def self.nav_menu(hash_item) | ||
menu_title = hash_item.fetch(:title, '') | ||
menu_items = hash_item.fetch(:links, []) | ||
|
||
group_title = '' | ||
apps = menu_items.map do |item| | ||
if item.is_a?(String) | ||
item = { apps: item } | ||
end | ||
|
||
group_title = item.fetch(:group, group_title) | ||
if item.fetch(:url, nil) | ||
nav_link(item, menu_title, group_title) | ||
elsif item.fetch(:apps, nil) | ||
nav_apps(item, menu_title, group_title) | ||
elsif item.fetch(:profile, nil) | ||
nav_profile(item, menu_title, group_title) | ||
end | ||
end.flatten.compact | ||
|
||
OodAppGroup.new(apps: apps, title: menu_title, sort: false) | ||
end | ||
|
||
def self.nav_link(item, category, subcategory) | ||
OodAppLink.new(item).categorize(category: category, subcategory: subcategory) | ||
end | ||
|
||
def self.nav_apps(item, category, subcategory) | ||
app_configs = Array.wrap(item.fetch(:apps, [])) | ||
app_configs.map do |config_string| | ||
matched_apps = Router.pinned_apps_from_token(config_string, SysRouter.apps) | ||
matched_apps.map do |reg_app| | ||
AppRecategorizer.new(reg_app, category: category, subcategory: subcategory) | ||
end | ||
end.flatten | ||
end | ||
|
||
def self.nav_profile(item, category, subcategory) | ||
profile = item.fetch(:profile) | ||
profile_data = item.clone | ||
profile_data[:title] = profile unless item.fetch(:title, nil) | ||
profile_data[:url] = Rails.application.routes.url_helpers.settings_path('settings[profile]' => profile) | ||
profile_data[:data] = { method: 'post' } | ||
profile_data[:new_tab] = false | ||
OodAppLink.new(profile_data).categorize(category: category, subcategory: subcategory) | ||
end | ||
|
||
def self.item_from_token(token) | ||
static_link_template = STATIC_LINKS.fetch(token.to_sym, nil) | ||
if static_link_template | ||
return NavItemDecorator.new({}, static_link_template) | ||
end | ||
|
||
matched_apps = Router.pinned_apps_from_token(token, SysRouter.apps) | ||
if matched_apps.size == 1 | ||
extend_link(matched_apps.first.links.first) | ||
elsif matched_apps.size > 1 | ||
extend_group(OodAppGroup.groups_for(apps: matched_apps).first) | ||
else | ||
group = OodAppGroup.groups_for(apps: SysRouter.apps).select { |g| g.title.downcase == token.downcase }.first | ||
group.nil? ? nil : extend_group(group) | ||
end | ||
end | ||
|
||
def self.extend_group(item) | ||
NavItemDecorator.new(item, 'layouts/nav/group') | ||
end | ||
|
||
def self.extend_link(item) | ||
NavItemDecorator.new(item, 'layouts/nav/link') | ||
end | ||
|
||
class NavItemDecorator < SimpleDelegator | ||
attr_reader :partial_path | ||
|
||
def initialize(nav_item, partial_path) | ||
super(nav_item) | ||
@partial_path = partial_path | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'test_helper' | ||
|
||
class AppRecategorizerTest < ActiveSupport::TestCase | ||
|
||
test "should create an OodApp with category and subcategory" do | ||
result = AppRecategorizer.new(stub(), category: "test_category", subcategory: "test_subcategory") | ||
assert_equal "test_category", result.category | ||
assert_equal "test_subcategory", result.subcategory | ||
end | ||
|
||
end |
Oops, something went wrong.