diff --git a/app/views/mileage_cp/_copy_script.html.erb b/app/views/mileage_cp/_copy_script.html.erb new file mode 100644 index 0000000..6e3179b --- /dev/null +++ b/app/views/mileage_cp/_copy_script.html.erb @@ -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 = '' + message + ''; + 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 %> diff --git a/assets/javascripts/application.js b/assets/javascripts/application.js deleted file mode 100644 index bca5f55..0000000 --- a/assets/javascripts/application.js +++ /dev/null @@ -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 = "Copied!"; - firstElement.style.color = "#4CAF50"; // Turn green - - setTimeout(() => { - firstElement.innerText = text; - firstElement.style.color = ""; - }, 2000); - - } catch (err) { - console.error('Unable to copy', err); - } - }); - } -} -}); diff --git a/init.rb b/init.rb index 129099b..9f4fbd2 100644 --- a/init.rb +++ b/init.rb @@ -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 diff --git a/lib/redmine_mileage_cp/hooks/view_hook_listener.rb b/lib/redmine_mileage_cp/hooks/view_hook_listener.rb index 3460ece..67bf31d 100644 --- a/lib/redmine_mileage_cp/hooks/view_hook_listener.rb +++ b/lib/redmine_mileage_cp/hooks/view_hook_listener.rb @@ -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