Can i add labels with the Javascript API for blueprint wizards?

Hi,

is it possible to add labels by the Javascript API for blueprint wizards? Exists a variable for the label identifier?

E.g. in the $container or wizard data properties, like this to edit the page title:

state.pageData.title = titleString;

Link to the Atlassian Docs: https://developer.atlassian.com/display/CONFDEV/Javascript+API+for+blueprint+wizards

Thank you

Regards

Florian

4 answers

1 accepted

4 votes
Accepted answer

The above answer by Ruth can be made even more easy by changing the title and name attribute to name="labelsString" title="labelsString", this will enable a REST call within confluence to add the labels programatically like magic! No need to write any java code if you do this small change - give it a try.

Yagnesh Bhat, thanks, good tip! I removed the LabelManager code, and with name="labelsString" it works indeed. Ironically, I refactored the labelsString variable name for readbility, and that broke it. "title" seems to be another variable that we should not refactor.

But title="labelsString" is not needed, it only sets the tooltip in the wizard.

Atlassians, is this documented somewhere? My team keeps bumping into easy solutions like this by chance (for macros, for plugins, ...), after we spent a week trying to reinvent the wheel.

Try the following for a LabelPicker.


In atlassian-plugin.xml add:

<web-resource>
    <dependency>com.atlassian.confluence.plugins.confluence-ui-components:label-picker</dependency>
</web-resource>

In your soy file, add:

<div class="field-group">
  <label>
    {getText('my.company.myplugin.wizard.page.articlelabels.label')}
  </label>
  <input id="my_article-labels" class="text select2-input long-field" 
      name="articleLabels" title="Search labels" type="text"
      placeholder="{getText('my.com.myplugin.wizard.page.articlelabels.placeholder')}">
</div>

In your dialog-wizard.js add:

function postRender(e, state) {
  var wizardForm = state.$container;
  var labelsField = $("#my_article-labels", wizardForm);
  labelsField.auiSelect2(Confluence.UI.Components.LabelPicker.build({
      separator: " ",
      queryOpts: {
          spaceKey: state.wizardData.spaceKey
      }
  }));}

In BlueprintCreatedListener(LabelManager labelManager...)'s onPageCreateEvent() method, add:

Set<String> labelsList = new HashSet<String>();
labelsList.addAll(Arrays.asList((((String) context.get("articleLabels"))).split(",")));
for (String label : labelsList) {
    Label labelToAdd = new Label(label);
    labelManager.addLabel((Labelable) blueprintPage, labelToAdd);
}

The name="articleLabels" is your context variable that stores the labels.
The id="my_article-labels" is what connects the SOY input field to the JavaScript ui-component.

I'm also looking for a solution to do this within my custom blueprint (as Atlassian have done with their Troubleshooting Article blueprint). Can I aks did you find a solution - is there any snippets you can share?

Need this too any suggestions

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Thursday in Agile

How Scrum works? It starts with training and education

To answer “How scrum works,” most of the teams I've worked with first addressed the question: “where to start?”  That question applies to both implementation and improvements on the Scrum framew...

211 views 3 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you