javajquerymacrosconfluenceatlassian-plugin-sdk

Atlassian Confluence: Macro type plugin that will display a popup window when the page is accessed


Pretty basic idea, i want to show a popup window when some one access the page. I have written a sample code. Actually what i want to do is create a Macro-type plugin which has a body so we could add text, links and different elements into it, and it would display them in a popup window.

Here is the code i have been able to display the popup window when page is accessed but when i choose 'PLAIN_TEXT' as 'getBodyType()'. It shows unformatted text, and when i choose 'RICH_TEXT' it shows nothing. please help!

package com.elixir;

import com.atlassian.confluence.content.render.xhtml.ConversionContext;
import com.atlassian.confluence.content.render.xhtml.XhtmlException;
import com.atlassian.confluence.macro.Macro;
import com.atlassian.confluence.macro.MacroExecutionException;
import com.atlassian.confluence.xhtml.api.MacroDefinition;
import com.atlassian.confluence.xhtml.api.MacroDefinitionHandler;
import com.atlassian.confluence.xhtml.api.XhtmlContent;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MyMacro implements Macro
{
    private final XhtmlContent xhtmlUtils;

    public MyMacro(XhtmlContent xhtmlUtils)
    {
        this.xhtmlUtils = xhtmlUtils;
    }

    @Override
    public BodyType getBodyType()
    {
        return BodyType.RICH_TEXT;
    }

    @Override
    public OutputType getOutputType()
    {
        return OutputType.BLOCK;
    }

    @Override
    public String execute(Map<String, String> parameters, String bodyContent, ConversionContext conversionContext) throws MacroExecutionException
    {
        String body = conversionContext.getEntity().getBodyAsString();

        final List<MacroDefinition> macros = new ArrayList<MacroDefinition>();

        try
        {
            xhtmlUtils.handleMacroDefinitions(body, conversionContext, new MacroDefinitionHandler()
            {
                @Override
                public void handle(MacroDefinition macroDefinition)
                {
                    macros.add(macroDefinition);
                }
            });
        }
        catch (XhtmlException e)
        {
            throw new MacroExecutionException(e);
        }

        StringBuilder builder = new StringBuilder();
        if (!macros.isEmpty())
        {
            for (MacroDefinition defn : macros) {
                builder.append("<script>AJS.$(document).ready(function() {var popup2 = AJS.popup({width:400, height:200, id:'my-popup2', closeOnOutsideClick: true});");
                builder.append("$('#my-popup2').css({padding:4});$('#my-popup2').html(\"").append(defn.getBody()).append("\");");
                builder.append("popup2.show();});</script>");

            }
        }
        else
        {
            builder.append("Body not defined");
        }

        return builder.toString();
    }
}

Solution

  • I think you need hasBody='true', bodyType='raw' or bodyType='rendered', and outputType='html' or outputType='wiki'.

    Here is an example of how to implement this solution: https://answers.atlassian.com/questions/93630/user-macro-module-body-parameter