Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,456,260
Community Members
 
Community Events
176
Community Groups

random uuid in user maco

I want to write a Confluence user macro, which should get a unique uuid. Currently I fail to randomly generate this UUID.
The format should look like "#c199f52d-9724-4911-ad47-479e937d3b88".
I guess I can solve this, if someone shows me how I can access the java function Math.random(); from my user macro.

2 answers

1 accepted

0 votes
Answer accepted

This is how I do it. I was never able to find a good way to generate a random number or find a way to get at Math.random() but this seems to work well.

#set( $id = $action.dateFormatter.calendar.timeInMillis )

Even when the user macro is used multiple times per page I find that I get different mills. So for velocity I reference it this way ...

<div id="mydiv-$id"></div>

 and then I can target it with JavaScript this way.

<script>
var id = '$id';

ASJ.toInit(function(){
var stuff = AJS.$('#' + id).html();
});
</script>

Thanks for your help. I will test it on occasion. For now my hash solution (see above) works fine for me.

0 votes
Bill Bailey Community Leader Mar 23, 2021

For my user macros, I don't worry about that. Confluence will generate what it needs. Even if I am wrapping an existing macro, I delete that reference. It works fine.

I want to modify the rendered results via JavaScript so I need a html object id for reference the right object on the page if a user used the macro multiple times on the same page.

Bill Bailey Community Leader Mar 23, 2021

Hmm, seems a bit hard. You could create your own ID, but you would then need to increment it each time, which complicated. And seems like a brittle solution to rely on javascript to modify an object based on ID that could change.

Is there a way to do it within the user macro so that it is dynamic?

Maybe sharing a bit more detail on what you are trying to do would help?

I have now solved this by forming a hash over all input parameters. This should be sufficient for my purposes.

@René Kray could you explain or paste your macro solution to help me get started?

I'm still looking for a solution to generate a unique ID on a page

Hi Cyrus:

If I remember correctly, I was looking for a solution for the following macro.

In the end, I used id="$paramtitle" as "unique" id. For me it was unique enough, but for a general purpose, I would suggest finding a different solution.

Regards, René

 

## Macro title: roadmap_panel
## Macro has a body: Y
## Body processing: Selected body processing option
## Output: Selected output option
##
## Developed by: René Kray
## Date created: 2021-03-23
## Installed by: René Kray
## @param title:title=Title|type=string|required=true|default=Title for this panel
## @param o1:title=O1|type=boolean|required=true|default=false
## @param o1ext:title=O1 Ext|type=string|required=false|default=
## @param o2:title=O2|type=boolean|required=true|default=false
## @param o2ext:title=O2 Ext|type=string|required=false|default=
## @param o3:title=O3|type=boolean|required=true|default=false
## @param o3ext:title=O3 Ext|type=string|required=false|default=
## @param health:title=Health|type=boolean|required=true|default=false

<style>
div.roadmap_panel{
border: solid grey 1px;
border-radius: 5px;
background-color: #ddd;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 3px 5px 0 rgba(0, 0, 0, 0.19);
}
div.roadmap_panel div.roadmap_panel_title{
padding: 5px 5px 5px 10px;
border-radius: 5px;
font-weight: bold;
}
div.roadmap_panel div.roadmap_panel_title span.button{
padding: 2px 5px 2px 5px;
float: right;
}
div.roadmap_panel div.roadmap_panel_title span.status{
padding: 2px 5px 2px 5px;
border-radius: 3px;
border: solid black 1px;
color: white;
font-size: 80%;
float: right;
margin-right: 5px;
}
div.roadmap_panel div.roadmap_panel_body{
background-color: white;
padding: 5px;
border-radius: 0 0 5px 5px;
border-top: solid #999 1px;
display: none;
}
</style>
<div class="roadmap_panel" id="$paramtitle">
<div onclick="roadmap_panel_show_hide('$paramtitle')" class="roadmap_panel_title">
<span class="button">+</span>
#if ($paramhealth==true)
<span class="status" style="background-color: green;">Health</span>
#end
#if ($paramo3==true)
<span class="status" style="background-color: red;">
O3
#if ( "$paramo3ext" != "" )
- $paramo3ext
#end
</span>
#end
#if ($paramo2==true)
<span class="status" style="background-color: red;">
O2
#if ( "$paramo2ext" != "" )
- $paramo2ext
#end
</span>
#end
#if ($paramo1==true)
<span class="status" style="background-color: red;">
O1
#if ( "$paramo1ext" != "" )
- $paramo1ext
#end
</span>
#end
<p style="margin:0">$paramtitle</p>
</div>
<div class="roadmap_panel_body">
$body
</div>
</div>
<script>
function roadmap_panel_show_hide(id) {
panel=document.getElementById(id);
button=panel.getElementsByTagName("SPAN")[0];
body=panel.getElementsByTagName("DIV")[1];
if(body.style.getPropertyValue('display')=="block"){
body.style.setProperty('display','none');
button.innerHTML="+";
}else{
body.style.setProperty('display','block');
button.innerHTML="-";
}
}
</script>

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events