Custom Math Plugin

Hey! I am probably just doing something goofy here, but I can’t seem to get custom plugins to work for math operations.

(I know I can just make it happen in JS, but I’m trying to learn how to take advantage of the builder’s features because, as you know since you built the thing, it would make it a lot more convenient :slight_smile: .)

I’m trying to build “Next” and “Previous” buttons. I’ve set it up so that my url has both a routing variable and a query parameter for the week number I want to fetch (e.g. if I click on the “4” button, it gets me values filtered to only include week 4, the end of the url for which is ‘/4?week=4’).

I tried making a plugin that seems to work in preview, but not in execution (it comes up as ‘null’ when I try to click the “Next” button).

The plugin takes one parameter (‘add’), which I’m intending to be the number I want to add to the base ‘value’.

Here’s the plugin code:

mdFunctions.OnReady(() =>
{
    mdFunctions.ItemAdd({
        id: 'add',
        parameters: {
            add: {type: 'INPUT'},
        },
        description: 'Performs addition.',
        group: 'String',
        function: function(data, value, add)
        {
            if(typeof value !== 'String')
            {
                return value;
            }

            const num = parseFloat(value);
            const mathNum = parseFloat(add);
  
            const newNum = num + mathNum;
            const stringNum = newNum.toString();
            
            return stringNum;
        }
    })
});

I’m setting up my routing variable and my parameter as such:

$query->get('week')->add('1')

What am I doing wrong?

Also, if you’re willing to share, it would be helpful to know where I can use JS and jquery within the builder (for example, unless I’m mistaken, it seems like the attributes allow for this, but I’m not sure about the routing variables, query parameters, etc).

Thank you in advance!

This is interesting :slight_smile:

Let’s start simple, first you need to create plugin inside Creator app (https://hessler.divhunt.art/creator/) - “Marketplace Plugin”

Then you need to create javascript file, just name it let’s say add.js, then paste the code you provided.

That’s it, it should work.

@chelce

As you can see, I already did it for you, only mistake you made is the following:

if(typeof value !== 'String')
{
    return value;
}

I think ‘String’ should be lowercase ‘string’

@dejan

Awesome, thank you so much! The custom plugins make interaction between JS and the builder a lot cleaner and faster. You guys are the best!

1 Like

@chelce

I’ll implement some math native functions soon, probably single function that can do most of math operations.