VIN click-to-copy: shared snippet, last 8 bold only for 17-char VINs

- New vehicles/_vin partial used on the issue page, vehicle page and
  vehicle lists (replaces three copies of the markup, inline onclick
  handlers and a fixed id="copyLink")
- 17-character VINs show the last 8 in bold (what dealerships ask for
  when ordering parts); older or short VINs are shown plain
- Copies the VIN from a data attribute, restores the exact display
  afterwards, shows "Copied!" or "Couldn't copy", falls back when the
  Clipboard API isn't available, and supports Enter/Space; pointer
  cursor and "Click to copy" tooltip
- The copy script is written into the page by the layout hook instead
  of served as copy.js, so it can't 404 when Redmine's asset manifest
  is out of date; one listener handles every VIN on the page
- Issue PDFs split 17-character VINs before the last 8; others whole

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 20:09:27 -04:00
1 parent d4684b8ae9
commit 129a9f668c
11 files changed
+111 -53

No files matched your search

-1
View File
@@ -56,7 +56,6 @@ class VehiclesController < ApplicationController
end
def show
@vin = @vehicle.vin.scan(/.{1,9}/) if @vehicle.vin
@issues = @vehicle.issues
.joins(:status)
.includes(:estimate, :invoices, :status, :project, :tracker, :priority)
@@ -6,7 +6,7 @@
<div class="vehicle_vin attribute">
<div class="label"><%= t(:field_vin) %>:</div>
<div class="value" id="vin">
<div id="copyLink" onclick="handleCopy(event)"><%= split_vin[0] if split_vin %><b><%= split_vin[1] if split_vin %></b></div>
<%= render partial: 'vehicles/vin', locals: { vin: vin } %>
</div>
</div>
@@ -0,0 +1,82 @@
<%#
The MIT License (MIT)
Copyright (c) 2016 - 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.
%>
<%#
Copies a VIN (vehicles/_vin) when it's clicked or activated with Enter/Space. Written into the page
head by the layout hook rather than served as a compiled asset, so it can't 404 when Redmine's asset
manifest is out of date. One listener on the page handles every VIN, including ones added later.
%>
<style>
.vehicle-vin { cursor: pointer; display: inline-block; }
</style>
<%= javascript_tag do %>
(function () {
var COPIED = <%= raw l(:text_copied).to_json %>;
var FAILED = <%= raw l(:text_copy_failed).to_json %>;
// 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'));
});
}
function copyVin(el) {
if (el.dataset.copyBusy === '1') return;
el.dataset.copyBusy = '1';
var original = el.innerHTML;
function flash(message, color) {
el.innerHTML = '<b>' + message + '</b>';
el.style.color = color;
setTimeout(function () {
el.innerHTML = original;
el.style.color = '';
el.dataset.copyBusy = '';
}, 2000);
}
copyText(el.dataset.vin).then(
function () { flash(COPIED, '#2e7d32'); },
function (err) {
console.error('Unable to copy VIN', err);
flash(FAILED, '#c62828');
}
);
}
document.addEventListener('click', function (e) {
var el = e.target.closest && e.target.closest('.vehicle-vin[data-vin]');
if (el) copyVin(el);
});
document.addEventListener('keydown', function (e) {
if (e.key !== 'Enter' && e.key !== ' ') return;
var el = e.target.closest && e.target.closest('.vehicle-vin[data-vin]');
if (!el) return;
e.preventDefault();
copyVin(el);
});
})();
<% end %>
+1 -1
View File
@@ -18,7 +18,7 @@
<tr>
<th><%= t(:field_vin) %></th>
<td>
<div onclick="handleCopy(event)"><%= @vin[0] if @vin %><b><%= @vin[1] if @vin %></b></div>
<%= render partial: 'vehicles/vin', locals: { vin: vehicle.vin } %>
</td>
</tr>
+1 -1
View File
@@ -17,7 +17,7 @@
</div>
<div class="label-sub">
<div onclick="handleCopy(event)"><%= vehicle.vin.scan(/.{1,9}/)[0] if vehicle.vin %><b><%=vehicle.vin.scan(/.{1,9}/)[1] if vehicle.vin%></b></div>
<%= render partial: 'vehicles/vin', locals: { vin: vehicle.vin } %>
<% if show_customer %>
<%= vehicle.customer %>
<% end %>
+10
View File
@@ -0,0 +1,10 @@
<%#
A VIN that can be clicked (or Enter/Space) to copy it. 17-character VINs show the last 8 in bold,
which is what dealerships ask for when ordering parts; older or short VINs are shown plain.
The copy script is in vehicles/_copy_vin_script.
locals: vin
%>
<% if vin.present? %>
<% vin = vin.to_s %>
<div class="vehicle-vin" data-vin="<%= vin %>" role="button" tabindex="0" title="<%= l(:text_click_to_copy) %>"><% if vin.length == 17 %><%= vin[0, 9] %><b><%= vin[9, 8] %></b><% else %><%= vin %><% end %></div>
<% end %>
-45
View File
@@ -1,45 +0,0 @@
async function handleCopy(event) {
console.log("Copy link clicked");
let text;
let link;
// Grab the text from our clicked link
if(event.target.tagName.toLowerCase() === 'b'){
text = event.target.parentElement.innerText;
link = event.target.parentElement;
} else {
text = event.target.innerText;
link = event.target;
}
// If the text is already "Copied!", don't do anything
if (text == "Copied!") {
return;
}
try {
// Write to clipboard
await navigator.clipboard.writeText(text);
// Update the UI to show it worked
const originalText = link.innerText;
link.innerHTML = "<b>Copied!</b>";
link.style.color = "#4CAF50"; // Turn green
// Reset after 2 seconds
setTimeout(() => {
// Check if the text is long enough to prevent errors
if (originalText.length >= 8) {
const firstPart = originalText.slice(0, -8);
const lastEight = originalText.slice(-8);
link.innerHTML = `${firstPart}<b>${lastEight}</b>`;
} else {
link.innerText = originalText;
}
link.style.color = "";
}, 2000);
} catch (err) {
console.error('Unable to copy', err);
}
}
+3
View File
@@ -59,3 +59,6 @@ en:
notice_vin_decode_timed_out: "VIN decode timed out. Please refresh or try again."
text_mileage_custom_field_info: "Issue custom field used for \"Last serviced\" mileage. Leave blank to use a field named Mileage."
text_vin_already_on_file: "A vehicle with this VIN is already on file:"
text_click_to_copy: "Click to copy"
text_copied: "Copied!"
text_copy_failed: "Couldn't copy"
@@ -19,8 +19,8 @@ module Vehicles
context[:controller].send(:render_to_string, {
partial: 'issues/show_issue_view_right',
locals: {
vehicle: context[:issue].vehicle ? link_to(context[:issue].vehicle) : nil,
split_vin: context[:issue].vehicle ? context[:issue].vehicle.vin.to_s.scan(/.{1,9}/) : nil
vehicle: context[:issue].vehicle ? link_to(context[:issue].vehicle) : nil,
vin: context[:issue].vehicle&.vin
}
})
end
+9 -1
View File
@@ -20,10 +20,18 @@ module Vehicles
def pdf_left(context={})
output = []
output << [l(:field_vehicles), context[:issue].vehicle ? context[:issue].vehicle.to_s : nil]
output << [l(:field_vin), context[:issue].vehicle ? context[:issue].vehicle.vin.gsub(/(.{9})/, '\1 ') : nil]
output << [l(:field_vin), context[:issue].vehicle ? pdf_vin(context[:issue].vehicle.vin) : nil]
output << [l(:field_notes), context[:issue].vehicle ? context[:issue].vehicle.notes : nil]
return output
end
private
# 17-character VINs are split before the last 8 (what dealerships ask for); others stay whole
def pdf_vin(vin)
vin = vin.to_s
vin.length == 17 ? "#{vin[0, 9]} #{vin[9, 8]}" : vin
end
end
end
+2 -1
View File
@@ -15,7 +15,8 @@ module Vehicles
def view_layouts_base_html_head(context = {})
safe_join([
stylesheet_link_tag('style', plugin: :redmine_qbo_vehicles, media: :all),
javascript_include_tag('copy', plugin: :redmine_qbo_vehicles)
# VIN click-to-copy, written into the page (see vehicles/_copy_vin_script)
context[:hook_caller].render(partial: 'vehicles/copy_vin_script')
])
end