Skip to content

Commit

Permalink
Preparation and adjustments for upcoming features (eg action button)
Browse files Browse the repository at this point in the history
THIS COMMIT BREAKS BLOCKLY TILL NEXT RELEASE
  • Loading branch information
sestriel committed Apr 18, 2024
1 parent 3a72ff6 commit c29e830
Show file tree
Hide file tree
Showing 3 changed files with 271 additions and 116 deletions.
242 changes: 208 additions & 34 deletions admin/blockly.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
'use strict';

if (typeof goog !== 'undefined') {
goog.provide('Blockly.JavaScript.Sendto');
goog.provide('Blockly.JavaScript.Ntfy');

Check failure on line 4 in admin/blockly.js

View workflow job for this annotation

GitHub Actions / check-and-lint

'goog' is not defined
goog.require('Blockly.JavaScript');

Check failure on line 5 in admin/blockly.js

View workflow job for this annotation

GitHub Actions / check-and-lint

'goog' is not defined
}

Blockly.Words['ntfy'] = {'en': 'ntfy', 'de': 'ntfy'};
Blockly.Words['ntfy_instance'] = {'en': 'Ntfy instance', 'de': 'Ntfy Instanz'};




Blockly.Words['Ntfy'] = {'en': 'Ntfy', 'de': 'Ntfy'};

Check failure on line 12 in admin/blockly.js

View workflow job for this annotation

GitHub Actions / check-and-lint

'Blockly' is not defined

Blockly.Words['ntfy_instance'] = {'en': 'Instance', 'de': 'Instanz'};

Check failure on line 14 in admin/blockly.js

View workflow job for this annotation

GitHub Actions / check-and-lint

'Blockly' is not defined
Blockly.Words['ntfy_message'] = {'en': 'Message', 'de': 'Nachricht'};

Check failure on line 15 in admin/blockly.js

View workflow job for this annotation

GitHub Actions / check-and-lint

'Blockly' is not defined
Blockly.Words['ntfy_title'] = {'en': 'Title (optional)', 'de': 'Titel (optional)'};

Check failure on line 16 in admin/blockly.js

View workflow job for this annotation

GitHub Actions / check-and-lint

'Blockly' is not defined
Blockly.Words['ntfy_priority'] = {'en': 'Priority', 'de': 'Priorität'};

Check failure on line 17 in admin/blockly.js

View workflow job for this annotation

GitHub Actions / check-and-lint

'Blockly' is not defined
Blockly.Words['ntfy_topic'] = {'en': 'Topic', 'de': 'Topic'};

Check failure on line 18 in admin/blockly.js

View workflow job for this annotation

GitHub Actions / check-and-lint

'Blockly' is not defined
Blockly.Words['ntfy_delay'] = {'en': 'Delay (optional)', 'de': 'Verzögerung in s (optional)'};
Blockly.Words['ntfy_delay'] = {'en': 'Delay (seconds, optional)', 'de': 'Verzögerung in s (optional)'};

Check failure on line 19 in admin/blockly.js

View workflow job for this annotation

GitHub Actions / check-and-lint

'Blockly' is not defined

Blockly.Words['ntfy_prio_urgent'] = {'en': 'Urgent', 'de': 'Dringend'};

Check failure on line 21 in admin/blockly.js

View workflow job for this annotation

GitHub Actions / check-and-lint

