Skip to content

Commit

Permalink
Added support for custom JS feature
Browse files Browse the repository at this point in the history
  • Loading branch information
abujeda committed May 1, 2023
1 parent e484eee commit d1cfbbc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions apps/dashboard/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,17 @@ def help_links
NavBar.menu_items({ links: help_items }) unless help_items.empty?
end

# Creates the custom CSS paths based on user configuration and the public URL
def custom_css_paths
@user_configuration.custom_css_files.map do |css_file|
css_file.to_s.empty? ? nil : File.join(@user_configuration.public_url, css_file)
end.compact
end

# Creates the custom JS paths based on user configuration and the public URL
def custom_javascript_paths
@user_configuration.custom_javascript_files.map do |js_file|
js_file.to_s.empty? ? nil : File.join(@user_configuration.public_url, js_file)
end.compact
end
end
6 changes: 6 additions & 0 deletions apps/dashboard/app/models/user_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class UserConfiguration
# example:
# custom_css_files: ["core.css", "/custom/team1.css"]
ConfigurationProperty.property(name: :custom_css_files, default_value: []),
# Custom JavaScript files to add to the application.html.erb template
# The files need to be deployed to the Apache public directory: /var/www/ood/public
# The URL path will be prepended with the public_url property
# example:
# custom_css_files: ["tracking.js", "/custom/team1_scripts.js"]
ConfigurationProperty.property(name: :custom_javascript_files, default_value: []),

ConfigurationProperty.property(name: :dashboard_title, default_value: 'Open OnDemand', read_from_env: true),

Expand Down
3 changes: 3 additions & 0 deletions apps/dashboard/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<%= favicon_link_tag 'favicon.ico', href: @user_configuration.public_url.join('favicon.ico'), skip_pipeline: true %>
<%= javascript_include_tag 'application', nonce: true %>
<% custom_javascript_paths.each do |path| %>
<script src="<%= path %>" nonce="<%= content_security_policy_nonce %>"></script>
<% end %>
<%= stylesheet_link_tag 'application', nonce: content_security_policy_nonce, media: 'all', rel: 'preload stylesheet', as: 'style', type: 'text/css' %>
<%= render partial: '/layouts/nav/styles', locals: { bg_color: @user_configuration.brand_bg_color, link_active_color: @user_configuration.brand_link_active_bg_color } %>
<% custom_css_paths.each do |path| %>
Expand Down
30 changes: 30 additions & 0 deletions apps/dashboard/test/helpers/application_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,36 @@ def setup
assert_equal [], custom_css_paths
end

test 'custom_javascript_paths should prepend public_url to all js file paths' do
@user_configuration = stub(:custom_javascript_files => ['/test.js'], :public_url => Pathname.new('/public'))
assert_equal ['/public/test.js'], custom_javascript_paths

@user_configuration = stub(:custom_javascript_files => ['test.js'], :public_url => Pathname.new('/public'))
assert_equal ['/public/test.js'], custom_javascript_paths

@user_configuration = stub(:custom_javascript_files => ['/custom/js/test.js'], :public_url => Pathname.new('/public'))
assert_equal ['/public/custom/js/test.js'], custom_javascript_paths

@user_configuration = stub(:custom_javascript_files => ['custom/js/test.js'], :public_url => Pathname.new('/public'))
assert_equal ['/public/custom/js/test.js'], custom_javascript_paths
end

test 'custom_javascript_paths should should handle nil and empty file paths' do
@user_configuration = stub(:custom_javascript_files => ['/test.js', nil, 'other.js'],
:public_url => Pathname.new('/public'))
assert_equal ['/public/test.js', '/public/other.js'], custom_javascript_paths

@user_configuration = stub(:custom_javascript_files => [nil], :public_url => Pathname.new('/public'))
assert_equal [], custom_javascript_paths

@user_configuration = stub(:custom_javascript_files => ['/test.js', '', 'other.js'],
:public_url => Pathname.new('/public'))
assert_equal ['/public/test.js', '/public/other.js'], custom_javascript_paths

@user_configuration = stub(:custom_javascript_files => [''], :public_url => Pathname.new('/public'))
assert_equal [], custom_javascript_paths
end

test "icon_tag should should render icon tag for known icon schemas" do
@user_configuration = stub({ public_url: Pathname.new('/public') })
['fa', 'fas', 'far', 'fab', 'fal'].each do |icon_schema|
Expand Down
1 change: 1 addition & 0 deletions apps/dashboard/test/models/user_configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class UserConfigurationTest < ActiveSupport::TestCase
pinned_apps_menu_length: 6,
profile_links: [],
custom_css_files: [],
custom_javascript_files: [],
dashboard_title: "Open OnDemand",
public_url: Pathname.new("/public"),
announcement_path: [Pathname.new('/etc/ood/config/announcement.md'), Pathname.new('/etc/ood/config/announcement.yml'), Pathname.new('/etc/ood/config/announcements.d')],
Expand Down

0 comments on commit d1cfbbc

Please sign in to comment.