Hi all,
I built an add-on in which there are some custom fields and each custom field has javascript blocks in template vm file. It is working as a charm in issue main view.
However if I search issues and click on the link to issue in issue navigator, it redirects me to a URL similar http://localhost/jira/browse/BAR-21?jql= and javascript does not work. When I debugged from developer console I realised that script block is not included to response html.
It works after refreshing the page or just go to http://localhost/jira/browse/BAR-21
Anyone has any idea on what to do?
Thanks in advance
Tuncay
Community moderators have prevented the ability to post new answers.
Hi Tuncay,
That's because when you click issue from the Issue navigator the page is not reloaded and issue details are loaded/shown through ajax and JavaScript.
So you need to make sure your scripts are loaded to the Issue Navigator screen. You can achieve this by adding appropriate context to the web-resource that declares your JavaScript:
<web-resource> ... <context>jira.navigator.advanced</context> <context>jira.navigator.simple</context> </web-resource>
============ Update ================
To catch issue switch event in Issue Navigator and fire a code when issue is switched:
1) declare web-resource that loads a script into navigator context:
<web-resource key="cust-field-resource" name="cust-field-resource"> <dependency>com.atlassian.auiplugin:ajs</dependency> <resource type="download" name="issue-nav.js" location="/js/issue-nav.js" /> <context>jira.navigator.advanced</context> <context>jira.navigator.simple</context> </web-resource
2) In the script file add a code to listen for issue change:
AJS.$(function() { JIRA.Issues.Application.on("issueEditor:loadComplete", function(model, props) { if (!this.standalone && !props.loadReason) { alert("Do stuff"); } }) });
This code works for JIRA 6.3.x and 6.4.x and does NOT work under JIRA 7.0.0. I did not test it under other JIRA versions. Please contribute if you find how to do the same under 7.0.0.
Hi Volodymyr, Thanks for your reply. I am aware that the page is loaded by AJAX, and already tried to add those contexts you mentioned. However this is custom field and I do not know how to add those contexts to custom field in atlassian-plugin.xml Any idea on this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you separate your JavaScript into web-resource? Posting your atlassian-plugin.xml may help me to understand the problem.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I appreciate your help. Assume I have custom field definition as below in atlassian-plugin.xml <customfield-type key="myFieldKey" name="My Custom Field" class="com.mycompany.MyCFType"> <resource type="velocity" name="view" location="templates/view.vm" /> <resource type="velocity" name="column-view" location="templates/list.vm" /> </customfield-type> In view.vm I am using javascript. What I understand from your comment is to - define <web-resource> - convert javascript block in vml file to a js file and add it to <web-resource> - in vml file load the web-resource
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do you mean that you have script block inside of your view.vm like: <script> // some script here </script> and it's not returned with html when you click the link from Issue View?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, but not from issue view, from issue navigator
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That's strange. Please post snippet of the view.vm.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
#* @vtlvariable name="remoteUser" type="com.atlassian.crowd.embedded.api.User" *# #* @vtlvariable name="customField" type="com.atlassian.jira.issue.fields.CustomField" *# #* @vtlvariable name="labelUtil" type="com.atlassian.jira.issue.label.LabelUtil" *# <script type="text/javascript"> //script comment (function($) { some code AJS.$(document).ready(function () { .... }); </script> <some html elements here> I double checked, when I click on issue, and inspect source I can not find my script block. Thanks again
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you try to move it below? If it does not help it's worth to try loading the web-resource in vm file.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Volodymyr, Thanks for your help, and sorry I could not notice your answer. Here is what I did in the meantime I put all script block to a new js file. Assume I have my-script.js file. I added below lines to atlassian-plugin.xml <web-resource key="my-script-resource" name="My Plugin Web Resources"> <dependency>com.atlassian.auiplugin:ajs</dependency> <resource type="download" name="my-script.js" location="/assets/js/my-script.js"/> <context>jira.navigator.advanced</context> <context>jira.navigator.simple</context> </web-resource> Last, my vm file now looks like this #* @vtlvariable name="remoteUser" type="com.atlassian.crowd.embedded.api.User" *# #* @vtlvariable name="customField" type="com.atlassian.jira.issue.fields.CustomField" *# #* @vtlvariable name="labelUtil" type="com.atlassian.jira.issue.label.LabelUtil" *# #if ($value) <some-html-here /> #end When I open issue navigator (both detailed and list view) javascript does not run. If I refresh the page it is OK. I also tried adding $webResourceManager.requireResource("com.myplugin:my-script-resource") to the vm file, but no success.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It might sound silly but you can try add some invisible 'img' in your template file with attribute 'onload' containing your script. It works for me but I'm aware that it shouldn't be the way to do that. I use something like that: <img src="${baseUrl}/images/icons/bullet_notdone.gif" onload=" // your script " style="display: none;" width="1" height="1">
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Mateusz for the brilliant idea. However I hate writing scripts between quotes/double quotes. You always have to care about quotes because you're already in quotes. But this technique is good for small scripts.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Tuncay, I did a test for your case by adding "Before<script>alert(1)</script>After" to view.vm of my custom field. The alert box is shown in all cases: for Issue view screen and when changing issues though Issue Navigator. Although only "Before After" is shown in source for Issue Navigator case and that makes sense: When switching through Issue navigator the content of the issue is retrieved through Ajax and attached to the page and in this case inline JavaScript is not executed. I guess that the inline JavaScript is retrieved separately and attached to the page context. Moreover when switching through Issue Navigator I get 3 alerts and I did not find how to make to be executable only once. Adding $webResourceManager.requireResource("com.myplugin:my-script-resource") does not work for the Issue Navigator as well as defining the <context>.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi again Volodymyr and I really appreciate your help. You're right, script is being loaded but not seen in HTML content, anyway it is loaded. But I really have strange case, when I click on any issue on issue navigator, script runs twice (not 3), and what I expect from the script does not work, then I click on other issue again script runs twice. The interesting part is, when I click back to first issue script runs three times and result is OK. All issues that I click at least twice work OK. I prepared a screencast. In the screencast, I am expecting to see an inline dialog after clicking the inventory field (what the script does, I can share if you want). Here is the video, please count the alerts each time I click on issue and see if inline dialog appears. http://www.screencast.com/t/gRVAIqaVp
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Tuncay Senturk [Snapbytes], I updated the answer.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Volodymyr, Really really appreciated. As you mentioned, it does not work with JIRA 7, but works in JIRA 6.x I will work on it and keep here updated if I find anything. Thanks so much again.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Tuncay! Hint for JIRA 7.0.0: when you are on Issue Navigator page do global script search for "selectIssue". Looks like you need to define backbone controller and listen for this event. I do not have backbone experience so have not proceeded with it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi everyone, I'm having the same problem in JIRA 7.X. Did anyone manage to solve this problem? Thank you very much! Greetings.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.