Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FieldImage opt_onClick initialisation when the block is defined in json #2132

Closed
retd-jerome opened this issue Nov 21, 2018 · 3 comments
Closed

Comments

@retd-jerome
Copy link

retd-jerome commented Nov 21, 2018

Problem statement

Missing APIs in the JSON definitions.

There is no possibility to define a FieldImage opt_onClick via JSON (cf https://github.com/google/blockly/blob/master/core/field_image.js#L73)

More, as the FieldImage has no name attribute, you can't call "this.getField(name)" in an extension to define it.

Expected Behavior

Possibility to define the opt_onclick via JSON like

Blockly.Blocks['test_json'] = {
   init: function() {
     this.jsonInit({
        "type": "test_json",
        "message0": "json %1",
        "args0": [
           {
              "type": "field_image",
              "src": "https://www.gstatic.com/codesite/ph/images/star_on.gif",
              "width": 15,
              "height": 15,
              "alt": "*",
              **"onclick": "myFunction"**
           }
       ],
       "colour": 230,
       "tooltip": "",
       "helpUrl": ""
     });
   }
};

Actual Behavior

You can only define it via javascript

Blockly.Blocks['test_js'] = {
    init: function () {
        this.appendDummyInput()
            .appendField("js")
            .appendField(new Blockly.FieldImage("https://www.gstatic.com/codesite/ph/images/star_on.gif", 15, 15, "*", myFunction));
        this.setColour(230);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

function myFunction(e) {
    alert("Clicked");
    return 0;
};

Additional Information

I post this issue because of my initial message on the Blockly Google group (cf https://groups.google.com/forum/#!topic/blockly/rzcpN8iIrRU)

Thanks,
Best regards,
Jerome.

@RoboErikG
Copy link
Contributor

Note that for the click callback since we don't know where the callback is defined when we load the JSON it'll need to be added somehow.

I'm not sure if this should require an extension to add the onclick function or use a registration method like registerButtonCallback() in the toolbox.

Adding the name attribute to the field_image should be a lot more straightforward and can be done separately in a small PR.

@RoboErikG
Copy link
Contributor

Oops, Andrew pointed out to me the 'name' field gets used in block.js, not in the field, so adding a name should already work.

@retd-jerome
Copy link
Author

Hi,

It fix my issue. I didn't realize I could give a name to the FieldImage type.
Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants