165
Views
8
Comments
Onclick event on a button to trigger a variable within a javascript

Hi guys, I am new to coding,
I need to understand how to use the onclick feature of a button within a javascript to change the value of a varibable within the JS.

2018-10-18 08-34-00
Eric Bulters
 
MVP

Hi Faizan,

Could you please elaborate a bit more. What are you trying to achieve, what is the context of this code?
Are you referring to the onclick event from OutSystems or are you defining your own onclick inside the javascript?
What variable inside your JS do you want to change? Is it related to an OutSystems parameter or only used within that javascript?

UserImage.jpg
Faizan Mohammed

I have added a button using HTML tag and now I want to change a variable inside of a JS within OnReady action. I understand that the ID for this is needed, so I made a new ID and then referenced the id of the element into the JS but I don't understand how to proceed further @Eric Bulters 

2018-10-18 08-34-00
Eric Bulters
 
MVP

is there a specific reason you are making this button with an html tag rather than using an OutSystems button?

About your JS
your document.getElementById() currently does nothing.
It is not getting anything it should have the Id name between the brackets
and it should be assigned to a variable

var x = document.getElementById('idname')

To set a global js variable I would declare a variable on the page's js 

and then in your onclick change the value.


You could also use the OutSystems parameters in your JS and set them from your JS instead.


What are you trying to achieve with your onclick and javascript? what is the goal of the button? Maybe there is a better solution

UserImage.jpg
Faizan Mohammed

I am trying to lure in the functionality of fabric JS as mark-up or highlighting an image, I didn't know that outsystems button could be used in a JS. i need to trigger the .click() of this button in JS to change the mode of canvasIs this what you meant ?

2018-10-18 08-34-00
Eric Bulters
 
MVP

I might still be misunderstanding what you need. But why don't you just use a normal button:

Have a CurrentMode variable on your screen

On the onClick of your button set CurrentMode to the correct value and use the variable in your javascript:


if your function is on a script where you can't reach the onscreen parameters you can define the variable you need in the page's javascript:

and set it in the onbuttonclick action

UserImage.jpg
Faizan Mohammed

I'll share you my code it would make it easy to understand, since I am not good at it is better you have a look at it to understand.

var Id = document.getElementById($parameters.CanvasId);

//var canvas = new fabric.Canvas(Id, {

//    width: 272,

//    height: 272

//});

//canvas.renderAll();

//fabric.Image.fromURL('data:image/gif;base64,'+$parameters.image, function (img) {

//    canvas.backgroundImage = img;

//    canvas.renderAll();

//});


var initCanvas = function initCanvas(id) {

    return new fabric.Canvas(id, {

        width: 272,

        height: 272,

        selection: false

    });

};


var setBackground = function setBackground(url, canvas) {

    fabric.Image.fromURL(url, function (img) 

    {

    canvas.backgroundImage = img;

    canvas.renderAll();

    });

};


var canvas = initCanvas(Id);

setBackground('data:image/gif;base64,'+$parameters.image,canvas);


var mousePressed = false;


var currentMode = void 0;


var tPan = document.getElementById($parameters.PanId).click(function(){

    togglePan();

});

var togglePan = function togglePan() {

  if (currentMode === modes.pan) {

    currentMode = '';

  } else {

    currentMode = modes.pan;

  }

};


var modes = {

  pan: 'pan'

};


canvas.on('mouse:move', function (event) {

  if (mousePressed && currentMode === modes.pan) {

    canvas.setCursor('grab');

    canvas.renderAll();

    var mEvent = event.e;

    var Delta = new fabric.Point(mEvent.movementX, mEvent.movementY);

    canvas.relativePan(Delta);

  }

});


canvas.on('mouse:down', function (event) {

    mousePressed = true;

    if (currentMode === modes.pan){

    canvas.setCursor('grab');

    canvas.renderAll();}

});


canvas.on('mouse:up', function (event) {

  mousePressed = false;

  canvas.setCursor('default');

  canvas.renderAll();

});

This is the code within OnReady action. In the canvas.

2020-09-01 10-42-42
Stefano Valente

I think you are building a widget in javascript, using canvas object.


It would be great if you would think of it as a component/widget and think what kind of interaction you want with the outside world. think of settings, properties and events.

Those properties you should link to Outsystem objects when creating an instance of your widget.


your code could look like this:

function onLoadMyObject(ContainerId, DisplayFileName, SelectedFile, contentType, MySaveFunction, MyCancelFunction) {   

    var mydiv = document.getElementById( ContainerId);

    MyObject.build(

            mydiv, {

                width: anonimyzerWidth,

                height: anonimyzerHeight,

                file: MyFile,

                onSave: function(files) {return uploadFile(files, DisplayFileName, MySaveFunction);},

                onCancel: function() {return MyCancelFunction()},

                onError: function(message) {window.alert("Oops :" + message)},

                watermarkLabel: "Watermarklabel",

            }

    );

    console.log('MyObject initialised');

}


And calling it:

onLoadMyObject($parameters.ContainerId, $parameters.DisplayFileName, $parameters.SelectedFile, $parameters.ContentType, $actions.OnSave, $actions.OnCancel);


So you can assign your client actions into your js code.


This is just a sample to show how to assign object, variables and even functions to properties and events.


Think of your widget as an object. what you want it to do, and then build it accordingly. This way you can reuse it more easily.

UserImage.jpg
Faizan Mohammed

Seems okay but I don't understand the coding part can you give me a better explanation?

 

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.