Ever wondered how the nice little JIRA notification messages are created? The ones you get when an issue is created in JIRA? Doing this is so easy that you will want to try it out in every new plugin that you write! All you need is to inject the following javascript snippet into the page: JIRA.Messages.showSuccessMsg('Your message goes here'); Let me show you a simple demonstration of this. I am going to put this into the announcement banner so that I don't have to create a plugin to inject this javascript ;) Let us say we need to show this message when a user visits the profile. All you need to do is to add this snippet in the Announcement banner. <script > var pathname = window.location.pathname; if (pathname.indexOf("ViewProfile.jspa") >= 0) { JIRA.Messages.showSuccessMsg('You are viewing your profile, Success'); } </script> And the message will appear on the screen when you visit the profile. But, did you notice that the message didn't have a close button as you normally see in the Create notification? This is where you can pass properties to the function. <script > var pathname = window.location.pathname; if (pathname.indexOf("ViewProfile.jspa") >= 0) { JIRA.Messages.showSuccessMsg('You are viewing your profile', {closeable: true}); } </script> Here we passed the closeable property to the same function and the message will now have a close option. Now, you can replace the showSuccessMsg function with showErrorMsg or showWarningMsg to get the appropriate message format. In all the 3 cases, the message disappears itself after the default timeout of 10 seconds. If you need a bigger timeout, use the timeout property.
<script > var pathname = window.location.pathname; if (pathname.indexOf("ViewProfile.jspa") >= 0) { JIRA.Messages.showSuccessMsg('You are viewing your profile', {closeable: true, timeout: 60}); } </script> Are there any other properties? The only other property is type and you can use it when you use the generic function showMsg instead of showSuccessMsg, showErrorMsg or showWarningMsg. The possible type values are SUCCESS, ERROR or WARNING. For example: JIRA.Messages.showSuccessMsg('You are viewing your profile', {closeable: true, timeout: 60}); is same as JIRA.Messages.showMsg('You are viewing your profile', {type: SUCCESS, closeable: true, timeout: 60}); For more details on how these functions work and for more more useful functions like showReloadSuccessMsg, take a look at includes/jira/common/messages.js file. Hope you find it useful. PS: If you find these little tutorials useful, you might also like the JIRA Development Cookbook.
12 Comments
Joel Holmberg
7/2/2014 01:52:41 am
"so that I don't have to create a plugin to inject this javascript ;)"
Reply
J-Tricks
7/3/2014 03:46:24 am
Sorry you felt that way. I always try to keep it really simple and address one specific issue at a time. Most plugin developers already know how to inject javascript into a plugin because it is a module in itself. Just google and you will find out ;)
Reply
Dennis
1/27/2015 10:46:14 pm
HI, is it also possible to trigger a banner by workflow transition ? I'de like to setup a reminder for people like "Don't forget to log your time" when switching from in Progress to Resolved and from Test to Closed.
Reply
J-Tricks
1/28/2015 01:38:20 am
You can, by adding some javascripts on the screen (via field descriptions or something).
Reply
Srini Chimata
2/11/2015 04:34:32 pm
Starting from Jira v5.2.5, the Toolkit posed a problem and we had a tough time with all custom velocity (edit) fields as these fields were not present in the next version Toolkit plugin.. they thought that the velocity (edit) fields were causing a security threat to Jira.
Kim
6/10/2015 07:59:58 pm
Hi,
Reply
J-Tricks
6/12/2015 03:39:39 am
You can't load the web resource in a listener because listener is a separate thread. How is the javascript invoked? From which action?
Reply
Prakash
12/9/2016 11:33:14 pm
Is you issue addressed. I am working on the same scenario with event listener. How to post a message after the event handling succeeded or failed
Reply
Tom
6/15/2015 05:27:15 am
I would like to show a system maintenance warning message on a "User login" event. Is this possible or can I just invoke these messages on certain pages in Jira?
Reply
J-Tricks
6/15/2015 03:31:59 pm
I'm sure you know about the announcement banner. Have you tried adding a javascript in there? You will have to mark the message as read and handle that in the session.
Reply
Tom
6/17/2015 05:02:03 am
Sure, I also placed your example scripts in the announcement banner and they work fine (show message on user profile page). What I don't want to do is to place a maintenance announcement there.
zac
3/8/2016 01:12:18 am
Thanks, its indeed a great tutorial for a beginner. Couldn't find any help on the net for few hours.
Reply
Your comment will be posted after it is approved.
Leave a Reply. |
AuthorJobin Kuruvilla - Works in Adaptavist as Head of DevOps Professional Services. Categories
All
Archives
October 2016
|