'Blockly' is not defined
Blockly.Words['ntfy_prio_high'] = {'en': 'High', 'de': 'Hoch'};
Expand All @@ -29,14 +34,71 @@ Blockly.Words['ntfy_all_instances'] = {'en': 'All instances', 'de
Blockly.Words['ntfy_tooltip'] = {'en': 'Send message to Ntfy Topic', 'de': 'Nachricht an Ntfy Topic senden'};
Blockly.Words['ntfy_help'] = {'en': 'https://github.com/ioBroker/ioBroker.ntfy/blob/master/README.md', 'de': 'https://github.com/ioBroker/ioBroker.ntfy/blob/master/README.md'};

Blockly.Sendto.blocks['ntfy'] =
'<block type="ntfy">'
Blockly.Words['ntfy_send_message'] = {'en': 'Send Ntfy Message', 'de': 'Ntfy Nachricht senden'};

Blockly.Words['ntfy_action_button_view'] = {'en': 'Ntfy Action-Button (View)', 'de': 'Ntfy Aktions-Button (Ansicht)'};
Blockly.Words['ntfy_action_button_label'] = {'en': 'Button-Text', 'de': 'Button-Text'};
Blockly.Words['ntfy_action_button_url'] = {'en': 'URL', 'de': 'URL'};
Blockly.Words['ntfy_action_button_clear'] = {'en': 'Clear notification', 'de': 'Clear notification'};
Blockly.Words['ntfy_message_content'] = {'en': 'Content', 'de': 'Inhalt'};
Blockly.Words['ntfy_tags'] = {'en': 'Tags (List, optional)', 'de': 'Tags (Liste, optional)'};
Blockly.Words['ntfy_attachment'] = {'en': 'Attachment (optional)', 'de': 'Anhang (optional)'};
Blockly.Words['ntfy_url_attachment'] = {'en': 'Ntfy Attachment from URL', 'de': 'Ntfy Anhang von URL'};
Blockly.Words['ntfy_url_attachment_content'] = {'en': 'Content', 'de': 'Inhalt'};
Blockly.Words['ntfy_clickurl'] = {'en': 'Click-URL (optional)', 'de': 'Klick-URL (optional)'};
Blockly.Words['ntfy_actionbutton'] = {'en': 'Action-Button (optional)', 'de': 'Aktions-Button (optional)'};


Blockly.CustomBlocks = Blockly.CustomBlocks || [];
Blockly.CustomBlocks.push('Ntfy');

Blockly.Ntfy = {
HUE: 120,
blocks: {},
};

const NtfyBlockHelpers = {
getInstances: () => {
const options = [];
if (typeof main !== 'undefined' && main.instances) {
for (const instance of main.instances) {
const m = instance.match(/^system.adapter.ntfy.(\d+)$/);
if (m) {
const k = parseInt(m[1], 10);
options.push(['ntfy.' + k, '.' + k]);
}
}
if (options.length === 0) {
for (let u = 0; u <= 4; u++) {
options.push(['ntfy.' + u, '.' + u]);
}
}
} else {
for (let n = 0; n <= 4; n++) {
options.push(['ntfy.' + n, '.' + n]);
}
}
return options;
},
getTopics: () => {
const topics = [];
topics.push([main.objects['system.adapter.ntfy.0'].native.defaultTopic, main.objects['system.adapter.ntfy.0'].native.defaultTopic]);
if (typeof main !== 'undefined' && main.instances) {
const presetTopics = main.objects['system.adapter.ntfy.0'].native.presetTopics;
for (const topicIndex in presetTopics) {
topics.push([ presetTopics[topicIndex].presetTopicName, presetTopics[topicIndex].presetTopicName ]);
}
}
if (!topics.length) topics.push(['No input', 'No input']);
return topics;
}
};

// -- START -- ntfy_send_simple_message
Blockly.Ntfy.blocks['ntfy_send_message'] =
'<block type="ntfy_send_message">'
+ ' <value name="INSTANCE"></value>'
+ ' <value name="TOPIC">'
+ ' <shadow type="text">'
+ ' <field name="TEXT"></field>'
+ ' </shadow>'
+ ' </value>'
+ ' <value name="TOPIC"></value>'
+ ' <value name="TITLE">'
+ ' <shadow type="text">'
+ ' <field name="TEXT"></field>'
Expand All @@ -47,47 +109,65 @@ Blockly.Sendto.blocks['ntfy'] =
+ ' <field name="TEXT"></field>'
+ ' </shadow>'
+ ' </value>'
+ ' <value name="TAGS">'
+ ' <shadow type="logic_null"></shadow>'
+ ' </value>'
+ ' <value name="ATTACHMENT">'
+ ' <shadow type="logic_null"></shadow>'
+ ' </value>'
+ ' <value name="CLICKURL">'
+ ' <shadow type="text"></shadow>'
+ ' </value>'
+ ' <value name="ACTIONBUTTON">'
+ ' <shadow type="logic_null"></shadow>'
+ ' </value>'
+ ' <value name="PRIORITY">'
+ ' </value>'
+ ' <value name="DELAY">'
+ ' </value>'
+ '</block>';

Blockly.Blocks['ntfy'] = {
Blockly.Blocks['ntfy_send_message'] = {
init: function() {

let options = [[Blockly.Translate('ntfy_all_instances'), '']];
if (typeof main !== 'undefined' && main.instances) {
for (let i = 0; i < main.instances.length; i++) {
const m = main.instances[i].match(/^system.adapter.ntfy.(\d+)$/);
if (m) {
const n = parseInt(m[1], 10);
options.push(['ntfy.' + n, '.' + n]);
}
}
}
const options = NtfyBlockHelpers.getInstances();
const topics = NtfyBlockHelpers.getTopics();

if (!options.length) {
for (let u = 0; u <= 4; u++) {
options.push(['ntfy.' + u, '.' + u]);
}
}
this.appendDummyInput('_label')
.appendField(Blockly.Translate('ntfy_send_message'));

this.appendDummyInput('INSTANCE')
.appendField(Blockly.Translate('ntfy_instance'))
.appendField(new Blockly.FieldDropdown(options), "INSTANCE");

this.appendValueInput('TOPIC')
.setCheck('String')
.appendField(Blockly.Translate('ntfy_topic'));
this.appendDummyInput('TOPIC')
.appendField(Blockly.Translate('ntfy_topic'))
.appendField(new Blockly.FieldDropdown(topics), "TOPIC");

this.appendValueInput('TITLE')
.setCheck('String')
.appendField(Blockly.Translate('ntfy_title'));

this.appendValueInput('MESSAGE')
.setCheck(['String'])
.appendField(Blockly.Translate('ntfy_message'));

this.appendValueInput('TAGS')
.setCheck(['Array'])
.appendField(Blockly.Translate('ntfy_tags'));

this.appendValueInput('ATTACHMENT')
.setCheck(['NtfyAttachmentContent'])
.appendField(Blockly.Translate('ntfy_attachment'));

this.appendValueInput('CLICKURL')
.setCheck(['String'])
.appendField(Blockly.Translate('ntfy_clickurl'));

this.appendValueInput('ACTIONBUTTON')
.setCheck(['NtfyActionButtonContent'])
.appendField(Blockly.Translate('ntfy_actionbutton'));

this.appendDummyInput('PRIORITY')
.appendField(Blockly.Translate('ntfy_priority'))
.appendField(new Blockly.FieldDropdown([
Expand All @@ -102,18 +182,17 @@ Blockly.Blocks['ntfy'] = {
.setCheck('String')
.appendField(Blockly.Translate('ntfy_delay'));


this.setInputsInline(false);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);

this.setColour(Blockly.Sendto.HUE);
this.setColour(Blockly.Ntfy.HUE);
this.setTooltip(Blockly.Translate('ntfy_tooltip'));
this.setHelpUrl(Blockly.Translate('ntfy_help'));
}
};

Blockly.JavaScript['ntfy'] = function(block) {
Blockly.JavaScript['ntfy_send_message'] = function(block) {
const dropdown_instance = block.getFieldValue('INSTANCE');
const topic = Blockly.JavaScript.valueToCode(block, 'TOPIC', Blockly.JavaScript.ORDER_ATOMIC);
const title = Blockly.JavaScript.valueToCode(block, 'TITLE', Blockly.JavaScript.ORDER_ATOMIC);
Expand Down Expand Up @@ -143,4 +222,99 @@ Blockly.JavaScript['ntfy'] = function(block) {
console.log('str:', jsonStr);

return 'sendTo("ntfy' + dropdown_instance + '", "send", ' + jsonStr + ');\n';
};
};

// -- END -- ntfy_send_simple_message
// -- START -- ntfy_send_extended_message




// --- Allocation block : ntfy_url_attachment ----------------------------------------------------
Blockly.Ntfy.blocks['ntfy_url_attachment'] =
`<block type='ntfy_url_attachment'>
<value name='CONTENT'>
<shadow type='TEXT'>
<field name='TEXT'></field>
</shadow>
</value>
</block>`;

Blockly.Blocks['ntfy_url_attachment'] = {
init: function () {
this.appendDummyInput('_title')
.appendField(Blockly.Translate('ntfy_url_attachment'));

this.appendValueInput('content')
.setCheck('String')
.appendField(Blockly.Translate('ntfy_url_attachment_content'));

this.setInputsInline(false);

this.setOutput(true, 'NtfyAttachmentContent');

this.setColour(Blockly.Ntfy.HUE);
},
};

// --- Allocation block : ntfy_action_button_view ----------------------------------------------------
Blockly.Ntfy.blocks['ntfy_action_button_view'] =
`<block type='ntfy_action_button_view'>
<value name='LABEL'>
<shadow type='TEXT'>
<field name='TEXT'></field>
</shadow>
</value>
<value name='URL'>
<shadow type='TEXT'>
<field name='TEXT'></field>
</shadow>
</value>
<value name='CLEAR'></value>
</block>`;

Blockly.Blocks['ntfy_action_button_view'] = {
init: function () {
this.appendDummyInput('_title')
.appendField(Blockly.Translate('ntfy_action_button_view'));

this.appendValueInput('label')
.setCheck('String')
.appendField(Blockly.Translate('ntfy_action_button_label'));

this.appendValueInput('url')
.setCheck('String')
.appendField(Blockly.Translate('ntfy_action_button_url'));

this.appendDummyInput('CLEAR')
.appendField(Blockly.Translate('ntfy_action_button_clear'))
.appendField(new Blockly.FieldDropdown([['false', 'false'],['true', 'true']]), 'CLEAR');

this.setInputsInline(false);

this.setOutput(true, 'NtfyActionButtonContent');

this.setColour(Blockly.Ntfy.HUE);
},
};

Blockly.JavaScript['ntfy_action_button_view'] = function (block) {
/*
const content = Blockly.JavaScript.valueToCode(block, 'content', Blockly.JavaScript.ORDER_ATOMIC);
const replyToId = Blockly.JavaScript.valueToCode(block, 'replyToId', Blockly.JavaScript.ORDER_ATOMIC);
const embeds = Blockly.JavaScript.valueToCode(block, 'embeds', Blockly.JavaScript.ORDER_ATOMIC);
const files = Blockly.JavaScript.valueToCode(block, 'files', Blockly.JavaScript.ORDER_ATOMIC);
const propEmbeds = embeds && embeds !== 'null' ? `\n embeds: ${embeds},` : '';
const propFiles = files && files !== 'null' ? `\n files: ${files},` : '';
const propReply = replyToId && replyToId !== 'null' ? `\n reply: { messageReference: ${replyToId} },` : '';
const ret = `{
content: ${content},${propEmbeds}${propFiles}${propReply}
}`;
return [ret, Blockly.JavaScript.ORDER_ATOMIC];
*/
};

Loading

0 comments on commit c29e830

Please sign in to comment.