mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Lazy-load Blockly for board administrators, preserve existing rule documents and execution, and provide direct List, Workflow, Blocks and History choices. Route workflow writes through server methods. Include local media, complete message-key coverage, translation merging and placeholder checks. Translation coverage remains incomplete in additional locales. Low-confidence specialist terminology needs native review in Aragonese, Asturian, Corsican, Neapolitan, Occitan, Sardinian, Sicilian, Kashubian, Upper Sorbian, Silesian, Cornish, Manx, Gaelic and Breton; Faroese and Frisian also merit review. Verified five Blockly/catalog Node tests, existing rules suites and four Blockly Chromium scenarios, including admin permissions, localization, drag editing and persisted execution. Thanks to xet7 !
169 lines
6.5 KiB
JavaScript
169 lines
6.5 KiB
JavaScript
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import { TAPi18n } from '/imports/i18n';
|
|
import { Utils } from '/client/lib/utils';
|
|
|
|
// A drag-and-drop visual workflow editor for board rules. Triggers and actions
|
|
// are draggable chips; drag one of each into the builder (When → Then) and add
|
|
// the rule, or drag an action onto an existing rule to change what it does.
|
|
//
|
|
// The palette offers the parameter-free / wildcard ("any") variants so a rule
|
|
// can be built entirely by dragging. Parameterized rules (specific list, label,
|
|
// member, schedule time, …) are still created with the form builder on the
|
|
// List view.
|
|
// Each palette entry carries an i18n `labelKey` (translated at render time via
|
|
// TAPi18n.__) instead of a hardcoded English string, so the whole workflow view
|
|
// follows the UI language. `labelParams` is passed to TAPi18n.__ for labels that
|
|
// need a placeholder (e.g. the scheduled time). Existing rule keys are reused
|
|
// where they fit; workflow-only labels use new `r-w-*` keys (see en.i18n.json).
|
|
import { TRIGGER_PALETTE, ACTION_PALETTE } from '/models/lib/rulesWorkflowPalette';
|
|
|
|
// Translate a palette entry's label for display / storage in the current UI
|
|
// language (matches the classic Rules view, which also stores already-translated
|
|
// description text at creation time).
|
|
function paletteLabel(entry) {
|
|
if (!entry) return '';
|
|
return TAPi18n.__(entry.labelKey, entry.labelParams);
|
|
}
|
|
|
|
Template.rulesWorkflow.onCreated(function () {
|
|
this.error = new ReactiveVar('');
|
|
this.busy = new ReactiveVar(false);
|
|
this.builderTrigger = new ReactiveVar(null);
|
|
this.builderAction = new ReactiveVar(null);
|
|
this.dragItem = null; // {type:'trigger'|'action', idx}
|
|
this.autorun(() => {
|
|
const boardId = Session.get('currentBoard');
|
|
if (boardId) this.subscribe('boardRules', boardId);
|
|
});
|
|
});
|
|
|
|
Template.rulesWorkflow.helpers({
|
|
workflowError() { return Template.instance().error.get(); },
|
|
currentBoard() {
|
|
return Utils.getCurrentBoard();
|
|
},
|
|
triggerPalette() {
|
|
return TRIGGER_PALETTE.map((t, idx) => ({ idx, label: paletteLabel(t) }));
|
|
},
|
|
actionPalette() {
|
|
return ACTION_PALETTE.map((a, idx) => ({ idx, label: paletteLabel(a) }));
|
|
},
|
|
builderTrigger() {
|
|
return Template.instance().builderTrigger.get();
|
|
},
|
|
builderTriggerLabel() {
|
|
return paletteLabel(Template.instance().builderTrigger.get());
|
|
},
|
|
builderAction() {
|
|
return Template.instance().builderAction.get();
|
|
},
|
|
builderActionLabel() {
|
|
return paletteLabel(Template.instance().builderAction.get());
|
|
},
|
|
createDisabled() {
|
|
const tpl = Template.instance();
|
|
return tpl.busy.get() || !(tpl.builderTrigger.get() && tpl.builderAction.get());
|
|
},
|
|
rules() {
|
|
const boardId = Session.get('currentBoard');
|
|
const rules = ReactiveCache.getRules({ boardId });
|
|
return rules.map(rule => {
|
|
const trigger = ReactiveCache.getTrigger(rule.triggerId);
|
|
const action = ReactiveCache.getAction(rule.actionId);
|
|
return {
|
|
ruleId: rule._id,
|
|
whenText: (trigger && trigger.desc) || (trigger && trigger.activityType) || '',
|
|
thenText: (action && action.desc) || (action && action.actionType) || '',
|
|
};
|
|
});
|
|
},
|
|
});
|
|
|
|
async function persistRule(tpl) {
|
|
const t = tpl.builderTrigger.get();
|
|
const a = tpl.builderAction.get();
|
|
if (!t || !a || tpl.busy.get()) return;
|
|
const boardId = Session.get('currentBoard');
|
|
const titleField = tpl.find('.js-workflow-rule-title');
|
|
const tLabel = paletteLabel(t);
|
|
const aLabel = paletteLabel(a);
|
|
const title = (titleField.value || '').trim() || `${tLabel} → ${aLabel}`;
|
|
tpl.busy.set(true); tpl.error.set('');
|
|
try {
|
|
await Meteor.callAsync('rules.createRule', boardId, title,
|
|
{ ...t.doc, desc: tLabel }, { ...a.doc, desc: aLabel });
|
|
tpl.builderTrigger.set(null);
|
|
tpl.builderAction.set(null);
|
|
titleField.value = '';
|
|
} catch (error) { tpl.error.set(error.reason || error.message); }
|
|
finally { tpl.busy.set(false); }
|
|
}
|
|
|
|
Template.rulesWorkflow.events({
|
|
'dragstart .js-trigger-chip'(event, tpl) {
|
|
tpl.dragItem = { type: 'trigger', idx: Number(event.currentTarget.dataset.idx) };
|
|
},
|
|
'dragstart .js-action-chip'(event, tpl) {
|
|
tpl.dragItem = { type: 'action', idx: Number(event.currentTarget.dataset.idx) };
|
|
},
|
|
// Allow dropping by preventing the default (which disables drop) on dragover.
|
|
'dragover .js-when-slot, dragover .js-then-slot, dragover .js-rule-node'(event) {
|
|
event.preventDefault();
|
|
},
|
|
'drop .js-when-slot'(event, tpl) {
|
|
event.preventDefault();
|
|
if (tpl.dragItem && tpl.dragItem.type === 'trigger') {
|
|
tpl.builderTrigger.set(TRIGGER_PALETTE[tpl.dragItem.idx]);
|
|
}
|
|
tpl.dragItem = null;
|
|
},
|
|
'drop .js-then-slot'(event, tpl) {
|
|
event.preventDefault();
|
|
if (tpl.dragItem && tpl.dragItem.type === 'action') {
|
|
tpl.builderAction.set(ACTION_PALETTE[tpl.dragItem.idx]);
|
|
}
|
|
tpl.dragItem = null;
|
|
},
|
|
// Drop an action onto an existing rule to replace what it does.
|
|
async 'drop .js-rule-node'(event, tpl) {
|
|
event.preventDefault();
|
|
if (!tpl.dragItem || tpl.dragItem.type !== 'action') return;
|
|
const ruleId = event.currentTarget.dataset.ruleId;
|
|
const rule = ReactiveCache.getRule(ruleId);
|
|
if (!rule) return;
|
|
const a = ACTION_PALETTE[tpl.dragItem.idx];
|
|
const trigger = ReactiveCache.getTrigger(rule.triggerId);
|
|
if (!trigger || tpl.busy.get()) { tpl.error.set(TAPi18n.__('r-blocks-unavailable')); return; }
|
|
tpl.busy.set(true); tpl.error.set('');
|
|
try {
|
|
await Meteor.callAsync('rules.updateRule', ruleId, rule.title, trigger,
|
|
{ ...a.doc, desc: paletteLabel(a) });
|
|
} catch (error) { tpl.error.set(error.reason || error.message); }
|
|
finally { tpl.busy.set(false); }
|
|
tpl.dragItem = null;
|
|
},
|
|
'click .js-clear-when'(event, tpl) {
|
|
event.preventDefault();
|
|
tpl.builderTrigger.set(null);
|
|
},
|
|
'click .js-clear-then'(event, tpl) {
|
|
event.preventDefault();
|
|
tpl.builderAction.set(null);
|
|
},
|
|
'click .js-create-workflow-rule'(event, tpl) {
|
|
event.preventDefault();
|
|
void persistRule(tpl);
|
|
},
|
|
'click .js-delete-workflow-rule'(event, tpl) {
|
|
event.preventDefault();
|
|
const node = event.currentTarget.closest('.js-rule-node');
|
|
const ruleId = node && node.dataset.ruleId;
|
|
const rule = ReactiveCache.getRule(ruleId);
|
|
if (!rule) return;
|
|
// Delete the rule + its trigger + action server-side (see
|
|
// server/rulesButton.js `rules.deleteRule`): three client-side
|
|
// Collection.remove() calls were rejected with 403 "Access denied".
|
|
Meteor.call('rules.deleteRule', rule._id);
|
|
},
|
|
});
|