Conditional loading of resources

Hello, I am working on a plugin that displays a panel for a specific project.

This panel and these resources (Javascript) should only apply in this project.

For the panel I have no problem, using a display condition it works.

On the other hand javascript should only be applied in this context, because it hides certain elements of the view of the request.

I tried to add a context for my resources

<web-resource key="dcp-view-resources" name="dcp-view-screen">
        <dependency>com.atlassian.auiplugin:ajs</dependency>
        <resource type="download" name="dcp.js" location="/js/dcp.js"/>
        <context>jira.view.issue</context>
        <context>jira.navigator.advanced</context>
        <context>jira.navigator.simple</context>
        <context>dcp.view.context</context>
    </web-resource>

and load them into my veloticy template with

$webResourceManager.requireResourcesForContext ("dcp.view.context")

But it does not work

Moreover if I do not add

<context> jira.view.issue </context>
<context> jira.navigator.advanced </context>
<context> jira.navigator.simple </context>

for my resources, the resources are not displayed on all possible cases of access to a request (search, queue, direct access)

I tried to add a condition tag in my resources and to make my java inherit from “Condition” but in the “shouldDisplay ()” the context is empty.

public boolean shouldDisplay(Map<String, Object> map) {
       LOG.warn(map) // empty
        return false;
    }

Currently I have found a workaround via javascript by getting the project key from the url and conditioning my javascript on the project key, but it is not maintainable.

So my question is:
Is it possible to condition the loading of resources on a project?

Thanks for your help

Just do not define any <context> for your resource, then require it in the Velocity using:

$webResourceManager.requireResource("[app_key]:dcp-view-resources")

You resource should be:

<web-resource key="dcp-view-resources" name="dcp-view-screen">
  <dependency>com.atlassian.auiplugin:ajs</dependency>
  <resource type="download" name="dcp.js" location="/js/dcp.js"/>
</web-resource>
2 Likes

Hi, thanks for your reply, it almost works.

it works from direct access to a request, but the resources do not load from access from queues or research for example

For example from this URL scheme the display is correct
https: //BASE_URL/browse/CHG-78

but from searches or queues the resources do not load

https: //BASE_URL/browse/CHG-78?jql=project%20%3D%20%Gestion%20des%20changements%22%20

https: //BASE_URL/projects/CHG/queues/custom/600/CHG-78

the only solution for the resources to load is to add the context

<context> jira.view.issue </context>
<context> jira.navigator.advanced </context>
 <context> jira.navigator.simple </context>

But the resources apply to all the projects; (

I did not try with all possible solutions yet.

But if the problem happens as you describe, I think the best solution is that you do the condition checking within your javascript code, and only hide elements if those conditions are passed.

This is the simplest way as well.

const projectKey = JIRA.API.Projects.getCurrentProjectKey();
// const projectId = JIRA.API.Projects.getCurrentProjectId()
// const issueKey = JIRA.API.IssueSearch.getActiveIssueKey()
// const issueKey = JIRA.Issue.getIssueKey()

if (projectKey === "CHG") {
  // Hide elements
}

Thank you, yes I will stay on the solution of checking in javascript so as not to impact the other projects.

This is not the best solution because suddenly the project key is hard-stored in JavaScript which makes it difficult to maintain without having to modify the plugin if I have to extend it to another project.

But that will allow me to offer a first functional version while waiting to find a more maintainable solution;)

thanks a lot for your help

1 Like