LuminaWebsite/assets/js/gear-macro.js
Maaxxs 247dad4cf4 Gear planner: macro entries point to a key of the skill bar, like in the game
Reported in the bug tracker: "Es stehen die einzelnen Skills zur Auswahl. Das ist so falsch. Richtig kann
man lediglich die Schnellzugriffstaste auswählen." In Aion 2 a macro entry chains buttons, not skills: it
points to a slot of the skill bar, and when the macro gets there, the game uses the skills stacked on that
slot (rows 0-3) one after another.

- Skill bar per skill preset (includes/gear_macro.php): the 12 slots of the game with their default keys
  (GEAR_MACRO_KEYS: 1-8, Q, E, left and right mouse button, in three groups of four) and up to
  GEAR_MACRO_KEY_ROWS = 4 active or stigma skills per key in rows 0-3 (row 0 is the one the bar shows).
  Drawn like the skill window: one column per key with rows 3 to 0 and the key below; on narrow screens
  (container query) two lines of six keys.
- A click on a field opens the skill picker below the bar: place, replace (a skill already on the key
  swaps rows), use earlier/later, remove. Skills that are already on a key carry its label. "Leiste von
  Preset N übernehmen" copies the bar of another preset (asks first if the bar is not empty). Without
  JavaScript the field submits cell=Key:Row and the page comes back with the picker open.
- Macro entries are {"k": key, "d": delay}. "Makro hinzufügen" and the icon of an entry open the key
  picker (the 12 keys with their row 0 skill and "+N" for the stacked ones, empty keys disabled). An entry
  shows the key, the row 0 skill and "dann ..." with the other skills of the key; "Als Text kopieren"
  writes each key with all its skills.
- gear_action.php save_macro: entries from key[] and delay[], new ops slot, unslot, slotup, slotdown and
  copybar; skills of other classes drop off the bar with a note.
- Stored in gear_set_plans "macro" as presets + bars. Macros from before (entries with a skill "s") are
  converted when read: each skill goes to the key where it already is in row 0, else to the next free key,
  and is saved that way with the next change.
- Patch notes (with thanks for the report) and README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 22:50:09 +02:00

