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:
1 parent
fea86725ab
commit
fa9010c063
4 files changed
+107
-49
No files matched your search
@@ -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 %>
|
||||
@@ -1,40 +0,0 @@
|
||||
$(function() {
|
||||
// Only run on issue show page
|
||||
const regex = /^\/issues\/\d+/;
|
||||
if (regex.test(window.location.pathname)) {
|
||||
|
||||
//search for the span element that contains the text "Mileage" and get its parent element's class name
|
||||
const allSpans = document.querySelectorAll('span');
|
||||
const wantedSpan = [...allSpans].find(el => el.textContent.includes('Mileage'));
|
||||
const element = wantedSpan.parentElement.parentElement.className;
|
||||
console.log(element);
|
||||
|
||||
// Get the first child element of the element with the class name and add a click event listener to it
|
||||
const firstElement = document.getElementsByClassName(element)[0].children[1];
|
||||
if (firstElement) {
|
||||
firstElement.addEventListener('click', function() {
|
||||
try {
|
||||
const text = firstElement.innerText;
|
||||
|
||||
// If the text is already "Copied!", don't do anything
|
||||
if (text == "Copied!") {
|
||||
return;
|
||||
}
|
||||
|
||||
navigator.clipboard.writeText(text);
|
||||
|
||||
firstElement.innerHTML = "<b>Copied!</b>";
|
||||
firstElement.style.color = "#4CAF50"; // Turn green
|
||||
|
||||
setTimeout(() => {
|
||||
firstElement.innerText = text;
|
||||
firstElement.style.color = "";
|
||||
}, 2000);
|
||||
|
||||
} catch (err) {
|
||||
console.error('Unable to copy', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -13,11 +13,12 @@ Redmine::Plugin.register :redmine_mileage_cp do
|
||||
# About
|
||||
name 'Redmine Mileage CP Plugin'
|
||||
author 'Rick Barrette'
|
||||
description 'A plugin for Redmine to load javascript to copy on click'
|
||||
description 'Click an issue\'s Mileage value to copy it to the clipboard'
|
||||
version '2026.3.0'
|
||||
url 'https://github.com/rickbarrette'
|
||||
url 'https://gitea.rickbarrette.org/ricky/redmine_mileage_cp'
|
||||
author_url 'https://barrettefabrication.com'
|
||||
|
||||
requires_redmine version_or_higher: '6.1.0'
|
||||
|
||||
end
|
||||
|
||||
# Dynamically load all Hooks & Patches recursively
|
||||
|
||||
@@ -13,12 +13,9 @@ module RedmineMileageCp
|
||||
|
||||
class ViewHookListener < Redmine::Hook::ViewListener
|
||||
|
||||
# Load the javascript to support the autocomplete forms
|
||||
def view_layouts_base_html_head(context = {})
|
||||
safe_join([
|
||||
javascript_include_tag( 'application.js', plugin: :redmine_mileage_cp)
|
||||
])
|
||||
end
|
||||
# Writes the click-to-copy script into the issue page, just after the issue's fields. Only issue
|
||||
# pages get it, and it doesn't depend on Redmine's compiled assets.
|
||||
render_on :view_issues_show_details_bottom, partial: 'mileage_cp/copy_script'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user