Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

turn dashboard.js into a module #3698

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions apps/dashboard/app/javascript/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
'use strict';

jQuery(function(){
$("a[target=_blank]").on("click", function(event) {
// open url using javascript, instead of following directly
event.preventDefault();
import { openLinkInJs } from './utils';

if(window.open($(this).attr("href")) == null){
// link was not opened in new window, so display error msg to user
const html = $("#js-alert-danger-template").html();
const msg = "This link is configured to open in a new window, but it doesn't seem to have opened. " +
"Please disable your popup blocker for this page and try again.";
document.addEventListener('DOMContentLoaded', () => {
const anchors = document.querySelectorAll('a[target=_blank]');

// replace message in alert and add to main div of layout
$("div[role=main]").prepend(html.split("ALERT_MSG").join(msg));
}
anchors.forEach(anchor => {
anchor.addEventListener('click', (event) => { openLinkInJs(event); });
});
});
24 changes: 24 additions & 0 deletions apps/dashboard/app/javascript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,27 @@ export function bindFullPageSpinnerEvent() {
}
});
}

// open links in javascript and display an alert
export function openLinkInJs(event) {
event.preventDefault();
const href = event.target.href;

// do nothing if there's no href.
if(href == null){
return;
}

if(window.open(href) == null) {
// link was not opened in new window, so display error msg to user
const html = document.getElementById('js-alert-danger-template').innerHTML;
const msg = "This link is configured to open in a new window, but it doesn't seem to have opened. " +
"Please disable your popup blocker for this page and try again.";

// replace message in alert and add to main div of layout
const mainDiv = document.querySelectorAll('div[role="main"]')[0];
const alertDiv = document.createElement('div');
alertDiv.innerHTML = html.split("ALERT_MSG").join(msg);
mainDiv.prepend(alertDiv);
}
}
2 changes: 1 addition & 1 deletion apps/dashboard/app/views/dashboard/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%- content_for :head do -%>
<%= javascript_include_tag 'dashboard', nonce: true %>
<%= javascript_include_tag 'dashboard', nonce: true, type: 'module' %>
<%= javascript_include_tag 'pinned_apps', nonce: true %>
<%- end -%>
<%= render partial: 'shared/welcome' unless @user_configuration.disable_dashboard_welcome_message%>
Expand Down
Loading