Skip to content

Commit

Permalink
Add macro class (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
bewee authored Feb 26, 2021
1 parent c4d9ed0 commit a87d024
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
54 changes: 54 additions & 0 deletions classes/macro/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(() => {
class MacroClass {

constructor(handler) {
this.macros = {};
const load_card = handler.addLoadCard(null, (copy) => {
if (copy.getInternalAttribute('macro_id') in this.macros) {
this.setupExecuteMacro(copy);
} else {
copy.setAttribute('macro-waiting', copy.getInternalAttribute('macro_id'));
}
});
load_card.setTooltipText(`Execute Macro _/_`);
load_card.addInternalAttribute('macro_id', null);
load_card.setShutdownText('Execute Macro (id=%a)', 'macro_id');
load_card.addInput('moment', {type: 'string', enum: ['started', 'finished']});
load_card.addAbilities('trigger', 'macro');

{
const block = handler.addBlock('execute', ['Macros']);
block.addParameter('macro', {accepts: 'macro', text: 'Macro'});
block.setTooltipText('Execute Macro');
block.setText('Execute %p', 'macro');
}

window.API.postJson('/extensions/macrozilla/api/list-macropaths').then((res) => {
res.list.forEach((p) => {
window.API.postJson('/extensions/macrozilla/api/list-macros', {path_id: p.id}).then((res) => {
res.list.forEach((m) => {
this.macros[m.id] = `${p.name}/${m.name}`;
const card = handler.addCard(null, ['Macros'], load_card);
card.updateInternalAttribute('macro_id', m.id);
this.setupMacro(card);
document.querySelectorAll(`macro-card[macro-waiting='${m.id}']`).forEach((c) => {
c.removeAttribute('macro-waiting');
this.setupMacro(c);
});
});
});
});
});
}

setupMacro(card) {
card.updateAbility('trigger', `${this.macros[card.getInternalAttribute('macro_id')]} %i executing`, 'moment');
card.setTooltipText(`Macro ${this.macros[card.getInternalAttribute('macro_id')]}`);
card.setText(`${this.macros[card.getInternalAttribute('macro_id')]}`);
card.revive();
}

}

window.exports = MacroClass;
})();
13 changes: 13 additions & 0 deletions classes/macro/schema_exec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"title": "exec",
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"allOf": [{"$ref": "/class"}],
"properties": {
"macro": {"$ref": "/class"},
"qualifier": {
"enum": ["execute"]
}
},
"required": ["qualifier", "macro"]
}
15 changes: 15 additions & 0 deletions classes/macro/schema_trigger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"title": "trigger",
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"allOf": [{"$ref": "/class"}],
"properties": {
"macro_id": {
"type": "integer"
},
"moment": {
"enum": ["started", "finished"]
}
},
"required": ["macro_id", "moment"]
}
26 changes: 26 additions & 0 deletions classes/macro/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

module.exports = {

exec: async function() {
await this.handler.macrozilla.macrohandler.execMacro(this.params.description.macro.macro_id, `macro ${this.inf.macro_id}`);
},

trigger: function() {
const fn = (macro_id, _value) => {
if (macro_id == this.params.description.macro_id)
this.params.callback();
};
if (this.params.description.moment === 'started')
this.handler.macrozilla.eventhandler.on(`macroExecutionStarted`, fn);
else
this.handler.macrozilla.eventhandler.on(`macroExecutionFinished`, fn);
this.params.destruct = () => {
if (this.params.description.moment === 'started')
this.handler.macrozilla.eventhandler.removeListener(`macroExecutionStarted`, fn);
else
this.handler.macrozilla.eventhandler.removeListener(`macroExecutionFinished`, fn);
};
},

};

0 comments on commit a87d024

Please sign in to comment.