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

How to restrict inserting more than one macro in a confluence page.

Andrian F April 30, 2015

Hi everyone,

I am working on a confluence plugin which has two macros. I only need users to have one macro per page (either macro 1 or macro 2. Not both or not 2 from one macro). If users add the second macro i need to display an AUI error message and restrict user from saving the page with two macros.

Thank you

Andrian

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Yagnesh Bhat
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 30, 2015

One approach to try would be this : if the macro is rendered from a vm, then give the div containing the vm a unique id. That way if two macros are rendered, you can replace it with a simple message as shown in the below html:

 

<div id = "id1"> Hey </div>
<div id = "id1"> Hey 2 </div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
var x = $('div[id=id1]').length;
console.log("X is " + x);
if (x > 1) {
	$("html").html("Page has two divs");
}
</script>
Andrian F April 30, 2015

Thanks Yagnesh. Will try this

3 votes
Andrian F April 30, 2015

Hi guys,

I was able to come up with the below code and it worked.

jQuery(document).bind('DOMSubtreeModified', function(event) {  
      var noOfMacro1=jQuery('#wysiwyg iframe').contents().find('table[data-macro-name="macro1"]').length;
      var noOfMacro2=jQuery('#wysiwyg iframe').contents().find('table[data-macro-name="macro2"]').length;
      var totalMacros=noOfMacro1+noOfMacro2;
      if(totalMacros>1){
            jQuery('#moreThanOneMacroError').removeClass('hidden');
            var message= '<div id="moreThanOneMacroError" class="aui-message aui-message-error closeable"><p class="title"><strong>Error!</strong></p><p>Only one macro1 or macro2 macro is allowed per page.</p></div>';
            if(jQuery('#moreThanOneMacroError').length<1){
                jQuery('#editor-messages').append(message);
            }
      }else{
            jQuery('#moreThanOneMacroError').addClass('hidden');
      }     
});
Steven F Behnke
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 30, 2015

Thank you for sharing Andrian. This is really interesting, and is a much better solution than hiding the Save/Preview buttons (users would get confused, probably think the system is borked)

Andrian F April 30, 2015

Yep it is.. Your welcome

0 votes
Andrian F April 30, 2015

Thank you for the quick reply Steven..i agree with you. I also thought about disabling the Save and Preview button as long as there's more than one macro. Then i hope i only have to worry about the front end..Was not sure whether this will be a good approach...any thoughts ?

0 votes
Steven F Behnke
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 30, 2015

My initial thought is that the most simple way would be to validate this when they submit the page for Save or Preview, and reject it if the conditions aren't met. Adding a javascript check to the layer would be an improvement, but you should still validate it server-side to ensure that your conditions are met.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events