Find Mileage by its label precisely; inline script on issue pages

- Looks only in the issue's attributes for the field labelled exactly
  "Mileage" (any case): no settings and no field id needed, and menus,
  history, other plugins' text or fields like "Mileage Out" can't match;
  issues without the field no longer throw a JavaScript error
- Shows "Copied!" only when the copy succeeded ("Couldn't copy"
  otherwise), falls back when the Clipboard API isn't available, and
  restores the value's original HTML afterwards
- Pointer cursor, "Click to copy" tooltip, keyboard support (Enter/Space)
- The script is written into the issue page by the
  view_issues_show_details_bottom hook instead of being served as a
  compiled asset, so it only loads on issue pages and can't 404 when
  Redmine's asset manifest is stale; removes assets/javascripts and the
  leftover console.log
- init.rb: requires Redmine 6.1+, points to the Gitea repository

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-10-29 19:53:46 -04:00
1 parent fea86725ab
commit fa9010c063
4 files changed
+107 -49

No files matched your search

+100
View File
@@ -0,0 +1,100 @@
<%#
The MIT License (MIT)
Copyright (c) 2026 rick barrette
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%>
<%#
Click (or Enter/Space) on an issue's Mileage value to copy it to the clipboard.
The field is found by its label, so it works on any Redmine without setup: the issue attribute
labelled exactly "Mileage" (any capitalisation). Issues without one are left alone.
Written into the page by the issue view hook rather than served as a compiled asset, so it never
depends on Redmine's asset manifest being up to date.
%>
<%= javascript_tag do %>
$(function () {
var FIELD_LABEL = 'mileage';
function findMileageValue() {
var rows = document.querySelectorAll('div.issue.details .attributes .attribute');
for (var i = 0; i < rows.length; i++) {
var label = rows[i].querySelector('.label');
var value = rows[i].querySelector('.value');
if (!label || !value) continue;
var text = label.textContent.replace(/:\s*$/, '').trim().toLowerCase();
if (text === FIELD_LABEL) return value;
}
return null;
}
// Clipboard API where available (HTTPS); falls back to the older copy command otherwise
function copyText(text) {
if (navigator.clipboard && window.isSecureContext) {
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; }
document.body.removeChild(area);
ok ? resolve() : reject(new Error('copy command failed'));
});
}
var value = findMileageValue();
if (!value) return;
var text = value.textContent.trim();
if (!text) return; // no mileage recorded
var original = value.innerHTML;
var busy = false;
value.style.cursor = 'pointer';
value.setAttribute('title', 'Click to copy');
value.setAttribute('role', 'button');
value.setAttribute('tabindex', '0');
function flash(message, color) {
value.innerHTML = '<b>' + message + '</b>';
value.style.color = color;
setTimeout(function () {
value.innerHTML = original;
value.style.color = '';
busy = false;
}, 2000);
}
function copy() {
if (busy) return;
busy = true;
copyText(text).then(
function () { flash('Copied!', '#2e7d32'); },
function (err) {
console.error('Unable to copy mileage', err);
flash("Couldn't copy", '#c62828');
}
);
}
value.addEventListener('click', copy);
value.addEventListener('keydown', function (e) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
copy();
}
});
});
<% end %>