443 lines
20 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Gear-Planer, Unterseite Makro (includes/gear_macro.php): Presets 1-3 ohne Neuladen wechseln, das Makro als Text
* kopieren (z. B. fuer Discord) und - im eigenen Set - die Skill-Leiste belegen sowie Eintraege hinzufuegen, austauschen,
* verschieben und loeschen. Jede Aenderung geht sofort an gear_action.php (save_macro), das neu gezeichnete Preset
* (Leiste und Makro-Fenster) kommt zurueck; Verzoegerungen werden kurz nach dem Tippen gespeichert. Ein Klick auf ein
* Feld der Leiste oeffnet darunter die Skill-Auswahl, ein Klick auf das Symbol eines Eintrags direkt darunter die
* Tasten-Auswahl.
*/
(function () {
'use strict';
var root = document.querySelector('[data-macro]');
if (!root) return;
function visibleForm() {
return root.querySelector('[data-macro-form]:not([hidden])');
}
// ---- Presets 1-3 ----
function showPreset(no) {
root.querySelectorAll('[data-macro-form]').forEach(function (form) { form.hidden = form.dataset.preset !== no; });
root.querySelectorAll('[data-macro-preset-link]').forEach(function (link) {
var on = link.dataset.macroPresetLink === no;
link.classList.toggle('is-active', on);
if (on) link.setAttribute('aria-current', 'true');
else link.removeAttribute('aria-current');
});
try {
var url = new URL(window.location.href);
url.searchParams.set('preset', no);
url.searchParams.delete('cell');
window.history.replaceState(null, '', url.toString());
} catch (e) {
// aeltere Browser: die Adresse bleibt
}
}
// ---- Als Text kopieren ----
var copyButton = root.querySelector('[data-macro-copy]');
function copyText(text) {
if (navigator.clipboard && navigator.clipboard.writeText) return navigator.clipboard.writeText(text);
return new Promise(function (resolve, reject) {
var area = document.createElement('textarea');
area.value = text;
area.setAttribute('readonly', '');
area.style.position = 'fixed';
area.style.opacity = '0';
document.body.appendChild(area);
area.select();
var ok = false;
try { ok = document.execCommand('copy'); } catch (e) { ok = false; }
area.remove();
if (ok) resolve(); else reject(new Error('copy'));
});
}
if (copyButton) {
var copyLabel = copyButton.textContent;
copyButton.addEventListener('click', function () {
var form = visibleForm();
if (!form) return;
var lines = [(root.dataset.macroTitle || 'Gear-Set') + ' – Makro, Preset ' + form.dataset.preset];
form.querySelectorAll('[data-macro-entry]').forEach(function (entry, index) {
var input = entry.querySelector('[data-macro-delay]');
var shown = entry.querySelector('.gear-macro-delay strong');
var delay = input ? input.value : (shown ? shown.textContent : '');
// "Taste E: Pfeilhagel (Stufe 10), dann …" - der Server schreibt die Skills der Taste dazu
lines.push((index + 1) + '. ' + entry.dataset.macroText + ' – ' + delay + ' ms');
});
if (lines.length === 1) lines.push('(leer)');
copyText(lines.join('\n')).then(
function () { copyButton.textContent = '✓ Kopiert'; },
function () { copyButton.textContent = 'Kopieren ging nicht'; }
).then(function () {
setTimeout(function () { copyButton.textContent = copyLabel; }, 2000);
});
});
}
var editable = !!root.querySelector('[data-macro-form] input[name="action"]') && window.fetch && window.FormData;
// ---- Tasten-Auswahl: unten zum Hinzufuegen, unter einem Eintrag zum Austauschen ----
var picking = null; // { details, home, next, entry, index }
function pickerTitle(details, text) {
var title = details.querySelector('[data-macro-picker-title]');
if (title) title.textContent = text;
}
function openPicker(form, index) {
var details = form.querySelector('[data-macro-picker]');
var entry = form.querySelector('[data-macro-entry="' + index + '"]');
if (!details || !entry) return;
closePicker(false);
closeBarPicker(false);
picking = { details: details, home: details.parentNode, next: details.nextSibling, entry: entry, index: index };
entry.classList.add('is-picking');
// Zwischen Taste und Verzoegerung des Eintrags
entry.insertBefore(details, entry.querySelector('.gear-macro-delay'));
details.classList.add('is-replacing');
pickerTitle(details, 'Taste für Eintrag ' + (Number(index) + 1) + ' wählen');
details.open = true;
var keyField = entry.querySelector('input[name="key[]"]');
var current = keyField && details.querySelector('[data-pick-key="' + keyField.value + '"]:not(:disabled)');
(current || details.querySelector('[data-pick-key]:not(:disabled)') || entry.querySelector('[data-macro-pick]')).focus();
}
function closePicker(focusEntry) {
root.querySelectorAll('[data-macro-picker][open]').forEach(function (details) {
if (!picking || details !== picking.details) details.open = false;
});
if (!picking) return;
var p = picking;
picking = null;
p.entry.classList.remove('is-picking');
p.details.open = false;
p.details.classList.remove('is-replacing');
p.home.insertBefore(p.details, p.next);
pickerTitle(p.details, 'Taste für den neuen Eintrag wählen');
if (focusEntry) {
var button = p.entry.querySelector('[data-macro-pick]');
if (button) button.focus();
}
}
// ---- Skill-Auswahl fuer ein Feld der Skill-Leiste ----
var barPicking = null; // { picker, button }
function openBarPicker(form, button) {
var picker = form.querySelector('[data-bar-picker]');
if (!picker) return;
closePicker(false);
closeBarPicker(false);
var cell = button.dataset.barCell;
var filled = button.hasAttribute('data-bar-skill');
picker.dataset.cell = cell;
var title = picker.querySelector('[data-bar-picker-title]');
if (title) title.textContent = button.dataset.barTitle || '';
var tools = picker.querySelector('[data-bar-tools]');
if (tools) {
tools.hidden = !filled;
var up = tools.querySelector('[data-bar-op="slotup"]');
var down = tools.querySelector('[data-bar-op="slotdown"]');
if (up) up.disabled = cell.split(':')[1] === '0';
if (down) down.disabled = button.hasAttribute('data-bar-last');
}
picker.querySelectorAll('[data-pick-bar-skill]').forEach(function (pick) {
pick.classList.toggle('is-current', filled && pick.dataset.pickBarSkill === button.dataset.barSkill);
});
picker.hidden = false;
var wrap = button.closest('.gear-macro-cell');
if (wrap) wrap.classList.add('is-picking');
barPicking = { picker: picker, button: button };
var current = filled && picker.querySelector('[data-pick-bar-skill="' + button.dataset.barSkill + '"]');
var first = current || picker.querySelector('[data-pick-bar-skill]');
if (first) first.focus({ preventScroll: true });
picker.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
}
function closeBarPicker(focusCell) {
if (!barPicking) return;
var p = barPicking;
barPicking = null;
p.picker.hidden = true;
var wrap = p.button.closest('.gear-macro-cell');
if (wrap) wrap.classList.remove('is-picking');
if (focusCell && document.contains(p.button)) p.button.focus();
}
// ---- Speichern ----
var inflight = null; // 'op' (Aenderung, danach neu gezeichnet) oder 'delay' (nur Verzoegerungen)
var queued = null; // eine Aenderung, die kam, waehrend Verzoegerungen gespeichert wurden
var delayAgain = null; // Formular, dessen Verzoegerungen danach noch einmal gespeichert werden
var timers = {};
function setStatus(form, text, kind) {
form.querySelectorAll('[data-macro-status]').forEach(function (el) {
el.textContent = text;
el.className = 'gear-save-status' + (kind ? ' is-' + kind : '');
});
}
function prepare(form) {
form.querySelectorAll('[data-autosave-fallback]').forEach(function (button) { button.hidden = true; });
}
/** Nach einer Aenderung dorthin, wo man weitermachen will (Tastatur): mit dem Eintrag mitwandern usw. */
function focusAfter(form, op) {
var parts = op.split(':');
var index = parseInt(parts[1], 10);
var find = function (selector) { return form.querySelector(selector); };
var cell = function (key, row) { return find('[data-bar-cell="' + key + ':' + row + '"]'); };
var target = null;
if (parts[0] === 'up') {
target = find('[name="op"][value="up:' + (index - 1) + '"]:not(:disabled)') || find('[name="op"][value="down:' + (index - 1) + '"]');
} else if (parts[0] === 'down') {
target = find('[name="op"][value="down:' + (index + 1) + '"]:not(:disabled)') || find('[name="op"][value="up:' + (index + 1) + '"]');
} else if (parts[0] === 'remove') {
target = find('[name="op"][value="remove:' + index + '"]') || find('[name="op"][value="remove:' + (index - 1) + '"]') || find('.gear-macro-add-button');
} else if (parts[0] === 'replace') {
target = find('[data-macro-pick="' + index + '"]');
} else if (parts[0] === 'add') {
target = find('[data-pick-key="' + parts[1] + '"]') || find('.gear-macro-add-button');
} else if (parts[0] === 'slot' || parts[0] === 'unslot') {
target = cell(parts[1], parts[2]) || find('[data-bar-cell^="' + parts[1] + ':"]');
} else if (parts[0] === 'slotup' || parts[0] === 'slotdown') {
target = cell(parts[1], parseInt(parts[2], 10) + (parts[0] === 'slotup' ? -1 : 1));
}
if (target) target.focus({ preventScroll: true });
}
function render(form, html, op) {
var holder = document.createElement('div');
holder.innerHTML = html;
var fresh = holder.firstElementChild;
if (!fresh) return form;
fresh.hidden = form.hidden;
form.replaceWith(fresh);
prepare(fresh);
// Nach dem Hinzufuegen bleibt die Auswahl offen - so lassen sich mehrere Tasten nacheinander eintragen
var picker = fresh.querySelector('[data-macro-picker]');
if (picker && op.indexOf('add:') === 0 && !picker.hasAttribute('data-full')) picker.open = true;
focusAfter(fresh, op);
if (window.gearTipRefresh) window.gearTipRefresh();
return fresh;
}
function send(form, op) {
if (inflight) {
// Waehrend Verzoegerungen gespeichert werden, bleiben die Nummern gleich - die Aenderung folgt danach.
// Laeuft schon eine Aenderung, zaehlt der Klick nicht: die Nummern stimmen erst nach dem Neuzeichnen.
if (op && inflight === 'delay') queued = { form: form, op: op };
else if (!op) delayAgain = form;
return;
}
if (!op && !form.checkValidity()) {
setStatus(form, 'Verzögerung: ganze Zahl von 10 bis 9999 ms – nicht gespeichert', 'error');
return;
}
inflight = op ? 'op' : 'delay';
clearTimeout(timers[form.dataset.preset]);
var body = new FormData(form);
if (op) {
body.append('op', op);
form.setAttribute('aria-busy', 'true');
}
setStatus(form, 'Speichert …', 'busy');
fetch(form.getAttribute('action'), {
method: 'POST',
body: body,
credentials: 'same-origin',
headers: { 'X-Requested-With': 'fetch', 'Accept': 'application/json' }
})
.then(function (response) { return response.json(); })
.then(function (result) {
if (!result.ok) throw new Error(result.error || 'Speichern fehlgeschlagen');
var current = form;
if (op) {
current = render(form, result.html, op);
} else {
// Vom Server begrenzte Verzoegerungen (10-9999 ms) uebernehmen
var inputs = form.querySelectorAll('[data-macro-delay]');
(result.entries || []).forEach(function (entry, i) {
if (inputs[i] && inputs[i] !== document.activeElement && String(entry.d) !== inputs[i].value) inputs[i].value = entry.d;
});
}
var tabCount = document.querySelector('.gear-tab.is-active .gear-tab-count');
if (tabCount) tabCount.textContent = result.count || '';
var link = root.querySelector('[data-macro-preset-link="' + current.dataset.preset + '"]');
if (link) {
link.classList.toggle('has-entries', !!result.filled);
var entries = (result.entries || []).length;
link.title = 'Preset ' + current.dataset.preset + (entries ? ' – ' + entries + (entries === 1 ? ' Eintrag' : ' Einträge') : ' – leer');
}
var errors = result.errors || [];
setStatus(current, errors.length ? errors.join(' ') : '✓ Gespeichert', errors.length ? 'error' : 'ok');
})
.catch(function (error) {
form.removeAttribute('aria-busy');
setStatus(form, error.message || 'Speichern fehlgeschlagen', 'error');
})
.then(function () {
inflight = null;
if (queued) {
var next = queued;
queued = null;
send(next.form, next.op);
} else if (delayAgain) {
var again = delayAgain;
delayAgain = null;
send(again, null);
}
});
}
// ---- Klicks: Presets, Leiste, Eintraege, Auswahl ----
root.addEventListener('click', function (event) {
var presetLink = event.target.closest('[data-macro-preset-link]');
if (presetLink) {
event.preventDefault();
closePicker(false);
closeBarPicker(false);
showPreset(presetLink.dataset.macroPresetLink);
return;
}
if (!editable) return;
var cellButton = event.target.closest('[data-bar-cell]');
if (cellButton) {
event.preventDefault();
if (barPicking && barPicking.button === cellButton) closeBarPicker(true);
else openBarPicker(cellButton.closest('[data-macro-form]'), cellButton);
return;
}
if (event.target.closest('[data-bar-close]')) {
event.preventDefault();
if (barPicking) {
closeBarPicker(true);
} else {
// Ohne JavaScript geoeffnet (cell=… in der Adresse)
event.target.closest('[data-bar-picker]').hidden = true;
}
return;
}
var pick = event.target.closest('[data-macro-pick]');
if (pick) {
event.preventDefault();
var form = pick.closest('[data-macro-form]');
if (picking && picking.entry.contains(pick)) closePicker(true);
else openPicker(form, pick.dataset.macroPick);
return;
}
var summary = event.target.closest('.gear-macro-add-button');
if (summary && summary.tagName === 'SUMMARY') {
var details = summary.parentNode;
if (picking) closePicker(false);
closeBarPicker(false);
// Volles Makro: nichts hinzufuegen, den Hinweis zeigen
if (details.hasAttribute('data-full') && !details.open) {
event.preventDefault();
var note = details.parentNode.querySelector('[data-macro-full]');
if (note) {
note.classList.remove('is-flash');
void note.offsetWidth;
note.classList.add('is-flash');
}
}
return;
}
var button = event.target.closest('button[name="op"]');
if (button) {
event.preventDefault();
if (button.disabled) return;
var op = button.value;
var targetForm = button.closest('[data-macro-form]');
var barPicker = button.closest('[data-bar-picker]');
if (barPicker) {
// Feld aus der geoeffneten Auswahl (mit JavaScript eingestellt, ohne vom Server)
var cell = barPicker.dataset.cell;
if (!cell) return;
op = button.hasAttribute('data-bar-op') ? button.dataset.barOp + ':' + cell : 'slot:' + cell + ':' + button.dataset.pickBarSkill;
} else if (picking && button.hasAttribute('data-pick-key')) {
op = 'replace:' + picking.index + ':' + button.dataset.pickKey;
}
// Beim Hinzufuegen bleibt die Auswahl offen, sonst geht sie zu
if (op.indexOf('add:') !== 0) closePicker(false);
closeBarPicker(false);
if (button.hasAttribute('data-confirm') && window.luminaDialog) {
window.luminaDialog.confirm(button.getAttribute('data-confirm'), {
title: button.getAttribute('data-confirm-title') || undefined,
okLabel: button.getAttribute('data-confirm-ok') || undefined
}).then(function (ok) {
if (ok) send(targetForm, op);
});
return;
}
send(targetForm, op);
}
});
if (!editable) return;
root.querySelectorAll('[data-macro-form]').forEach(prepare);
// Ohne JavaScript geoeffnete Auswahl (cell=… in der Adresse) wie eine selbst geoeffnete behandeln
root.querySelectorAll('[data-bar-picker]:not([hidden])').forEach(function (picker) {
var form = picker.closest('[data-macro-form]');
var button = form && picker.dataset.cell && form.querySelector('[data-bar-cell="' + picker.dataset.cell + '"]');
if (button) barPicking = { picker: picker, button: button };
});
// Ausserhalb der Auswahl geklickt oder Escape: zu
document.addEventListener('click', function (event) {
if (barPicking && !barPicking.picker.contains(event.target) && !event.target.closest('[data-bar-cell]')) closeBarPicker(false);
if (picking) {
if (!picking.details.contains(event.target) && !event.target.closest('[data-macro-pick]')) closePicker(false);
return;
}
root.querySelectorAll('[data-macro-picker][open]').forEach(function (details) {
if (!details.contains(event.target)) details.open = false;
});
});
document.addEventListener('keydown', function (event) {
if (event.key !== 'Escape') return;
if (barPicking) {
closeBarPicker(true);
return;
}
if (picking) {
closePicker(true);
return;
}
root.querySelectorAll('[data-macro-picker][open]').forEach(function (details) {
details.open = false;
var summary = details.querySelector('summary');
if (summary) summary.focus();
});
});
// ---- Verzoegerungen: kurz nach dem Tippen speichern, Enter sofort ----
root.addEventListener('input', function (event) {
if (!event.target.matches('[data-macro-delay]')) return;
var form = event.target.closest('[data-macro-form]');
clearTimeout(timers[form.dataset.preset]);
setStatus(form, 'Ungespeicherte Änderung …', 'busy');
timers[form.dataset.preset] = setTimeout(function () { send(form, null); }, 700);
});
root.addEventListener('change', function (event) {
if (event.target.matches('[data-macro-delay]')) send(event.target.closest('[data-macro-form]'), null);
});
root.addEventListener('keydown', function (event) {
if (event.key === 'Enter' && event.target.matches('[data-macro-delay]')) {
event.preventDefault();
send(event.target.closest('[data-macro-form]'), null);
}
});
root.addEventListener('submit', function (event) {
event.preventDefault();
send(event.target, null);
});
})();