Hello,
I am looking for a way to get the number of labels from the current page that I have open in the editor.
This script runs when the editor is loaded:
// load editor first
AJS.bind("init.rte", function(){
var buttonID = "rte-button-publish";
//check if publish button exist
if(AJS.$("#" + buttonID).length > 0) {
//onClick
AJS.$("#" + buttonID).click(function (e) {
console.log("Clicked 'Publish' page button", this);
var labelbutton = "rte-button-labels";
// HOW DO I GET THE LABEL COUNT
if(AJS.$("#" + labelbutton).find(".trigger-text").text() == "Labels")
{
//show alert
alert("Please add at least one or more labels!");
console.log("Show Alert", this);
//Dont finish Action
e.preventDefault();
console.log("Default prevented", this);
e.stopPropagation();
console.log("Stoped Propagation", this);
}
});
}
});
Basically only the last the if statement in the code above doesn't work for me.
I need something like:
if (LABELCOUNT == 0) {DO THIS;}
I am thankful for every help!
Hey friend! :D
If you're trying to get the labels for a page, it may be easier for you to just make an AJAX call to the confluence REST API. Specifically, there's an API for labels, that will give you the label data for whatever page you specify.
Here's a quick and dirty example of how this could work:
let pageId = (new URL(document.location)).searchParams.get("pageId")
AJS.$.ajax({
url: AJS.contextPath() + "/rest/api/content/" + pageId + "/label",
success: function(result){
console.log(result.size); //result.size is the number of labels
}});
Obviously you'd want to add all of your logic around that and clean it up, but I believe that will get you what you need.
Let me know if you have any questions or if you'd like me to clarify anything further!
Best,
Aidan
Hey Aidan,
that works well if you want to edit an existing page.
Is there a way to get the number of labels from an unpublished site?
The problem is, when I create a new page, the page in the editor doesn't have a PageID yet.
That means, getting the amount with the REST API does not work
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok nevermind, I can use DraftId instead of pageID :D
Nevertheless, thank you very much for your help!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.